LineStyle.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using UnityEngine;
  2. namespace XCharts.Runtime
  3. {
  4. /// <summary>
  5. /// The style of line.
  6. /// ||线条样式。
  7. /// 注: 修改 lineStyle 中的颜色不会影响图例颜色,如果需要图例颜色和折线图颜色一致,需修改 itemStyle.color,线条颜色默认也会取该颜色。
  8. /// toColor,toColor2可设置水平方向的渐变,如需要设置垂直方向的渐变,可使用VisualMap。
  9. /// </summary>
  10. [System.Serializable]
  11. public class LineStyle : ChildComponent, ISerieDataComponent
  12. {
  13. /// <summary>
  14. /// 线的类型。
  15. /// </summary>
  16. public enum Type
  17. {
  18. /// <summary>
  19. /// 实线
  20. /// </summary>
  21. Solid,
  22. /// <summary>
  23. /// 虚线
  24. /// </summary>
  25. Dashed,
  26. /// <summary>
  27. /// 点线
  28. /// </summary>
  29. Dotted,
  30. /// <summary>
  31. /// 点划线
  32. /// </summary>
  33. DashDot,
  34. /// <summary>
  35. /// 双点划线
  36. /// </summary>
  37. DashDotDot,
  38. None,
  39. }
  40. [SerializeField] private bool m_Show = true;
  41. [SerializeField] private Type m_Type = Type.Solid;
  42. [SerializeField] private Color32 m_Color;
  43. [SerializeField] private Color32 m_ToColor;
  44. [SerializeField] private Color32 m_ToColor2;
  45. [SerializeField] private float m_Width = 0;
  46. [SerializeField] private float m_Length = 0;
  47. [SerializeField][Range(0, 1)] private float m_Opacity = 1;
  48. [SerializeField][Since("v3.8.1")] private float m_DashLength = 4;
  49. [SerializeField][Since("v3.8.1")] private float m_DotLength = 2;
  50. [SerializeField][Since("v3.8.1")] private float m_GapLength = 2;
  51. /// <summary>
  52. /// Whether show line.
  53. /// ||是否显示线条。当作为子组件,它的父组件有参数控制是否显示时,改参数无效。
  54. /// </summary>
  55. public bool show
  56. {
  57. get { return m_Show; }
  58. set { if (PropertyUtil.SetStruct(ref m_Show, value)) SetVerticesDirty(); }
  59. }
  60. /// <summary>
  61. /// the type of line.
  62. /// ||线的类型。
  63. /// </summary>
  64. public Type type
  65. {
  66. get { return m_Type; }
  67. set { if (PropertyUtil.SetStruct(ref m_Type, value)) SetVerticesDirty(); }
  68. }
  69. /// <summary>
  70. /// the color of line, default use serie color.
  71. /// ||线的颜色。
  72. /// </summary>
  73. public Color32 color
  74. {
  75. get { return m_Color; }
  76. set { if (PropertyUtil.SetColor(ref m_Color, value)) SetVerticesDirty(); }
  77. }
  78. /// <summary>
  79. /// the middle color of line, default use serie color.
  80. /// ||线的渐变颜色(需要水平方向渐变时)。
  81. /// </summary>
  82. public Color32 toColor
  83. {
  84. get { return m_ToColor; }
  85. set { if (PropertyUtil.SetColor(ref m_ToColor, value)) SetVerticesDirty(); }
  86. }
  87. /// <summary>
  88. /// the end color of line, default use serie color.
  89. /// ||线的渐变颜色2(需要水平方向三个渐变色的渐变时)。
  90. /// </summary>
  91. public Color32 toColor2
  92. {
  93. get { return m_ToColor2; }
  94. set { if (PropertyUtil.SetColor(ref m_ToColor2, value)) SetVerticesDirty(); }
  95. }
  96. /// <summary>
  97. /// the width of line.
  98. /// ||线宽。
  99. /// </summary>
  100. public float width
  101. {
  102. get { return m_Width; }
  103. set { if (PropertyUtil.SetStruct(ref m_Width, value)) SetVerticesDirty(); }
  104. }
  105. /// <summary>
  106. /// the length of line.
  107. /// ||线长。
  108. /// </summary>
  109. public float length
  110. {
  111. get { return m_Length; }
  112. set { if (PropertyUtil.SetStruct(ref m_Length, value)) SetVerticesDirty(); }
  113. }
  114. /// <summary>
  115. /// Opacity of the line. Supports value from 0 to 1, and the line will not be drawn when set to 0.
  116. /// ||线的透明度。支持从 0 到 1 的数字,为 0 时不绘制该图形。
  117. /// </summary>
  118. public float opacity
  119. {
  120. get { return m_Opacity; }
  121. set { if (PropertyUtil.SetStruct(ref m_Opacity, value)) SetVerticesDirty(); }
  122. }
  123. /// <summary>
  124. /// the length of dash line. default value is 0, which means the length of dash line is 12 times of line width.
  125. /// Represents a multiple of the number of segments in a line chart.
  126. /// ||虚线的长度。默认0时为线条宽度的12倍。在折线图中代表分割段数的倍数。
  127. /// </summary>
  128. public float dashLength
  129. {
  130. get { return m_DashLength; }
  131. set { if (PropertyUtil.SetStruct(ref m_DashLength, value)) SetVerticesDirty(); }
  132. }
  133. /// <summary>
  134. /// the length of dot line. default value is 0, which means the length of dot line is 2 times of line width.
  135. /// Represents a multiple of the number of segments in a line chart.
  136. /// ||点线的长度。默认0时为线条宽度的3倍。在折线图中代表分割段数的倍数。
  137. /// </summary>
  138. public float dotLength
  139. {
  140. get { return m_DotLength; }
  141. set { if (PropertyUtil.SetStruct(ref m_DotLength, value)) SetVerticesDirty(); }
  142. }
  143. /// <summary>
  144. /// the length of gap line. default value is 0, which means the length of gap line is 3 times of line width.
  145. /// Represents a multiple of the number of segments in a line chart.
  146. /// ||点线的长度。默认0时为线条宽度的3倍。在折线图中代表分割段数的倍数。
  147. /// </summary>
  148. public float gapLength
  149. {
  150. get { return m_GapLength; }
  151. set { if (PropertyUtil.SetStruct(ref m_GapLength, value)) SetVerticesDirty(); }
  152. }
  153. public LineStyle()
  154. { }
  155. public LineStyle(float width)
  156. {
  157. this.width = width;
  158. }
  159. public LineStyle(LineStyle.Type type)
  160. {
  161. this.type = type;
  162. }
  163. public LineStyle(LineStyle.Type type, float width)
  164. {
  165. this.type = type;
  166. this.width = width;
  167. }
  168. public LineStyle Clone()
  169. {
  170. var lineStyle = new LineStyle();
  171. lineStyle.show = show;
  172. lineStyle.type = type;
  173. lineStyle.color = color;
  174. lineStyle.toColor = toColor;
  175. lineStyle.toColor2 = toColor2;
  176. lineStyle.width = width;
  177. lineStyle.opacity = opacity;
  178. lineStyle.dashLength = dashLength;
  179. lineStyle.dotLength = dotLength;
  180. lineStyle.gapLength = gapLength;
  181. return lineStyle;
  182. }
  183. public void Copy(LineStyle lineStyle)
  184. {
  185. show = lineStyle.show;
  186. type = lineStyle.type;
  187. color = lineStyle.color;
  188. toColor = lineStyle.toColor;
  189. toColor2 = lineStyle.toColor2;
  190. width = lineStyle.width;
  191. opacity = lineStyle.opacity;
  192. dashLength = lineStyle.dashLength;
  193. dotLength = lineStyle.dotLength;
  194. gapLength = lineStyle.gapLength;
  195. }
  196. public bool IsNotSolidLine()
  197. {
  198. return type != Type.Solid && type != Type.None;
  199. }
  200. public Color32 GetColor()
  201. {
  202. if (m_Opacity == 1)
  203. return m_Color;
  204. var color = m_Color;
  205. color.a = (byte)(color.a * m_Opacity);
  206. return color;
  207. }
  208. public bool IsNeedGradient()
  209. {
  210. return !ChartHelper.IsClearColor(m_ToColor) || !ChartHelper.IsClearColor(m_ToColor2);
  211. }
  212. public Color32 GetGradientColor(float value, Color32 defaultColor)
  213. {
  214. var color = ChartConst.clearColor32;
  215. if (!IsNeedGradient())
  216. return color;
  217. value = Mathf.Clamp01(value);
  218. var startColor = ChartHelper.IsClearColor(m_Color) ? defaultColor : m_Color;
  219. if (!ChartHelper.IsClearColor(m_ToColor2))
  220. {
  221. if (value <= 0.5f)
  222. color = Color32.Lerp(startColor, m_ToColor, 2 * value);
  223. else
  224. color = Color32.Lerp(m_ToColor, m_ToColor2, 2 * (value - 0.5f));
  225. }
  226. else
  227. {
  228. color = Color32.Lerp(startColor, m_ToColor, value);
  229. }
  230. if (m_Opacity != 1)
  231. {
  232. color.a = (byte)(color.a * m_Opacity);
  233. }
  234. return color;
  235. }
  236. public Type GetType(Type themeType)
  237. {
  238. return type == Type.None ? themeType : type;
  239. }
  240. public float GetWidth(float themeWidth)
  241. {
  242. return width == 0 ? themeWidth : width;
  243. }
  244. public float GetLength(float themeLength)
  245. {
  246. return length == 0 ? themeLength : length;
  247. }
  248. public Color32 GetColor(Color32 themeColor)
  249. {
  250. if (!ChartHelper.IsClearColor(color))
  251. {
  252. return GetColor();
  253. }
  254. else
  255. {
  256. var color = themeColor;
  257. color.a = (byte)(color.a * opacity);
  258. return color;
  259. }
  260. }
  261. }
  262. }