Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

RadarHandler.cs 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using XUGL;
  5. namespace XCharts.Runtime
  6. {
  7. [UnityEngine.Scripting.Preserve]
  8. internal sealed class RadarHandler : SerieHandler<Radar>
  9. {
  10. private RadarCoord m_RadarCoord;
  11. public override void Update()
  12. {
  13. base.Update();
  14. UpdateSerieContext();
  15. }
  16. public override void DrawSerie(VertexHelper vh)
  17. {
  18. if (!serie.show) return;
  19. switch (serie.radarType)
  20. {
  21. case RadarType.Multiple:
  22. DrawMutipleRadar(vh);
  23. break;
  24. case RadarType.Single:
  25. DrawSingleRadar(vh);
  26. break;
  27. }
  28. }
  29. public override void UpdateTooltipSerieParams(int dataIndex, bool showCategory, string category,
  30. string marker, string itemFormatter, string numericFormatter, string ignoreDataDefaultContent,
  31. ref List<SerieParams> paramList, ref string title)
  32. {
  33. if (!serie.context.pointerEnter)
  34. return;
  35. dataIndex = serie.context.pointerItemDataIndex;
  36. if (dataIndex < 0)
  37. return;
  38. if (serie.radarType == RadarType.Single)
  39. {
  40. var colorIndex1 = serie.colorByData ? dataIndex : serie.context.colorIndex;
  41. UpdateItemSerieParams(ref paramList, ref title, dataIndex, category,
  42. marker, itemFormatter, numericFormatter, ignoreDataDefaultContent, 1, colorIndex1);
  43. return;
  44. }
  45. var radar = chart.GetChartComponent<RadarCoord>(serie.radarIndex);
  46. if (radar == null)
  47. return;
  48. var serieData = serie.GetSerieData(dataIndex);
  49. if (serieData == null)
  50. return;
  51. Color32 color, toColor;
  52. var colorIndex = serie.colorByData ? chart.GetLegendRealShowNameIndex(serieData.legendName) : serie.context.colorIndex;
  53. SerieHelper.GetItemColor(out color, out toColor, serie, serieData, chart.theme, colorIndex, SerieState.Normal);
  54. title = serieData.name;
  55. for (int i = 0; i < serieData.data.Count; i++)
  56. {
  57. var indicator = radar.GetIndicator(i);
  58. if (indicator == null) continue;
  59. var param = new SerieParams();
  60. param.serieName = serie.serieName;
  61. param.serieIndex = serie.index;
  62. param.dimension = i;
  63. param.serieData = serieData;
  64. param.dataCount = serie.dataCount;
  65. param.value = serieData.GetData(i);
  66. param.total = indicator.max;
  67. param.color = color;
  68. param.marker = SerieHelper.GetItemMarker(serie, serieData, marker);
  69. param.itemFormatter = SerieHelper.GetItemFormatter(serie, serieData, itemFormatter);
  70. param.numericFormatter = SerieHelper.GetNumericFormatter(serie, serieData, numericFormatter);
  71. param.columns.Clear();
  72. param.columns.Add(param.marker);
  73. param.columns.Add(indicator.name);
  74. param.columns.Add(ChartCached.NumberToStr(serieData.GetData(i), param.numericFormatter));
  75. paramList.Add(param);
  76. }
  77. }
  78. private void UpdateSerieContext()
  79. {
  80. var needCheck = m_LegendEnter ||
  81. (chart.isPointerInChart && (m_RadarCoord != null && m_RadarCoord.IsPointerEnter()));
  82. var needInteract = false;
  83. var needHideAll = false;
  84. if (!needCheck)
  85. {
  86. if (m_LastCheckContextFlag == needCheck)
  87. return;
  88. needHideAll = true;
  89. }
  90. m_LastCheckContextFlag = needCheck;
  91. serie.highlight = false;
  92. serie.context.pointerEnter = false;
  93. serie.context.pointerItemDataIndex = -1;
  94. var areaStyle = serie.areaStyle;
  95. var themeSymbolSize = chart.theme.serie.lineSymbolSize;
  96. switch (serie.radarType)
  97. {
  98. case RadarType.Multiple:
  99. for (int i = 0; i < serie.data.Count; i++)
  100. {
  101. var serieData = serie.data[i];
  102. var symbol = SerieHelper.GetSerieSymbol(serie, serieData);
  103. var symbolSize = symbol.GetSize(serieData.data, chart.theme.serie.lineSymbolSize);
  104. if (needHideAll || m_LegendEnter)
  105. {
  106. serieData.context.highlight = needHideAll ? false : true;
  107. serieData.interact.SetValue(ref needInteract, symbolSize, serieData.context.highlight);
  108. }
  109. else
  110. {
  111. serieData.context.highlight = false;
  112. foreach (var pos in serieData.context.dataPoints)
  113. {
  114. if (Vector3.Distance(chart.pointerPos, pos) < symbolSize * 2)
  115. {
  116. serie.highlight = true;
  117. serie.context.pointerEnter = true;
  118. serie.context.pointerItemDataIndex = i;
  119. serieData.context.highlight = true;
  120. break;
  121. }
  122. }
  123. if (!serieData.context.highlight && areaStyle != null)
  124. {
  125. var center = m_RadarCoord.context.center;
  126. var dataPoints = serieData.context.dataPoints;
  127. for (int n = 0; n < dataPoints.Count; n++)
  128. {
  129. var p1 = dataPoints[n];
  130. var p2 = n >= dataPoints.Count - 1 ? dataPoints[0] : dataPoints[n + 1];
  131. if (UGLHelper.IsPointInTriangle(p1, center, p2, chart.pointerPos))
  132. {
  133. serie.highlight = true;
  134. serie.context.pointerEnter = true;
  135. serie.context.pointerItemDataIndex = i;
  136. serieData.context.highlight = true;
  137. break;
  138. }
  139. }
  140. }
  141. serieData.interact.SetValue(ref needInteract, symbolSize, serieData.context.highlight);
  142. }
  143. }
  144. break;
  145. case RadarType.Single:
  146. needInteract = false;
  147. for (int i = 0; i < serie.data.Count; i++)
  148. {
  149. var serieData = serie.data[i];
  150. var size = SerieHelper.GetSysmbolSize(serie, serieData, chart.theme, themeSymbolSize);
  151. if (Vector3.Distance(chart.pointerPos, serieData.context.position) < size * 2)
  152. {
  153. serie.context.pointerEnter = true;
  154. serie.context.pointerItemDataIndex = i;
  155. serieData.context.highlight = true;
  156. needInteract = true;
  157. }
  158. else
  159. {
  160. serieData.context.highlight = false;
  161. }
  162. }
  163. if (!serie.context.pointerEnter && areaStyle != null)
  164. {
  165. var center = m_RadarCoord.context.center;
  166. var dataPoints = serie.data;
  167. for (int n = 0; n < dataPoints.Count; n++)
  168. {
  169. var p1 = dataPoints[n];
  170. var p2 = n >= dataPoints.Count - 1 ? dataPoints[0] : dataPoints[n + 1];
  171. if (UGLHelper.IsPointInTriangle(p1.context.position, center, p2.context.position, chart.pointerPos))
  172. {
  173. serie.context.pointerEnter = true;
  174. serie.context.pointerItemDataIndex = n;
  175. p1.context.highlight = true;
  176. needInteract = true;
  177. break;
  178. }
  179. }
  180. }
  181. break;
  182. }
  183. if (needInteract)
  184. {
  185. chart.RefreshPainter(serie);
  186. }
  187. }
  188. private void DrawMutipleRadar(VertexHelper vh)
  189. {
  190. if (!serie.show) return;
  191. m_RadarCoord = chart.GetChartComponent<RadarCoord>(serie.radarIndex);
  192. if (m_RadarCoord == null) return;
  193. serie.containerIndex = m_RadarCoord.index;
  194. serie.containterInstanceId = m_RadarCoord.instanceId;
  195. var startPoint = Vector3.zero;
  196. var toPoint = Vector3.zero;
  197. var firstPoint = Vector3.zero;
  198. var indicatorNum = m_RadarCoord.indicatorList.Count;
  199. var angle = 2 * Mathf.PI / indicatorNum;
  200. var centerPos = m_RadarCoord.context.center;
  201. serie.animation.InitProgress(0, 1);
  202. if (!serie.show || serie.animation.HasFadeOut())
  203. {
  204. return;
  205. }
  206. var rate = serie.animation.GetCurrRate();
  207. var dataChanging = false;
  208. var interacting = false;
  209. var dataChangeDuration = serie.animation.GetUpdateAnimationDuration();
  210. var unscaledTime = serie.animation.unscaledTime;
  211. SerieHelper.GetAllMinMaxData(serie, m_RadarCoord.ceilRate);
  212. Color32 areaColor, areaToColor;
  213. var startAngle = m_RadarCoord.startAngle * Mathf.PI / 180;
  214. for (int j = 0; j < serie.data.Count; j++)
  215. {
  216. var serieData = serie.data[j];
  217. string dataName = serieData.name;
  218. if (!serieData.show)
  219. {
  220. continue;
  221. }
  222. var serieState = SerieHelper.GetSerieState(serie, serieData, true);
  223. var lineStyle = SerieHelper.GetLineStyle(serie, serieData);
  224. var symbol = SerieHelper.GetSerieSymbol(serie, serieData, serieState);
  225. var colorIndex = serie.colorByData ? chart.GetLegendRealShowNameIndex(serieData.legendName) : serie.context.colorIndex;
  226. var showArea = SerieHelper.GetAreaColor(out areaColor, out areaToColor, serie, serieData, chart.theme, colorIndex);
  227. var lineColor = SerieHelper.GetLineColor(serie, serieData, chart.theme, colorIndex);
  228. var lineWidth = lineStyle.GetWidth(chart.theme.serie.lineWidth);
  229. int dataCount = m_RadarCoord.indicatorList.Count;
  230. serieData.context.dataPoints.Clear();
  231. for (int n = 0; n < dataCount; n++)
  232. {
  233. if (n >= serieData.data.Count) break;
  234. var min = m_RadarCoord.GetIndicatorMin(n);
  235. var max = m_RadarCoord.GetIndicatorMax(n);
  236. var value = serieData.GetCurrData(n, dataChangeDuration, unscaledTime);
  237. if (serieData.IsDataChanged()) dataChanging = true;
  238. if (max == 0)
  239. {
  240. if (serie.data.Count > 1)
  241. {
  242. SerieHelper.GetMinMaxData(serie, n, out min, out max);
  243. min = ChartHelper.GetMinDivisibleValue(min, 0);
  244. max = ChartHelper.GetMaxDivisibleValue(max, 0);
  245. if (min > 0) min = 0;
  246. }
  247. else
  248. {
  249. max = serie.context.dataMax;
  250. }
  251. }
  252. var radius = (float) (m_RadarCoord.context.dataRadius * (value - min) / (max - min));
  253. var currAngle = startAngle + (n + (m_RadarCoord.positionType == RadarCoord.PositionType.Between ? 0.5f : 0)) * angle;
  254. radius *= rate;
  255. if (n == 0)
  256. {
  257. startPoint = new Vector3(centerPos.x + radius * Mathf.Sin(currAngle),
  258. centerPos.y + radius * Mathf.Cos(currAngle));
  259. firstPoint = startPoint;
  260. }
  261. else
  262. {
  263. toPoint = new Vector3(centerPos.x + radius * Mathf.Sin(currAngle),
  264. centerPos.y + radius * Mathf.Cos(currAngle));
  265. if (showArea && !serie.smooth)
  266. {
  267. UGL.DrawTriangle(vh, startPoint, toPoint, centerPos, areaColor, areaColor, areaToColor);
  268. }
  269. if (lineStyle.show && !serie.smooth)
  270. {
  271. ChartDrawer.DrawLineStyle(vh, lineStyle.type, lineWidth, startPoint, toPoint, lineColor);
  272. }
  273. startPoint = toPoint;
  274. }
  275. serieData.context.dataPoints.Add(startPoint);
  276. }
  277. if (showArea && !serie.smooth)
  278. {
  279. UGL.DrawTriangle(vh, startPoint, firstPoint, centerPos, areaColor, areaColor, areaToColor);
  280. }
  281. if (lineStyle.show && !serie.smooth)
  282. {
  283. ChartDrawer.DrawLineStyle(vh, lineStyle.type, lineWidth, startPoint, firstPoint, lineColor);
  284. }
  285. if (serie.smooth)
  286. {
  287. UGL.DrawCurves(vh, serieData.context.dataPoints, lineWidth, lineColor,
  288. chart.settings.lineSmoothStyle,
  289. chart.settings.lineSmoothness,
  290. UGL.Direction.Random,
  291. float.NaN, true);
  292. }
  293. if (symbol.show && symbol.type != SymbolType.None)
  294. {
  295. float symbolBorder = 0f;
  296. float[] cornerRadius = null;
  297. Color32 symbolColor, symbolToColor, symbolEmptyColor, borderColor;
  298. for (int m = 0; m < serieData.context.dataPoints.Count; m++)
  299. {
  300. var point = serieData.context.dataPoints[m];
  301. var symbolSize = 0f;
  302. if (!serieData.interact.TryGetValue(ref symbolSize, ref interacting))
  303. {
  304. symbolSize = SerieHelper.GetSysmbolSize(serie, serieData, chart.theme, chart.theme.serie.lineSymbolSize, serieState);
  305. serieData.interact.SetValue(ref interacting, symbolSize);
  306. symbolSize = serie.animation.GetSysmbolSize(symbolSize);
  307. }
  308. SerieHelper.GetItemColor(out symbolColor, out symbolToColor, out symbolEmptyColor, serie, serieData, chart.theme, colorIndex, serieState);
  309. SerieHelper.GetSymbolInfo(out borderColor, out symbolBorder, out cornerRadius, serie, serieData, chart.theme, serieState);
  310. chart.DrawSymbol(vh, symbol.type, symbolSize, symbolBorder, point, symbolColor,
  311. symbolToColor, symbolEmptyColor, borderColor, symbol.gap, cornerRadius);
  312. }
  313. }
  314. }
  315. if (!serie.animation.IsFinish())
  316. {
  317. serie.animation.CheckProgress(1);
  318. chart.RefreshPainter(serie);
  319. }
  320. if (dataChanging || interacting)
  321. {
  322. chart.RefreshPainter(serie);
  323. }
  324. }
  325. private void DrawSingleRadar(VertexHelper vh)
  326. {
  327. m_RadarCoord = chart.GetChartComponent<RadarCoord>(serie.radarIndex);
  328. if (m_RadarCoord == null)
  329. return;
  330. var indicatorNum = m_RadarCoord.indicatorList.Count;
  331. var angle = 2 * Mathf.PI / indicatorNum;
  332. var centerPos = m_RadarCoord.context.center;
  333. serie.animation.InitProgress(0, 1);
  334. if (!serie.show || serie.animation.HasFadeOut())
  335. {
  336. return;
  337. }
  338. var startPoint = Vector3.zero;
  339. var toPoint = Vector3.zero;
  340. var firstPoint = Vector3.zero;
  341. var lastColor = ColorUtil.clearColor32;
  342. var firstColor = ColorUtil.clearColor32;
  343. var rate = serie.animation.GetCurrRate();
  344. var dataChanging = false;
  345. var dataChangeDuration = serie.animation.GetUpdateAnimationDuration();
  346. var unscaledTime = serie.animation.unscaledTime;
  347. var startIndex = GetStartShowIndex(serie);
  348. var endIndex = GetEndShowIndex(serie);
  349. var startAngle = m_RadarCoord.startAngle * Mathf.PI / 180;
  350. SerieHelper.UpdateMinMaxData(serie, 1, m_RadarCoord.ceilRate);
  351. for (int j = 0; j < serie.data.Count; j++)
  352. {
  353. var serieData = serie.data[j];
  354. string dataName = serieData.name;
  355. if (!serieData.show)
  356. {
  357. serieData.context.labelPosition = Vector3.zero;
  358. continue;
  359. }
  360. var lineStyle = SerieHelper.GetLineStyle(serie, serieData);
  361. Color32 areaColor, areaToColor;
  362. var colorIndex = serie.colorByData?j : serie.context.colorIndex;
  363. var showArea = SerieHelper.GetAreaColor(out areaColor, out areaToColor, serie, serieData, chart.theme, colorIndex - 1);
  364. var lineColor = SerieHelper.GetLineColor(serie, serieData, chart.theme, colorIndex);
  365. int dataCount = m_RadarCoord.indicatorList.Count;
  366. var index = serieData.index;
  367. var p = m_RadarCoord.context.center;
  368. var max = m_RadarCoord.GetIndicatorMax(index);
  369. var value = serieData.GetCurrData(1, dataChangeDuration, unscaledTime);
  370. if (serieData.IsDataChanged()) dataChanging = true;
  371. if (max == 0)
  372. {
  373. max = serie.context.dataMax;
  374. }
  375. if (!m_RadarCoord.IsInIndicatorRange(j, serieData.GetData(1)))
  376. {
  377. lineColor = m_RadarCoord.outRangeColor;
  378. }
  379. var radius = (float) (max < 0 ? m_RadarCoord.context.dataRadius - m_RadarCoord.context.dataRadius * value / max :
  380. m_RadarCoord.context.dataRadius * value / max);
  381. var currAngle = startAngle + (index + (m_RadarCoord.positionType == RadarCoord.PositionType.Between ? 0.5f : 0)) * angle;
  382. radius *= rate;
  383. if (index == startIndex)
  384. {
  385. startPoint = new Vector3(p.x + radius * Mathf.Sin(currAngle),
  386. p.y + radius * Mathf.Cos(currAngle));
  387. firstPoint = startPoint;
  388. lastColor = lineColor;
  389. firstColor = lineColor;
  390. }
  391. else
  392. {
  393. toPoint = new Vector3(p.x + radius * Mathf.Sin(currAngle),
  394. p.y + radius * Mathf.Cos(currAngle));
  395. if (showArea && !serie.smooth)
  396. {
  397. UGL.DrawTriangle(vh, startPoint, toPoint, p, areaColor, areaColor, areaToColor);
  398. }
  399. if (lineStyle.show && !serie.smooth)
  400. {
  401. if (m_RadarCoord.connectCenter)
  402. ChartDrawer.DrawLineStyle(vh, lineStyle, startPoint, centerPos,
  403. chart.theme.serie.lineWidth, LineStyle.Type.Solid, lastColor, lastColor);
  404. ChartDrawer.DrawLineStyle(vh, lineStyle, startPoint, toPoint, chart.theme.serie.lineWidth,
  405. LineStyle.Type.Solid, m_RadarCoord.lineGradient ? lastColor : lineColor, lineColor);
  406. }
  407. startPoint = toPoint;
  408. lastColor = lineColor;
  409. }
  410. serie.context.dataPoints.Add(startPoint);
  411. serie.context.dataIndexs.Add(serieData.index);
  412. serieData.context.position = startPoint;
  413. serieData.context.labelPosition = startPoint;
  414. if (showArea && j == endIndex && !serie.smooth)
  415. {
  416. SerieHelper.GetAreaColor(out areaColor, out areaToColor, serie, serieData, chart.theme, colorIndex);
  417. UGL.DrawTriangle(vh, startPoint, firstPoint, centerPos, areaColor, areaColor, areaToColor);
  418. }
  419. if (lineStyle.show && j == endIndex && !serie.smooth)
  420. {
  421. if (m_RadarCoord.connectCenter)
  422. ChartDrawer.DrawLineStyle(vh, lineStyle, startPoint, centerPos,
  423. chart.theme.serie.lineWidth, LineStyle.Type.Solid, lastColor, lastColor);
  424. ChartDrawer.DrawLineStyle(vh, lineStyle, startPoint, firstPoint, chart.theme.serie.lineWidth,
  425. LineStyle.Type.Solid, lineColor, m_RadarCoord.lineGradient ? firstColor : lineColor);
  426. }
  427. }
  428. if (serie.smooth)
  429. {
  430. var lineWidth = serie.lineStyle.GetWidth(chart.theme.serie.lineWidth);
  431. var lineColor = SerieHelper.GetLineColor(serie, null, chart.theme, serie.context.colorIndex);
  432. UGL.DrawCurves(vh, serie.context.dataPoints, lineWidth, lineColor,
  433. chart.settings.lineSmoothStyle,
  434. chart.settings.lineSmoothness,
  435. UGL.Direction.Random,
  436. float.NaN, true);
  437. }
  438. if (serie.symbol.show && serie.symbol.type != SymbolType.None)
  439. {
  440. float symbolBorder = 0f;
  441. float[] cornerRadius = null;
  442. Color32 symbolColor, symbolToColor, symbolEmptyColor, borderColor;
  443. for (int j = 0; j < serie.data.Count; j++)
  444. {
  445. var serieData = serie.data[j];
  446. if (!serieData.show) continue;
  447. var state = SerieHelper.GetSerieState(serie, serieData);
  448. var symbolSize = SerieHelper.GetSysmbolSize(serie, serieData, chart.theme, chart.theme.serie.lineSymbolSize, state);
  449. var colorIndex = serie.colorByData ? serieData.index : serie.context.colorIndex;
  450. SerieHelper.GetItemColor(out symbolColor, out symbolToColor, out symbolEmptyColor, serie, serieData, chart.theme, colorIndex, state);
  451. SerieHelper.GetSymbolInfo(out borderColor, out symbolBorder, out cornerRadius, serie, serieData, chart.theme, state);
  452. if (!m_RadarCoord.IsInIndicatorRange(j, serieData.GetData(1)))
  453. {
  454. symbolColor = m_RadarCoord.outRangeColor;
  455. symbolToColor = m_RadarCoord.outRangeColor;
  456. }
  457. chart.DrawSymbol(vh, serie.symbol.type, symbolSize, symbolBorder, serieData.context.labelPosition, symbolColor,
  458. symbolToColor, symbolEmptyColor, borderColor, serie.symbol.gap, cornerRadius);
  459. }
  460. }
  461. if (!serie.animation.IsFinish())
  462. {
  463. serie.animation.CheckProgress(1);
  464. chart.RefreshPainter(serie);
  465. }
  466. if (dataChanging)
  467. {
  468. chart.RefreshPainter(serie);
  469. }
  470. }
  471. private int GetStartShowIndex(Serie serie)
  472. {
  473. for (int i = 0; i < serie.dataCount; i++)
  474. {
  475. if (serie.data[i].show) return i;
  476. }
  477. return 0;
  478. }
  479. private int GetEndShowIndex(Serie serie)
  480. {
  481. for (int i = serie.dataCount - 1; i >= 0; i--)
  482. {
  483. if (serie.data[i].show) return i;
  484. }
  485. return 0;
  486. }
  487. }
  488. }