Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Legend.cs 16KB

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