Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

AxisLabel.cs 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace XCharts.Runtime
  5. {
  6. /// <summary>
  7. /// Settings related to axis label.
  8. /// |坐标轴刻度标签的相关设置。
  9. /// </summary>
  10. [Serializable]
  11. public class AxisLabel : LabelStyle
  12. {
  13. [SerializeField] private int m_Interval = 0;
  14. [SerializeField] private bool m_Inside = false;
  15. [SerializeField] private bool m_ShowAsPositiveNumber = false;
  16. [SerializeField] private bool m_OnZero = false;
  17. [SerializeField] private bool m_ShowStartLabel = true;
  18. [SerializeField] private bool m_ShowEndLabel = true;
  19. [SerializeField] private TextLimit m_TextLimit = new TextLimit();
  20. /// <summary>
  21. /// The display interval of the axis label.
  22. /// |坐标轴刻度标签的显示间隔,在类目轴中有效。0表示显示所有标签,1表示隔一个隔显示一个标签,以此类推。
  23. /// </summary>
  24. public int interval
  25. {
  26. get { return m_Interval; }
  27. set { if (PropertyUtil.SetStruct(ref m_Interval, value)) SetComponentDirty(); }
  28. }
  29. /// <summary>
  30. /// Set this to true so the axis labels face the inside direction.
  31. /// |刻度标签是否朝内,默认朝外。
  32. /// </summary>
  33. public bool inside
  34. {
  35. get { return m_Inside; }
  36. set { if (PropertyUtil.SetStruct(ref m_Inside, value)) SetComponentDirty(); }
  37. }
  38. /// <summary>
  39. /// Show negative number as positive number.
  40. /// |将负数数值显示为正数。一般和`Serie`的`showAsPositiveNumber`配合使用。
  41. /// </summary>
  42. public bool showAsPositiveNumber
  43. {
  44. get { return m_ShowAsPositiveNumber; }
  45. set { if (PropertyUtil.SetStruct(ref m_ShowAsPositiveNumber, value)) SetComponentDirty(); }
  46. }
  47. /// <summary>
  48. /// 刻度标签显示在0刻度上。
  49. /// </summary>
  50. public bool onZero
  51. {
  52. get { return m_OnZero; }
  53. set { if (PropertyUtil.SetStruct(ref m_OnZero, value)) SetComponentDirty(); }
  54. }
  55. /// <summary>
  56. /// Whether to display the first label.
  57. /// |是否显示第一个文本。
  58. /// </summary>
  59. public bool showStartLabel
  60. {
  61. get { return m_ShowStartLabel; }
  62. set { if (PropertyUtil.SetStruct(ref m_ShowStartLabel, value)) SetComponentDirty(); }
  63. }
  64. /// <summary>
  65. /// Whether to display the last label.
  66. /// |是否显示最后一个文本。
  67. /// </summary>
  68. public bool showEndLabel
  69. {
  70. get { return m_ShowEndLabel; }
  71. set { if (PropertyUtil.SetStruct(ref m_ShowEndLabel, value)) SetComponentDirty(); }
  72. }
  73. /// <summary>
  74. /// 文本限制。
  75. /// </summary>
  76. public TextLimit textLimit
  77. {
  78. get { return m_TextLimit; }
  79. set { if (value != null) { m_TextLimit = value; SetComponentDirty(); } }
  80. }
  81. public override bool componentDirty { get { return m_ComponentDirty || m_TextLimit.componentDirty; } }
  82. public override void ClearComponentDirty()
  83. {
  84. base.ClearComponentDirty();
  85. textLimit.ClearComponentDirty();
  86. }
  87. public static AxisLabel defaultAxisLabel
  88. {
  89. get
  90. {
  91. return new AxisLabel()
  92. {
  93. m_Show = true,
  94. m_Interval = 0,
  95. m_Inside = false,
  96. m_Distance = 8,
  97. m_TextStyle = new TextStyle(),
  98. };
  99. }
  100. }
  101. public new AxisLabel Clone()
  102. {
  103. var axisLabel = new AxisLabel();
  104. axisLabel.show = show;
  105. axisLabel.formatter = formatter;
  106. axisLabel.interval = interval;
  107. axisLabel.inside = inside;
  108. axisLabel.distance = distance;
  109. axisLabel.numericFormatter = numericFormatter;
  110. axisLabel.width = width;
  111. axisLabel.height = height;
  112. axisLabel.showStartLabel = showStartLabel;
  113. axisLabel.showEndLabel = showEndLabel;
  114. axisLabel.textLimit = textLimit.Clone();
  115. axisLabel.textStyle.Copy(textStyle);
  116. return axisLabel;
  117. }
  118. public void Copy(AxisLabel axisLabel)
  119. {
  120. show = axisLabel.show;
  121. formatter = axisLabel.formatter;
  122. interval = axisLabel.interval;
  123. inside = axisLabel.inside;
  124. distance = axisLabel.distance;
  125. numericFormatter = axisLabel.numericFormatter;
  126. width = axisLabel.width;
  127. height = axisLabel.height;
  128. showStartLabel = axisLabel.showStartLabel;
  129. showEndLabel = axisLabel.showEndLabel;
  130. textLimit.Copy(axisLabel.textLimit);
  131. textStyle.Copy(axisLabel.textStyle);
  132. }
  133. public void SetRelatedText(ChartText txt, float labelWidth)
  134. {
  135. m_TextLimit.SetRelatedText(txt, labelWidth);
  136. }
  137. public override string GetFormatterContent(int labelIndex, string category)
  138. {
  139. if (string.IsNullOrEmpty(category))
  140. return GetFormatterFunctionContent(labelIndex, category, category);
  141. if (string.IsNullOrEmpty(m_Formatter))
  142. {
  143. return GetFormatterFunctionContent(labelIndex, category, m_TextLimit.GetLimitContent(category));
  144. }
  145. else
  146. {
  147. var content = m_Formatter;
  148. FormatterHelper.ReplaceAxisLabelContent(ref content, category);
  149. return GetFormatterFunctionContent(labelIndex, category, m_TextLimit.GetLimitContent(content));
  150. }
  151. }
  152. public override string GetFormatterContent(int labelIndex, double value, double minValue, double maxValue, bool isLog = false)
  153. {
  154. if (showAsPositiveNumber && value < 0)
  155. {
  156. value = Math.Abs(value);
  157. }
  158. return base.GetFormatterContent(labelIndex, value, minValue, maxValue, isLog);
  159. }
  160. }
  161. }