설명 없음
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 36KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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.AnimationReset();
  605. if (active) serie.AnimationFadeIn();
  606. UpdateLegendColor(serie.serieName, active);
  607. }
  608. /// <summary>
  609. /// Add a category data to xAxis.
  610. /// |添加一个类目数据到指定的x轴。
  611. /// </summary>
  612. /// <param name="category">the category data</param>
  613. /// <param name="xAxisIndex">which xAxis should category add to</param>
  614. public void AddXAxisData(string category, int xAxisIndex = 0)
  615. {
  616. var xAxis = GetChartComponent<XAxis>(xAxisIndex);
  617. if (xAxis != null)
  618. {
  619. xAxis.AddData(category);
  620. }
  621. }
  622. /// <summary>
  623. /// Update category data.
  624. /// |更新X轴类目数据。
  625. /// </summary>
  626. /// <param name="index">the index of category data</param>
  627. /// <param name="category"></param>
  628. /// <param name="xAxisIndex">which xAxis index to update to</param>
  629. public void UpdateXAxisData(int index, string category, int xAxisIndex = 0)
  630. {
  631. var xAxis = GetChartComponent<XAxis>(xAxisIndex);
  632. if (xAxis != null)
  633. {
  634. xAxis.UpdateData(index, category);
  635. }
  636. }
  637. /// <summary>
  638. /// Add an icon to xAxis.
  639. /// |添加一个图标到指定的x轴。
  640. /// </summary>
  641. /// <param name="icon"></param>
  642. /// <param name="xAxisIndex"></param>
  643. public void AddXAxisIcon(Sprite icon, int xAxisIndex = 0)
  644. {
  645. var xAxis = GetChartComponent<XAxis>(xAxisIndex);
  646. if (xAxis != null)
  647. {
  648. xAxis.AddIcon(icon);
  649. }
  650. }
  651. /// <summary>
  652. /// Update xAxis icon.
  653. /// |更新X轴图标。
  654. /// </summary>
  655. /// <param name="index"></param>
  656. /// <param name="icon"></param>
  657. /// <param name="xAxisIndex"></param>
  658. public void UpdateXAxisIcon(int index, Sprite icon, int xAxisIndex = 0)
  659. {
  660. var xAxis = GetChartComponent<XAxis>(xAxisIndex);
  661. if (xAxis != null)
  662. {
  663. xAxis.UpdateIcon(index, icon);
  664. }
  665. }
  666. /// <summary>
  667. /// Add a category data to yAxis.
  668. /// |添加一个类目数据到指定的y轴。
  669. /// </summary>
  670. /// <param name="category">the category data</param>
  671. /// <param name="yAxisIndex">which yAxis should category add to</param>
  672. public void AddYAxisData(string category, int yAxisIndex = 0)
  673. {
  674. var yAxis = GetChartComponent<YAxis>(yAxisIndex);
  675. if (yAxis != null)
  676. {
  677. yAxis.AddData(category);
  678. }
  679. }
  680. /// <summary>
  681. /// Update category data.
  682. /// |更新Y轴类目数据。
  683. /// </summary>
  684. /// <param name="index">the index of category data</param>
  685. /// <param name="category"></param>
  686. /// <param name="yAxisIndex">which yAxis index to update to</param>
  687. public void UpdateYAxisData(int index, string category, int yAxisIndex = 0)
  688. {
  689. var yAxis = GetChartComponent<YAxis>(yAxisIndex);
  690. if (yAxis != null)
  691. {
  692. yAxis.UpdateData(index, category);
  693. }
  694. }
  695. /// <summary>
  696. /// Add an icon to yAxis.
  697. /// |添加一个图标到指定的y轴。
  698. /// </summary>
  699. /// <param name="icon"></param>
  700. /// <param name="yAxisIndex"></param>
  701. public void AddYAxisIcon(Sprite icon, int yAxisIndex = 0)
  702. {
  703. var yAxis = GetChartComponent<YAxis>(yAxisIndex);
  704. if (yAxis != null)
  705. {
  706. yAxis.AddIcon(icon);
  707. }
  708. }
  709. /// <summary>
  710. /// 更新Y轴图标。
  711. /// </summary>
  712. /// <param name="index"></param>
  713. /// <param name="icon"></param>
  714. /// <param name="yAxisIndex"></param>
  715. public void UpdateYAxisIcon(int index, Sprite icon, int yAxisIndex = 0)
  716. {
  717. var yAxis = GetChartComponent<YAxis>(yAxisIndex);
  718. if (yAxis != null)
  719. {
  720. yAxis.UpdateIcon(index, icon);
  721. }
  722. }
  723. public float GetSerieBarGap<T>() where T : Serie
  724. {
  725. float gap = 0f;
  726. for (int i = 0; i < m_Series.Count; i++)
  727. {
  728. var serie = m_Series[i];
  729. if (serie is T)
  730. {
  731. if (serie.barGap != 0)
  732. {
  733. gap = serie.barGap;
  734. }
  735. }
  736. }
  737. return gap;
  738. }
  739. public double GetSerieSameStackTotalValue<T>(string stack, int dataIndex) where T : Serie
  740. {
  741. if (string.IsNullOrEmpty(stack)) return 0;
  742. double total = 0;
  743. foreach (var serie in m_Series)
  744. {
  745. if (serie is T)
  746. {
  747. if (stack.Equals(serie.stack))
  748. {
  749. total += serie.data[dataIndex].data[1];
  750. }
  751. }
  752. }
  753. return total;
  754. }
  755. public int GetSerieBarRealCount<T>() where T : Serie
  756. {
  757. var count = 0;
  758. barStackSet.Clear();
  759. for (int i = 0; i < m_Series.Count; i++)
  760. {
  761. var serie = m_Series[i];
  762. if (!serie.show) continue;
  763. if (serie is T)
  764. {
  765. if (!string.IsNullOrEmpty(serie.stack))
  766. {
  767. if (barStackSet.Contains(serie.stack)) continue;
  768. barStackSet.Add(serie.stack);
  769. }
  770. count++;
  771. }
  772. }
  773. return count;
  774. }
  775. private HashSet<string> barStackSet = new HashSet<string>();
  776. public float GetSerieTotalWidth<T>(float categoryWidth, float gap, int realBarCount) where T : Serie
  777. {
  778. float total = 0;
  779. float lastGap = 0;
  780. barStackSet.Clear();
  781. for (int i = 0; i < m_Series.Count; i++)
  782. {
  783. var serie = m_Series[i];
  784. if (!serie.show) continue;
  785. if (serie is T)
  786. {
  787. if (!string.IsNullOrEmpty(serie.stack))
  788. {
  789. if (barStackSet.Contains(serie.stack)) continue;
  790. barStackSet.Add(serie.stack);
  791. }
  792. var width = GetStackBarWidth<T>(categoryWidth, serie, realBarCount);
  793. if (gap == -1)
  794. {
  795. if (width > total) total = width;
  796. }
  797. else
  798. {
  799. lastGap = ChartHelper.GetActualValue(gap, width);
  800. total += width;
  801. total += lastGap;
  802. }
  803. }
  804. }
  805. if (total > 0 && gap != -1) total -= lastGap;
  806. return total;
  807. }
  808. public float GetSerieTotalGap<T>(float categoryWidth, float gap, int index) where T : Serie
  809. {
  810. if (index <= 0) return 0;
  811. var total = 0f;
  812. var count = 0;
  813. var totalRealBarCount = GetSerieBarRealCount<T>();
  814. barStackSet.Clear();
  815. for (int i = 0; i < m_Series.Count; i++)
  816. {
  817. var serie = m_Series[i];
  818. if (!serie.show) continue;
  819. if (serie is T)
  820. {
  821. if (!string.IsNullOrEmpty(serie.stack))
  822. {
  823. if (barStackSet.Contains(serie.stack)) continue;
  824. barStackSet.Add(serie.stack);
  825. }
  826. var width = GetStackBarWidth<T>(categoryWidth, serie, totalRealBarCount);
  827. if (gap == -1)
  828. {
  829. if (width > total) total = width;
  830. }
  831. else
  832. {
  833. total += width + ChartHelper.GetActualValue(gap, width);
  834. }
  835. if (count + 1 >= index)
  836. break;
  837. else
  838. count++;
  839. }
  840. }
  841. return total;
  842. }
  843. private float GetStackBarWidth<T>(float categoryWidth, Serie now, int realBarCount) where T : Serie
  844. {
  845. if (string.IsNullOrEmpty(now.stack)) return now.GetBarWidth(categoryWidth, realBarCount);
  846. float barWidth = 0;
  847. for (int i = 0; i < m_Series.Count; i++)
  848. {
  849. var serie = m_Series[i];
  850. if ((serie is T) &&
  851. serie.show && now.stack.Equals(serie.stack))
  852. {
  853. if (serie.barWidth > barWidth) barWidth = serie.barWidth;
  854. }
  855. }
  856. if (barWidth == 0)
  857. {
  858. var width = ChartHelper.GetActualValue(0.6f, categoryWidth);
  859. if (realBarCount == 0)
  860. return width < 1 ? categoryWidth : width;
  861. else
  862. return width / realBarCount;
  863. }
  864. else
  865. return ChartHelper.GetActualValue(barWidth, categoryWidth);
  866. }
  867. private List<string> tempList = new List<string>();
  868. public int GetSerieIndexIfStack<T>(Serie currSerie) where T : Serie
  869. {
  870. tempList.Clear();
  871. int index = 0;
  872. for (int i = 0; i < m_Series.Count; i++)
  873. {
  874. var serie = m_Series[i];
  875. if (!(serie is T)) continue;
  876. if (string.IsNullOrEmpty(serie.stack))
  877. {
  878. if (serie.index == currSerie.index) return index;
  879. tempList.Add(string.Empty);
  880. index++;
  881. }
  882. else
  883. {
  884. if (!tempList.Contains(serie.stack))
  885. {
  886. if (serie.index == currSerie.index) return index;
  887. tempList.Add(serie.stack);
  888. index++;
  889. }
  890. else
  891. {
  892. if (serie.index == currSerie.index) return tempList.IndexOf(serie.stack);
  893. }
  894. }
  895. }
  896. return 0;
  897. }
  898. internal void InitSerieHandlers()
  899. {
  900. m_SerieHandlers.Clear();
  901. for (int i = 0; i < m_Series.Count; i++)
  902. {
  903. var serie = m_Series[i];
  904. serie.index = i;
  905. CreateSerieHandler(serie);
  906. }
  907. }
  908. private void CreateSerieHandler(Serie serie)
  909. {
  910. if (serie == null)
  911. throw new ArgumentNullException("serie is null");
  912. if (!serie.GetType().IsDefined(typeof(SerieHandlerAttribute), false))
  913. {
  914. Debug.LogError("Serie no Handler:" + serie.GetType());
  915. return;
  916. }
  917. var attribute = serie.GetType().GetAttribute<SerieHandlerAttribute>();
  918. var handler = (SerieHandler) Activator.CreateInstance(attribute.handler);
  919. handler.attribute = attribute;
  920. handler.chart = this;
  921. handler.defaultDimension = 1;
  922. handler.SetSerie(serie);
  923. serie.handler = handler;
  924. m_SerieHandlers.Add(handler);
  925. }
  926. private Serie InsertSerie(int index, Type type, string serieName, bool show = true, bool addToHead = false)
  927. {
  928. CheckAddRequireChartComponent(type);
  929. var serie = Activator.CreateInstance(type) as Serie;
  930. serie.show = show;
  931. serie.serieName = serieName;
  932. serie.serieType = type.Name;
  933. serie.index = m_Series.Count;
  934. if (type == typeof(Scatter))
  935. {
  936. serie.symbol.show = true;
  937. serie.symbol.type = SymbolType.Circle;
  938. }
  939. else if (type == typeof(Line))
  940. {
  941. serie.symbol.show = true;
  942. serie.symbol.type = SymbolType.EmptyCircle;
  943. }
  944. else if (type == typeof(Heatmap))
  945. {
  946. serie.symbol.show = true;
  947. serie.symbol.type = SymbolType.Rect;
  948. }
  949. else
  950. {
  951. serie.symbol.show = false;
  952. }
  953. InsertSerie(serie, index, addToHead);
  954. return serie;
  955. }
  956. private void ResetSeriesIndex()
  957. {
  958. #if UNITY_EDITOR && UNITY_2019_1_OR_NEWER
  959. UnityEditor.EditorUtility.SetDirty(this);
  960. #endif
  961. for (int i = 0; i < m_Series.Count; i++)
  962. {
  963. m_Series[i].index = i;
  964. }
  965. }
  966. private void AddSerieAfterDeserialize(Serie serie)
  967. {
  968. serie.OnAfterDeserialize();
  969. m_Series.Add(serie);
  970. }
  971. public string GenerateDefaultSerieName()
  972. {
  973. return "serie" + m_Series.Count;
  974. }
  975. public bool IsSerieName(string name)
  976. {
  977. if (string.IsNullOrEmpty(name))
  978. return false;
  979. foreach (var serie in m_Series)
  980. {
  981. if (name.Equals(serie.serieName))
  982. return true;
  983. }
  984. return false;
  985. }
  986. }
  987. }