Bez popisu
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.

SerieSymbl.cs 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace XCharts.Runtime
  5. {
  6. /// <summary>
  7. /// The way to get serie symbol size.
  8. /// |获取标记图形大小的方式。
  9. /// </summary>
  10. public enum SymbolSizeType
  11. {
  12. /// <summary>
  13. /// Specify constant for symbol size.
  14. /// |自定义大小。
  15. /// </summary>
  16. Custom,
  17. /// <summary>
  18. /// Specify the dataIndex and dataScale to calculate symbol size.
  19. /// |通过 dataIndex 从数据中获取,再乘以一个比例系数 dataScale 。
  20. /// </summary>
  21. FromData,
  22. /// <summary>
  23. /// Specify function for symbol size.
  24. /// |通过委托函数获取。
  25. /// </summary>
  26. Function,
  27. }
  28. /// <summary>
  29. /// 系列数据项的标记的图形
  30. /// </summary>
  31. [System.Serializable]
  32. public class SerieSymbol : SymbolStyle, ISerieDataComponent
  33. {
  34. [SerializeField] private SymbolSizeType m_SizeType = SymbolSizeType.Custom;
  35. [SerializeField] private int m_DataIndex = 1;
  36. [SerializeField] private float m_DataScale = 1;
  37. [SerializeField] private SymbolSizeFunction m_SizeFunction;
  38. [SerializeField] private int m_StartIndex;
  39. [SerializeField] private int m_Interval;
  40. [SerializeField] private bool m_ForceShowLast = false;
  41. [SerializeField] private bool m_Repeat = false;
  42. [SerializeField][Since("v3.3.0")] private float m_MinSize = 0f;
  43. [SerializeField][Since("v3.3.0")] private float m_MaxSize = 0f;
  44. public override void Reset()
  45. {
  46. base.Reset();
  47. m_SizeType = SymbolSizeType.Custom;
  48. m_DataIndex = 1;
  49. m_DataScale = 1;
  50. m_SizeFunction = null;
  51. m_StartIndex = 0;
  52. m_Interval = 0;
  53. m_ForceShowLast = false;
  54. m_Repeat = false;
  55. m_MinSize = 0f;
  56. m_MaxSize = 0f;
  57. }
  58. /// <summary>
  59. /// the type of symbol size.
  60. /// |标记图形的大小获取方式。
  61. /// </summary>
  62. public SymbolSizeType sizeType
  63. {
  64. get { return m_SizeType; }
  65. set { if (PropertyUtil.SetStruct(ref m_SizeType, value)) SetVerticesDirty(); }
  66. }
  67. /// <summary>
  68. /// whitch data index is when the sizeType assined as FromData.
  69. /// |当sizeType指定为FromData时,指定的数据源索引。
  70. /// </summary>
  71. public int dataIndex
  72. {
  73. get { return m_DataIndex; }
  74. set { if (PropertyUtil.SetStruct(ref m_DataIndex, value)) SetVerticesDirty(); }
  75. }
  76. /// <summary>
  77. /// the scale of data when sizeType assined as FromData.
  78. /// |当sizeType指定为FromData时,指定的倍数系数。
  79. /// </summary>
  80. public float dataScale
  81. {
  82. get { return m_DataScale; }
  83. set { if (PropertyUtil.SetStruct(ref m_DataScale, value)) SetVerticesDirty(); }
  84. }
  85. /// <summary>
  86. /// the function of size when sizeType assined as Function.
  87. /// |当sizeType指定为Function时,指定的委托函数。
  88. /// </summary>
  89. public SymbolSizeFunction sizeFunction
  90. {
  91. get { return m_SizeFunction; }
  92. set { if (PropertyUtil.SetClass(ref m_SizeFunction, value)) SetVerticesDirty(); }
  93. }
  94. /// <summary>
  95. /// the index start to show symbol.
  96. /// |开始显示图形标记的索引。
  97. /// </summary>
  98. public int startIndex
  99. {
  100. get { return m_StartIndex; }
  101. set { if (PropertyUtil.SetStruct(ref m_StartIndex, value)) SetVerticesDirty(); }
  102. }
  103. /// <summary>
  104. /// the interval of show symbol.
  105. /// |显示图形标记的间隔。0表示显示所有标签,1表示隔一个隔显示一个标签,以此类推。
  106. /// </summary>
  107. public int interval
  108. {
  109. get { return m_Interval; }
  110. set { if (PropertyUtil.SetStruct(ref m_Interval, value)) SetVerticesDirty(); }
  111. }
  112. /// <summary>
  113. /// whether to show the last symbol.
  114. /// |是否强制显示最后一个图形标记。
  115. /// </summary>
  116. public bool forceShowLast
  117. {
  118. get { return m_ForceShowLast; }
  119. set { if (PropertyUtil.SetStruct(ref m_ForceShowLast, value)) SetVerticesDirty(); }
  120. }
  121. /// <summary>
  122. /// 图形是否重复。
  123. /// </summary>
  124. public bool repeat
  125. {
  126. get { return m_Repeat; }
  127. set { if (PropertyUtil.SetStruct(ref m_Repeat, value)) SetAllDirty(); }
  128. }
  129. /// <summary>
  130. /// Minimum symbol size.
  131. /// |图形最小尺寸。只在sizeType为SymbolSizeType.FromData时有效。
  132. /// </summary>
  133. public float minSize
  134. {
  135. get { return m_MinSize; }
  136. set { if (PropertyUtil.SetStruct(ref m_MinSize, value)) SetVerticesDirty(); }
  137. }
  138. /// <summary>
  139. /// Maximum symbol size.
  140. /// |图形最大尺寸。只在sizeType为SymbolSizeType.FromData时有效。
  141. /// </summary>
  142. public float maxSize
  143. {
  144. get { return m_MaxSize; }
  145. set { if (PropertyUtil.SetStruct(ref m_MaxSize, value)) SetVerticesDirty(); }
  146. }
  147. /// <summary>
  148. /// 根据指定的sizeType获得标记的大小
  149. /// </summary>
  150. /// <param name="data"></param>
  151. /// <returns></returns>
  152. public float GetSize(List<double> data, float themeSize)
  153. {
  154. switch (m_SizeType)
  155. {
  156. case SymbolSizeType.Custom:
  157. return size == 0 ? themeSize : size;
  158. case SymbolSizeType.FromData:
  159. if (data != null && dataIndex >= 0 && dataIndex < data.Count)
  160. {
  161. var value = (float) data[dataIndex] * m_DataScale;
  162. if (m_MinSize != 0 && value < m_MinSize) value = m_MinSize;
  163. if (m_MaxSize != 0 && value > m_MaxSize) value = m_MaxSize;
  164. return value;
  165. }
  166. else
  167. {
  168. return size == 0 ? themeSize : size;
  169. }
  170. case SymbolSizeType.Function:
  171. if (data != null && sizeFunction != null) return sizeFunction(data);
  172. else return size == 0 ? themeSize : size;
  173. default:
  174. return size == 0 ? themeSize : size;
  175. }
  176. }
  177. public bool ShowSymbol(int dataIndex, int dataCount)
  178. {
  179. if (!show)
  180. return false;
  181. if (dataIndex < startIndex)
  182. return false;
  183. if (m_Interval <= 0)
  184. return true;
  185. if (m_ForceShowLast && dataIndex == dataCount - 1)
  186. return true;
  187. return (dataIndex - startIndex) % (m_Interval + 1) == 0;
  188. }
  189. }
  190. }