CoordinateChart_DrawCandlestick.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /************************************************/
  2. /* */
  3. /* Copyright (c) 2018 - 2021 monitor1394 */
  4. /* https://github.com/monitor1394 */
  5. /* */
  6. /************************************************/
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. using XUGL;
  10. namespace XCharts
  11. {
  12. public partial class CoordinateChart
  13. {
  14. protected void DrawCandlestickSerie(VertexHelper vh, int colorIndex, Serie serie)
  15. {
  16. if (!IsActive(serie.index)) return;
  17. if (serie.animation.HasFadeOut()) return;
  18. var yAxis = m_YAxes[serie.yAxisIndex];
  19. var xAxis = m_XAxes[serie.xAxisIndex];
  20. var grid = GetSerieGridOrDefault(serie);
  21. var dataZoom = DataZoomHelper.GetAxisRelatedDataZoom(xAxis, dataZooms);
  22. var showData = serie.GetDataList(dataZoom);
  23. float categoryWidth = AxisHelper.GetDataWidth(xAxis, grid.runtimeWidth, showData.Count, dataZoom);
  24. float barWidth = serie.GetBarWidth(categoryWidth);
  25. float space = (categoryWidth - barWidth) / 2;
  26. int maxCount = serie.maxShow > 0
  27. ? (serie.maxShow > showData.Count ? showData.Count : serie.maxShow)
  28. : showData.Count;
  29. bool dataChanging = false;
  30. float dataChangeDuration = serie.animation.GetUpdateAnimationDuration();
  31. double yMinValue = yAxis.GetCurrMinValue(dataChangeDuration);
  32. double yMaxValue = yAxis.GetCurrMaxValue(dataChangeDuration);
  33. var isAllBarEnd = true;
  34. var isYAxis = false;
  35. for (int i = serie.minShow; i < maxCount; i++)
  36. {
  37. var serieData = showData[i];
  38. if (serie.IsIgnoreValue(serieData))
  39. {
  40. serie.dataPoints.Add(Vector3.zero);
  41. continue;
  42. }
  43. var highlight = (tooltip.show && tooltip.IsSelected(i))
  44. || serie.data[i].highlighted
  45. || serie.highlighted;
  46. var itemStyle = SerieHelper.GetItemStyle(serie, serieData, highlight);
  47. var open = serieData.GetCurrData(0, dataChangeDuration, yAxis.inverse, yMinValue, yMaxValue);
  48. var close = serieData.GetCurrData(1, dataChangeDuration, yAxis.inverse, yMinValue, yMaxValue);
  49. var lowest = serieData.GetCurrData(2, dataChangeDuration, yAxis.inverse, yMinValue, yMaxValue);
  50. var heighest = serieData.GetCurrData(3, dataChangeDuration, yAxis.inverse, yMinValue, yMaxValue);
  51. var isRise = yAxis.inverse ? close < open : close > open;
  52. var borderWidth = open == 0 ? 0f
  53. : (itemStyle.runtimeBorderWidth == 0 ? m_Theme.serie.candlestickBorderWidth
  54. : itemStyle.runtimeBorderWidth);
  55. if (serieData.IsDataChanged()) dataChanging = true;
  56. float pX = grid.runtimeX + i * categoryWidth;
  57. float zeroY = grid.runtimeY + yAxis.runtimeZeroYOffset;
  58. if (!xAxis.boundaryGap) pX -= categoryWidth / 2;
  59. float pY = zeroY;
  60. var barHig = 0f;
  61. double valueTotal = yMaxValue - yMinValue;
  62. var minCut = (yMinValue > 0 ? yMinValue : 0);
  63. if (valueTotal != 0)
  64. {
  65. barHig = (float)((close - open) / valueTotal * grid.runtimeHeight);
  66. pY += (float)((open - minCut) / valueTotal * grid.runtimeHeight);
  67. }
  68. serieData.runtimeStackHig = barHig;
  69. var isBarEnd = false;
  70. float currHig = Internal_CheckBarAnimation(serie, i, barHig, out isBarEnd);
  71. if (!isBarEnd) isAllBarEnd = false;
  72. Vector3 plb, plt, prt, prb, top;
  73. plb = new Vector3(pX + space + borderWidth, pY + borderWidth);
  74. plt = new Vector3(pX + space + borderWidth, pY + currHig - borderWidth);
  75. prt = new Vector3(pX + space + barWidth - borderWidth, pY + currHig - borderWidth);
  76. prb = new Vector3(pX + space + barWidth - borderWidth, pY + borderWidth);
  77. top = new Vector3(pX + space + barWidth / 2, pY + currHig - borderWidth);
  78. if (serie.clip)
  79. {
  80. plb = ClampInGrid(grid, plb);
  81. plt = ClampInGrid(grid, plt);
  82. prt = ClampInGrid(grid, prt);
  83. prb = ClampInGrid(grid, prb);
  84. top = ClampInGrid(grid, top);
  85. }
  86. serie.dataPoints.Add(top);
  87. var areaColor = isRise
  88. ? itemStyle.GetColor(m_Theme.serie.candlestickColor)
  89. : itemStyle.GetColor0(m_Theme.serie.candlestickColor0);
  90. var borderColor = isRise
  91. ? itemStyle.GetBorderColor(m_Theme.serie.candlestickBorderColor)
  92. : itemStyle.GetBorderColor0(m_Theme.serie.candlestickBorderColor0);
  93. var itemWidth = Mathf.Abs(prt.x - plb.x);
  94. var itemHeight = Mathf.Abs(plt.y - prb.y);
  95. var center = new Vector3((plb.x + prt.x) / 2, (plt.y + prb.y) / 2);
  96. var lowPos = new Vector3(center.x, zeroY + (float)((lowest - minCut) / valueTotal * grid.runtimeHeight));
  97. var heighPos = new Vector3(center.x, zeroY + (float)((heighest - minCut) / valueTotal * grid.runtimeHeight));
  98. var openCenterPos = new Vector3(center.x, prb.y);
  99. var closeCenterPos = new Vector3(center.x, prt.y);
  100. if (barWidth > 2f * borderWidth)
  101. {
  102. if (itemWidth > 0 && itemHeight > 0)
  103. {
  104. if (ItemStyleHelper.IsNeedCorner(itemStyle))
  105. {
  106. UGL.DrawRoundRectangle(vh, center, itemWidth, itemHeight, areaColor, areaColor, 0,
  107. itemStyle.cornerRadius, isYAxis, 0.5f);
  108. }
  109. else
  110. {
  111. Internal_CheckClipAndDrawPolygon(vh, ref prb, ref plb, ref plt, ref prt, areaColor, areaColor,
  112. serie.clip, grid);
  113. }
  114. UGL.DrawBorder(vh, center, itemWidth, itemHeight, 2 * borderWidth, borderColor, 0,
  115. itemStyle.cornerRadius, isYAxis, 0.5f);
  116. }
  117. }
  118. else
  119. {
  120. UGL.DrawLine(vh, openCenterPos, closeCenterPos, Mathf.Max(borderWidth, barWidth / 2), borderColor);
  121. }
  122. if (isRise)
  123. {
  124. UGL.DrawLine(vh, openCenterPos, lowPos, borderWidth, borderColor);
  125. UGL.DrawLine(vh, closeCenterPos, heighPos, borderWidth, borderColor);
  126. }
  127. else
  128. {
  129. UGL.DrawLine(vh, closeCenterPos, lowPos, borderWidth, borderColor);
  130. UGL.DrawLine(vh, openCenterPos, heighPos, borderWidth, borderColor);
  131. }
  132. }
  133. if (isAllBarEnd)
  134. {
  135. serie.animation.AllBarEnd();
  136. }
  137. if (dataChanging)
  138. {
  139. RefreshPainter(serie);
  140. }
  141. }
  142. }
  143. }