暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChartCached.cs 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using UnityEngine;
  5. namespace XCharts.Runtime
  6. {
  7. public static class ChartCached
  8. {
  9. private const string NUMERIC_FORMATTER_D = "D";
  10. private const string NUMERIC_FORMATTER_d = "d";
  11. private const string NUMERIC_FORMATTER_X = "X";
  12. private const string NUMERIC_FORMATTER_x = "x";
  13. private static readonly string s_DefaultAxis = "axis_";
  14. private static CultureInfo ci = new CultureInfo("en-us"); // "en-us", "zh-cn", "ar-iq", "de-de"
  15. private static Dictionary<Color, string> s_ColorToStr = new Dictionary<Color, string>(100);
  16. private static Dictionary<int, string> s_SerieLabelName = new Dictionary<int, string>(1000);
  17. private static Dictionary<Color, string> s_ColorDotStr = new Dictionary<Color, string>(100);
  18. private static Dictionary<Type, Dictionary<int, string>> s_ComponentObjectName = new Dictionary<Type, Dictionary<int, string>>();
  19. private static Dictionary<int, string> s_AxisLabelName = new Dictionary<int, string>();
  20. private static Dictionary<Type, string> s_TypeName = new Dictionary<Type, string>();
  21. private static Dictionary<double, Dictionary<string, string>> s_NumberToStr = new Dictionary<double, Dictionary<string, string>>();
  22. private static Dictionary<int, Dictionary<string, string>> s_PrecisionToStr = new Dictionary<int, Dictionary<string, string>>();
  23. public static string FloatToStr(double value, string numericFormatter = "F", int precision = 0)
  24. {
  25. if (precision > 0 && numericFormatter.Length == 1)
  26. {
  27. if (!s_PrecisionToStr.ContainsKey(precision))
  28. {
  29. s_PrecisionToStr[precision] = new Dictionary<string, string>();
  30. }
  31. if (!s_PrecisionToStr[precision].ContainsKey(numericFormatter))
  32. {
  33. s_PrecisionToStr[precision][numericFormatter] = numericFormatter + precision;
  34. }
  35. return NumberToStr(value, s_PrecisionToStr[precision][numericFormatter]);
  36. }
  37. else
  38. {
  39. return NumberToStr(value, numericFormatter);
  40. }
  41. }
  42. public static string NumberToStr(double value, string formatter)
  43. {
  44. if (!s_NumberToStr.ContainsKey(value))
  45. {
  46. s_NumberToStr[value] = new Dictionary<string, string>();
  47. }
  48. if (!s_NumberToStr[value].ContainsKey(formatter))
  49. {
  50. if (string.IsNullOrEmpty(formatter))
  51. {
  52. s_NumberToStr[value][formatter] = value.ToString();
  53. }
  54. else if (formatter.StartsWith(NUMERIC_FORMATTER_D) ||
  55. formatter.StartsWith(NUMERIC_FORMATTER_d) ||
  56. formatter.StartsWith(NUMERIC_FORMATTER_X) ||
  57. formatter.StartsWith(NUMERIC_FORMATTER_x)
  58. )
  59. {
  60. s_NumberToStr[value][formatter] = ((int)value).ToString(formatter, ci);
  61. }
  62. else
  63. {
  64. s_NumberToStr[value][formatter] = value.ToString(formatter, ci);
  65. }
  66. }
  67. return s_NumberToStr[value][formatter];
  68. }
  69. public static string IntToStr(int value, string numericFormatter = "")
  70. {
  71. return NumberToStr(value, numericFormatter);
  72. }
  73. public static string ColorToStr(Color color)
  74. {
  75. if (s_ColorToStr.ContainsKey(color))
  76. {
  77. return s_ColorToStr[color];
  78. }
  79. else
  80. {
  81. s_ColorToStr[color] = ColorUtility.ToHtmlStringRGBA(color);
  82. return s_ColorToStr[color];
  83. }
  84. }
  85. public static string ColorToDotStr(Color color)
  86. {
  87. if (!s_ColorDotStr.ContainsKey(color))
  88. {
  89. s_ColorDotStr[color] = "<color=#" + ColorToStr(color) + ">● </color>";
  90. }
  91. return s_ColorDotStr[color];
  92. }
  93. public static string GetSerieLabelName(string prefix, int i, int j)
  94. {
  95. int key = i * 10000000 + j;
  96. if (s_SerieLabelName.ContainsKey(key))
  97. {
  98. return s_SerieLabelName[key];
  99. }
  100. else
  101. {
  102. string name = prefix + "_" + i + "_" + j;
  103. s_SerieLabelName[key] = name;
  104. return name;
  105. }
  106. }
  107. internal static string GetComponentObjectName(MainComponent component)
  108. {
  109. Dictionary<int, string> dict;
  110. var type = component.GetType();
  111. if (s_ComponentObjectName.TryGetValue(type, out dict))
  112. {
  113. string name;
  114. if (!dict.TryGetValue(component.index, out name))
  115. {
  116. name = GetTypeName(type) + component.index;
  117. dict[component.index] = name;
  118. }
  119. return name;
  120. }
  121. else
  122. {
  123. var name = GetTypeName(type) + component.index;
  124. dict = new Dictionary<int, string>();
  125. dict.Add(component.index, name);
  126. s_ComponentObjectName[type] = dict;
  127. return name;
  128. }
  129. }
  130. internal static string GetAxisLabelName(int index)
  131. {
  132. string name;
  133. if (!s_AxisLabelName.TryGetValue(index, out name))
  134. {
  135. name = s_DefaultAxis + index;
  136. s_AxisLabelName[index] = name;
  137. return name;
  138. }
  139. else
  140. {
  141. return name;
  142. }
  143. }
  144. internal static string GetTypeName<T>()
  145. {
  146. return GetTypeName(typeof(T));
  147. }
  148. internal static string GetTypeName(Type type)
  149. {
  150. if (s_TypeName.ContainsKey(type)) return s_TypeName[type];
  151. else
  152. {
  153. var name = type.Name;
  154. s_TypeName[type] = name;
  155. return name;
  156. }
  157. }
  158. }
  159. }