CoordinateChart_DrawScatter.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /************************************************/
  2. /* */
  3. /* Copyright (c) 2018 - 2021 monitor1394 */
  4. /* https://github.com/monitor1394 */
  5. /* */
  6. /************************************************/
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. namespace XCharts
  10. {
  11. public partial class CoordinateChart
  12. {
  13. protected void DrawScatterSerie(VertexHelper vh, int colorIndex, Serie serie)
  14. {
  15. if (serie.animation.HasFadeOut()) return;
  16. if (!serie.show) return;
  17. DataZoom xDataZoom, yDataZoom;
  18. DataZoomHelper.GetSerieRelatedDataZoom(serie, dataZooms, out xDataZoom, out yDataZoom);
  19. var yAxis = m_YAxes[serie.yAxisIndex];
  20. var xAxis = m_XAxes[serie.xAxisIndex];
  21. var grid = GetSerieGridOrDefault(serie);
  22. int maxCount = serie.maxShow > 0 ?
  23. (serie.maxShow > serie.dataCount ? serie.dataCount : serie.maxShow)
  24. : serie.dataCount;
  25. serie.animation.InitProgress(1, 0, 1);
  26. var rate = serie.animation.GetCurrRate();
  27. var dataChangeDuration = serie.animation.GetUpdateAnimationDuration();
  28. var dataChanging = false;
  29. var dataList = serie.GetDataList(xDataZoom);
  30. foreach (var serieData in dataList)
  31. {
  32. var symbol = SerieHelper.GetSerieSymbol(serie, serieData);
  33. if (!symbol.ShowSymbol(serieData.index, maxCount)) continue;
  34. var highlight = serie.highlighted || serieData.highlighted;
  35. var color = SerieHelper.GetItemColor(serie, serieData, m_Theme, colorIndex, highlight);
  36. var toColor = SerieHelper.GetItemToColor(serie, serieData, m_Theme, colorIndex, highlight);
  37. var backgroundColor = SerieHelper.GetItemBackgroundColor(serie, serieData, m_Theme, colorIndex, highlight, false);
  38. var symbolBorder = SerieHelper.GetSymbolBorder(serie, serieData, m_Theme, highlight);
  39. var cornerRadius = SerieHelper.GetSymbolCornerRadius(serie, serieData, highlight);
  40. double xValue = serieData.GetCurrData(0, dataChangeDuration, xAxis.inverse);
  41. double yValue = serieData.GetCurrData(1, dataChangeDuration, yAxis.inverse);
  42. if (serieData.IsDataChanged()) dataChanging = true;
  43. float pX = grid.runtimeX + xAxis.axisLine.GetWidth(m_Theme.axis.lineWidth);
  44. float pY = grid.runtimeY + yAxis.axisLine.GetWidth(m_Theme.axis.lineWidth);
  45. float xDataHig = GetDataHig(xAxis, xValue, grid.runtimeWidth);
  46. float yDataHig = GetDataHig(yAxis, yValue, grid.runtimeHeight);
  47. var pos = new Vector3(pX + xDataHig, pY + yDataHig);
  48. if (!IsInGrid(grid, pos)) continue;
  49. serie.dataPoints.Add(pos);
  50. serieData.runtimePosition = pos;
  51. var datas = serieData.data;
  52. float symbolSize = 0;
  53. if (serie.highlighted || serieData.highlighted)
  54. {
  55. symbolSize = symbol.GetSelectedSize(datas, m_Theme.serie.scatterSymbolSelectedSize);
  56. }
  57. else
  58. {
  59. symbolSize = symbol.GetSize(datas, m_Theme.serie.scatterSymbolSize);
  60. }
  61. symbolSize *= rate;
  62. if (symbolSize > 100) symbolSize = 100;
  63. if (serie.type == SerieType.EffectScatter)
  64. {
  65. for (int count = 0; count < symbol.animationSize.Count; count++)
  66. {
  67. var nowSize = symbol.animationSize[count];
  68. color.a = (byte)(255 * (symbolSize - nowSize) / symbolSize);
  69. DrawSymbol(vh, symbol.type, nowSize, symbolBorder, pos, color, toColor, backgroundColor, symbol.gap, cornerRadius);
  70. }
  71. RefreshPainter(serie);
  72. }
  73. else
  74. {
  75. DrawSymbol(vh, symbol.type, symbolSize, symbolBorder, pos, color, toColor, backgroundColor, symbol.gap, cornerRadius);
  76. }
  77. }
  78. if (!serie.animation.IsFinish())
  79. {
  80. serie.animation.CheckProgress(1);
  81. m_IsPlayingAnimation = true;
  82. RefreshPainter(serie);
  83. }
  84. if (dataChanging)
  85. {
  86. RefreshPainter(serie);
  87. }
  88. }
  89. private float GetDataHig(Axis axis, double value, float totalWidth)
  90. {
  91. if (axis.IsLog())
  92. {
  93. int minIndex = axis.runtimeMinLogIndex;
  94. float nowIndex = axis.GetLogValue(value);
  95. return (nowIndex - minIndex) / axis.splitNumber * totalWidth;
  96. }
  97. else
  98. {
  99. return (float)((value - axis.runtimeMinValue) / axis.runtimeMinMaxRange * totalWidth);
  100. }
  101. }
  102. }
  103. }