Açıklama Yok
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.

LabelStyle.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. using System;
  2. using UnityEngine;
  3. namespace XCharts.Runtime
  4. {
  5. /// <summary>
  6. /// Text label of chart, to explain some data information about graphic item like value, name and so on.
  7. /// |图形上的文本标签,可用于说明图形的一些数据信息,比如值,名称等。
  8. /// </summary>
  9. [System.Serializable]
  10. public class LabelStyle : ChildComponent, ISerieComponent, ISerieDataComponent
  11. {
  12. /// <summary>
  13. /// The position of label.
  14. /// |标签的位置。
  15. /// </summary>
  16. public enum Position
  17. {
  18. Default,
  19. /// <summary>
  20. /// Outside of sectors of pie chart, which relates to corresponding sector through visual guide line.
  21. /// |饼图扇区外侧,通过视觉引导线连到相应的扇区。
  22. /// </summary>
  23. Outside,
  24. /// <summary>
  25. /// Inside the sectors of pie chart.
  26. /// |饼图扇区内部。
  27. /// </summary>
  28. Inside,
  29. /// <summary>
  30. /// In the center of pie chart.
  31. /// |在饼图中心位置。
  32. /// </summary>
  33. Center,
  34. /// <summary>
  35. /// top of symbol.
  36. /// |图形标志的顶部。
  37. /// </summary>
  38. Top,
  39. /// <summary>
  40. /// the bottom of symbol.
  41. /// |图形标志的底部。
  42. /// </summary>
  43. Bottom,
  44. /// <summary>
  45. /// the left of symbol.
  46. /// |图形标志的左边。
  47. /// </summary>
  48. Left,
  49. /// <summary>
  50. /// the right of symbol.
  51. /// |图形标志的右边。
  52. /// </summary>
  53. Right,
  54. /// <summary>
  55. /// the start of line.
  56. /// |线的起始点。
  57. /// </summary>
  58. Start,
  59. /// <summary>
  60. /// the middle of line.
  61. /// |线的中点。
  62. /// </summary>
  63. Middle,
  64. /// <summary>
  65. /// the end of line.
  66. /// |线的结束点。
  67. /// </summary>
  68. End
  69. }
  70. [SerializeField] protected bool m_Show = true;
  71. [SerializeField] Position m_Position = Position.Default;
  72. [SerializeField] protected bool m_AutoOffset = false;
  73. [SerializeField] protected Vector3 m_Offset;
  74. [SerializeField] protected float m_Rotate;
  75. [SerializeField][Since("v3.6.0")] protected bool m_AutoRotate = false;
  76. [SerializeField] protected float m_Distance;
  77. [SerializeField] protected string m_Formatter;
  78. [SerializeField] protected string m_NumericFormatter = "";
  79. [SerializeField] protected float m_Width = 0;
  80. [SerializeField] protected float m_Height = 0;
  81. [SerializeField] protected IconStyle m_Icon = new IconStyle();
  82. [SerializeField] protected ImageStyle m_Background = new ImageStyle();
  83. [SerializeField] protected TextPadding m_TextPadding = new TextPadding();
  84. [SerializeField] protected TextStyle m_TextStyle = new TextStyle();
  85. protected LabelFormatterFunction m_FormatterFunction;
  86. public void Reset()
  87. {
  88. m_Show = false;
  89. m_Position = Position.Default;
  90. m_Offset = Vector3.zero;
  91. m_Distance = 0;
  92. m_Rotate = 0;
  93. m_Width = 0;
  94. m_Height = 0;
  95. m_NumericFormatter = "";
  96. m_AutoOffset = false;
  97. }
  98. /// <summary>
  99. /// Whether the label is showed.
  100. /// |是否显示文本标签。
  101. /// </summary>
  102. public bool show
  103. {
  104. get { return m_Show; }
  105. set { if (PropertyUtil.SetStruct(ref m_Show, value)) SetAllDirty(); }
  106. }
  107. /// <summary>
  108. /// The position of label.
  109. /// |标签的位置。
  110. /// </summary>
  111. public Position position
  112. {
  113. get { return m_Position; }
  114. set { if (PropertyUtil.SetStruct(ref m_Position, value)) SetAllDirty(); }
  115. }
  116. /// <summary>
  117. /// formatter of label.
  118. /// |标签内容字符串模版格式器。支持用 \n 换行。
  119. /// 模板变量有:
  120. /// {.}:圆点标记。
  121. /// {a}:系列名。
  122. /// {a}:系列名。
  123. /// {b}:类目值或数据名。
  124. /// {c}:数据值。
  125. /// {d}:百分比。
  126. /// {e}:数据名。
  127. /// {f}:数据和。
  128. /// 示例:“{b}:{c}”
  129. /// </summary>
  130. public string formatter
  131. {
  132. get { return m_Formatter; }
  133. set { if (PropertyUtil.SetClass(ref m_Formatter, value)) SetComponentDirty(); }
  134. }
  135. /// <summary>
  136. /// offset to the host graphic element.
  137. /// |距离图形元素的偏移
  138. /// </summary>
  139. public Vector3 offset
  140. {
  141. get { return m_Offset; }
  142. set { if (PropertyUtil.SetStruct(ref m_Offset, value)) SetAllDirty(); }
  143. }
  144. /// <summary>
  145. /// Rotation of label.
  146. /// |文本的旋转。
  147. /// </summary>
  148. public float rotate
  149. {
  150. get { return m_Rotate; }
  151. set { if (PropertyUtil.SetStruct(ref m_Rotate, value)) SetComponentDirty(); }
  152. }
  153. /// <summary>
  154. /// auto rotate of label.
  155. /// |是否自动旋转。
  156. /// </summary>
  157. public bool autoRotate
  158. {
  159. get { return m_AutoRotate; }
  160. set { if (PropertyUtil.SetStruct(ref m_AutoRotate, value)) SetComponentDirty(); }
  161. }
  162. /// <summary>
  163. /// 距离轴线的距离。
  164. /// </summary>
  165. public float distance
  166. {
  167. get { return m_Distance; }
  168. set { if (PropertyUtil.SetStruct(ref m_Distance, value)) SetAllDirty(); }
  169. }
  170. /// <summary>
  171. /// the width of label. If set as default value 0, it means than the label width auto set as the text width.
  172. /// |标签的宽度。一般不用指定,不指定时则自动是文字的宽度。
  173. /// </summary>
  174. /// <value></value>
  175. public float width
  176. {
  177. get { return m_Width; }
  178. set { if (PropertyUtil.SetStruct(ref m_Width, value)) SetComponentDirty(); }
  179. }
  180. /// <summary>
  181. /// the height of label. If set as default value 0, it means than the label height auto set as the text height.
  182. /// |标签的高度。一般不用指定,不指定时则自动是文字的高度。
  183. /// </summary>
  184. /// <value></value>
  185. public float height
  186. {
  187. get { return m_Height; }
  188. set { if (PropertyUtil.SetStruct(ref m_Height, value)) SetComponentDirty(); }
  189. }
  190. /// <summary>
  191. /// the text padding of label.
  192. /// |文本的边距。
  193. /// </summary>
  194. public TextPadding textPadding
  195. {
  196. get { return m_TextPadding; }
  197. set { if (PropertyUtil.SetClass(ref m_TextPadding, value)) SetComponentDirty(); }
  198. }
  199. /// <summary>
  200. /// Standard numeric format strings.
  201. /// |标准数字格式字符串。用于将数值格式化显示为字符串。
  202. /// 使用Axx的形式:A是格式说明符的单字符,支持C货币、D十进制、E指数、F定点数、G常规、N数字、P百分比、R往返、X十六进制的。xx是精度说明,从0-99。
  203. /// 参考:https://docs.microsoft.com/zh-cn/dotnet/standard/base-types/standard-numeric-format-strings
  204. /// </summary>
  205. /// <value></value>
  206. public string numericFormatter
  207. {
  208. get { return m_NumericFormatter; }
  209. set { if (PropertyUtil.SetClass(ref m_NumericFormatter, value)) SetComponentDirty(); }
  210. }
  211. /// <summary>
  212. /// 是否开启自动偏移。当开启时,Y的偏移会自动判断曲线的开口来决定向上还是向下偏移。
  213. /// </summary>
  214. public bool autoOffset
  215. {
  216. get { return m_AutoOffset; }
  217. set { if (PropertyUtil.SetStruct(ref m_AutoOffset, value)) SetAllDirty(); }
  218. }
  219. /// <summary>
  220. /// the sytle of background.
  221. /// |背景图样式。
  222. /// </summary>
  223. public ImageStyle background
  224. {
  225. get { return m_Background; }
  226. set { if (PropertyUtil.SetClass(ref m_Background, value)) SetAllDirty(); }
  227. }
  228. /// <summary>
  229. /// the sytle of icon.
  230. /// |图标样式。
  231. /// </summary>
  232. public IconStyle icon
  233. {
  234. get { return m_Icon; }
  235. set { if (PropertyUtil.SetClass(ref m_Icon, value)) SetAllDirty(); }
  236. }
  237. /// <summary>
  238. /// the sytle of text.
  239. /// |文本样式。
  240. /// </summary>
  241. public TextStyle textStyle
  242. {
  243. get { return m_TextStyle; }
  244. set { if (PropertyUtil.SetClass(ref m_TextStyle, value)) SetAllDirty(); }
  245. }
  246. public LabelFormatterFunction formatterFunction
  247. {
  248. get { return m_FormatterFunction; }
  249. set { m_FormatterFunction = value; }
  250. }
  251. public bool IsInside()
  252. {
  253. return m_Position == Position.Inside || m_Position == Position.Center;
  254. }
  255. public bool IsDefaultPosition(Position position)
  256. {
  257. return m_Position == Position.Default || m_Position == position;
  258. }
  259. public bool IsAutoSize()
  260. {
  261. return width == 0 && height == 0;
  262. }
  263. public Vector3 GetOffset(float radius)
  264. {
  265. var x = ChartHelper.GetActualValue(m_Offset.x, radius);
  266. var y = ChartHelper.GetActualValue(m_Offset.y, radius);
  267. var z = ChartHelper.GetActualValue(m_Offset.z, radius);
  268. return new Vector3(x, y, z);
  269. }
  270. public Color GetColor(Color defaultColor)
  271. {
  272. if (ChartHelper.IsClearColor(textStyle.color))
  273. {
  274. return IsInside() ? Color.black : defaultColor;
  275. }
  276. else
  277. {
  278. return textStyle.color;
  279. }
  280. }
  281. public virtual LabelStyle Clone()
  282. {
  283. var label = new LabelStyle();
  284. label.m_Show = m_Show;
  285. label.m_Position = m_Position;
  286. label.m_Offset = m_Offset;
  287. label.m_Rotate = m_Rotate;
  288. label.m_Distance = m_Distance;
  289. label.m_Formatter = m_Formatter;
  290. label.m_Width = m_Width;
  291. label.m_Height = m_Height;
  292. label.m_NumericFormatter = m_NumericFormatter;
  293. label.m_AutoOffset = m_AutoOffset;
  294. label.m_Icon.Copy(m_Icon);
  295. label.m_Background.Copy(m_Background);
  296. label.m_TextPadding = m_TextPadding;
  297. label.m_TextStyle.Copy(m_TextStyle);
  298. return label;
  299. }
  300. public virtual void Copy(LabelStyle label)
  301. {
  302. m_Show = label.m_Show;
  303. m_Position = label.m_Position;
  304. m_Offset = label.m_Offset;
  305. m_Rotate = label.m_Rotate;
  306. m_Distance = label.m_Distance;
  307. m_Formatter = label.m_Formatter;
  308. m_Width = label.m_Width;
  309. m_Height = label.m_Height;
  310. m_NumericFormatter = label.m_NumericFormatter;
  311. m_AutoOffset = label.m_AutoOffset;
  312. m_Icon.Copy(label.m_Icon);
  313. m_Background.Copy(label.m_Background);
  314. m_TextPadding = label.m_TextPadding;
  315. m_TextStyle.Copy(label.m_TextStyle);
  316. }
  317. public virtual string GetFormatterContent(int labelIndex, string category)
  318. {
  319. if (string.IsNullOrEmpty(category))
  320. return GetFormatterFunctionContent(labelIndex, category, category);
  321. if (string.IsNullOrEmpty(m_Formatter))
  322. {
  323. return GetFormatterFunctionContent(labelIndex, category, category);
  324. }
  325. else
  326. {
  327. var content = m_Formatter;
  328. FormatterHelper.ReplaceAxisLabelContent(ref content, category);
  329. return GetFormatterFunctionContent(labelIndex, category, category);
  330. }
  331. }
  332. public virtual string GetFormatterContent(int labelIndex, double value, double minValue, double maxValue, bool isLog = false)
  333. {
  334. var newNumericFormatter = numericFormatter;
  335. if (string.IsNullOrEmpty(newNumericFormatter) && !isLog)
  336. {
  337. newNumericFormatter = MathUtil.IsInteger(maxValue) ? "0" : "f" + MathUtil.GetPrecision(maxValue);
  338. }
  339. if (string.IsNullOrEmpty(m_Formatter))
  340. {
  341. if (isLog)
  342. {
  343. return GetFormatterFunctionContent(labelIndex, value, ChartCached.NumberToStr(value, newNumericFormatter));
  344. }
  345. if (minValue >= -1 && minValue <= 1 && maxValue >= -1 && maxValue <= 1)
  346. {
  347. int minAcc = MathUtil.GetPrecision(minValue);
  348. int maxAcc = MathUtil.GetPrecision(maxValue);
  349. int curAcc = MathUtil.GetPrecision(value);
  350. int acc = Mathf.Max(Mathf.Max(minAcc, maxAcc), curAcc);
  351. return GetFormatterFunctionContent(labelIndex, value, ChartCached.FloatToStr(value, newNumericFormatter, acc));
  352. }
  353. return GetFormatterFunctionContent(labelIndex, value, ChartCached.NumberToStr(value, newNumericFormatter));
  354. }
  355. else
  356. {
  357. var content = m_Formatter;
  358. FormatterHelper.ReplaceAxisLabelContent(ref content, newNumericFormatter, value);
  359. return GetFormatterFunctionContent(labelIndex, value, content);
  360. }
  361. }
  362. public string GetFormatterDateTime(int labelIndex, double value, double minValue, double maxValue)
  363. {
  364. var timestamp = (int)value;
  365. var dateTime = DateTimeUtil.GetDateTime(timestamp);
  366. var dateString = string.Empty;
  367. if (string.IsNullOrEmpty(numericFormatter) || numericFormatter.Equals("f2"))
  368. {
  369. dateString = DateTimeUtil.GetDateTimeFormatString(dateTime, maxValue - minValue);
  370. }
  371. else
  372. {
  373. dateString = dateTime.ToString(numericFormatter);
  374. }
  375. if (!string.IsNullOrEmpty(m_Formatter))
  376. {
  377. var content = m_Formatter;
  378. FormatterHelper.ReplaceAxisLabelContent(ref content, dateString);
  379. return GetFormatterFunctionContent(labelIndex, value, content);
  380. }
  381. else
  382. {
  383. return GetFormatterFunctionContent(labelIndex, value, dateString);
  384. }
  385. }
  386. protected string GetFormatterFunctionContent(int labelIndex, string category, string currentContent)
  387. {
  388. return m_FormatterFunction == null ? currentContent :
  389. m_FormatterFunction(labelIndex, labelIndex, category, currentContent);
  390. }
  391. protected string GetFormatterFunctionContent(int labelIndex, double value, string currentContent)
  392. {
  393. return m_FormatterFunction == null ? currentContent :
  394. m_FormatterFunction(labelIndex, labelIndex, null, currentContent);
  395. }
  396. }
  397. }