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