BaseChart.Serie.cs 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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. /// Add a link data to serie.
  414. /// ||添加一个关系图的关系数据。
  415. /// </summary>
  416. /// <param name="serieIndex">the index of serie</param>
  417. /// <param name="sourceName">the source name of link</param>
  418. /// <param name="targetName">the target name of link</param>
  419. /// <param name="value">the value of link</param>
  420. /// <returns></returns>
  421. public SerieDataLink AddLink(int serieIndex, string sourceName, string targetName, double value)
  422. {
  423. var serie = GetSerie(serieIndex);
  424. if (serie != null)
  425. {
  426. var link = serie.AddLink(sourceName, targetName, value);
  427. RefreshPainter(serie.painter);
  428. return link;
  429. }
  430. return null;
  431. }
  432. /// <summary>
  433. /// Update serie data by serie name.
  434. /// ||更新指定系列中的指定索引数据。
  435. /// </summary>
  436. /// <param name="serieName">the name of serie</param>
  437. /// <param name="dataIndex">the index of data</param>
  438. /// <param name="value">the data will be update</param>
  439. public bool UpdateData(string serieName, int dataIndex, double value)
  440. {
  441. var serie = GetSerie(serieName);
  442. if (serie != null)
  443. {
  444. if (serie.UpdateYData(dataIndex, value))
  445. {
  446. RefreshPainter(serie);
  447. return true;
  448. }
  449. else
  450. {
  451. return false;
  452. }
  453. }
  454. return false;
  455. }
  456. /// <summary>
  457. /// Update serie data by serie index.
  458. /// ||更新指定系列中的指定索引数据。
  459. /// </summary>
  460. /// <param name="serieIndex">the index of serie</param>
  461. /// <param name="dataIndex">the index of data</param>
  462. /// <param name="value">the data will be update</param>
  463. public bool UpdateData(int serieIndex, int dataIndex, double value)
  464. {
  465. var serie = GetSerie(serieIndex);
  466. if (serie != null)
  467. {
  468. if (serie.UpdateYData(dataIndex, value))
  469. {
  470. RefreshPainter(serie);
  471. return true;
  472. }
  473. else
  474. {
  475. return false;
  476. }
  477. }
  478. return false;
  479. }
  480. /// <summary>
  481. /// 更新指定系列指定索引的数据项的多维数据。
  482. /// </summary>
  483. /// <param name="serieName"></param>
  484. /// <param name="dataIndex"></param>
  485. /// <param name="multidimensionalData">一个数据项的多维数据列表,而不是多个数据项的数据</param>
  486. public bool UpdateData(string serieName, int dataIndex, List<double> multidimensionalData)
  487. {
  488. var serie = GetSerie(serieName);
  489. if (serie != null)
  490. {
  491. if (serie.UpdateData(dataIndex, multidimensionalData))
  492. {
  493. RefreshPainter(serie);
  494. return true;
  495. }
  496. else
  497. {
  498. return false;
  499. }
  500. }
  501. return false;
  502. }
  503. /// <summary>
  504. /// 更新指定系列指定索引的数据项的多维数据。
  505. /// </summary>
  506. /// <param name="serieIndex"></param>
  507. /// <param name="dataIndex"></param>
  508. /// <param name="multidimensionalData">一个数据项的多维数据列表,而不是多个数据项的数据</param>
  509. public bool UpdateData(int serieIndex, int dataIndex, List<double> multidimensionalData)
  510. {
  511. var serie = GetSerie(serieIndex);
  512. if (serie != null)
  513. {
  514. if (serie.UpdateData(dataIndex, multidimensionalData))
  515. {
  516. RefreshPainter(serie);
  517. return true;
  518. }
  519. else
  520. {
  521. return false;
  522. }
  523. }
  524. return false;
  525. }
  526. /// <summary>
  527. /// 更新指定系列指定索引指定维数的数据。维数从0开始。
  528. /// </summary>
  529. /// <param name="serieName"></param>
  530. /// <param name="dataIndex"></param>
  531. /// <param name="dimension">指定维数,从0开始</param>
  532. /// <param name="value"></param>
  533. public bool UpdateData(string serieName, int dataIndex, int dimension, double value)
  534. {
  535. var serie = GetSerie(serieName);
  536. if (serie != null)
  537. {
  538. if (serie.UpdateData(dataIndex, dimension, value))
  539. {
  540. RefreshPainter(serie);
  541. return true;
  542. }
  543. else
  544. {
  545. return false;
  546. }
  547. }
  548. return false;
  549. }
  550. /// <summary>
  551. /// 更新指定系列指定索引指定维数的数据。维数从0开始。
  552. /// </summary>
  553. /// <param name="serieIndex"></param>
  554. /// <param name="dataIndex"></param>
  555. /// <param name="dimension">指定维数,从0开始</param>
  556. /// <param name="value"></param>
  557. public bool UpdateData(int serieIndex, int dataIndex, int dimension, double value)
  558. {
  559. var serie = GetSerie(serieIndex);
  560. if (serie != null)
  561. {
  562. if (serie.UpdateData(dataIndex, dimension, value))
  563. {
  564. RefreshPainter(serie);
  565. return true;
  566. }
  567. else
  568. {
  569. return false;
  570. }
  571. }
  572. return false;
  573. }
  574. /// <summary>
  575. /// Update serie data name.
  576. /// ||更新指定系列中的指定索引数据名称。
  577. /// </summary>
  578. /// <param name="serieName"></param>
  579. /// <param name="dataIndex"></param>
  580. /// <param name="dataName"></param>
  581. public bool UpdateDataName(string serieName, int dataIndex, string dataName)
  582. {
  583. var serie = GetSerie(serieName);
  584. if (serie != null)
  585. {
  586. return serie.UpdateDataName(dataIndex, dataName);
  587. }
  588. return false;
  589. }
  590. /// <summary>
  591. /// Update serie data name.
  592. /// ||更新指定系列中的指定索引数据名称。
  593. /// </summary>
  594. /// <param name="serieIndex"></param>
  595. /// <param name="dataName"></param>
  596. /// <param name="dataIndex"></param>
  597. public bool UpdateDataName(int serieIndex, int dataIndex, string dataName)
  598. {
  599. var serie = GetSerie(serieIndex);
  600. if (serie != null)
  601. {
  602. return serie.UpdateDataName(dataIndex, dataName);
  603. }
  604. return false;
  605. }
  606. public double GetData(string serieName, int dataIndex, int dimension = 1)
  607. {
  608. var serie = GetSerie(serieName);
  609. if (serie != null)
  610. {
  611. return serie.GetData(dataIndex, dimension);
  612. }
  613. return 0;
  614. }
  615. public double GetData(int serieIndex, int dataIndex, int dimension = 1)
  616. {
  617. var serie = GetSerie(serieIndex);
  618. if (serie != null)
  619. {
  620. return serie.GetData(dataIndex, dimension);
  621. }
  622. return 0;
  623. }
  624. public int GetAllSerieDataCount()
  625. {
  626. var total = 0;
  627. foreach (var serie in m_Series)
  628. total += serie.dataCount;
  629. return total;
  630. }
  631. /// <summary>
  632. /// Whether to show serie.
  633. /// ||设置指定系列是否显示。
  634. /// </summary>
  635. /// <param name="serieName">the name of serie</param>
  636. /// <param name="active">Active or not</param>
  637. public void SetSerieActive(string serieName, bool active)
  638. {
  639. var serie = GetSerie(serieName);
  640. if (serie != null)
  641. SetSerieActive(serie, active);
  642. }
  643. /// <summary>
  644. /// Whether to show serie.
  645. /// ||设置指定系列是否显示。
  646. /// </summary>
  647. /// <param name="serieIndex">the index of serie</param>
  648. /// <param name="active">Active or not</param>
  649. public void SetSerieActive(int serieIndex, bool active)
  650. {
  651. var serie = GetSerie(serieIndex);
  652. if (serie != null)
  653. SetSerieActive(serie, active);
  654. }
  655. public void SetSerieActive(Serie serie, bool active)
  656. {
  657. serie.show = active;
  658. serie.RefreshLabel();
  659. serie.AnimationReset();
  660. if (active) serie.AnimationFadeIn();
  661. UpdateLegendColor(serie.serieName, active);
  662. }
  663. /// <summary>
  664. /// Add a category data to xAxis.
  665. /// ||添加一个类目数据到指定的x轴。
  666. /// </summary>
  667. /// <param name="category">the category data</param>
  668. /// <param name="xAxisIndex">which xAxis should category add to</param>
  669. public void AddXAxisData(string category, int xAxisIndex = 0)
  670. {
  671. var xAxis = GetChartComponent<XAxis>(xAxisIndex);
  672. if (xAxis != null)
  673. {
  674. xAxis.AddData(category);
  675. }
  676. }
  677. /// <summary>
  678. /// Update category data.
  679. /// ||更新X轴类目数据。
  680. /// </summary>
  681. /// <param name="index">the index of category data</param>
  682. /// <param name="category"></param>
  683. /// <param name="xAxisIndex">which xAxis index to update to</param>
  684. public void UpdateXAxisData(int index, string category, int xAxisIndex = 0)
  685. {
  686. var xAxis = GetChartComponent<XAxis>(xAxisIndex);
  687. if (xAxis != null)
  688. {
  689. xAxis.UpdateData(index, category);
  690. }
  691. }
  692. /// <summary>
  693. /// Add an icon to xAxis.
  694. /// ||添加一个图标到指定的x轴。
  695. /// </summary>
  696. /// <param name="icon"></param>
  697. /// <param name="xAxisIndex"></param>
  698. public void AddXAxisIcon(Sprite icon, int xAxisIndex = 0)
  699. {
  700. var xAxis = GetChartComponent<XAxis>(xAxisIndex);
  701. if (xAxis != null)
  702. {
  703. xAxis.AddIcon(icon);
  704. }
  705. }
  706. /// <summary>
  707. /// Update xAxis icon.
  708. /// ||更新X轴图标。
  709. /// </summary>
  710. /// <param name="index"></param>
  711. /// <param name="icon"></param>
  712. /// <param name="xAxisIndex"></param>
  713. public void UpdateXAxisIcon(int index, Sprite icon, int xAxisIndex = 0)
  714. {
  715. var xAxis = GetChartComponent<XAxis>(xAxisIndex);
  716. if (xAxis != null)
  717. {
  718. xAxis.UpdateIcon(index, icon);
  719. }
  720. }
  721. /// <summary>
  722. /// Add a category data to yAxis.
  723. /// ||添加一个类目数据到指定的y轴。
  724. /// </summary>
  725. /// <param name="category">the category data</param>
  726. /// <param name="yAxisIndex">which yAxis should category add to</param>
  727. public void AddYAxisData(string category, int yAxisIndex = 0)
  728. {
  729. var yAxis = GetChartComponent<YAxis>(yAxisIndex);
  730. if (yAxis != null)
  731. {
  732. yAxis.AddData(category);
  733. }
  734. }
  735. /// <summary>
  736. /// Update category data.
  737. /// ||更新Y轴类目数据。
  738. /// </summary>
  739. /// <param name="index">the index of category data</param>
  740. /// <param name="category"></param>
  741. /// <param name="yAxisIndex">which yAxis index to update to</param>
  742. public void UpdateYAxisData(int index, string category, int yAxisIndex = 0)
  743. {
  744. var yAxis = GetChartComponent<YAxis>(yAxisIndex);
  745. if (yAxis != null)
  746. {
  747. yAxis.UpdateData(index, category);
  748. }
  749. }
  750. /// <summary>
  751. /// Add an icon to yAxis.
  752. /// ||添加一个图标到指定的y轴。
  753. /// </summary>
  754. /// <param name="icon"></param>
  755. /// <param name="yAxisIndex"></param>
  756. public void AddYAxisIcon(Sprite icon, int yAxisIndex = 0)
  757. {
  758. var yAxis = GetChartComponent<YAxis>(yAxisIndex);
  759. if (yAxis != null)
  760. {
  761. yAxis.AddIcon(icon);
  762. }
  763. }
  764. /// <summary>
  765. /// 更新Y轴图标。
  766. /// </summary>
  767. /// <param name="index"></param>
  768. /// <param name="icon"></param>
  769. /// <param name="yAxisIndex"></param>
  770. public void UpdateYAxisIcon(int index, Sprite icon, int yAxisIndex = 0)
  771. {
  772. var yAxis = GetChartComponent<YAxis>(yAxisIndex);
  773. if (yAxis != null)
  774. {
  775. yAxis.UpdateIcon(index, icon);
  776. }
  777. }
  778. public float GetSerieBarGap<T>() where T : Serie
  779. {
  780. float gap = 0f;
  781. for (int i = 0; i < m_Series.Count; i++)
  782. {
  783. var serie = m_Series[i];
  784. if (serie.show && serie is T)
  785. {
  786. if (serie.barGap != 0)
  787. {
  788. gap = serie.barGap;
  789. }
  790. }
  791. }
  792. return gap;
  793. }
  794. public double GetSerieSameStackTotalValue<T>(string stack, int dataIndex) where T : Serie
  795. {
  796. if (string.IsNullOrEmpty(stack)) return 0;
  797. double total = 0;
  798. foreach (var serie in m_Series)
  799. {
  800. if (serie is T)
  801. {
  802. if (stack.Equals(serie.stack))
  803. {
  804. total += serie.data[dataIndex].data[1];
  805. }
  806. }
  807. }
  808. return total;
  809. }
  810. public int GetSerieBarRealCount<T>() where T : Serie
  811. {
  812. var count = 0;
  813. barStackSet.Clear();
  814. for (int i = 0; i < m_Series.Count; i++)
  815. {
  816. var serie = m_Series[i];
  817. if (!serie.show) continue;
  818. if (serie is T)
  819. {
  820. if (!string.IsNullOrEmpty(serie.stack))
  821. {
  822. if (barStackSet.Contains(serie.stack)) continue;
  823. barStackSet.Add(serie.stack);
  824. }
  825. count++;
  826. }
  827. }
  828. return count;
  829. }
  830. private HashSet<string> barStackSet = new HashSet<string>();
  831. public float GetSerieTotalWidth<T>(float categoryWidth, float gap, int realBarCount) where T : Serie
  832. {
  833. float total = 0;
  834. float lastGap = 0;
  835. barStackSet.Clear();
  836. for (int i = 0; i < m_Series.Count; i++)
  837. {
  838. var serie = m_Series[i];
  839. if (!serie.show) continue;
  840. if (serie is T)
  841. {
  842. if (!string.IsNullOrEmpty(serie.stack))
  843. {
  844. if (barStackSet.Contains(serie.stack)) continue;
  845. barStackSet.Add(serie.stack);
  846. }
  847. var width = GetStackBarWidth<T>(categoryWidth, serie, realBarCount);
  848. if (gap == -1)
  849. {
  850. if (width > total) total = width;
  851. }
  852. else
  853. {
  854. lastGap = ChartHelper.GetActualValue(gap, width);
  855. total += width;
  856. total += lastGap;
  857. }
  858. }
  859. }
  860. if (total > 0 && gap != -1) total -= lastGap;
  861. return total;
  862. }
  863. public float GetSerieTotalGap<T>(float categoryWidth, float gap, int index) where T : Serie
  864. {
  865. if (index <= 0) return 0;
  866. var total = 0f;
  867. var count = 0;
  868. var totalRealBarCount = GetSerieBarRealCount<T>();
  869. barStackSet.Clear();
  870. for (int i = 0; i < m_Series.Count; i++)
  871. {
  872. var serie = m_Series[i];
  873. if (!serie.show) continue;
  874. if (serie is T)
  875. {
  876. if (!string.IsNullOrEmpty(serie.stack))
  877. {
  878. if (barStackSet.Contains(serie.stack)) continue;
  879. barStackSet.Add(serie.stack);
  880. }
  881. var width = GetStackBarWidth<T>(categoryWidth, serie, totalRealBarCount);
  882. if (gap == -1)
  883. {
  884. if (width > total) total = width;
  885. }
  886. else
  887. {
  888. total += width + ChartHelper.GetActualValue(gap, width);
  889. }
  890. if (count + 1 >= index)
  891. break;
  892. else
  893. count++;
  894. }
  895. }
  896. return total;
  897. }
  898. private float GetStackBarWidth<T>(float categoryWidth, Serie now, int realBarCount) where T : Serie
  899. {
  900. if (string.IsNullOrEmpty(now.stack)) return now.GetBarWidth(categoryWidth, realBarCount);
  901. float barWidth = 0;
  902. for (int i = 0; i < m_Series.Count; i++)
  903. {
  904. var serie = m_Series[i];
  905. if ((serie is T) &&
  906. serie.show && now.stack.Equals(serie.stack))
  907. {
  908. if (serie.barWidth > barWidth) barWidth = serie.barWidth;
  909. }
  910. }
  911. if (barWidth == 0)
  912. {
  913. var width = ChartHelper.GetActualValue(0.6f, categoryWidth);
  914. if (realBarCount == 0)
  915. return width < 1 ? categoryWidth : width;
  916. else
  917. return width / realBarCount;
  918. }
  919. else
  920. return ChartHelper.GetActualValue(barWidth, categoryWidth);
  921. }
  922. private List<string> tempList = new List<string>();
  923. public int GetSerieIndexIfStack<T>(Serie currSerie) where T : Serie
  924. {
  925. tempList.Clear();
  926. int index = 0;
  927. for (int i = 0; i < m_Series.Count; i++)
  928. {
  929. var serie = m_Series[i];
  930. if (!serie.show) continue;
  931. if (!(serie is T)) continue;
  932. if (string.IsNullOrEmpty(serie.stack))
  933. {
  934. if (serie.index == currSerie.index) return index;
  935. tempList.Add(string.Empty);
  936. index++;
  937. }
  938. else
  939. {
  940. if (!tempList.Contains(serie.stack))
  941. {
  942. if (serie.index == currSerie.index) return index;
  943. tempList.Add(serie.stack);
  944. index++;
  945. }
  946. else
  947. {
  948. if (serie.index == currSerie.index) return tempList.IndexOf(serie.stack);
  949. }
  950. }
  951. }
  952. return 0;
  953. }
  954. internal void InitSerieHandlers()
  955. {
  956. m_SerieHandlers.Clear();
  957. for (int i = 0; i < m_Series.Count; i++)
  958. {
  959. var serie = m_Series[i];
  960. serie.index = i;
  961. CreateSerieHandler(serie);
  962. }
  963. }
  964. private void CreateSerieHandler(Serie serie)
  965. {
  966. if (serie == null)
  967. throw new ArgumentNullException("serie is null");
  968. if (serie.GetType().IsDefined(typeof(DefaultTooltipAttribute), false))
  969. {
  970. var attribute1 = serie.GetType().GetAttribute<DefaultTooltipAttribute>();
  971. if (attribute1 != null)
  972. {
  973. serie.context.tooltipTrigger = attribute1.trigger;
  974. serie.context.tooltipType = attribute1.type;
  975. }
  976. }
  977. if (!serie.GetType().IsDefined(typeof(SerieHandlerAttribute), false))
  978. {
  979. Debug.LogError("Serie no Handler:" + serie.GetType());
  980. return;
  981. }
  982. var attribute = serie.GetType().GetAttribute<SerieHandlerAttribute>();
  983. var handler = (SerieHandler)Activator.CreateInstance(attribute.handler);
  984. handler.attribute = attribute;
  985. handler.chart = this;
  986. handler.defaultDimension = 1;
  987. handler.SetSerie(serie);
  988. serie.handler = handler;
  989. m_SerieHandlers.Add(handler);
  990. }
  991. private Serie InsertSerie(int index, Type type, string serieName, bool show = true, bool addToHead = false)
  992. {
  993. CheckAddRequireChartComponent(type);
  994. var serie = Activator.CreateInstance(type) as Serie;
  995. serie.show = show;
  996. serie.serieName = serieName;
  997. serie.serieType = type.Name;
  998. serie.index = m_Series.Count;
  999. if (type == typeof(Scatter))
  1000. {
  1001. serie.symbol.show = true;
  1002. serie.symbol.type = SymbolType.Circle;
  1003. }
  1004. else if (type == typeof(Line))
  1005. {
  1006. serie.symbol.show = true;
  1007. serie.symbol.type = SymbolType.EmptyCircle;
  1008. }
  1009. else if (type == typeof(Heatmap))
  1010. {
  1011. serie.symbol.show = true;
  1012. serie.symbol.type = SymbolType.Rect;
  1013. }
  1014. else
  1015. {
  1016. serie.symbol.show = false;
  1017. }
  1018. InsertSerie(serie, index, addToHead);
  1019. return serie;
  1020. }
  1021. private void ResetSeriesIndex()
  1022. {
  1023. #if UNITY_EDITOR && UNITY_2019_1_OR_NEWER
  1024. UnityEditor.EditorUtility.SetDirty(this);
  1025. #endif
  1026. for (int i = 0; i < m_Series.Count; i++)
  1027. {
  1028. m_Series[i].index = i;
  1029. }
  1030. }
  1031. private void AddSerieAfterDeserialize(Serie serie)
  1032. {
  1033. serie.OnAfterDeserialize();
  1034. m_Series.Add(serie);
  1035. }
  1036. public string GenerateDefaultSerieName()
  1037. {
  1038. return "serie" + m_Series.Count;
  1039. }
  1040. public bool IsSerieName(string name)
  1041. {
  1042. if (string.IsNullOrEmpty(name))
  1043. return false;
  1044. foreach (var serie in m_Series)
  1045. {
  1046. if (name.Equals(serie.serieName))
  1047. return true;
  1048. }
  1049. return false;
  1050. }
  1051. }
  1052. }