ChartCached.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. private static Dictionary<string, Dictionary<int, string>> s_StringIntDict = new Dictionary<string, Dictionary<int, string>>();
  24. public static string FloatToStr(double value, string numericFormatter = "F", int precision = 0)
  25. {
  26. if (precision > 0 && numericFormatter.Length == 1)
  27. {
  28. if (!s_PrecisionToStr.ContainsKey(precision))
  29. {
  30. s_PrecisionToStr[precision] = new Dictionary<string, string>();
  31. }
  32. if (!s_PrecisionToStr[precision].ContainsKey(numericFormatter))
  33. {
  34. s_PrecisionToStr[precision][numericFormatter] = numericFormatter + precision;
  35. }
  36. return NumberToStr(value, s_PrecisionToStr[precision][numericFormatter]);
  37. }
  38. else
  39. {
  40. return NumberToStr(value, numericFormatter);
  41. }
  42. }
  43. public static string NumberToStr(double value, string formatter)
  44. {
  45. if (!s_NumberToStr.ContainsKey(value))
  46. {
  47. s_NumberToStr[value] = new Dictionary<string, string>();
  48. }
  49. if (!s_NumberToStr[value].ContainsKey(formatter))
  50. {
  51. if (string.IsNullOrEmpty(formatter))
  52. {
  53. s_NumberToStr[value][formatter] = value.ToString();
  54. }
  55. else if (formatter.StartsWith(NUMERIC_FORMATTER_D) ||
  56. formatter.StartsWith(NUMERIC_FORMATTER_d) ||
  57. formatter.StartsWith(NUMERIC_FORMATTER_X) ||
  58. formatter.StartsWith(NUMERIC_FORMATTER_x)
  59. )
  60. {
  61. s_NumberToStr[value][formatter] = ((int)value).ToString(formatter, ci);
  62. }
  63. else
  64. {
  65. s_NumberToStr[value][formatter] = value.ToString(formatter, ci);
  66. }
  67. }
  68. return s_NumberToStr[value][formatter];
  69. }
  70. public static string IntToStr(int value, string numericFormatter = "")
  71. {
  72. return NumberToStr(value, numericFormatter);
  73. }
  74. public static string ColorToStr(Color color)
  75. {
  76. if (s_ColorToStr.ContainsKey(color))
  77. {
  78. return s_ColorToStr[color];
  79. }
  80. else
  81. {
  82. s_ColorToStr[color] = ColorUtility.ToHtmlStringRGBA(color);
  83. return s_ColorToStr[color];
  84. }
  85. }
  86. public static string ColorToDotStr(Color color)
  87. {
  88. if (!s_ColorDotStr.ContainsKey(color))
  89. {
  90. s_ColorDotStr[color] = "<color=#" + ColorToStr(color) + ">● </color>";
  91. }
  92. return s_ColorDotStr[color];
  93. }
  94. public static string GetSerieLabelName(string prefix, int i, int j)
  95. {
  96. int key = i * 10000000 + j;
  97. if (s_SerieLabelName.ContainsKey(key))
  98. {
  99. return s_SerieLabelName[key];
  100. }
  101. else
  102. {
  103. string name = prefix + "_" + i + "_" + j;
  104. s_SerieLabelName[key] = name;
  105. return name;
  106. }
  107. }
  108. public static string GetString(string prefix, int suffix)
  109. {
  110. if (!s_StringIntDict.ContainsKey(prefix))
  111. {
  112. s_StringIntDict[prefix] = new Dictionary<int, string>();
  113. }
  114. if (!s_StringIntDict[prefix].ContainsKey(suffix))
  115. {
  116. s_StringIntDict[prefix][suffix] = prefix + suffix;
  117. }
  118. return s_StringIntDict[prefix][suffix];
  119. }
  120. internal static string GetComponentObjectName(MainComponent component)
  121. {
  122. Dictionary<int, string> dict;
  123. var type = component.GetType();
  124. if (s_ComponentObjectName.TryGetValue(type, out dict))
  125. {
  126. string name;
  127. if (!dict.TryGetValue(component.index, out name))
  128. {
  129. name = GetTypeName(type) + component.index;
  130. dict[component.index] = name;
  131. }
  132. return name;
  133. }
  134. else
  135. {
  136. var name = GetTypeName(type) + component.index;
  137. dict = new Dictionary<int, string>();
  138. dict.Add(component.index, name);
  139. s_ComponentObjectName[type] = dict;
  140. return name;
  141. }
  142. }
  143. internal static string GetAxisLabelName(int index)
  144. {
  145. string name;
  146. if (!s_AxisLabelName.TryGetValue(index, out name))
  147. {
  148. name = s_DefaultAxis + index;
  149. s_AxisLabelName[index] = name;
  150. return name;
  151. }
  152. else
  153. {
  154. return name;
  155. }
  156. }
  157. internal static string GetTypeName<T>()
  158. {
  159. return GetTypeName(typeof(T));
  160. }
  161. internal static string GetTypeName(Type type)
  162. {
  163. if (s_TypeName.ContainsKey(type)) return s_TypeName[type];
  164. else
  165. {
  166. var name = type.Name;
  167. s_TypeName[type] = name;
  168. return name;
  169. }
  170. }
  171. }
  172. }