Ei kuvausta
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.

LineStyle.cs 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. /// <summary>
  49. /// Whether show line.
  50. /// |是否显示线条。当作为子组件,它的父组件有参数控制是否显示时,改参数无效。
  51. /// </summary>
  52. public bool show
  53. {
  54. get { return m_Show; }
  55. set { if (PropertyUtil.SetStruct(ref m_Show, value)) SetVerticesDirty(); }
  56. }
  57. /// <summary>
  58. /// the type of line.
  59. /// |线的类型。
  60. /// </summary>
  61. public Type type
  62. {
  63. get { return m_Type; }
  64. set { if (PropertyUtil.SetStruct(ref m_Type, value)) SetVerticesDirty(); }
  65. }
  66. /// <summary>
  67. /// the color of line, default use serie color.
  68. /// |线的颜色。
  69. /// </summary>
  70. public Color32 color
  71. {
  72. get { return m_Color; }
  73. set { if (PropertyUtil.SetColor(ref m_Color, value)) SetVerticesDirty(); }
  74. }
  75. /// <summary>
  76. /// the middle color of line, default use serie color.
  77. /// |线的渐变颜色(需要水平方向渐变时)。
  78. /// </summary>
  79. public Color32 toColor
  80. {
  81. get { return m_ToColor; }
  82. set { if (PropertyUtil.SetColor(ref m_ToColor, value)) SetVerticesDirty(); }
  83. }
  84. /// <summary>
  85. /// the end color of line, default use serie color.
  86. /// |线的渐变颜色2(需要水平方向三个渐变色的渐变时)。
  87. /// </summary>
  88. public Color32 toColor2
  89. {
  90. get { return m_ToColor2; }
  91. set { if (PropertyUtil.SetColor(ref m_ToColor2, value)) SetVerticesDirty(); }
  92. }
  93. /// <summary>
  94. /// the width of line.
  95. /// |线宽。
  96. /// </summary>
  97. public float width
  98. {
  99. get { return m_Width; }
  100. set { if (PropertyUtil.SetStruct(ref m_Width, value)) SetVerticesDirty(); }
  101. }
  102. /// <summary>
  103. /// the length of line.
  104. /// |线长。
  105. /// </summary>
  106. public float length
  107. {
  108. get { return m_Length; }
  109. set { if (PropertyUtil.SetStruct(ref m_Length, value)) SetVerticesDirty(); }
  110. }
  111. /// <summary>
  112. /// Opacity of the line. Supports value from 0 to 1, and the line will not be drawn when set to 0.
  113. /// |线的透明度。支持从 0 到 1 的数字,为 0 时不绘制该图形。
  114. /// </summary>
  115. public float opacity
  116. {
  117. get { return m_Opacity; }
  118. set { if (PropertyUtil.SetStruct(ref m_Opacity, value)) SetVerticesDirty(); }
  119. }
  120. public LineStyle()
  121. { }
  122. public LineStyle(float width)
  123. {
  124. this.width = width;
  125. }
  126. public LineStyle(LineStyle.Type type)
  127. {
  128. this.type = type;
  129. }
  130. public LineStyle(LineStyle.Type type, float width)
  131. {
  132. this.type = type;
  133. this.width = width;
  134. }
  135. public LineStyle Clone()
  136. {
  137. var lineStyle = new LineStyle();
  138. lineStyle.show = show;
  139. lineStyle.type = type;
  140. lineStyle.color = color;
  141. lineStyle.toColor = toColor;
  142. lineStyle.toColor2 = toColor2;
  143. lineStyle.width = width;
  144. lineStyle.opacity = opacity;
  145. return lineStyle;
  146. }
  147. public void Copy(LineStyle lineStyle)
  148. {
  149. show = lineStyle.show;
  150. type = lineStyle.type;
  151. color = lineStyle.color;
  152. toColor = lineStyle.toColor;
  153. toColor2 = lineStyle.toColor2;
  154. width = lineStyle.width;
  155. opacity = lineStyle.opacity;
  156. }
  157. public Color32 GetColor()
  158. {
  159. if (m_Opacity == 1)
  160. return m_Color;
  161. var color = m_Color;
  162. color.a = (byte) (color.a * m_Opacity);
  163. return color;
  164. }
  165. public bool IsNeedGradient()
  166. {
  167. return !ChartHelper.IsClearColor(m_ToColor) || !ChartHelper.IsClearColor(m_ToColor2);
  168. }
  169. public Color32 GetGradientColor(float value, Color32 defaultColor)
  170. {
  171. var color = ChartConst.clearColor32;
  172. if (!IsNeedGradient())
  173. return color;
  174. value = Mathf.Clamp01(value);
  175. var startColor = ChartHelper.IsClearColor(m_Color) ? defaultColor : m_Color;
  176. if (!ChartHelper.IsClearColor(m_ToColor2))
  177. {
  178. if (value <= 0.5f)
  179. color = Color32.Lerp(startColor, m_ToColor, 2 * value);
  180. else
  181. color = Color32.Lerp(m_ToColor, m_ToColor2, 2 * (value - 0.5f));
  182. }
  183. else
  184. {
  185. color = Color32.Lerp(startColor, m_ToColor, value);
  186. }
  187. if (m_Opacity != 1)
  188. {
  189. color.a = (byte) (color.a * m_Opacity);
  190. }
  191. return color;
  192. }
  193. public Type GetType(Type themeType)
  194. {
  195. return type == Type.None ? themeType : type;
  196. }
  197. public float GetWidth(float themeWidth)
  198. {
  199. return width == 0 ? themeWidth : width;
  200. }
  201. public float GetLength(float themeLength)
  202. {
  203. return length == 0 ? themeLength : length;
  204. }
  205. public Color32 GetColor(Color32 themeColor)
  206. {
  207. if (!ChartHelper.IsClearColor(color))
  208. {
  209. return GetColor();
  210. }
  211. else
  212. {
  213. var color = themeColor;
  214. color.a = (byte) (color.a * opacity);
  215. return color;
  216. }
  217. }
  218. }
  219. }