ChartCached.cs 6.6 KB

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