SerieData.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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. public string legendName { get { return string.IsNullOrEmpty(name) ? ChartCached.IntToStr(index) : name; } }
  105. /// <summary>
  106. /// 单个数据项的标签设置。
  107. /// </summary>
  108. public LabelStyle labelStyle { get { return m_Labels.Count > 0 ? m_Labels[0] : null; } }
  109. public LabelLine labelLine { get { return m_LabelLines.Count > 0 ? m_LabelLines[0] : null; } }
  110. /// <summary>
  111. /// 单个数据项的样式设置。
  112. /// </summary>
  113. public ItemStyle itemStyle { get { return m_ItemStyles.Count > 0 ? m_ItemStyles[0] : null; } }
  114. /// <summary>
  115. /// 单个数据项的标记设置。
  116. /// </summary>
  117. public SerieSymbol symbol { get { return m_Symbols.Count > 0 ? m_Symbols[0] : null; } }
  118. public LineStyle lineStyle { get { return m_LineStyles.Count > 0 ? m_LineStyles[0] : null; } }
  119. public AreaStyle areaStyle { get { return m_AreaStyles.Count > 0 ? m_AreaStyles[0] : null; } }
  120. public TitleStyle titleStyle { get { return m_TitleStyles.Count > 0 ? m_TitleStyles[0] : null; } }
  121. /// <summary>
  122. /// 高亮状态的样式
  123. /// </summary>
  124. public EmphasisStyle emphasisStyle { get { return m_EmphasisStyles.Count > 0 ? m_EmphasisStyles[0] : null; } }
  125. /// <summary>
  126. /// 淡出状态的样式。
  127. /// </summary>
  128. public BlurStyle blurStyle { get { return m_BlurStyles.Count > 0 ? m_BlurStyles[0] : null; } }
  129. /// <summary>
  130. /// 选中状态的样式。
  131. /// </summary>
  132. public SelectStyle selectStyle { get { return m_SelectStyles.Count > 0 ? m_SelectStyles[0] : null; } }
  133. /// <summary>
  134. /// An arbitrary dimension data list of data item.
  135. /// ||可指定任意维数的数值列表。
  136. /// </summary>
  137. public List<double> data { get { return m_Data; } set { m_Data = value; } }
  138. /// <summary>
  139. /// [default:true] Whether the data item is showed.
  140. /// ||该数据项是否要显示。
  141. /// </summary>
  142. public bool show { get { return m_Show; } set { m_Show = value; } }
  143. private List<double> m_PreviousData = new List<double>();
  144. private List<float> m_DataUpdateTime = new List<float>();
  145. private List<bool> m_DataUpdateFlag = new List<bool>();
  146. private List<float> m_DataAddTime = new List<float>();
  147. private List<bool> m_DataAddFlag = new List<bool>();
  148. private List<Vector2> m_PolygonPoints = new List<Vector2>();
  149. public override bool vertsDirty
  150. {
  151. get
  152. {
  153. return m_VertsDirty ||
  154. IsVertsDirty(labelLine) ||
  155. IsVertsDirty(itemStyle) ||
  156. IsVertsDirty(symbol) ||
  157. IsVertsDirty(lineStyle) ||
  158. IsVertsDirty(areaStyle) ||
  159. IsVertsDirty(emphasisStyle) ||
  160. IsVertsDirty(blurStyle) ||
  161. IsVertsDirty(selectStyle);
  162. }
  163. }
  164. public override bool componentDirty
  165. {
  166. get
  167. {
  168. return m_ComponentDirty ||
  169. IsComponentDirty(labelStyle) ||
  170. IsComponentDirty(labelLine) ||
  171. IsComponentDirty(titleStyle) ||
  172. IsComponentDirty(emphasisStyle) ||
  173. IsComponentDirty(blurStyle) ||
  174. IsComponentDirty(selectStyle);
  175. }
  176. }
  177. public override void ClearVerticesDirty()
  178. {
  179. base.ClearVerticesDirty();
  180. ClearVerticesDirty(labelLine);
  181. ClearVerticesDirty(itemStyle);
  182. ClearVerticesDirty(lineStyle);
  183. ClearVerticesDirty(areaStyle);
  184. ClearVerticesDirty(emphasisStyle);
  185. ClearVerticesDirty(blurStyle);
  186. ClearVerticesDirty(selectStyle);
  187. }
  188. public override void ClearComponentDirty()
  189. {
  190. base.ClearComponentDirty();
  191. ClearComponentDirty(labelLine);
  192. ClearComponentDirty(itemStyle);
  193. ClearComponentDirty(lineStyle);
  194. ClearComponentDirty(areaStyle);
  195. ClearComponentDirty(symbol);
  196. ClearComponentDirty(emphasisStyle);
  197. ClearComponentDirty(blurStyle);
  198. ClearComponentDirty(selectStyle);
  199. }
  200. public void Reset()
  201. {
  202. index = 0;
  203. m_Id = null;
  204. m_ParentId = null;
  205. labelObject = null;
  206. m_Name = string.Empty;
  207. m_Show = true;
  208. context.Reset();
  209. interact.Reset();
  210. m_Data.Clear();
  211. m_PreviousData.Clear();
  212. m_DataUpdateTime.Clear();
  213. m_DataUpdateFlag.Clear();
  214. m_DataAddTime.Clear();
  215. m_DataAddFlag.Clear();
  216. m_Labels.Clear();
  217. m_LabelLines.Clear();
  218. m_ItemStyles.Clear();
  219. m_Symbols.Clear();
  220. m_LineStyles.Clear();
  221. m_AreaStyles.Clear();
  222. m_TitleStyles.Clear();
  223. m_EmphasisStyles.Clear();
  224. m_BlurStyles.Clear();
  225. m_SelectStyles.Clear();
  226. }
  227. public void OnAdd(AnimationStyle animation, double startValue = 0)
  228. {
  229. if (!animation.enable) return;
  230. if (!animation.context.enableSerieDataAddedAnimation)
  231. {
  232. animation.Addition();
  233. return;
  234. }
  235. #if UNITY_EDITOR
  236. if (!Application.isPlaying)
  237. return;
  238. #endif
  239. m_DataAddTime.Clear();
  240. m_DataAddFlag.Clear();
  241. if (animation.GetAdditionDuration() > 0)
  242. {
  243. for (int i = 0; i < m_Data.Count; i++)
  244. {
  245. m_DataAddTime.Add(animation.unscaledTime ? Time.unscaledTime : Time.time);
  246. m_DataAddFlag.Add(true);
  247. }
  248. }
  249. }
  250. [Obsolete("GetOrAddComponent is obsolete. Use EnsureComponent instead.")]
  251. public T GetOrAddComponent<T>() where T : ChildComponent, ISerieDataComponent
  252. {
  253. return EnsureComponent<T>();
  254. }
  255. /// <summary>
  256. /// Get the component of the serie data. return null if not exist.
  257. /// ||获取数据项的指定类型的组件,如果不存在则返回null。
  258. /// </summary>
  259. /// <typeparam name="T"></typeparam>
  260. /// <returns></returns>
  261. public T GetComponent<T>() where T : ChildComponent, ISerieDataComponent
  262. {
  263. return GetComponentInternal(typeof(T), false) as T;
  264. }
  265. /// <summary>
  266. /// Ensure the serie data has the component, if not, add it.
  267. /// ||确保数据项有指定类型的组件,如果没有则添加。
  268. /// </summary>
  269. /// <typeparam name="T"></typeparam>
  270. /// <returns></returns>
  271. [Since("v3.6.0")]
  272. public T EnsureComponent<T>() where T : ChildComponent, ISerieDataComponent
  273. {
  274. return GetComponentInternal(typeof(T), true) as T;
  275. }
  276. /// <summary>
  277. /// Ensure the serie data has the component, if not, add it.
  278. /// ||确保数据项有指定类型的组件,如果没有则添加。
  279. /// </summary>
  280. /// <param name="type"></param>
  281. /// <returns></returns>
  282. [Since("v3.6.0")]
  283. public ISerieDataComponent EnsureComponent(Type type)
  284. {
  285. return GetComponentInternal(type, true);
  286. }
  287. private ISerieDataComponent GetComponentInternal(Type type, bool addIfNotExist)
  288. {
  289. if (type == typeof(ItemStyle))
  290. {
  291. if (m_ItemStyles.Count == 0)
  292. {
  293. if (addIfNotExist)
  294. m_ItemStyles.Add(new ItemStyle() { show = true });
  295. else
  296. return null;
  297. }
  298. return m_ItemStyles[0];
  299. }
  300. else if (type == typeof(LabelStyle))
  301. {
  302. if (m_Labels.Count == 0)
  303. {
  304. if (addIfNotExist)
  305. m_Labels.Add(new LabelStyle() { show = true });
  306. else
  307. return null;
  308. }
  309. return m_Labels[0];
  310. }
  311. else if (type == typeof(LabelLine))
  312. {
  313. if (m_LabelLines.Count == 0)
  314. {
  315. if (addIfNotExist)
  316. m_LabelLines.Add(new LabelLine() { show = true });
  317. else
  318. return null;
  319. }
  320. return m_LabelLines[0];
  321. }
  322. else if (type == typeof(EmphasisStyle))
  323. {
  324. if (m_EmphasisStyles.Count == 0)
  325. {
  326. if (addIfNotExist)
  327. m_EmphasisStyles.Add(new EmphasisStyle() { show = true });
  328. else
  329. return null;
  330. }
  331. return m_EmphasisStyles[0];
  332. }
  333. else if (type == typeof(BlurStyle))
  334. {
  335. if (m_BlurStyles.Count == 0)
  336. {
  337. if (addIfNotExist)
  338. m_BlurStyles.Add(new BlurStyle() { show = true });
  339. else
  340. return null;
  341. }
  342. return m_BlurStyles[0];
  343. }
  344. else if (type == typeof(SelectStyle))
  345. {
  346. if (m_SelectStyles.Count == 0)
  347. {
  348. if (addIfNotExist)
  349. m_SelectStyles.Add(new SelectStyle() { show = true });
  350. else
  351. return null;
  352. }
  353. return m_SelectStyles[0];
  354. }
  355. else if (type == typeof(SerieSymbol))
  356. {
  357. if (m_Symbols.Count == 0)
  358. {
  359. if (addIfNotExist)
  360. m_Symbols.Add(new SerieSymbol() { show = true });
  361. else
  362. return null;
  363. }
  364. return m_Symbols[0];
  365. }
  366. else if (type == typeof(LineStyle))
  367. {
  368. if (m_LineStyles.Count == 0)
  369. {
  370. if (addIfNotExist)
  371. m_LineStyles.Add(new LineStyle() { show = true });
  372. else
  373. return null;
  374. }
  375. return m_LineStyles[0];
  376. }
  377. else if (type == typeof(AreaStyle))
  378. {
  379. if (m_AreaStyles.Count == 0)
  380. {
  381. if (addIfNotExist)
  382. m_AreaStyles.Add(new AreaStyle() { show = true });
  383. else
  384. return null;
  385. }
  386. return m_AreaStyles[0];
  387. }
  388. else if (type == typeof(TitleStyle))
  389. {
  390. if (m_TitleStyles.Count == 0)
  391. {
  392. if (addIfNotExist)
  393. m_TitleStyles.Add(new TitleStyle() { show = true });
  394. else
  395. return null;
  396. }
  397. return m_TitleStyles[0];
  398. }
  399. else
  400. {
  401. throw new System.Exception("SerieData not support component:" + type);
  402. }
  403. }
  404. public void RemoveAllComponent()
  405. {
  406. m_ItemStyles.Clear();
  407. m_Labels.Clear();
  408. m_LabelLines.Clear();
  409. m_Symbols.Clear();
  410. m_EmphasisStyles.Clear();
  411. m_BlurStyles.Clear();
  412. m_SelectStyles.Clear();
  413. m_LineStyles.Clear();
  414. m_AreaStyles.Clear();
  415. m_TitleStyles.Clear();
  416. }
  417. public void RemoveComponent<T>() where T : ISerieDataComponent
  418. {
  419. RemoveComponent(typeof(T));
  420. }
  421. public void RemoveComponent(Type type)
  422. {
  423. if (type == typeof(ItemStyle))
  424. m_ItemStyles.Clear();
  425. else if (type == typeof(LabelStyle))
  426. m_Labels.Clear();
  427. else if (type == typeof(LabelLine))
  428. m_LabelLines.Clear();
  429. else if (type == typeof(EmphasisStyle))
  430. m_EmphasisStyles.Clear();
  431. else if (type == typeof(BlurStyle))
  432. m_BlurStyles.Clear();
  433. else if (type == typeof(SelectStyle))
  434. m_SelectStyles.Clear();
  435. else if (type == typeof(SerieSymbol))
  436. m_Symbols.Clear();
  437. else if (type == typeof(LineStyle))
  438. m_LineStyles.Clear();
  439. else if (type == typeof(AreaStyle))
  440. m_AreaStyles.Clear();
  441. else if (type == typeof(TitleStyle))
  442. m_TitleStyles.Clear();
  443. else
  444. throw new System.Exception("SerieData not support component:" + type);
  445. }
  446. public double GetData(int index, bool inverse = false)
  447. {
  448. if (index >= 0 && index < m_Data.Count)
  449. {
  450. return inverse ? -m_Data[index] : m_Data[index];
  451. }
  452. else return 0;
  453. }
  454. public double GetData(int index, double min, double max)
  455. {
  456. if (index >= 0 && index < m_Data.Count)
  457. {
  458. var value = m_Data[index];
  459. if (value < min) return min;
  460. else if (value > max) return max;
  461. else return value;
  462. }
  463. else return 0;
  464. }
  465. public double GetPreviousData(int index, bool inverse = false)
  466. {
  467. if (index >= 0 && index < m_PreviousData.Count)
  468. {
  469. return inverse ? -m_PreviousData[index] : m_PreviousData[index];
  470. }
  471. else return 0;
  472. }
  473. public double GetFirstData(bool unscaledTime, float animationDuration = 500f)
  474. {
  475. if (m_Data.Count > 0) return GetCurrData(0, 0, animationDuration, unscaledTime);
  476. return 0;
  477. }
  478. public double GetLastData()
  479. {
  480. if (m_Data.Count > 0) return m_Data[m_Data.Count - 1];
  481. return 0;
  482. }
  483. public double GetCurrData(int index, AnimationStyle animation, bool inverse = false, bool loop = false)
  484. {
  485. if (animation == null || !animation.enable)
  486. return GetData(index, inverse);
  487. else
  488. return GetCurrData(index, animation.GetAdditionDuration(), animation.GetChangeDuration(),
  489. inverse, 0, 0, animation.unscaledTime, loop);
  490. }
  491. public double GetCurrData(int index, AnimationStyle animation, bool inverse, double min, double max, bool loop = false)
  492. {
  493. if (animation == null || !animation.enable)
  494. return GetData(index, inverse);
  495. else
  496. return GetCurrData(index, animation.GetAdditionDuration(), animation.GetChangeDuration(),
  497. inverse, min, max, animation.unscaledTime, loop);
  498. }
  499. public double GetCurrData(int index, float dataAddDuration = 500f, float animationDuration = 500f, bool unscaledTime = false, bool inverse = false)
  500. {
  501. return GetCurrData(index, dataAddDuration, animationDuration, inverse, 0, 0, unscaledTime);
  502. }
  503. public double GetCurrData(int index, float dataAddDuration, float animationDuration, bool inverse, double min, double max, bool unscaledTime, bool loop = false)
  504. {
  505. if (dataAddDuration > 0)
  506. {
  507. if (index < m_DataAddFlag.Count && m_DataAddFlag[index])
  508. {
  509. var time = (unscaledTime ? Time.unscaledTime : Time.time) - m_DataAddTime[index];
  510. var total = dataAddDuration / 1000;
  511. var rate = time / total;
  512. if (rate > 1) rate = 1;
  513. if (rate < 1)
  514. {
  515. var prev = min > 0 ? min : 0;
  516. var next = GetData(index);
  517. var curr = MathUtil.Lerp(prev, next, rate);
  518. curr = inverse ? -curr : curr;
  519. return curr;
  520. }
  521. else
  522. {
  523. for (int i = 0; i < m_DataAddFlag.Count; i++)
  524. m_DataAddFlag[i] = false;
  525. return GetData(index, inverse);
  526. }
  527. }
  528. }
  529. if (animationDuration > 0)
  530. {
  531. if (index < m_DataUpdateFlag.Count && m_DataUpdateFlag[index])
  532. {
  533. var time = (unscaledTime ? Time.unscaledTime : Time.time) - m_DataUpdateTime[index];
  534. var total = animationDuration / 1000;
  535. var rate = time / total;
  536. if (rate > 1) rate = 1;
  537. if (rate < 1)
  538. {
  539. CheckLastData(unscaledTime);
  540. var prev = GetPreviousData(index);
  541. var next = GetData(index);
  542. if (loop && next <= min && prev != 0)
  543. {
  544. next = max;
  545. }
  546. var curr = MathUtil.Lerp(prev, next, rate);
  547. if (min != 0 || max != 0)
  548. {
  549. if (inverse)
  550. {
  551. var temp = min;
  552. min = -max;
  553. max = -temp;
  554. }
  555. var pre = m_PreviousData[index];
  556. if (pre < min)
  557. {
  558. m_PreviousData[index] = min;
  559. curr = min;
  560. }
  561. else if (pre > max)
  562. {
  563. m_PreviousData[index] = max;
  564. curr = max;
  565. }
  566. }
  567. curr = inverse ? -curr : curr;
  568. return curr;
  569. }
  570. else
  571. {
  572. for (int i = 0; i < m_DataUpdateFlag.Count; i++)
  573. m_DataUpdateFlag[i] = false;
  574. return GetData(index, inverse);
  575. }
  576. }
  577. else
  578. {
  579. return GetData(index, inverse);
  580. }
  581. }
  582. return GetData(index, inverse);
  583. }
  584. public double GetAddAnimationData(double min, double max, float animationDuration = 500f, bool unscaledTime = false)
  585. {
  586. if (animationDuration > 0 && m_DataAddFlag.Count > 0 && m_DataAddFlag[0])
  587. {
  588. var time = (unscaledTime ? Time.unscaledTime : Time.time) - m_DataAddTime[0];
  589. var total = animationDuration / 1000;
  590. var rate = time / total;
  591. if (rate > 1) rate = 1;
  592. if (rate < 1)
  593. {
  594. var curr = MathUtil.Lerp(min, max, rate);
  595. return curr;
  596. }
  597. else
  598. {
  599. for (int i = 0; i < m_DataAddFlag.Count; i++)
  600. m_DataAddFlag[i] = false;
  601. return max;
  602. }
  603. }
  604. else
  605. {
  606. return max;
  607. }
  608. }
  609. /// <summary>
  610. /// the maxinum value.
  611. /// ||最大值。
  612. /// </summary>
  613. public double GetMaxData(bool inverse = false)
  614. {
  615. if (m_Data.Count == 0) return 0;
  616. var temp = double.MinValue;
  617. for (int i = 0; i < m_Data.Count; i++)
  618. {
  619. var value = GetData(i, inverse);
  620. if (value > temp) temp = value;
  621. }
  622. return temp;
  623. }
  624. /// <summary>
  625. /// the mininum value.
  626. /// ||最小值。
  627. /// </summary>
  628. public double GetMinData(bool inverse = false)
  629. {
  630. if (m_Data.Count == 0) return 0;
  631. var temp = double.MaxValue;
  632. for (int i = 0; i < m_Data.Count; i++)
  633. {
  634. var value = GetData(i, inverse);
  635. if (value < temp) temp = value;
  636. }
  637. return temp;
  638. }
  639. public void GetMinMaxData(int startDimensionIndex, bool inverse, out double min, out double max)
  640. {
  641. if (m_Data.Count == 0)
  642. {
  643. min = 0;
  644. max = 0;
  645. }
  646. min = double.MaxValue;
  647. max = double.MinValue;
  648. for (int i = startDimensionIndex; i < m_Data.Count; i++)
  649. {
  650. var value = GetData(i, inverse);
  651. if (value < min) min = value;
  652. if (value > max) max = value;
  653. }
  654. }
  655. public double GetTotalData()
  656. {
  657. var total = 0d;
  658. foreach (var value in m_Data)
  659. total += value;
  660. return total;
  661. }
  662. public bool UpdateData(int dimension, double value, bool updateAnimation, bool unscaledTime, float animationDuration = 500f)
  663. {
  664. if (dimension >= 0 && dimension < data.Count)
  665. {
  666. CheckLastData(unscaledTime);
  667. m_PreviousData[dimension] = GetCurrData(dimension, 0, animationDuration, unscaledTime);
  668. m_DataUpdateTime[dimension] = (unscaledTime ? Time.unscaledTime : Time.time);
  669. m_DataUpdateFlag[dimension] = updateAnimation;
  670. data[dimension] = value;
  671. return true;
  672. }
  673. return false;
  674. }
  675. public bool UpdateData(int dimension, double value)
  676. {
  677. if (dimension >= 0 && dimension < data.Count)
  678. {
  679. data[dimension] = value;
  680. return true;
  681. }
  682. return false;
  683. }
  684. private void CheckLastData(bool unscaledTime)
  685. {
  686. if (m_PreviousData.Count != m_Data.Count)
  687. {
  688. m_PreviousData.Clear();
  689. m_DataUpdateTime.Clear();
  690. m_DataUpdateFlag.Clear();
  691. for (int i = 0; i < m_Data.Count; i++)
  692. {
  693. m_PreviousData.Add(m_Data[i]);
  694. m_DataUpdateTime.Add((unscaledTime ? Time.unscaledTime : Time.time));
  695. m_DataUpdateFlag.Add(false);
  696. }
  697. }
  698. }
  699. public bool IsDataChanged()
  700. {
  701. for (int i = 0; i < m_DataUpdateFlag.Count; i++)
  702. if (m_DataUpdateFlag[i]) return true;
  703. for (int i = 0; i < m_DataAddFlag.Count; i++)
  704. if (m_DataAddFlag[i]) return true;
  705. return false;
  706. }
  707. public float GetLabelWidth()
  708. {
  709. if (labelObject != null) return labelObject.GetTextWidth();
  710. else return 0;
  711. }
  712. public float GetLabelHeight()
  713. {
  714. if (labelObject != null) return labelObject.GetTextHeight();
  715. return 0;
  716. }
  717. public void SetLabelActive(bool flag)
  718. {
  719. if (labelObject != null) labelObject.SetActive(flag);
  720. foreach (var labelObject in context.dataLabels)
  721. {
  722. labelObject.SetActive(false);
  723. }
  724. }
  725. public void SetIconActive(bool flag)
  726. {
  727. if (labelObject != null) labelObject.SetActive(flag);
  728. }
  729. public void SetPolygon(params Vector2[] points)
  730. {
  731. m_PolygonPoints.Clear();
  732. m_PolygonPoints.AddRange(points);
  733. }
  734. public void SetPolygon(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4)
  735. {
  736. m_PolygonPoints.Clear();
  737. m_PolygonPoints.Add(p1);
  738. m_PolygonPoints.Add(p2);
  739. m_PolygonPoints.Add(p3);
  740. m_PolygonPoints.Add(p4);
  741. }
  742. public void SetPolygon(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, Vector2 p5)
  743. {
  744. SetPolygon(p1, p2, p3, p4);
  745. m_PolygonPoints.Add(p5);
  746. }
  747. public bool IsInPolygon(Vector2 p)
  748. {
  749. return UGLHelper.IsPointInPolygon(p, m_PolygonPoints);
  750. }
  751. }
  752. }