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.

BaseChart.Serie.cs 37KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using UnityEngine;
  5. namespace XCharts.Runtime
  6. {
  7. public partial class BaseChart
  8. {
  9. public T AddSerie<T>(string serieName = null, bool show = true, bool addToHead = false) where T : Serie
  10. {
  11. if (!CanAddSerie<T>()) return null;
  12. var index = -1;
  13. var serie = InsertSerie(index, typeof(T), serieName, show, addToHead) as T;
  14. CreateSerieHandler(serie);
  15. return serie;
  16. }
  17. public T InsertSerie<T>(int index, string serieName = null, bool show = true) where T : Serie
  18. {
  19. if (!CanAddSerie<T>()) return null;
  20. var serie = InsertSerie(index, typeof(T), serieName, show) as T;
  21. InitSerieHandlers();
  22. return serie;
  23. }
  24. public void InsertSerie(Serie serie, int index = -1, bool addToHead = false)
  25. {
  26. serie.AnimationRestart();
  27. AnimationStyleHelper.UpdateSerieAnimation(serie);
  28. if (addToHead) m_Series.Insert(0, serie);
  29. else if (index >= 0) m_Series.Insert(index, serie);
  30. else m_Series.Add(serie);
  31. ResetSeriesIndex();
  32. SeriesHelper.UpdateSerieNameList(this, ref m_LegendRealShowName);
  33. }
  34. public bool MoveUpSerie(int serieIndex)
  35. {
  36. if (serieIndex < 0 || serieIndex > m_Series.Count - 1) return false;
  37. if (serieIndex == 0) return false;
  38. var up = GetSerie(serieIndex - 1);
  39. var temp = GetSerie(serieIndex);
  40. m_Series[serieIndex - 1] = temp;
  41. m_Series[serieIndex] = up;
  42. ResetSeriesIndex();
  43. InitSerieHandlers();
  44. RefreshChart();
  45. return true;
  46. }
  47. public bool MoveDownSerie(int serieIndex)
  48. {
  49. if (serieIndex < 0 || serieIndex > m_Series.Count - 1) return false;
  50. if (serieIndex == m_Series.Count - 1) return false;
  51. var down = GetSerie(serieIndex + 1);
  52. var temp = GetSerie(serieIndex);
  53. m_Series[serieIndex + 1] = temp;
  54. m_Series[serieIndex] = down;
  55. ResetSeriesIndex();
  56. InitSerieHandlers();
  57. RefreshChart();
  58. return true;
  59. }
  60. /// <summary>
  61. /// 重置serie的数据项索引。避免数据项索引异常。
  62. /// </summary>
  63. /// <param name="serieIndex"></param>
  64. public bool ResetDataIndex(int serieIndex)
  65. {
  66. var serie = GetSerie(serieIndex);
  67. if (serie != null)
  68. return serie.ResetDataIndex();
  69. return false;
  70. }
  71. public bool CanAddSerie<T>() where T : Serie
  72. {
  73. return CanAddSerie(typeof(T));
  74. }
  75. public bool CanAddSerie(Type type)
  76. {
  77. return m_TypeListForSerie.ContainsKey(type);
  78. }
  79. public bool HasSerie<T>() where T : Serie
  80. {
  81. return HasSerie(typeof(T));
  82. }
  83. public bool HasSerie(Type type)
  84. {
  85. if (!type.IsSubclassOf(typeof(Serie))) return false;
  86. foreach (var serie in m_Series)
  87. {
  88. if (serie.GetType() == type)
  89. return true;
  90. }
  91. return false;
  92. }
  93. public T GetSerie<T>() where T : Serie
  94. {
  95. foreach (var serie in m_Series)
  96. {
  97. if (serie is T) return serie as T;
  98. }
  99. return null;
  100. }
  101. public Serie GetSerie(string serieName)
  102. {
  103. foreach (var serie in m_Series)
  104. {
  105. if (string.IsNullOrEmpty(serie.serieName))
  106. {
  107. if (string.IsNullOrEmpty(serieName)) return serie;
  108. }
  109. else if (serie.serieName.Equals(serieName))
  110. {
  111. return serie;
  112. }
  113. }
  114. return null;
  115. }
  116. public Serie GetSerie(int serieIndex)
  117. {
  118. if (serieIndex < 0 || serieIndex > m_Series.Count - 1) return null;
  119. return m_Series[serieIndex];
  120. }
  121. public T GetSerie<T>(int serieIndex) where T : Serie
  122. {
  123. if (serieIndex < 0 || serieIndex > m_Series.Count - 1) return null;
  124. return m_Series[serieIndex] as T;
  125. }
  126. public void RemoveSerie(string serieName)
  127. {
  128. for (int i = m_Series.Count - 1; i >= 0; i--)
  129. {
  130. var serie = m_Series[i];
  131. if (string.IsNullOrEmpty(serieName))
  132. {
  133. if (string.IsNullOrEmpty(serie.serieName))
  134. RemoveSerie(serie);
  135. }
  136. else if (serieName.Equals(serie.serieName))
  137. {
  138. RemoveSerie(serie);
  139. }
  140. }
  141. }
  142. public void RemoveSerie(int serieIndex)
  143. {
  144. if (serieIndex < 0 || serieIndex > m_Series.Count - 1) return;
  145. RemoveSerie(m_Series[serieIndex]);
  146. }
  147. public void RemoveSerie<T>() where T : Serie
  148. {
  149. for (int i = m_Series.Count - 1; i >= 0; i--)
  150. {
  151. var serie = m_Series[i];
  152. if (serie is T)
  153. RemoveSerie(serie);
  154. }
  155. }
  156. public void RemoveSerie(Serie serie)
  157. {
  158. serie.OnRemove();
  159. m_SerieHandlers.Remove(serie.handler);
  160. m_Series.Remove(serie);
  161. RefreshChart();
  162. }
  163. public bool ConvertSerie<T>(Serie serie) where T : Serie
  164. {
  165. return ConvertSerie(serie, typeof(T));
  166. }
  167. public bool ConvertSerie(Serie serie, Type type)
  168. {
  169. try
  170. {
  171. var newSerie = type.InvokeMember("ConvertSerie",
  172. BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public, null, null,
  173. new object[] { serie }) as Serie;
  174. return ReplaceSerie(serie, newSerie);
  175. }
  176. catch
  177. {
  178. Debug.LogError(string.Format("ConvertSerie Failed: can't found {0}.ConvertSerie(Serie serie)", type.Name));
  179. return false;
  180. }
  181. }
  182. public bool ReplaceSerie(Serie oldSerie, Serie newSerie)
  183. {
  184. if (oldSerie == null || newSerie == null)
  185. return false;
  186. var index = m_Series.IndexOf(oldSerie);
  187. if (index < 0)
  188. return false;
  189. AnimationStyleHelper.UpdateSerieAnimation(newSerie);
  190. oldSerie.OnRemove();
  191. m_Series.RemoveAt(index);
  192. m_Series.Insert(index, newSerie);
  193. ResetSeriesIndex();
  194. InitSerieHandlers();
  195. RefreshAllComponent();
  196. RefreshChart();
  197. return true;
  198. }
  199. /// <summary>
  200. /// Add a data to serie.
  201. /// |If serieName doesn't exist in legend,will be add to legend.
  202. /// |添加一个数据到指定的系列中。
  203. /// </summary>
  204. /// <param name="serieName">the name of serie</param>
  205. /// <param name="data">the data to add</param>
  206. /// <param name="dataName">the name of data</param>
  207. /// <param name="dataId">the unique id of data</param>
  208. /// <returns>Returns True on success</returns>
  209. public SerieData AddData(string serieName, double data, string dataName = null, string dataId = null)
  210. {
  211. var serie = GetSerie(serieName);
  212. if (serie != null)
  213. {
  214. var serieData = serie.AddYData(data, dataName, dataId);
  215. RefreshPainter(serie.painter);
  216. return serieData;
  217. }
  218. return null;
  219. }
  220. /// <summary>
  221. /// Add a data to serie.
  222. /// |添加一个数据到指定的系列中。
  223. /// </summary>
  224. /// <param name="serieIndex">the index of serie</param>
  225. /// <param name="data">the data to add</param>
  226. /// <param name="dataName">the name of data</param>
  227. /// <param name="dataId">the unique id of data</param>
  228. /// <returns>Returns True on success</returns>
  229. public SerieData AddData(int serieIndex, double data, string dataName = null, string dataId = null)
  230. {
  231. var serie = GetSerie(serieIndex);
  232. if (serie != null)
  233. {
  234. var serieData = serie.AddYData(data, dataName, dataId);
  235. RefreshPainter(serie.painter);
  236. return serieData;
  237. }
  238. return null;
  239. }
  240. /// <summary>
  241. /// Add an arbitray dimension data to serie,such as (x,y,z,...).
  242. /// |添加多维数据(x,y,z...)到指定的系列中。
  243. /// </summary>
  244. /// <param name="serieName">the name of serie</param>
  245. /// <param name="multidimensionalData">the (x,y,z,...) data</param>
  246. /// <param name="dataName">the name of data</param>
  247. /// <param name="dataId">the unique id of data</param>
  248. /// <returns>Returns True on success</returns>
  249. public SerieData AddData(string serieName, List<double> multidimensionalData, string dataName = null, string dataId = null)
  250. {
  251. var serie = GetSerie(serieName);
  252. if (serie != null)
  253. {
  254. var serieData = serie.AddData(multidimensionalData, dataName, dataId);
  255. RefreshPainter(serie.painter);
  256. return serieData;
  257. }
  258. return null;
  259. }
  260. /// <summary>
  261. /// Add an arbitray dimension data to serie,such as (x,y,z,...).
  262. /// |添加多维数据(x,y,z...)到指定的系列中。
  263. /// </summary>
  264. /// <param name="serieIndex">the index of serie,index starts at 0</param>
  265. /// <param name="multidimensionalData">the (x,y,z,...) data</param>
  266. /// <param name="dataName">the name of data</param>
  267. /// <param name="dataId">the unique id of data</param>
  268. /// <returns>Returns True on success</returns>
  269. public SerieData AddData(int serieIndex, List<double> multidimensionalData, string dataName = null, string dataId = null)
  270. {
  271. var serie = GetSerie(serieIndex);
  272. if (serie != null)
  273. {
  274. var serieData = serie.AddData(multidimensionalData, dataName, dataId);
  275. RefreshPainter(serie.painter);
  276. return serieData;
  277. }
  278. return null;
  279. }
  280. [Since("v3.4.0")]
  281. /// <summary>
  282. /// Add an arbitray dimension data to serie,such as (x,y,z,...).
  283. /// |添加多维数据(x,y,z...)到指定的系列中。
  284. /// </summary>
  285. /// <param name="serieIndex">the index of serie</param>
  286. /// <param name="multidimensionalData">the (x,y,z,...) data</param>
  287. /// <returns></returns>
  288. public SerieData AddData(int serieIndex, params double[] multidimensionalData)
  289. {
  290. var serie = GetSerie(serieIndex);
  291. if (serie != null)
  292. {
  293. var serieData = serie.AddData(multidimensionalData);
  294. RefreshPainter(serie.painter);
  295. return serieData;
  296. }
  297. return null;
  298. }
  299. [Since("v3.4.0")]
  300. /// <summary>
  301. /// Add an arbitray dimension data to serie,such as (x,y,z,...).
  302. /// |添加多维数据(x,y,z...)到指定的系列中。
  303. /// </summary>
  304. /// <param name="serieName">the name of serie</param>
  305. /// <param name="multidimensionalData">the (x,y,z,...) data</param>
  306. /// <returns></returns>
  307. public SerieData AddData(string serieName, params double[] multidimensionalData)
  308. {
  309. var serie = GetSerie(serieName);
  310. if (serie != null)
  311. {
  312. var serieData = serie.AddData(multidimensionalData);
  313. RefreshPainter(serie.painter);
  314. return serieData;
  315. }
  316. return null;
  317. }
  318. /// <summary>
  319. /// Add a (x,y) data to serie.
  320. /// |添加(x,y)数据到指定系列中。
  321. /// </summary>
  322. /// <param name="serieName">the name of serie</param>
  323. /// <param name="xValue">x data</param>
  324. /// <param name="yValue">y data</param>
  325. /// <param name="dataName">the name of data</param>
  326. /// <param name="dataId">the unique id of data</param>
  327. /// <returns>Returns True on success</returns>
  328. public SerieData AddData(string serieName, double xValue, double yValue, string dataName = null, string dataId = null)
  329. {
  330. var serie = GetSerie(serieName);
  331. if (serie != null)
  332. {
  333. var serieData = serie.AddXYData(xValue, yValue, dataName, dataId);
  334. RefreshPainter(serie.painter);
  335. return serieData;
  336. }
  337. return null;
  338. }
  339. /// <summary>
  340. /// Add a (x,y) data to serie.
  341. /// |添加(x,y)数据到指定系列中。
  342. /// </summary>
  343. /// <param name="serieIndex">the index of serie</param>
  344. /// <param name="xValue">x data</param>
  345. /// <param name="yValue">y data</param>
  346. /// <param name="dataName">the name of data</param>
  347. /// <param name="dataId">the unique id of data</param>
  348. /// <returns>Returns True on success</returns>
  349. public SerieData AddData(int serieIndex, double xValue, double yValue, string dataName = null, string dataId = null)
  350. {
  351. var serie = GetSerie(serieIndex);
  352. if (serie != null)
  353. {
  354. var serieData = serie.AddXYData(xValue, yValue, dataName, dataId);
  355. RefreshPainter(serie.painter);
  356. return serieData;
  357. }
  358. return null;
  359. }
  360. /// <summary>
  361. /// Add a (time,y) data to serie.
  362. /// |添加(time,y)数据到指定的系列中。
  363. /// </summary>
  364. /// <param name="serieName"></param>
  365. /// <param name="time"></param>
  366. /// <param name="yValue"></param>
  367. /// <param name="dataName"></param>
  368. /// <param name="dataId"></param>
  369. /// <returns></returns>
  370. public SerieData AddData(string serieName, DateTime time, double yValue, string dataName = null, string dataId = null)
  371. {
  372. var xValue = DateTimeUtil.GetTimestamp(time);
  373. return AddData(serieName, xValue, yValue, dataName, dataId);
  374. }
  375. /// <summary>
  376. /// Add a (time,y) data to serie.
  377. /// |添加(time,y)数据到指定的系列中。
  378. /// </summary>
  379. /// <param name="serieIndex"></param>
  380. /// <param name="time"></param>
  381. /// <param name="yValue"></param>
  382. /// <param name="dataName"></param>
  383. /// <param name="dataId"></param>
  384. /// <returns></returns>
  385. public SerieData AddData(int serieIndex, DateTime time, double yValue, string dataName = null, string dataId = null)
  386. {
  387. var xValue = DateTimeUtil.GetTimestamp(time);
  388. return AddData(serieIndex, xValue, yValue, dataName, dataId);
  389. }
  390. public SerieData AddData(int serieIndex, double indexOrTimestamp, double open, double close, double lowest, double heighest, string dataName = null, string dataId = null)
  391. {
  392. var serie = GetSerie(serieIndex);
  393. if (serie != null)
  394. {
  395. var serieData = serie.AddData(indexOrTimestamp, open, close, lowest, heighest, dataName, dataId);
  396. RefreshPainter(serie.painter);
  397. return serieData;
  398. }
  399. return null;
  400. }
  401. public SerieData AddData(string serieName, double indexOrTimestamp, double open, double close, double lowest, double heighest, string dataName = null, string dataId = null)
  402. {
  403. var serie = GetSerie(serieName);
  404. if (serie != null)
  405. {
  406. var serieData = serie.AddData(indexOrTimestamp, open, close, lowest, heighest, dataName, dataId);
  407. RefreshPainter(serie.painter);
  408. return serieData;
  409. }
  410. return null;
  411. }
  412. /// <summary>
  413. /// Update serie data by serie name.
  414. /// |更新指定系列中的指定索引数据。
  415. /// </summary>
  416. /// <param name="serieName">the name of serie</param>
  417. /// <param name="dataIndex">the index of data</param>
  418. /// <param name="value">the data will be update</param>
  419. public bool UpdateData(string serieName, int dataIndex, double value)
  420. {
  421. var serie = GetSerie(serieName);
  422. if (serie != null)
  423. {
  424. serie.UpdateYData(dataIndex, value);
  425. RefreshPainter(serie);
  426. return true;
  427. }
  428. return false;
  429. }
  430. /// <summary>
  431. /// Update serie data by serie index.
  432. /// |更新指定系列中的指定索引数据。
  433. /// </summary>
  434. /// <param name="serieIndex">the index of serie</param>
  435. /// <param name="dataIndex">the index of data</param>
  436. /// <param name="value">the data will be update</param>
  437. public bool UpdateData(int serieIndex, int dataIndex, double value)
  438. {
  439. var serie = GetSerie(serieIndex);
  440. if (serie != null)
  441. {
  442. serie.UpdateYData(dataIndex, value);
  443. RefreshPainter(serie);
  444. return true;
  445. }
  446. return false;
  447. }
  448. /// <summary>
  449. /// 更新指定系列指定索引的数据项的多维数据。
  450. /// </summary>
  451. /// <param name="serieName"></param>
  452. /// <param name="dataIndex"></param>
  453. /// <param name="multidimensionalData">一个数据项的多维数据列表,而不是多个数据项的数据</param>
  454. public bool UpdateData(string serieName, int dataIndex, List<double> multidimensionalData)
  455. {
  456. var serie = GetSerie(serieName);
  457. if (serie != null)
  458. {
  459. serie.UpdateData(dataIndex, multidimensionalData);
  460. RefreshPainter(serie);
  461. return true;
  462. }
  463. return false;
  464. }
  465. /// <summary>
  466. /// 更新指定系列指定索引的数据项的多维数据。
  467. /// </summary>
  468. /// <param name="serieIndex"></param>
  469. /// <param name="dataIndex"></param>
  470. /// <param name="multidimensionalData">一个数据项的多维数据列表,而不是多个数据项的数据</param>
  471. public bool UpdateData(int serieIndex, int dataIndex, List<double> multidimensionalData)
  472. {
  473. var serie = GetSerie(serieIndex);
  474. if (serie != null)
  475. {
  476. serie.UpdateData(dataIndex, multidimensionalData);
  477. RefreshPainter(serie);
  478. return true;
  479. }
  480. return false;
  481. }
  482. /// <summary>
  483. /// 更新指定系列指定索引指定维数的数据。维数从0开始。
  484. /// </summary>
  485. /// <param name="serieName"></param>
  486. /// <param name="dataIndex"></param>
  487. /// <param name="dimension">指定维数,从0开始</param>
  488. /// <param name="value"></param>
  489. public bool UpdateData(string serieName, int dataIndex, int dimension, double value)
  490. {
  491. var serie = GetSerie(serieName);
  492. if (serie != null)
  493. {
  494. serie.UpdateData(dataIndex, dimension, value);
  495. RefreshPainter(serie);
  496. return true;
  497. }
  498. return false;
  499. }
  500. /// <summary>
  501. /// 更新指定系列指定索引指定维数的数据。维数从0开始。
  502. /// </summary>
  503. /// <param name="serieIndex"></param>
  504. /// <param name="dataIndex"></param>
  505. /// <param name="dimension">指定维数,从0开始</param>
  506. /// <param name="value"></param>
  507. public bool UpdateData(int serieIndex, int dataIndex, int dimension, double value)
  508. {
  509. var serie = GetSerie(serieIndex);
  510. if (serie != null)
  511. {
  512. serie.UpdateData(dataIndex, dimension, value);
  513. RefreshPainter(serie);
  514. return true;
  515. }
  516. return false;
  517. }
  518. /// <summary>
  519. /// Update serie data name.
  520. /// |更新指定系列中的指定索引数据名称。
  521. /// </summary>
  522. /// <param name="serieName"></param>
  523. /// <param name="dataIndex"></param>
  524. /// <param name="dataName"></param>
  525. public bool UpdateDataName(string serieName, int dataIndex, string dataName)
  526. {
  527. var serie = GetSerie(serieName);
  528. if (serie != null)
  529. {
  530. serie.UpdateDataName(dataIndex, dataName);
  531. return true;
  532. }
  533. return false;
  534. }
  535. /// <summary>
  536. /// Update serie data name.
  537. /// |更新指定系列中的指定索引数据名称。
  538. /// </summary>
  539. /// <param name="serieIndex"></param>
  540. /// <param name="dataName"></param>
  541. /// <param name="dataIndex"></param>
  542. public bool UpdateDataName(int serieIndex, int dataIndex, string dataName)
  543. {
  544. var serie = GetSerie(serieIndex);
  545. if (serie != null)
  546. {
  547. serie.UpdateDataName(dataIndex, dataName);
  548. return true;
  549. }
  550. return false;
  551. }
  552. public double GetData(string serieName, int dataIndex, int dimension = 1)
  553. {
  554. var serie = GetSerie(serieName);
  555. if (serie != null)
  556. {
  557. return serie.GetData(dataIndex, dimension);
  558. }
  559. return 0;
  560. }
  561. public double GetData(int serieIndex, int dataIndex, int dimension = 1)
  562. {
  563. var serie = GetSerie(serieIndex);
  564. if (serie != null)
  565. {
  566. return serie.GetData(dataIndex, dimension);
  567. }
  568. return 0;
  569. }
  570. public int GetAllSerieDataCount()
  571. {
  572. var total = 0;
  573. foreach (var serie in m_Series)
  574. total += serie.dataCount;
  575. return total;
  576. }
  577. /// <summary>
  578. /// Whether to show serie.
  579. /// |设置指定系列是否显示。
  580. /// </summary>
  581. /// <param name="serieName">the name of serie</param>
  582. /// <param name="active">Active or not</param>
  583. public void SetSerieActive(string serieName, bool active)
  584. {
  585. var serie = GetSerie(serieName);
  586. if (serie != null)
  587. SetSerieActive(serie, active);
  588. }
  589. /// <summary>
  590. /// Whether to show serie.
  591. /// |设置指定系列是否显示。
  592. /// </summary>
  593. /// <param name="serieIndex">the index of serie</param>
  594. /// <param name="active">Active or not</param>
  595. public void SetSerieActive(int serieIndex, bool active)
  596. {
  597. var serie = GetSerie(serieIndex);
  598. if (serie != null)
  599. SetSerieActive(serie, active);
  600. }
  601. public void SetSerieActive(Serie serie, bool active)
  602. {
  603. serie.show = active;
  604. serie.RefreshLabel();
  605. serie.AnimationReset();
  606. if (active) serie.AnimationFadeIn();
  607. UpdateLegendColor(serie.serieName, active);
  608. }
  609. /// <summary>
  610. /// Add a category data to xAxis.
  611. /// |添加一个类目数据到指定的x轴。
  612. /// </summary>
  613. /// <param name="category">the category data</param>
  614. /// <param name="xAxisIndex">which xAxis should category add to</param>
  615. public void AddXAxisData(string category, int xAxisIndex = 0)
  616. {
  617. var xAxis = GetChartComponent<XAxis>(xAxisIndex);
  618. if (xAxis != null)
  619. {
  620. xAxis.AddData(category);
  621. }
  622. }
  623. /// <summary>
  624. /// Update category data.
  625. /// |更新X轴类目数据。
  626. /// </summary>
  627. /// <param name="index">the index of category data</param>
  628. /// <param name="category"></param>
  629. /// <param name="xAxisIndex">which xAxis index to update to</param>
  630. public void UpdateXAxisData(int index, string category, int xAxisIndex = 0)
  631. {
  632. var xAxis = GetChartComponent<XAxis>(xAxisIndex);
  633. if (xAxis != null)
  634. {
  635. xAxis.UpdateData(index, category);
  636. }
  637. }
  638. /// <summary>
  639. /// Add an icon to xAxis.
  640. /// |添加一个图标到指定的x轴。
  641. /// </summary>
  642. /// <param name="icon"></param>
  643. /// <param name="xAxisIndex"></param>
  644. public void AddXAxisIcon(Sprite icon, int xAxisIndex = 0)
  645. {
  646. var xAxis = GetChartComponent<XAxis>(xAxisIndex);
  647. if (xAxis != null)
  648. {
  649. xAxis.AddIcon(icon);
  650. }
  651. }
  652. /// <summary>
  653. /// Update xAxis icon.
  654. /// |更新X轴图标。
  655. /// </summary>
  656. /// <param name="index"></param>
  657. /// <param name="icon"></param>
  658. /// <param name="xAxisIndex"></param>
  659. public void UpdateXAxisIcon(int index, Sprite icon, int xAxisIndex = 0)
  660. {
  661. var xAxis = GetChartComponent<XAxis>(xAxisIndex);
  662. if (xAxis != null)
  663. {
  664. xAxis.UpdateIcon(index, icon);
  665. }
  666. }
  667. /// <summary>
  668. /// Add a category data to yAxis.
  669. /// |添加一个类目数据到指定的y轴。
  670. /// </summary>
  671. /// <param name="category">the category data</param>
  672. /// <param name="yAxisIndex">which yAxis should category add to</param>
  673. public void AddYAxisData(string category, int yAxisIndex = 0)
  674. {
  675. var yAxis = GetChartComponent<YAxis>(yAxisIndex);
  676. if (yAxis != null)
  677. {
  678. yAxis.AddData(category);
  679. }
  680. }
  681. /// <summary>
  682. /// Update category data.
  683. /// |更新Y轴类目数据。
  684. /// </summary>
  685. /// <param name="index">the index of category data</param>
  686. /// <param name="category"></param>
  687. /// <param name="yAxisIndex">which yAxis index to update to</param>
  688. public void UpdateYAxisData(int index, string category, int yAxisIndex = 0)
  689. {
  690. var yAxis = GetChartComponent<YAxis>(yAxisIndex);
  691. if (yAxis != null)
  692. {
  693. yAxis.UpdateData(index, category);
  694. }
  695. }
  696. /// <summary>
  697. /// Add an icon to yAxis.
  698. /// |添加一个图标到指定的y轴。
  699. /// </summary>
  700. /// <param name="icon"></param>
  701. /// <param name="yAxisIndex"></param>
  702. public void AddYAxisIcon(Sprite icon, int yAxisIndex = 0)
  703. {
  704. var yAxis = GetChartComponent<YAxis>(yAxisIndex);
  705. if (yAxis != null)
  706. {
  707. yAxis.AddIcon(icon);
  708. }
  709. }
  710. /// <summary>
  711. /// 更新Y轴图标。
  712. /// </summary>
  713. /// <param name="index"></param>
  714. /// <param name="icon"></param>
  715. /// <param name="yAxisIndex"></param>
  716. public void UpdateYAxisIcon(int index, Sprite icon, int yAxisIndex = 0)
  717. {
  718. var yAxis = GetChartComponent<YAxis>(yAxisIndex);
  719. if (yAxis != null)
  720. {
  721. yAxis.UpdateIcon(index, icon);
  722. }
  723. }
  724. public float GetSerieBarGap<T>() where T : Serie
  725. {
  726. float gap = 0f;
  727. for (int i = 0; i < m_Series.Count; i++)
  728. {
  729. var serie = m_Series[i];
  730. if (serie is T)
  731. {
  732. if (serie.barGap != 0)
  733. {
  734. gap = serie.barGap;
  735. }
  736. }
  737. }
  738. return gap;
  739. }
  740. public double GetSerieSameStackTotalValue<T>(string stack, int dataIndex) where T : Serie
  741. {
  742. if (string.IsNullOrEmpty(stack)) return 0;
  743. double total = 0;
  744. foreach (var serie in m_Series)
  745. {
  746. if (serie is T)
  747. {
  748. if (stack.Equals(serie.stack))
  749. {
  750. total += serie.data[dataIndex].data[1];
  751. }
  752. }
  753. }
  754. return total;
  755. }
  756. public int GetSerieBarRealCount<T>() where T : Serie
  757. {
  758. var count = 0;
  759. barStackSet.Clear();
  760. for (int i = 0; i < m_Series.Count; i++)
  761. {
  762. var serie = m_Series[i];
  763. if (!serie.show) continue;
  764. if (serie is T)
  765. {
  766. if (!string.IsNullOrEmpty(serie.stack))
  767. {
  768. if (barStackSet.Contains(serie.stack)) continue;
  769. barStackSet.Add(serie.stack);
  770. }
  771. count++;
  772. }
  773. }
  774. return count;
  775. }
  776. private HashSet<string> barStackSet = new HashSet<string>();
  777. public float GetSerieTotalWidth<T>(float categoryWidth, float gap, int realBarCount) where T : Serie
  778. {
  779. float total = 0;
  780. float lastGap = 0;
  781. barStackSet.Clear();
  782. for (int i = 0; i < m_Series.Count; i++)
  783. {
  784. var serie = m_Series[i];
  785. if (!serie.show) continue;
  786. if (serie is T)
  787. {
  788. if (!string.IsNullOrEmpty(serie.stack))
  789. {
  790. if (barStackSet.Contains(serie.stack)) continue;
  791. barStackSet.Add(serie.stack);
  792. }
  793. var width = GetStackBarWidth<T>(categoryWidth, serie, realBarCount);
  794. if (gap == -1)
  795. {
  796. if (width > total) total = width;
  797. }
  798. else
  799. {
  800. lastGap = ChartHelper.GetActualValue(gap, width);
  801. total += width;
  802. total += lastGap;
  803. }
  804. }
  805. }
  806. if (total > 0 && gap != -1) total -= lastGap;
  807. return total;
  808. }
  809. public float GetSerieTotalGap<T>(float categoryWidth, float gap, int index) where T : Serie
  810. {
  811. if (index <= 0) return 0;
  812. var total = 0f;
  813. var count = 0;
  814. var totalRealBarCount = GetSerieBarRealCount<T>();
  815. barStackSet.Clear();
  816. for (int i = 0; i < m_Series.Count; i++)
  817. {
  818. var serie = m_Series[i];
  819. if (!serie.show) continue;
  820. if (serie is T)
  821. {
  822. if (!string.IsNullOrEmpty(serie.stack))
  823. {
  824. if (barStackSet.Contains(serie.stack)) continue;
  825. barStackSet.Add(serie.stack);
  826. }
  827. var width = GetStackBarWidth<T>(categoryWidth, serie, totalRealBarCount);
  828. if (gap == -1)
  829. {
  830. if (width > total) total = width;
  831. }
  832. else
  833. {
  834. total += width + ChartHelper.GetActualValue(gap, width);
  835. }
  836. if (count + 1 >= index)
  837. break;
  838. else
  839. count++;
  840. }
  841. }
  842. return total;
  843. }
  844. private float GetStackBarWidth<T>(float categoryWidth, Serie now, int realBarCount) where T : Serie
  845. {
  846. if (string.IsNullOrEmpty(now.stack)) return now.GetBarWidth(categoryWidth, realBarCount);
  847. float barWidth = 0;
  848. for (int i = 0; i < m_Series.Count; i++)
  849. {
  850. var serie = m_Series[i];
  851. if ((serie is T) &&
  852. serie.show && now.stack.Equals(serie.stack))
  853. {
  854. if (serie.barWidth > barWidth) barWidth = serie.barWidth;
  855. }
  856. }
  857. if (barWidth == 0)
  858. {
  859. var width = ChartHelper.GetActualValue(0.6f, categoryWidth);
  860. if (realBarCount == 0)
  861. return width < 1 ? categoryWidth : width;
  862. else
  863. return width / realBarCount;
  864. }
  865. else
  866. return ChartHelper.GetActualValue(barWidth, categoryWidth);
  867. }
  868. private List<string> tempList = new List<string>();
  869. public int GetSerieIndexIfStack<T>(Serie currSerie) where T : Serie
  870. {
  871. tempList.Clear();
  872. int index = 0;
  873. for (int i = 0; i < m_Series.Count; i++)
  874. {
  875. var serie = m_Series[i];
  876. if (!(serie is T)) continue;
  877. if (string.IsNullOrEmpty(serie.stack))
  878. {
  879. if (serie.index == currSerie.index) return index;
  880. tempList.Add(string.Empty);
  881. index++;
  882. }
  883. else
  884. {
  885. if (!tempList.Contains(serie.stack))
  886. {
  887. if (serie.index == currSerie.index) return index;
  888. tempList.Add(serie.stack);
  889. index++;
  890. }
  891. else
  892. {
  893. if (serie.index == currSerie.index) return tempList.IndexOf(serie.stack);
  894. }
  895. }
  896. }
  897. return 0;
  898. }
  899. internal void InitSerieHandlers()
  900. {
  901. m_SerieHandlers.Clear();
  902. for (int i = 0; i < m_Series.Count; i++)
  903. {
  904. var serie = m_Series[i];
  905. serie.index = i;
  906. CreateSerieHandler(serie);
  907. }
  908. }
  909. private void CreateSerieHandler(Serie serie)
  910. {
  911. if (serie == null)
  912. throw new ArgumentNullException("serie is null");
  913. if (!serie.GetType().IsDefined(typeof(SerieHandlerAttribute), false))
  914. {
  915. Debug.LogError("Serie no Handler:" + serie.GetType());
  916. return;
  917. }
  918. var attribute = serie.GetType().GetAttribute<SerieHandlerAttribute>();
  919. var handler = (SerieHandler) Activator.CreateInstance(attribute.handler);
  920. handler.attribute = attribute;
  921. handler.chart = this;
  922. handler.defaultDimension = 1;
  923. handler.SetSerie(serie);
  924. serie.handler = handler;
  925. m_SerieHandlers.Add(handler);
  926. }
  927. private Serie InsertSerie(int index, Type type, string serieName, bool show = true, bool addToHead = false)
  928. {
  929. CheckAddRequireChartComponent(type);
  930. var serie = Activator.CreateInstance(type) as Serie;
  931. serie.show = show;
  932. serie.serieName = serieName;
  933. serie.serieType = type.Name;
  934. serie.index = m_Series.Count;
  935. if (type == typeof(Scatter))
  936. {
  937. serie.symbol.show = true;
  938. serie.symbol.type = SymbolType.Circle;
  939. }
  940. else if (type == typeof(Line))
  941. {
  942. serie.symbol.show = true;
  943. serie.symbol.type = SymbolType.EmptyCircle;
  944. }
  945. else if (type == typeof(Heatmap))
  946. {
  947. serie.symbol.show = true;
  948. serie.symbol.type = SymbolType.Rect;
  949. }
  950. else
  951. {
  952. serie.symbol.show = false;
  953. }
  954. InsertSerie(serie, index, addToHead);
  955. return serie;
  956. }
  957. private void ResetSeriesIndex()
  958. {
  959. #if UNITY_EDITOR && UNITY_2019_1_OR_NEWER
  960. UnityEditor.EditorUtility.SetDirty(this);
  961. #endif
  962. for (int i = 0; i < m_Series.Count; i++)
  963. {
  964. m_Series[i].index = i;
  965. }
  966. }
  967. private void AddSerieAfterDeserialize(Serie serie)
  968. {
  969. serie.OnAfterDeserialize();
  970. m_Series.Add(serie);
  971. }
  972. public string GenerateDefaultSerieName()
  973. {
  974. return "serie" + m_Series.Count;
  975. }
  976. public bool IsSerieName(string name)
  977. {
  978. if (string.IsNullOrEmpty(name))
  979. return false;
  980. foreach (var serie in m_Series)
  981. {
  982. if (name.Equals(serie.serieName))
  983. return true;
  984. }
  985. return false;
  986. }
  987. }
  988. }