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.

SerieData.cs 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using XUGL;
  5. namespace XCharts.Runtime
  6. {
  7. /// <summary>
  8. /// A data item of serie.
  9. /// |系列中的一个数据项。可存储数据名和1-n维个数据。
  10. /// </summary>
  11. [System.Serializable]
  12. public class SerieData : ChildComponent
  13. {
  14. public static List<string> extraFieldList = new List<string>()
  15. {
  16. "m_Id",
  17. "m_ParentId",
  18. "m_State",
  19. "m_Ignore",
  20. "m_Selected",
  21. "m_Radius",
  22. };
  23. public static Dictionary<Type, string> extraComponentMap = new Dictionary<Type, string>
  24. { { typeof(ItemStyle), "m_ItemStyles" },
  25. { typeof(LabelStyle), "m_Labels" },
  26. { typeof(LabelLine), "m_LabelLines" },
  27. { typeof(SerieSymbol), "m_Symbols" },
  28. { typeof(LineStyle), "m_LineStyles" },
  29. { typeof(AreaStyle), "m_AreaStyles" },
  30. { typeof(TitleStyle), "m_TitleStyles" },
  31. { typeof(EmphasisStyle), "m_EmphasisStyles" },
  32. { typeof(BlurStyle), "m_BlurStyles" },
  33. { typeof(SelectStyle), "m_SelectStyles" },
  34. };
  35. [SerializeField] private int m_Index;
  36. [SerializeField] private string m_Name;
  37. [SerializeField] private string m_Id;
  38. [SerializeField] private string m_ParentId;
  39. [SerializeField] private bool m_Ignore;
  40. [SerializeField] private bool m_Selected;
  41. [SerializeField] private float m_Radius;
  42. [SerializeField][Since("v3.2.0")] private SerieState m_State = SerieState.Auto;
  43. [SerializeField][IgnoreDoc] private List<ItemStyle> m_ItemStyles = new List<ItemStyle>();
  44. [SerializeField][IgnoreDoc] private List<LabelStyle> m_Labels = new List<LabelStyle>();
  45. [SerializeField][IgnoreDoc] private List<LabelLine> m_LabelLines = new List<LabelLine>();
  46. [SerializeField][IgnoreDoc] private List<SerieSymbol> m_Symbols = new List<SerieSymbol>();
  47. [SerializeField][IgnoreDoc] private List<LineStyle> m_LineStyles = new List<LineStyle>();
  48. [SerializeField][IgnoreDoc] private List<AreaStyle> m_AreaStyles = new List<AreaStyle>();
  49. [SerializeField][IgnoreDoc] private List<TitleStyle> m_TitleStyles = new List<TitleStyle>();
  50. [SerializeField][IgnoreDoc] private List<EmphasisStyle> m_EmphasisStyles = new List<EmphasisStyle>();
  51. [SerializeField][IgnoreDoc] private List<BlurStyle> m_BlurStyles = new List<BlurStyle>();
  52. [SerializeField][IgnoreDoc] private List<SelectStyle> m_SelectStyles = new List<SelectStyle>();
  53. [SerializeField] private List<double> m_Data = new List<double>();
  54. [NonSerialized] public SerieDataContext context = new SerieDataContext();
  55. [NonSerialized] public InteractData interact = new InteractData();
  56. public ChartLabel labelObject { get; set; }
  57. public ChartLabel titleObject { get; set; }
  58. private bool m_Show = true;
  59. /// <summary>
  60. /// the index of SerieData.
  61. /// |数据项索引。
  62. /// </summary>
  63. public override int index { get { return m_Index; } set { m_Index = value; } }
  64. /// <summary>
  65. /// the name of data item.
  66. /// |数据项名称。
  67. /// </summary>
  68. public string name { get { return m_Name; } set { m_Name = value; } }
  69. /// <summary>
  70. /// the id of data.
  71. /// |数据项的唯一id。唯一id不是必须设置的。
  72. /// </summary>
  73. public string id { get { return m_Id; } set { m_Id = value; } }
  74. /// <summary>
  75. /// the id of parent SerieData.
  76. /// |父节点id。父节点id不是必须设置的。
  77. /// </summary>
  78. public string parentId { get { return m_ParentId; } set { m_ParentId = value; } }
  79. /// <summary>
  80. /// 是否忽略数据。当为 true 时,数据不进行绘制。
  81. /// </summary>
  82. public bool ignore
  83. {
  84. get { return m_Ignore; }
  85. set { if (PropertyUtil.SetStruct(ref m_Ignore, value)) SetVerticesDirty(); }
  86. }
  87. /// <summary>
  88. /// 自定义半径。可用在饼图中自定义某个数据项的半径。
  89. /// </summary>
  90. public float radius { get { return m_Radius; } set { m_Radius = value; } }
  91. /// <summary>
  92. /// Whether the data item is selected.
  93. /// |该数据项是否被选中。
  94. /// </summary>
  95. public bool selected { get { return m_Selected; } set { m_Selected = value; } }
  96. /// <summary>
  97. /// the state of serie data.
  98. /// |数据项的默认状态。
  99. /// </summary>
  100. public SerieState state { get { return m_State; } set { m_State = value; } }
  101. /// <summary>
  102. /// 数据项图例名称。当数据项名称不为空时,图例名称即为系列名称;反之则为索引index。
  103. /// </summary>
  104. /// <value></value>
  105. public string legendName { get { return string.IsNullOrEmpty(name) ? ChartCached.IntToStr(index) : name; } }
  106. /// <summary>
  107. /// 单个数据项的标签设置。
  108. /// </summary>
  109. public LabelStyle labelStyle { get { return m_Labels.Count > 0 ? m_Labels[0] : null; } }
  110. public LabelLine labelLine { get { return m_LabelLines.Count > 0 ? m_LabelLines[0] : null; } }
  111. /// <summary>
  112. /// 单个数据项的样式设置。
  113. /// </summary>
  114. public ItemStyle itemStyle { get { return m_ItemStyles.Count > 0 ? m_ItemStyles[0] : null; } }
  115. /// <summary>
  116. /// 单个数据项的标记设置。
  117. /// </summary>
  118. public SerieSymbol symbol { get { return m_Symbols.Count > 0 ? m_Symbols[0] : null; } }
  119. public LineStyle lineStyle { get { return m_LineStyles.Count > 0 ? m_LineStyles[0] : null; } }
  120. public AreaStyle areaStyle { get { return m_AreaStyles.Count > 0 ? m_AreaStyles[0] : null; } }
  121. public TitleStyle titleStyle { get { return m_TitleStyles.Count > 0 ? m_TitleStyles[0] : null; } }
  122. /// <summary>
  123. /// 高亮状态的样式
  124. /// </summary>
  125. public EmphasisStyle emphasisStyle { get { return m_EmphasisStyles.Count > 0 ? m_EmphasisStyles[0] : null; } }
  126. /// <summary>
  127. /// 淡出状态的样式。
  128. /// </summary>
  129. public BlurStyle blurStyle { get { return m_BlurStyles.Count > 0 ? m_BlurStyles[0] : null; } }
  130. /// <summary>
  131. /// 选中状态的样式。
  132. /// </summary>
  133. public SelectStyle selectStyle { get { return m_SelectStyles.Count > 0 ? m_SelectStyles[0] : null; } }
  134. /// <summary>
  135. /// An arbitrary dimension data list of data item.
  136. /// |可指定任意维数的数值列表。
  137. /// </summary>
  138. public List<double> data { get { return m_Data; } set { m_Data = value; } }
  139. /// <summary>
  140. /// [default:true] Whether the data item is showed.
  141. /// |该数据项是否要显示。
  142. /// </summary>
  143. public bool show { get { return m_Show; } set { m_Show = value; } }
  144. private List<double> m_PreviousData = new List<double>();
  145. private List<float> m_DataUpdateTime = new List<float>();
  146. private List<bool> m_DataUpdateFlag = new List<bool>();
  147. private List<Vector2> m_PolygonPoints = new List<Vector2>();
  148. public override bool vertsDirty
  149. {
  150. get
  151. {
  152. return m_VertsDirty ||
  153. IsVertsDirty(labelLine) ||
  154. IsVertsDirty(itemStyle) ||
  155. IsVertsDirty(symbol) ||
  156. IsVertsDirty(lineStyle) ||
  157. IsVertsDirty(areaStyle) ||
  158. IsVertsDirty(emphasisStyle) ||
  159. IsVertsDirty(blurStyle) ||
  160. IsVertsDirty(selectStyle);
  161. }
  162. }
  163. public override bool componentDirty
  164. {
  165. get
  166. {
  167. return m_ComponentDirty ||
  168. IsComponentDirty(labelStyle) ||
  169. IsComponentDirty(labelLine) ||
  170. IsComponentDirty(titleStyle) ||
  171. IsComponentDirty(emphasisStyle) ||
  172. IsComponentDirty(blurStyle) ||
  173. IsComponentDirty(selectStyle);
  174. }
  175. }
  176. public override void ClearVerticesDirty()
  177. {
  178. base.ClearVerticesDirty();
  179. ClearVerticesDirty(labelLine);
  180. ClearVerticesDirty(itemStyle);
  181. ClearVerticesDirty(lineStyle);
  182. ClearVerticesDirty(areaStyle);
  183. ClearVerticesDirty(emphasisStyle);
  184. ClearVerticesDirty(blurStyle);
  185. ClearVerticesDirty(selectStyle);
  186. }
  187. public override void ClearComponentDirty()
  188. {
  189. base.ClearComponentDirty();
  190. ClearComponentDirty(labelLine);
  191. ClearComponentDirty(itemStyle);
  192. ClearComponentDirty(lineStyle);
  193. ClearComponentDirty(areaStyle);
  194. ClearComponentDirty(symbol);
  195. ClearComponentDirty(emphasisStyle);
  196. ClearComponentDirty(blurStyle);
  197. ClearComponentDirty(selectStyle);
  198. }
  199. public void Reset()
  200. {
  201. index = 0;
  202. m_Id = null;
  203. m_ParentId = null;
  204. labelObject = null;
  205. m_Name = string.Empty;
  206. m_Show = true;
  207. context.Reset();
  208. interact.Reset();
  209. m_Data.Clear();
  210. m_PreviousData.Clear();
  211. m_DataUpdateTime.Clear();
  212. m_DataUpdateFlag.Clear();
  213. m_Labels.Clear();
  214. m_LabelLines.Clear();
  215. m_ItemStyles.Clear();
  216. m_Symbols.Clear();
  217. m_LineStyles.Clear();
  218. m_AreaStyles.Clear();
  219. m_TitleStyles.Clear();
  220. m_EmphasisStyles.Clear();
  221. m_BlurStyles.Clear();
  222. m_SelectStyles.Clear();
  223. }
  224. public T GetOrAddComponent<T>() where T : ChildComponent, ISerieDataComponent
  225. {
  226. return GetOrAddComponent(typeof(T)) as T;
  227. }
  228. public ISerieDataComponent GetOrAddComponent(Type type)
  229. {
  230. if (type == typeof(ItemStyle))
  231. {
  232. if (m_ItemStyles.Count == 0)
  233. m_ItemStyles.Add(new ItemStyle() { show = true });
  234. return m_ItemStyles[0];
  235. }
  236. else if (type == typeof(LabelStyle))
  237. {
  238. if (m_Labels.Count == 0)
  239. m_Labels.Add(new LabelStyle() { show = true });
  240. return m_Labels[0];
  241. }
  242. else if (type == typeof(LabelLine))
  243. {
  244. if (m_LabelLines.Count == 0)
  245. m_LabelLines.Add(new LabelLine() { show = true });
  246. return m_LabelLines[0];
  247. }
  248. else if (type == typeof(EmphasisStyle))
  249. {
  250. if (m_EmphasisStyles.Count == 0)
  251. m_EmphasisStyles.Add(new EmphasisStyle() { show = true });
  252. return m_EmphasisStyles[0];
  253. }
  254. else if (type == typeof(BlurStyle))
  255. {
  256. if (m_BlurStyles.Count == 0)
  257. m_BlurStyles.Add(new BlurStyle() { show = true });
  258. return m_BlurStyles[0];
  259. }
  260. else if (type == typeof(SelectStyle))
  261. {
  262. if (m_SelectStyles.Count == 0)
  263. m_SelectStyles.Add(new SelectStyle() { show = true });
  264. return m_SelectStyles[0];
  265. }
  266. else if (type == typeof(SerieSymbol))
  267. {
  268. if (m_Symbols.Count == 0)
  269. m_Symbols.Add(new SerieSymbol() { show = true });
  270. return m_Symbols[0];
  271. }
  272. else if (type == typeof(LineStyle))
  273. {
  274. if (m_LineStyles.Count == 0)
  275. m_LineStyles.Add(new LineStyle() { show = true });
  276. return m_LineStyles[0];
  277. }
  278. else if (type == typeof(AreaStyle))
  279. {
  280. if (m_AreaStyles.Count == 0)
  281. m_AreaStyles.Add(new AreaStyle() { show = true });
  282. return m_AreaStyles[0];
  283. }
  284. else if (type == typeof(TitleStyle))
  285. {
  286. if (m_TitleStyles.Count == 0)
  287. m_TitleStyles.Add(new TitleStyle() { show = true });
  288. return m_TitleStyles[0];
  289. }
  290. else
  291. {
  292. throw new System.Exception("SerieData not support component:" + type);
  293. }
  294. }
  295. public void RemoveAllComponent()
  296. {
  297. m_ItemStyles.Clear();
  298. m_Labels.Clear();
  299. m_LabelLines.Clear();
  300. m_Symbols.Clear();
  301. m_EmphasisStyles.Clear();
  302. m_BlurStyles.Clear();
  303. m_SelectStyles.Clear();
  304. m_LineStyles.Clear();
  305. m_AreaStyles.Clear();
  306. m_TitleStyles.Clear();
  307. }
  308. public void RemoveComponent<T>() where T : ISerieDataComponent
  309. {
  310. RemoveComponent(typeof(T));
  311. }
  312. public void RemoveComponent(Type type)
  313. {
  314. if (type == typeof(ItemStyle))
  315. m_ItemStyles.Clear();
  316. else if (type == typeof(LabelStyle))
  317. m_Labels.Clear();
  318. else if (type == typeof(LabelLine))
  319. m_LabelLines.Clear();
  320. else if (type == typeof(EmphasisStyle))
  321. m_EmphasisStyles.Clear();
  322. else if (type == typeof(BlurStyle))
  323. m_BlurStyles.Clear();
  324. else if (type == typeof(SelectStyle))
  325. m_SelectStyles.Clear();
  326. else if (type == typeof(SerieSymbol))
  327. m_Symbols.Clear();
  328. else if (type == typeof(LineStyle))
  329. m_LineStyles.Clear();
  330. else if (type == typeof(AreaStyle))
  331. m_AreaStyles.Clear();
  332. else if (type == typeof(TitleStyle))
  333. m_TitleStyles.Clear();
  334. else
  335. throw new System.Exception("SerieData not support component:" + type);
  336. }
  337. public double GetData(int index, bool inverse = false)
  338. {
  339. if (index >= 0 && index < m_Data.Count)
  340. {
  341. return inverse ? -m_Data[index] : m_Data[index];
  342. }
  343. else return 0;
  344. }
  345. public double GetData(int index, double min, double max)
  346. {
  347. if (index >= 0 && index < m_Data.Count)
  348. {
  349. var value = m_Data[index];
  350. if (value < min) return min;
  351. else if (value > max) return max;
  352. else return value;
  353. }
  354. else return 0;
  355. }
  356. public double GetPreviousData(int index, bool inverse = false)
  357. {
  358. if (index >= 0 && index < m_PreviousData.Count)
  359. {
  360. return inverse ? -m_PreviousData[index] : m_PreviousData[index];
  361. }
  362. else return 0;
  363. }
  364. public double GetFirstData(bool unscaledTime, float animationDuration = 500f)
  365. {
  366. if (m_Data.Count > 0) return GetCurrData(0, animationDuration, unscaledTime);
  367. return 0;
  368. }
  369. public double GetLastData()
  370. {
  371. if (m_Data.Count > 0) return m_Data[m_Data.Count - 1];
  372. return 0;
  373. }
  374. public double GetCurrData(int index, float animationDuration = 500f, bool unscaledTime = false, bool inverse = false)
  375. {
  376. return GetCurrData(index, animationDuration, inverse, 0, 0, unscaledTime);
  377. }
  378. public double GetCurrData(int index, float animationDuration, bool inverse, double min, double max, bool unscaledTime)
  379. {
  380. if (index < m_DataUpdateFlag.Count && m_DataUpdateFlag[index] && animationDuration > 0)
  381. {
  382. var time = (unscaledTime ? Time.unscaledTime : Time.time) - m_DataUpdateTime[index];
  383. var total = animationDuration / 1000;
  384. var rate = time / total;
  385. if (rate > 1) rate = 1;
  386. if (rate < 1)
  387. {
  388. CheckLastData(unscaledTime);
  389. var curr = MathUtil.Lerp(GetPreviousData(index), GetData(index), rate);
  390. if (min != 0 || max != 0)
  391. {
  392. if (inverse)
  393. {
  394. var temp = min;
  395. min = -max;
  396. max = -temp;
  397. }
  398. var pre = m_PreviousData[index];
  399. if (pre < min)
  400. {
  401. m_PreviousData[index] = min;
  402. curr = min;
  403. }
  404. else if (pre > max)
  405. {
  406. m_PreviousData[index] = max;
  407. curr = max;
  408. }
  409. }
  410. curr = inverse ? -curr : curr;
  411. return curr;
  412. }
  413. else
  414. {
  415. m_DataUpdateFlag[index] = false;
  416. return GetData(index, inverse);
  417. }
  418. }
  419. else
  420. {
  421. return GetData(index, inverse);
  422. }
  423. }
  424. /// <summary>
  425. /// the maxinum value.
  426. /// |最大值。
  427. /// </summary>
  428. public double GetMaxData(bool inverse = false)
  429. {
  430. if (m_Data.Count == 0) return 0;
  431. var temp = double.MinValue;
  432. for (int i = 0; i < m_Data.Count; i++)
  433. {
  434. var value = GetData(i, inverse);
  435. if (value > temp) temp = value;
  436. }
  437. return temp;
  438. }
  439. /// <summary>
  440. /// the mininum value.
  441. /// |最小值。
  442. /// </summary>
  443. public double GetMinData(bool inverse = false)
  444. {
  445. if (m_Data.Count == 0) return 0;
  446. var temp = double.MaxValue;
  447. for (int i = 0; i < m_Data.Count; i++)
  448. {
  449. var value = GetData(i, inverse);
  450. if (value < temp) temp = value;
  451. }
  452. return temp;
  453. }
  454. public void GetMinMaxData(int startDimensionIndex, bool inverse, out double min, out double max)
  455. {
  456. if (m_Data.Count == 0)
  457. {
  458. min = 0;
  459. max = 0;
  460. }
  461. min = double.MaxValue;
  462. max = double.MinValue;
  463. for (int i = startDimensionIndex; i < m_Data.Count; i++)
  464. {
  465. var value = GetData(i, inverse);
  466. if (value < min) min = value;
  467. if (value > max) max = value;
  468. }
  469. }
  470. public double GetTotalData()
  471. {
  472. var total = 0d;
  473. foreach (var value in m_Data)
  474. total += value;
  475. return total;
  476. }
  477. public bool UpdateData(int dimension, double value, bool updateAnimation, bool unscaledTime, float animationDuration = 500f)
  478. {
  479. if (dimension >= 0 && dimension < data.Count)
  480. {
  481. CheckLastData(unscaledTime);
  482. m_PreviousData[dimension] = GetCurrData(dimension, animationDuration, unscaledTime);
  483. //m_PreviousData[dimension] = data[dimension];;
  484. m_DataUpdateTime[dimension] = (unscaledTime ? Time.unscaledTime : Time.time);
  485. m_DataUpdateFlag[dimension] = updateAnimation;
  486. data[dimension] = value;
  487. return true;
  488. }
  489. return false;
  490. }
  491. public bool UpdateData(int dimension, double value)
  492. {
  493. if (dimension >= 0 && dimension < data.Count)
  494. {
  495. data[dimension] = value;
  496. return true;
  497. }
  498. return false;
  499. }
  500. private void CheckLastData(bool unscaledTime)
  501. {
  502. if (m_PreviousData.Count != m_Data.Count)
  503. {
  504. m_PreviousData.Clear();
  505. m_DataUpdateTime.Clear();
  506. m_DataUpdateFlag.Clear();
  507. for (int i = 0; i < m_Data.Count; i++)
  508. {
  509. m_PreviousData.Add(m_Data[i]);
  510. m_DataUpdateTime.Add((unscaledTime ? Time.unscaledTime : Time.time));
  511. m_DataUpdateFlag.Add(false);
  512. }
  513. }
  514. }
  515. public bool IsDataChanged()
  516. {
  517. for (int i = 0; i < m_DataUpdateFlag.Count; i++)
  518. if (m_DataUpdateFlag[i]) return true;
  519. return false;
  520. }
  521. public float GetLabelWidth()
  522. {
  523. if (labelObject != null) return labelObject.GetTextWidth();
  524. else return 0;
  525. }
  526. public float GetLabelHeight()
  527. {
  528. if (labelObject != null) return labelObject.GetTextHeight();
  529. return 0;
  530. }
  531. public void SetLabelActive(bool flag)
  532. {
  533. if (labelObject != null) labelObject.SetActive(flag);
  534. foreach (var labelObject in context.dataLabels)
  535. {
  536. labelObject.SetActive(false);
  537. }
  538. }
  539. public void SetIconActive(bool flag)
  540. {
  541. if (labelObject != null) labelObject.SetActive(flag);
  542. }
  543. public void SetPolygon(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4)
  544. {
  545. m_PolygonPoints.Clear();
  546. m_PolygonPoints.Add(p1);
  547. m_PolygonPoints.Add(p2);
  548. m_PolygonPoints.Add(p3);
  549. m_PolygonPoints.Add(p4);
  550. }
  551. public void SetPolygon(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, Vector2 p5)
  552. {
  553. SetPolygon(p1, p2, p3, p4);
  554. m_PolygonPoints.Add(p5);
  555. }
  556. public bool IsInPolygon(Vector2 p)
  557. {
  558. return UGLHelper.IsPointInPolygon(p, m_PolygonPoints);
  559. }
  560. }
  561. }