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

SimplifiedBarHandler.cs 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. using System.Collections.Generic;
  2. using System.Text;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using XUGL;
  6. namespace XCharts.Runtime
  7. {
  8. [UnityEngine.Scripting.Preserve]
  9. internal sealed class SimplifiedBarHandler : SerieHandler<SimplifiedBar>
  10. {
  11. private GridCoord m_SerieGrid;
  12. public override void Update()
  13. {
  14. base.Update();
  15. UpdateSerieContext();
  16. }
  17. public override void UpdateTooltipSerieParams(int dataIndex, bool showCategory, string category,
  18. string marker, string itemFormatter, string numericFormatter, string ignoreDataDefaultContent,
  19. ref List<SerieParams> paramList, ref string title)
  20. {
  21. UpdateCoordSerieParams(ref paramList, ref title, dataIndex, showCategory, category,
  22. marker, itemFormatter, numericFormatter, ignoreDataDefaultContent);
  23. }
  24. public override void DrawSerie(VertexHelper vh)
  25. {
  26. DrawBarSerie(vh, serie, serie.context.colorIndex);
  27. }
  28. private void UpdateSerieContext()
  29. {
  30. if (m_SerieGrid == null)
  31. return;
  32. var needCheck = (chart.isPointerInChart && m_SerieGrid.IsPointerEnter()) || m_LegendEnter;
  33. var needInteract = false;
  34. Color32 color, toColor;
  35. if (!needCheck)
  36. {
  37. if (m_LastCheckContextFlag != needCheck)
  38. {
  39. m_LastCheckContextFlag = needCheck;
  40. serie.context.pointerItemDataIndex = -1;
  41. serie.context.pointerEnter = false;
  42. foreach (var serieData in serie.data)
  43. {
  44. SerieHelper.GetItemColor(out color, out toColor, serie, serieData, chart.theme, SerieState.Normal);
  45. serieData.interact.SetColor(ref needInteract, color, toColor);
  46. }
  47. if (needInteract)
  48. {
  49. chart.RefreshPainter(serie);
  50. }
  51. }
  52. return;
  53. }
  54. m_LastCheckContextFlag = needCheck;
  55. if (m_LegendEnter)
  56. {
  57. serie.context.pointerEnter = true;
  58. foreach (var serieData in serie.data)
  59. {
  60. SerieHelper.GetItemColor(out color, out toColor, serie, serieData, chart.theme, SerieState.Emphasis);
  61. serieData.interact.SetColor(ref needInteract, color, toColor);
  62. }
  63. }
  64. else
  65. {
  66. serie.context.pointerItemDataIndex = -1;
  67. serie.context.pointerEnter = false;
  68. foreach (var serieData in serie.data)
  69. {
  70. if (serieData.context.rect.Contains(chart.pointerPos))
  71. {
  72. serie.context.pointerItemDataIndex = serieData.index;
  73. serie.context.pointerEnter = true;
  74. serieData.context.highlight = true;
  75. SerieHelper.GetItemColor(out color, out toColor, serie, serieData, chart.theme, SerieState.Emphasis);
  76. serieData.interact.SetColor(ref needInteract, color, toColor);
  77. }
  78. else
  79. {
  80. serieData.context.highlight = false;
  81. SerieHelper.GetItemColor(out color, out toColor, serie, serieData, chart.theme, SerieState.Normal);
  82. serieData.interact.SetColor(ref needInteract, color, toColor);
  83. }
  84. }
  85. }
  86. if (needInteract)
  87. {
  88. chart.RefreshPainter(serie);
  89. }
  90. }
  91. private void DrawBarSerie(VertexHelper vh, SimplifiedBar serie, int colorIndex)
  92. {
  93. if (!serie.show || serie.animation.HasFadeOut())
  94. return;
  95. Axis axis;
  96. Axis relativedAxis;
  97. var isY = chart.GetSerieGridCoordAxis(serie, out axis, out relativedAxis);
  98. m_SerieGrid = chart.GetChartComponent<GridCoord>(axis.gridIndex);
  99. if (axis == null)
  100. return;
  101. if (relativedAxis == null)
  102. return;
  103. if (m_SerieGrid == null)
  104. return;
  105. var dataZoom = chart.GetDataZoomOfAxis(axis);
  106. var showData = serie.GetDataList(dataZoom);
  107. if (showData.Count <= 0)
  108. return;
  109. var axisLength = isY ? m_SerieGrid.context.height : m_SerieGrid.context.width;
  110. var axisXY = isY ? m_SerieGrid.context.y : m_SerieGrid.context.x;
  111. var barCount = chart.GetSerieBarRealCount<SimplifiedBar>();
  112. float categoryWidth = AxisHelper.GetDataWidth(axis, axisLength, showData.Count, dataZoom);
  113. float barGap = chart.GetSerieBarGap<SimplifiedBar>();
  114. float totalBarWidth = chart.GetSerieTotalWidth<SimplifiedBar>(categoryWidth, barGap, barCount);
  115. float barWidth = serie.GetBarWidth(categoryWidth, barCount);
  116. float offset = (categoryWidth - totalBarWidth) * 0.5f;
  117. float barGapWidth = barWidth + barWidth * barGap;
  118. float gap = serie.barGap == -1 ? offset : offset + serie.index * barGapWidth;
  119. int maxCount = serie.maxShow > 0 ?
  120. (serie.maxShow > showData.Count ? showData.Count : serie.maxShow) :
  121. showData.Count;
  122. bool dataChanging = false;
  123. float dataChangeDuration = serie.animation.GetUpdateAnimationDuration();
  124. double yMinValue = relativedAxis.context.minValue;
  125. double yMaxValue = relativedAxis.context.maxValue;
  126. var areaColor = ColorUtil.clearColor32;
  127. var areaToColor = ColorUtil.clearColor32;
  128. var interacting = false;
  129. serie.containerIndex = m_SerieGrid.index;
  130. serie.containterInstanceId = m_SerieGrid.instanceId;
  131. serie.animation.InitProgress(axisXY, axisXY + axisLength);
  132. for (int i = serie.minShow; i < maxCount; i++)
  133. {
  134. var serieData = showData[i];
  135. if (!serieData.show || serie.IsIgnoreValue(serieData))
  136. {
  137. serie.context.dataPoints.Add(Vector3.zero);
  138. serie.context.dataIndexs.Add(serieData.index);
  139. continue;
  140. }
  141. if (serieData.IsDataChanged())
  142. dataChanging = true;
  143. var highlight = serieData.context.highlight || serie.highlight;
  144. var itemStyle = SerieHelper.GetItemStyle(serie, serieData);
  145. var value = axis.IsCategory() ? i : serieData.GetData(0, axis.inverse);
  146. var relativedValue = serieData.GetCurrData(1, dataChangeDuration, relativedAxis.inverse, yMinValue, yMaxValue, serie.animation.unscaledTime);
  147. var borderWidth = relativedValue == 0 ? 0 : itemStyle.runtimeBorderWidth;
  148. if (!serieData.interact.TryGetColor(ref areaColor, ref areaToColor, ref interacting))
  149. {
  150. SerieHelper.GetItemColor(out areaColor, out areaToColor, serie, serieData, chart.theme);
  151. serieData.interact.SetColor(ref interacting, areaColor, areaToColor);
  152. }
  153. var pX = 0f;
  154. var pY = 0f;
  155. UpdateXYPosition(m_SerieGrid, isY, axis, relativedAxis, i, categoryWidth, barWidth, value, ref pX, ref pY);
  156. var barHig = AxisHelper.GetAxisValueLength(m_SerieGrid, relativedAxis, categoryWidth, relativedValue);
  157. var currHig = AnimationStyleHelper.CheckDataAnimation(chart, serie, i, barHig);
  158. Vector3 plb, plt, prt, prb, top;
  159. UpdateRectPosition(m_SerieGrid, isY, relativedValue, pX, pY, gap, borderWidth, barWidth, currHig,
  160. out plb, out plt, out prt, out prb, out top);
  161. serieData.context.stackHeight = barHig;
  162. serieData.context.position = top;
  163. serieData.context.rect = Rect.MinMaxRect(plb.x, plb.y, prb.x, prt.y);
  164. serie.context.dataPoints.Add(top);
  165. serie.context.dataIndexs.Add(serieData.index);
  166. DrawNormalBar(vh, serie, serieData, itemStyle, colorIndex, highlight, gap, barWidth,
  167. pX, pY, plb, plt, prt, prb, false, m_SerieGrid, areaColor, areaToColor);
  168. if (serie.animation.CheckDetailBreak(top, isY))
  169. {
  170. break;
  171. }
  172. }
  173. if (!serie.animation.IsFinish())
  174. {
  175. serie.animation.CheckProgress();
  176. chart.RefreshPainter(serie);
  177. }
  178. if (dataChanging || interacting)
  179. {
  180. chart.RefreshPainter(serie);
  181. }
  182. }
  183. private void UpdateXYPosition(GridCoord grid, bool isY, Axis axis, Axis relativedAxis, int i, float categoryWidth, float barWidth,
  184. double value, ref float pX, ref float pY)
  185. {
  186. if (isY)
  187. {
  188. if (axis.IsCategory())
  189. {
  190. pY = grid.context.y + i * categoryWidth + (axis.boundaryGap ? 0 : -categoryWidth * 0.5f);
  191. }
  192. else
  193. {
  194. if (axis.context.minMaxRange <= 0) pY = grid.context.y;
  195. else pY = grid.context.y + (float) ((value - axis.context.minValue) / axis.context.minMaxRange) * (grid.context.height - barWidth);
  196. }
  197. pX = AxisHelper.GetAxisValuePosition(grid, relativedAxis, categoryWidth, 0);
  198. }
  199. else
  200. {
  201. if (axis.IsCategory())
  202. {
  203. pX = grid.context.x + i * categoryWidth + (axis.boundaryGap ? 0 : -categoryWidth * 0.5f);
  204. }
  205. else
  206. {
  207. if (axis.context.minMaxRange <= 0) pX = grid.context.x;
  208. else pX = grid.context.x + (float) ((value - axis.context.minValue) / axis.context.minMaxRange) * (grid.context.width - barWidth);
  209. }
  210. pY = AxisHelper.GetAxisValuePosition(grid, relativedAxis, categoryWidth, 0);
  211. }
  212. }
  213. private void UpdateRectPosition(GridCoord grid, bool isY, double yValue, float pX, float pY, float gap, float borderWidth,
  214. float barWidth, float currHig,
  215. out Vector3 plb, out Vector3 plt, out Vector3 prt, out Vector3 prb, out Vector3 top)
  216. {
  217. if (isY)
  218. {
  219. if (yValue < 0)
  220. {
  221. plt = new Vector3(pX - borderWidth, pY + gap + barWidth - borderWidth);
  222. prt = new Vector3(pX + currHig + borderWidth, pY + gap + barWidth - borderWidth);
  223. prb = new Vector3(pX + currHig + borderWidth, pY + gap + borderWidth);
  224. plb = new Vector3(pX - borderWidth, pY + gap + borderWidth);
  225. }
  226. else
  227. {
  228. plt = new Vector3(pX + borderWidth, pY + gap + barWidth - borderWidth);
  229. prt = new Vector3(pX + currHig - borderWidth, pY + gap + barWidth - borderWidth);
  230. prb = new Vector3(pX + currHig - borderWidth, pY + gap + borderWidth);
  231. plb = new Vector3(pX + borderWidth, pY + gap + borderWidth);
  232. }
  233. top = new Vector3(pX + currHig - borderWidth, pY + gap + barWidth / 2);
  234. }
  235. else
  236. {
  237. if (yValue < 0)
  238. {
  239. plb = new Vector3(pX + gap + borderWidth, pY - borderWidth);
  240. plt = new Vector3(pX + gap + borderWidth, pY + currHig + borderWidth);
  241. prt = new Vector3(pX + gap + barWidth - borderWidth, pY + currHig + borderWidth);
  242. prb = new Vector3(pX + gap + barWidth - borderWidth, pY - borderWidth);
  243. }
  244. else
  245. {
  246. plb = new Vector3(pX + gap + borderWidth, pY + borderWidth);
  247. plt = new Vector3(pX + gap + borderWidth, pY + currHig - borderWidth);
  248. prt = new Vector3(pX + gap + barWidth - borderWidth, pY + currHig - borderWidth);
  249. prb = new Vector3(pX + gap + barWidth - borderWidth, pY + borderWidth);
  250. }
  251. top = new Vector3(pX + gap + barWidth / 2, pY + currHig - borderWidth);
  252. }
  253. if (serie.clip)
  254. {
  255. plb = chart.ClampInGrid(grid, plb);
  256. plt = chart.ClampInGrid(grid, plt);
  257. prt = chart.ClampInGrid(grid, prt);
  258. prb = chart.ClampInGrid(grid, prb);
  259. top = chart.ClampInGrid(grid, top);
  260. }
  261. }
  262. private void DrawNormalBar(VertexHelper vh, Serie serie, SerieData serieData, ItemStyle itemStyle, int colorIndex,
  263. bool highlight, float gap, float barWidth, float pX, float pY, Vector3 plb, Vector3 plt, Vector3 prt,
  264. Vector3 prb, bool isYAxis, GridCoord grid, Color32 areaColor, Color32 areaToColor)
  265. {
  266. var borderWidth = itemStyle.runtimeBorderWidth;
  267. if (isYAxis)
  268. {
  269. if (serie.clip)
  270. {
  271. prb = chart.ClampInGrid(grid, prb);
  272. plb = chart.ClampInGrid(grid, plb);
  273. plt = chart.ClampInGrid(grid, plt);
  274. prt = chart.ClampInGrid(grid, prt);
  275. }
  276. var itemWidth = Mathf.Abs(prb.x - plt.x);
  277. var itemHeight = Mathf.Abs(prt.y - plb.y);
  278. var center = new Vector3((plt.x + prb.x) / 2, (prt.y + plb.y) / 2);
  279. if (itemWidth > 0 && itemHeight > 0)
  280. {
  281. var invert = center.x < plb.x;
  282. if (itemStyle.IsNeedCorner())
  283. {
  284. UGL.DrawRoundRectangle(vh, center, itemWidth, itemHeight, areaColor, areaToColor, 0,
  285. itemStyle.cornerRadius, isYAxis, chart.settings.cicleSmoothness, invert);
  286. }
  287. else
  288. {
  289. chart.DrawClipPolygon(vh, plb, plt, prt, prb, areaColor, areaToColor, serie.clip, grid);
  290. }
  291. UGL.DrawBorder(vh, center, itemWidth, itemHeight, borderWidth, itemStyle.borderColor,
  292. itemStyle.borderToColor, 0, itemStyle.cornerRadius, isYAxis, chart.settings.cicleSmoothness, invert);
  293. }
  294. }
  295. else
  296. {
  297. if (serie.clip)
  298. {
  299. prb = chart.ClampInGrid(grid, prb);
  300. plb = chart.ClampInGrid(grid, plb);
  301. plt = chart.ClampInGrid(grid, plt);
  302. prt = chart.ClampInGrid(grid, prt);
  303. }
  304. var itemWidth = Mathf.Abs(prt.x - plb.x);
  305. var itemHeight = Mathf.Abs(plt.y - prb.y);
  306. var center = new Vector3((plb.x + prt.x) / 2, (plt.y + prb.y) / 2);
  307. if (itemWidth > 0 && itemHeight > 0)
  308. {
  309. var invert = center.y < plb.y;
  310. if (itemStyle.IsNeedCorner())
  311. {
  312. UGL.DrawRoundRectangle(vh, center, itemWidth, itemHeight, areaColor, areaToColor, 0,
  313. itemStyle.cornerRadius, isYAxis, chart.settings.cicleSmoothness, invert);
  314. }
  315. else
  316. {
  317. chart.DrawClipPolygon(vh, ref prb, ref plb, ref plt, ref prt, areaColor, areaToColor,
  318. serie.clip, grid);
  319. }
  320. UGL.DrawBorder(vh, center, itemWidth, itemHeight, borderWidth, itemStyle.borderColor,
  321. itemStyle.borderToColor, 0, itemStyle.cornerRadius, isYAxis, chart.settings.cicleSmoothness, invert);
  322. }
  323. }
  324. }
  325. }
  326. }