Geen omschrijving
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.

SeriesHelper.cs 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace XCharts.Runtime
  5. {
  6. public static class SeriesHelper
  7. {
  8. public static bool IsLegalLegendName(string name)
  9. {
  10. int numName = -1;
  11. if (int.TryParse(name, out numName))
  12. {
  13. if (numName >= 0 && numName < 100) return false;
  14. }
  15. return true;
  16. }
  17. public static List<string> GetLegalSerieNameList(List<Serie> series)
  18. {
  19. var list = new List<string>();
  20. for (int n = 0; n < series.Count; n++)
  21. {
  22. var serie = series[n];
  23. if (serie.placeHolder) continue;
  24. if (serie.colorByData)
  25. {
  26. for (int i = 0; i < serie.data.Count; i++)
  27. {
  28. var dataName = serie.data[i].name;
  29. if (!string.IsNullOrEmpty(dataName) && IsLegalLegendName(dataName) && !list.Contains(dataName))
  30. list.Add(dataName);
  31. }
  32. }
  33. else
  34. {
  35. if (!string.IsNullOrEmpty(serie.serieName) && !list.Contains(serie.serieName) && IsLegalLegendName(serie.serieName))
  36. list.Add(serie.serieName);
  37. }
  38. }
  39. return list;
  40. }
  41. /// <summary>
  42. /// 获得所有系列名,不包含空名字。
  43. /// </summary>
  44. /// <returns></returns>
  45. public static void UpdateSerieNameList(BaseChart chart, ref List<string> serieNameList)
  46. {
  47. serieNameList.Clear();
  48. for (int n = 0; n < chart.series.Count; n++)
  49. {
  50. var serie = chart.series[n];
  51. if (serie.placeHolder) continue;
  52. if (serie.colorByData)
  53. {
  54. for (int i = 0; i < serie.data.Count; i++)
  55. {
  56. var serieData = serie.data[i];
  57. if (serie is Pie && serie.IsIgnoreValue(serieData)) continue;
  58. if (string.IsNullOrEmpty(serieData.name))
  59. serieNameList.Add(ChartCached.IntToStr(i));
  60. else if (!serieNameList.Contains(serieData.name))
  61. serieNameList.Add(serieData.name);
  62. }
  63. }
  64. else
  65. {
  66. if (string.IsNullOrEmpty(serie.serieName))
  67. serieNameList.Add(ChartCached.IntToStr(n));
  68. else if (!serieNameList.Contains(serie.serieName))
  69. serieNameList.Add(serie.serieName);
  70. }
  71. }
  72. }
  73. public static Color GetNameColor(BaseChart chart, int index, string name)
  74. {
  75. Serie destSerie = null;
  76. SerieData destSerieData = null;
  77. var series = chart.series;
  78. for (int n = 0; n < series.Count; n++)
  79. {
  80. var serie = series[n];
  81. if (serie.placeHolder) continue;
  82. if (serie.colorByData)
  83. {
  84. bool found = false;
  85. for (int i = 0; i < serie.data.Count; i++)
  86. {
  87. if (name.Equals(serie.data[i].name))
  88. {
  89. destSerie = serie;
  90. destSerieData = serie.data[i];
  91. found = true;
  92. break;
  93. }
  94. }
  95. if (found) break;
  96. }
  97. if (name.Equals(serie.serieName))
  98. {
  99. destSerie = serie;
  100. destSerieData = null;
  101. break;
  102. }
  103. }
  104. var itemStyle = SerieHelper.GetItemStyle(destSerie, destSerieData, SerieState.Normal);
  105. if (ChartHelper.IsClearColor(itemStyle.markColor))
  106. {
  107. Color32 color, toColor;
  108. SerieHelper.GetItemColor(out color, out toColor, destSerie, destSerieData, chart.theme, index, SerieState.Normal);
  109. return color;
  110. }
  111. else
  112. {
  113. return itemStyle.markColor;
  114. }
  115. }
  116. /// <summary>
  117. /// 是否有需裁剪的serie。
  118. /// </summary>
  119. /// <returns></returns>
  120. public static bool IsAnyClipSerie(List<Serie> series)
  121. {
  122. foreach (var serie in series)
  123. {
  124. if (serie.clip) return true;
  125. }
  126. return false;
  127. }
  128. /// <summary>
  129. /// 获得上一个同堆叠且显示的serie。
  130. /// </summary>
  131. /// <param name="serie"></param>
  132. /// <returns></returns>
  133. public static Serie GetLastStackSerie(List<Serie> series, Serie serie)
  134. {
  135. if (serie == null || string.IsNullOrEmpty(serie.stack)) return null;
  136. for (int i = serie.index - 1; i >= 0; i--)
  137. {
  138. var temp = series[i];
  139. if (temp.show && serie.stack.Equals(temp.stack)) return temp;
  140. }
  141. return null;
  142. }
  143. private static HashSet<string> _setForStack = new HashSet<string>();
  144. /// <summary>
  145. /// 是否由数据堆叠
  146. /// </summary>
  147. /// <returns></returns>
  148. public static bool IsStack(List<Serie> series)
  149. {
  150. _setForStack.Clear();
  151. foreach (var serie in series)
  152. {
  153. if (string.IsNullOrEmpty(serie.stack)) continue;
  154. if (_setForStack.Contains(serie.stack)) return true;
  155. _setForStack.Add(serie.stack);
  156. }
  157. return false;
  158. }
  159. /// <summary>
  160. /// 是否堆叠
  161. /// </summary>
  162. /// <param name="stackName"></param>
  163. /// <param name="type"></param>
  164. /// <returns></returns>
  165. public static bool IsStack<T>(List<Serie> series, string stackName) where T : Serie
  166. {
  167. if (string.IsNullOrEmpty(stackName)) return false;
  168. int count = 0;
  169. foreach (var serie in series)
  170. {
  171. if (serie.show && serie is T)
  172. {
  173. if (stackName.Equals(serie.stack)) count++;
  174. if (count >= 2) return true;
  175. }
  176. }
  177. return false;
  178. }
  179. /// <summary>
  180. /// 是否时百分比堆叠
  181. /// </summary>
  182. /// <param name="type"></param>
  183. /// <returns></returns>
  184. public static bool IsPercentStack<T>(List<Serie> series) where T : Serie
  185. {
  186. int count = 0;
  187. bool isPercentStack = false;
  188. foreach (var serie in series)
  189. {
  190. if (serie.show && serie is T)
  191. {
  192. if (!string.IsNullOrEmpty(serie.stack))
  193. {
  194. count++;
  195. if (serie.barPercentStack) isPercentStack = true;
  196. }
  197. if (count >= 2 && isPercentStack) return true;
  198. }
  199. }
  200. return false;
  201. }
  202. /// <summary>
  203. /// 是否时百分比堆叠
  204. /// </summary>
  205. /// <param name="stackName"></param>
  206. /// <param name="type"></param>
  207. /// <returns></returns>
  208. public static bool IsPercentStack<T>(List<Serie> series, string stackName) where T : Serie
  209. {
  210. if (string.IsNullOrEmpty(stackName)) return false;
  211. int count = 0;
  212. bool isPercentStack = false;
  213. foreach (var serie in series)
  214. {
  215. if (serie.show && serie is T)
  216. {
  217. if (stackName.Equals(serie.stack))
  218. {
  219. count++;
  220. if (serie.barPercentStack) isPercentStack = true;
  221. }
  222. if (count >= 2 && isPercentStack) return true;
  223. }
  224. }
  225. return false;
  226. }
  227. private static Dictionary<string, int> sets = new Dictionary<string, int>();
  228. /// <summary>
  229. /// 获得堆叠系列列表
  230. /// </summary>
  231. /// <param name="Dictionary<int"></param>
  232. /// <param name="stackSeries"></param>
  233. public static void GetStackSeries(List<Serie> series, ref Dictionary<int, List<Serie>> stackSeries)
  234. {
  235. int count = 0;
  236. var serieCount = series.Count;
  237. sets.Clear();
  238. if (stackSeries == null)
  239. {
  240. stackSeries = new Dictionary<int, List<Serie>>(serieCount);
  241. }
  242. else
  243. {
  244. foreach (var kv in stackSeries)
  245. {
  246. kv.Value.Clear();
  247. }
  248. }
  249. for (int i = 0; i < serieCount; i++)
  250. {
  251. var serie = series[i];
  252. serie.index = i;
  253. if (string.IsNullOrEmpty(serie.stack))
  254. {
  255. if (!stackSeries.ContainsKey(count))
  256. stackSeries[count] = new List<Serie>(serieCount);
  257. stackSeries[count].Add(serie);
  258. count++;
  259. }
  260. else
  261. {
  262. if (!sets.ContainsKey(serie.stack))
  263. {
  264. sets.Add(serie.stack, count);
  265. if (!stackSeries.ContainsKey(count))
  266. stackSeries[count] = new List<Serie>(serieCount);
  267. stackSeries[count].Add(serie);
  268. count++;
  269. }
  270. else
  271. {
  272. int stackIndex = sets[serie.stack];
  273. stackSeries[stackIndex].Add(serie);
  274. }
  275. }
  276. }
  277. }
  278. public static void UpdateStackDataList(List<Serie> series, Serie currSerie, DataZoom dataZoom, List<List<SerieData>> dataList)
  279. {
  280. dataList.Clear();
  281. for (int i = 0; i <= currSerie.index; i++)
  282. {
  283. var serie = series[i];
  284. if (serie.GetType() == currSerie.GetType() && ChartHelper.IsValueEqualsString(serie.stack, currSerie.stack))
  285. {
  286. dataList.Add(serie.GetDataList(dataZoom));
  287. }
  288. }
  289. }
  290. /// <summary>
  291. /// 获得维度X的最大最小值
  292. /// </summary>
  293. /// <param name="dataZoom"></param>
  294. /// <param name="axisIndex"></param>
  295. /// <param name="minVaule"></param>
  296. /// <param name="maxValue"></param>
  297. public static void GetXMinMaxValue(BaseChart chart, int axisIndex, bool isValueAxis,
  298. bool inverse, out double minVaule, out double maxValue, bool isPolar = false, bool filterByDataZoom = true)
  299. {
  300. GetMinMaxValue(chart, axisIndex, isValueAxis, inverse, false, out minVaule, out maxValue, isPolar, filterByDataZoom);
  301. }
  302. /// <summary>
  303. /// 获得维度Y的最大最小值
  304. /// </summary>
  305. /// <param name="dataZoom"></param>
  306. /// <param name="axisIndex"></param>
  307. /// <param name="minVaule"></param>
  308. /// <param name="maxValue"></param>
  309. public static void GetYMinMaxValue(BaseChart chart, int axisIndex, bool isValueAxis,
  310. bool inverse, out double minVaule, out double maxValue, bool isPolar = false, bool filterByDataZoom = true)
  311. {
  312. GetMinMaxValue(chart, axisIndex, isValueAxis, inverse, true, out minVaule, out maxValue, isPolar, filterByDataZoom);
  313. }
  314. private static Dictionary<int, List<Serie>> _stackSeriesForMinMax = new Dictionary<int, List<Serie>>();
  315. private static Dictionary<int, double> _serieTotalValueForMinMax = new Dictionary<int, double>();
  316. private static DataZoom xDataZoom, yDataZoom;
  317. public static void GetMinMaxValue(BaseChart chart, int axisIndex, bool isValueAxis,
  318. bool inverse, bool yValue, out double minVaule, out double maxValue, bool isPolar = false,
  319. bool filterByDataZoom = true)
  320. {
  321. double min = double.MaxValue;
  322. double max = double.MinValue;
  323. var series = chart.series;
  324. var isPercentStack = SeriesHelper.IsPercentStack<Bar>(series);
  325. if (!SeriesHelper.IsStack(series))
  326. {
  327. for (int i = 0; i < series.Count; i++)
  328. {
  329. var serie = series[i];
  330. if ((isPolar && serie.polarIndex != axisIndex) ||
  331. (!isPolar && serie.yAxisIndex != axisIndex) ||
  332. !serie.show) continue;
  333. var updateDuration = serie.animation.enable ? serie.animation.dataChangeDuration : 0;
  334. var unscaledTime = serie.animation.unscaledTime;
  335. if (isPercentStack && SeriesHelper.IsPercentStack<Bar>(series, serie.serieName))
  336. {
  337. if (100 > max) max = 100;
  338. if (0 < min) min = 0;
  339. }
  340. else
  341. {
  342. var showData = serie.GetDataList(filterByDataZoom ? chart.GetXDataZoomOfSerie(serie) : null);
  343. if (serie is Candlestick || serie is SimplifiedCandlestick)
  344. {
  345. foreach (var data in showData)
  346. {
  347. double dataMin, dataMax;
  348. data.GetMinMaxData(1, inverse, out dataMin, out dataMax);
  349. if (dataMax > max) max = dataMax;
  350. if (dataMin < min) min = dataMin;
  351. }
  352. }
  353. else
  354. {
  355. var performanceMode = serie.IsPerformanceMode();
  356. foreach (var data in showData)
  357. {
  358. var currData = performanceMode ? data.GetData(yValue ? 1 : 0, inverse) :
  359. data.GetCurrData(yValue ? 1 : 0, updateDuration, unscaledTime, inverse);
  360. if (!serie.IsIgnoreValue(data, currData))
  361. {
  362. if (currData > max) max = currData;
  363. if (currData < min) min = currData;
  364. }
  365. }
  366. }
  367. }
  368. }
  369. }
  370. else
  371. {
  372. SeriesHelper.GetStackSeries(series, ref _stackSeriesForMinMax);
  373. foreach (var ss in _stackSeriesForMinMax)
  374. {
  375. _serieTotalValueForMinMax.Clear();
  376. for (int i = 0; i < ss.Value.Count; i++)
  377. {
  378. var serie = ss.Value[i];
  379. if ((isPolar && serie.polarIndex != axisIndex) ||
  380. (!isPolar && serie.yAxisIndex != axisIndex) ||
  381. !serie.show) continue;
  382. var showData = serie.GetDataList(filterByDataZoom ? chart.GetXDataZoomOfSerie(serie) : null);
  383. if (SeriesHelper.IsPercentStack<Bar>(series, serie.stack))
  384. {
  385. for (int j = 0; j < showData.Count; j++)
  386. {
  387. _serieTotalValueForMinMax[j] = 100;
  388. }
  389. }
  390. else
  391. {
  392. for (int j = 0; j < showData.Count; j++)
  393. {
  394. if (!_serieTotalValueForMinMax.ContainsKey(j))
  395. _serieTotalValueForMinMax[j] = 0;
  396. double currData = 0;
  397. if (serie is Candlestick)
  398. {
  399. currData = showData[j].GetMaxData(false);
  400. }
  401. else
  402. {
  403. currData = yValue ? showData[j].GetData(1) : showData[j].GetData(0);
  404. }
  405. if (inverse) currData = -currData;
  406. if (!serie.IsIgnoreValue(showData[j], currData))
  407. _serieTotalValueForMinMax[j] = _serieTotalValueForMinMax[j] + currData;
  408. }
  409. }
  410. }
  411. double tmax = double.MinValue;
  412. double tmin = double.MaxValue;
  413. foreach (var tt in _serieTotalValueForMinMax)
  414. {
  415. if (tt.Value > tmax) tmax = tt.Value;
  416. if (tt.Value < tmin) tmin = tt.Value;
  417. }
  418. if (tmax > max) max = tmax;
  419. if (tmin < min) min = tmin;
  420. }
  421. }
  422. if (max == double.MinValue && min == double.MaxValue)
  423. {
  424. minVaule = 0;
  425. maxValue = 0;
  426. }
  427. else
  428. {
  429. minVaule = min;
  430. maxValue = max;
  431. }
  432. }
  433. public static int GetMaxSerieDataCount(List<Serie> series)
  434. {
  435. int max = 0;
  436. foreach (var serie in series)
  437. {
  438. if (serie.dataCount > max) max = serie.dataCount;
  439. }
  440. return max;
  441. }
  442. }
  443. }