No Description
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.

Legend.cs 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace XCharts.Runtime
  4. {
  5. /// <summary>
  6. /// Legend component.The legend component shows different sets of tags, colors, and names.
  7. /// You can control which series are not displayed by clicking on the legend.
  8. /// |图例组件。
  9. /// 图例组件展现了不同系列的标记,颜色和名字。可以通过点击图例控制哪些系列不显示。
  10. /// </summary>
  11. [System.Serializable]
  12. [ComponentHandler(typeof(LegendHandler), true)]
  13. public class Legend : MainComponent, IPropertyChanged
  14. {
  15. public enum Type
  16. {
  17. /// <summary>
  18. /// 自动匹配。
  19. /// </summary>
  20. Auto,
  21. /// <summary>
  22. /// 自定义图标。
  23. /// </summary>
  24. Custom,
  25. /// <summary>
  26. /// 空心圆。
  27. /// </summary>
  28. EmptyCircle,
  29. /// <summary>
  30. /// 圆形。
  31. /// </summary>
  32. Circle,
  33. /// <summary>
  34. /// 正方形。可通过Setting的legendIconCornerRadius参数调整圆角。
  35. /// </summary>
  36. Rect,
  37. /// <summary>
  38. /// 三角形。
  39. /// </summary>
  40. Triangle,
  41. /// <summary>
  42. /// 菱形。
  43. /// </summary>
  44. Diamond,
  45. /// <summary>
  46. /// 烛台(可用于K线图)。
  47. /// </summary>
  48. Candlestick,
  49. }
  50. /// <summary>
  51. /// Selected mode of legend, which controls whether series can be toggled displaying by clicking legends.
  52. /// |图例选择的模式,控制是否可以通过点击图例改变系列的显示状态。默认开启图例选择,可以设成 None 关闭。
  53. /// </summary>
  54. public enum SelectedMode
  55. {
  56. /// <summary>
  57. /// 多选。
  58. /// </summary>
  59. Multiple,
  60. /// <summary>
  61. /// 单选。
  62. /// </summary>
  63. Single,
  64. /// <summary>
  65. /// 无法选择。
  66. /// </summary>
  67. None
  68. }
  69. [SerializeField] private bool m_Show = true;
  70. [SerializeField] private Type m_IconType = Type.Auto;
  71. [SerializeField] private SelectedMode m_SelectedMode = SelectedMode.Multiple;
  72. [SerializeField] private Orient m_Orient = Orient.Horizonal;
  73. [SerializeField] private Location m_Location = new Location() { align = Location.Align.TopCenter, top = 0.125f };
  74. [SerializeField] private float m_ItemWidth = 25.0f;
  75. [SerializeField] private float m_ItemHeight = 12.0f;
  76. [SerializeField] private float m_ItemGap = 10f;
  77. [SerializeField] private bool m_ItemAutoColor = true;
  78. [SerializeField] private float m_ItemOpacity = 1;
  79. [SerializeField] private string m_Formatter;
  80. [SerializeField] protected string m_NumericFormatter = "";
  81. [SerializeField] private LabelStyle m_LabelStyle = new LabelStyle();
  82. [SerializeField] private List<string> m_Data = new List<string>();
  83. [SerializeField] private List<Sprite> m_Icons = new List<Sprite>();
  84. [SerializeField] private List<Color> m_Colors = new List<Color>();
  85. [SerializeField][Since("v3.1.0")] protected ImageStyle m_Background = new ImageStyle() { show = false };
  86. [SerializeField][Since("v3.1.0")] protected Padding m_Padding = new Padding();
  87. public LegendContext context = new LegendContext();
  88. /// <summary>
  89. /// Whether to show legend component.
  90. /// |是否显示图例组件。
  91. /// </summary>
  92. public bool show
  93. {
  94. get { return m_Show; }
  95. set { if (PropertyUtil.SetStruct(ref m_Show, value)) SetComponentDirty(); }
  96. }
  97. /// <summary>
  98. /// Type of legend.
  99. /// |图例类型。
  100. /// </summary>
  101. public Type iconType
  102. {
  103. get { return m_IconType; }
  104. set { if (PropertyUtil.SetStruct(ref m_IconType, value)) SetAllDirty(); }
  105. }
  106. /// <summary>
  107. /// Selected mode of legend, which controls whether series can be toggled displaying by clicking legends.
  108. /// |选择模式。控制是否可以通过点击图例改变系列的显示状态。默认开启图例选择,可以设成 None 关闭。
  109. /// </summary>
  110. public SelectedMode selectedMode
  111. {
  112. get { return m_SelectedMode; }
  113. set { if (PropertyUtil.SetStruct(ref m_SelectedMode, value)) SetComponentDirty(); }
  114. }
  115. /// <summary>
  116. /// Specify whether the layout of legend component is horizontal or vertical.
  117. /// |布局方式是横还是竖。
  118. /// </summary>
  119. public Orient orient
  120. {
  121. get { return m_Orient; }
  122. set { if (PropertyUtil.SetStruct(ref m_Orient, value)) SetComponentDirty(); }
  123. }
  124. /// <summary>
  125. /// The location of legend.
  126. /// |图例显示的位置。
  127. /// </summary>
  128. public Location location
  129. {
  130. get { return m_Location; }
  131. set { if (PropertyUtil.SetClass(ref m_Location, value)) SetComponentDirty(); }
  132. }
  133. /// <summary>
  134. /// Image width of legend symbol.
  135. /// |图例标记的图形宽度。
  136. /// </summary>
  137. public float itemWidth
  138. {
  139. get { return m_ItemWidth; }
  140. set { if (PropertyUtil.SetStruct(ref m_ItemWidth, value)) SetComponentDirty(); }
  141. }
  142. /// <summary>
  143. /// Image height of legend symbol.
  144. /// |图例标记的图形高度。
  145. /// </summary>
  146. public float itemHeight
  147. {
  148. get { return m_ItemHeight; }
  149. set { if (PropertyUtil.SetStruct(ref m_ItemHeight, value)) SetComponentDirty(); }
  150. }
  151. /// <summary>
  152. /// The distance between each legend, horizontal distance in horizontal layout, and vertical distance in vertical layout.
  153. /// |图例每项之间的间隔。横向布局时为水平间隔,纵向布局时为纵向间隔。
  154. /// </summary>
  155. public float itemGap
  156. {
  157. get { return m_ItemGap; }
  158. set { if (PropertyUtil.SetStruct(ref m_ItemGap, value)) SetComponentDirty(); }
  159. }
  160. /// <summary>
  161. /// Whether the legend symbol matches the color automatically.
  162. /// |图例标记的图形是否自动匹配颜色。
  163. /// </summary>
  164. public bool itemAutoColor
  165. {
  166. get { return m_ItemAutoColor; }
  167. set { if (PropertyUtil.SetStruct(ref m_ItemAutoColor, value)) SetComponentDirty(); }
  168. }
  169. /// <summary>
  170. /// the opacity of item color.
  171. /// |图例标记的图形的颜色透明度。
  172. /// </summary>
  173. public float itemOpacity
  174. {
  175. get { return m_ItemOpacity; }
  176. set { if (PropertyUtil.SetStruct(ref m_ItemOpacity, value)) SetComponentDirty(); }
  177. }
  178. /// <summary>
  179. /// Standard numeric format strings.
  180. /// |标准数字格式字符串。用于将数值格式化显示为字符串。
  181. /// 使用Axx的形式:A是格式说明符的单字符,支持C货币、D十进制、E指数、F定点数、G常规、N数字、P百分比、R往返、X十六进制的。xx是精度说明,从0-99。
  182. /// 参考:https://docs.microsoft.com/zh-cn/dotnet/standard/base-types/standard-numeric-format-strings
  183. /// </summary>
  184. public string numericFormatter
  185. {
  186. get { return m_NumericFormatter; }
  187. set { if (PropertyUtil.SetClass(ref m_NumericFormatter, value)) SetComponentDirty(); }
  188. }
  189. /// <summary>
  190. /// Legend content string template formatter. Support for wrapping lines with \n. Template:{value}.
  191. /// |图例内容字符串模版格式器。支持用 \n 换行。
  192. /// 模板变量为图例名称 {value}。其他模板变量参考Toolip的itemFormatter。
  193. /// </summary>
  194. public string formatter
  195. {
  196. get { return m_Formatter; }
  197. set { if (PropertyUtil.SetClass(ref m_Formatter, value)) SetComponentDirty(); }
  198. }
  199. /// <summary>
  200. /// the style of text.
  201. /// |文本样式。
  202. /// </summary>
  203. public LabelStyle labelStyle
  204. {
  205. get { return m_LabelStyle; }
  206. set { if (PropertyUtil.SetClass(ref m_LabelStyle, value)) SetComponentDirty(); }
  207. }
  208. /// <summary>
  209. /// the sytle of background.
  210. /// |背景图样式。
  211. /// </summary>
  212. public ImageStyle background
  213. {
  214. get { return m_Background; }
  215. set { if (PropertyUtil.SetClass(ref m_Background, value)) SetAllDirty(); }
  216. }
  217. /// <summary>
  218. /// the paddinng of item and background.
  219. /// |图例标记和背景的间距。
  220. /// </summary>
  221. public Padding padding
  222. {
  223. get { return m_Padding; }
  224. set { if (PropertyUtil.SetClass(ref m_Padding, value)) SetAllDirty(); }
  225. }
  226. /// <summary>
  227. /// Data array of legend. An array item is usually a name representing string. (If it is a pie chart,
  228. /// it could also be the name of a single data in the pie chart) of a series.
  229. /// If data is not specified, it will be auto collected from series.
  230. /// |图例的数据数组。数组项通常为一个字符串,每一项代表一个系列的 name(如果是饼图,也可以是饼图单个数据的 name)。
  231. /// 如果 data 没有被指定,会自动从当前系列中获取。指定data时里面的数据项和serie匹配时才会生效。
  232. /// </summary>
  233. public List<string> data
  234. {
  235. get { return m_Data; }
  236. set { if (value != null) { m_Data = value; SetComponentDirty(); } }
  237. }
  238. /// <summary>
  239. /// 自定义的图例标记图形。
  240. /// </summary>
  241. public List<Sprite> icons
  242. {
  243. get { return m_Icons; }
  244. set { if (value != null) { m_Icons = value; SetComponentDirty(); } }
  245. }
  246. /// <summary>
  247. /// the colors of legend item.
  248. /// |图例标记的颜色列表。
  249. /// </summary>
  250. public List<Color> colors
  251. {
  252. get { return m_Colors; }
  253. set { if (value != null) { m_Colors = value; SetAllDirty(); } }
  254. }
  255. /// <summary>
  256. /// 图表是否需要刷新(图例组件不需要刷新图表)
  257. /// </summary>
  258. public override bool vertsDirty { get { return false; } }
  259. /// <summary>
  260. /// 组件是否需要刷新
  261. /// </summary>
  262. public override bool componentDirty
  263. {
  264. get { return m_ComponentDirty || location.componentDirty || labelStyle.componentDirty; }
  265. }
  266. public override void ClearComponentDirty()
  267. {
  268. base.ClearComponentDirty();
  269. location.ClearComponentDirty();
  270. labelStyle.ClearComponentDirty();
  271. }
  272. /// <summary>
  273. /// Clear legend data.
  274. /// |清空。
  275. /// </summary>
  276. public override void ClearData()
  277. {
  278. m_Data.Clear();
  279. SetComponentDirty();
  280. }
  281. /// <summary>
  282. /// Whether include in legend data by the specified name.
  283. /// |是否包括由指定名字的图例
  284. /// </summary>
  285. /// <param name="name"></param>
  286. /// <returns></returns>
  287. public bool ContainsData(string name)
  288. {
  289. return m_Data.Contains(name);
  290. }
  291. /// <summary>
  292. /// Removes the legend with the specified name.
  293. /// |移除指定名字的图例。
  294. /// </summary>
  295. /// <param name="name"></param>
  296. public void RemoveData(string name)
  297. {
  298. if (m_Data.Contains(name))
  299. {
  300. m_Data.Remove(name);
  301. SetComponentDirty();
  302. }
  303. }
  304. /// <summary>
  305. /// Add legend data.
  306. /// |添加图例。
  307. /// </summary>
  308. /// <param name="name"></param>
  309. public void AddData(string name)
  310. {
  311. if (!m_Data.Contains(name) && !string.IsNullOrEmpty(name))
  312. {
  313. m_Data.Add(name);
  314. SetComponentDirty();
  315. }
  316. }
  317. /// <summary>
  318. /// Gets the legend for the specified index.
  319. /// |获得指定索引的图例。
  320. /// </summary>
  321. /// <param name="index"></param>
  322. /// <returns></returns>
  323. public string GetData(int index)
  324. {
  325. if (index >= 0 && index < m_Data.Count)
  326. {
  327. return m_Data[index];
  328. }
  329. return null;
  330. }
  331. /// <summary>
  332. /// Gets the index of the specified legend.
  333. /// |获得指定图例的索引。
  334. /// </summary>
  335. /// <param name="legendName"></param>
  336. /// <returns></returns>
  337. public int GetIndex(string legendName)
  338. {
  339. return m_Data.IndexOf(legendName);
  340. }
  341. /// <summary>
  342. /// Remove all legend buttons.
  343. /// |移除所有图例按钮。
  344. /// </summary>
  345. public void RemoveButton()
  346. {
  347. context.buttonList.Clear();
  348. }
  349. /// <summary>
  350. /// Bind buttons to legends.
  351. /// |给图例绑定按钮。
  352. /// </summary>
  353. /// <param name="name"></param>
  354. /// <param name="btn"></param>
  355. /// <param name="total"></param>
  356. public void SetButton(string name, LegendItem item, int total)
  357. {
  358. context.buttonList[name] = item;
  359. int index = context.buttonList.Values.Count;
  360. item.SetIconActive(iconType == Type.Custom);
  361. item.SetActive(show);
  362. }
  363. /// <summary>
  364. /// Update the legend button color.
  365. /// |更新图例按钮颜色。
  366. /// </summary>
  367. /// <param name="name"></param>
  368. /// <param name="color"></param>
  369. public void UpdateButtonColor(string name, Color color)
  370. {
  371. if (context.buttonList.ContainsKey(name))
  372. {
  373. context.buttonList[name].SetIconColor(color);
  374. }
  375. }
  376. /// <summary>
  377. /// Update the text color of legend.
  378. /// |更新图例文字颜色。
  379. /// </summary>
  380. /// <param name="name"></param>
  381. /// <param name="color"></param>
  382. public void UpdateContentColor(string name, Color color)
  383. {
  384. if (context.buttonList.ContainsKey(name))
  385. {
  386. context.buttonList[name].SetContentColor(color);
  387. }
  388. }
  389. /// <summary>
  390. /// Gets the legend button for the specified index.
  391. /// |获得指定索引的图例按钮。
  392. /// </summary>
  393. /// <param name="index"></param>
  394. /// <returns></returns>
  395. public Sprite GetIcon(int index)
  396. {
  397. if (index >= 0 && index < m_Icons.Count)
  398. {
  399. return m_Icons[index];
  400. }
  401. else
  402. {
  403. return null;
  404. }
  405. }
  406. public Color GetColor(int index)
  407. {
  408. if (index >= 0 && index < m_Colors.Count)
  409. return m_Colors[index];
  410. else
  411. return Color.white;
  412. }
  413. /// <summary>
  414. /// Callback handling when parameters change.
  415. /// |参数变更时的回调处理。
  416. /// </summary>
  417. public void OnChanged()
  418. {
  419. m_Location.OnChanged();
  420. }
  421. }
  422. }