暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ChartCached.cs 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. if (value - (int) value == 0)
  53. s_NumberToStr[value][formatter] = ((int) value).ToString();
  54. else
  55. s_NumberToStr[value][formatter] = value.ToString();
  56. }
  57. else if (formatter.StartsWith(NUMERIC_FORMATTER_D) ||
  58. formatter.StartsWith(NUMERIC_FORMATTER_d) ||
  59. formatter.StartsWith(NUMERIC_FORMATTER_X) ||
  60. formatter.StartsWith(NUMERIC_FORMATTER_x)
  61. )
  62. {
  63. s_NumberToStr[value][formatter] = ((int) value).ToString(formatter, ci);
  64. }
  65. else
  66. {
  67. s_NumberToStr[value][formatter] = value.ToString(formatter, ci);
  68. }
  69. }
  70. return s_NumberToStr[value][formatter];
  71. }
  72. public static string IntToStr(int value, string numericFormatter = "")
  73. {
  74. return NumberToStr(value, numericFormatter);
  75. }
  76. public static string ColorToStr(Color color)
  77. {
  78. if (s_ColorToStr.ContainsKey(color))
  79. {
  80. return s_ColorToStr[color];
  81. }
  82. else
  83. {
  84. s_ColorToStr[color] = ColorUtility.ToHtmlStringRGBA(color);
  85. return s_ColorToStr[color];
  86. }
  87. }
  88. public static string ColorToDotStr(Color color)
  89. {
  90. if (!s_ColorDotStr.ContainsKey(color))
  91. {
  92. s_ColorDotStr[color] = "<color=#" + ColorToStr(color) + ">● </color>";
  93. }
  94. return s_ColorDotStr[color];
  95. }
  96. public static string GetSerieLabelName(string prefix, int i, int j)
  97. {
  98. int key = i * 10000000 + j;
  99. if (s_SerieLabelName.ContainsKey(key))
  100. {
  101. return s_SerieLabelName[key];
  102. }
  103. else
  104. {
  105. string name = prefix + "_" + i + "_" + j;
  106. s_SerieLabelName[key] = name;
  107. return name;
  108. }
  109. }
  110. internal static string GetComponentObjectName(MainComponent component)
  111. {
  112. Dictionary<int, string> dict;
  113. var type = component.GetType();
  114. if (s_ComponentObjectName.TryGetValue(type, out dict))
  115. {
  116. string name;
  117. if (!dict.TryGetValue(component.index, out name))
  118. {
  119. name = GetTypeName(type) + component.index;
  120. dict[component.index] = name;
  121. }
  122. return name;
  123. }
  124. else
  125. {
  126. var name = GetTypeName(type) + component.index;
  127. dict = new Dictionary<int, string>();
  128. dict.Add(component.index, name);
  129. s_ComponentObjectName[type] = dict;
  130. return name;
  131. }
  132. }
  133. internal static string GetAxisLabelName(int index)
  134. {
  135. string name;
  136. if (!s_AxisLabelName.TryGetValue(index, out name))
  137. {
  138. name = s_DefaultAxis + index;
  139. s_AxisLabelName[index] = name;
  140. return name;
  141. }
  142. else
  143. {
  144. return name;
  145. }
  146. }
  147. internal static string GetTypeName<T>()
  148. {
  149. return GetTypeName(typeof(T));
  150. }
  151. internal static string GetTypeName(Type type)
  152. {
  153. if (s_TypeName.ContainsKey(type)) return s_TypeName[type];
  154. else
  155. {
  156. var name = type.Name;
  157. s_TypeName[type] = name;
  158. return name;
  159. }
  160. }
  161. }
  162. }