■ 2개의 차트 축을 동기화시키는 방법을 보여준다.
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
using System; using System.Drawing; using System.Linq; using System.Windows.Forms; using ScottPlot; using ScottPlot.Plottable; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 차트 1 십자선 /// </summary> private Crosshair chart1Crosshair = null; /// <summary> /// 차트 2 수직선 /// </summary> private VLine chart2VLine = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); Random random = new Random(0); OHLC[] chart1OHLCArray = DataGen.RandomStockPrices(null, 60, TimeSpan.FromDays(1)); double[] chart2XArray = chart1OHLCArray.Select(OHLC => OHLC.DateTime.ToOADate()).ToArray(); double[] chart2YArray = DataGen.Random(random, chart2XArray.Length, 10000, 50); #region 차트 컨트롤 1을 설정한다. this.chartControl1.Plot.XAxis.DateTimeFormat(true); this.chartControl1.Plot.XAxis.Line(false); this.chartControl1.Plot.XAxis.TickDensity(5); this.chartControl1.Plot.XAxis.TickLabelStyle(rotation : 90); this.chartControl1.Plot.XAxis.Ticks(false); #endregion #region 차트 컨트롤 1에 재무 플롯을 추가한다. FinancePlot financePlot = this.chartControl1.Plot.AddCandlesticks(chart1OHLCArray); #endregion #region 차트 컨트롤 1에 십자선을 추가한다. this.chart1Crosshair = this.chartControl1.Plot.AddCrosshair(0, 0); this.chart1Crosshair.XAxisIndex = 0; this.chart1Crosshair.YAxisIndex = 0; this.chart1Crosshair.LineWidth = 1; this.chart1Crosshair.LineStyle = LineStyle.Solid; this.chart1Crosshair.VerticalLine.PositionLabelOppositeAxis = false; this.chart1Crosshair.VerticalLine.PositionFormatter = position => DateTime.FromOADate(position).ToString("yyyy-MM-dd"); this.chart1Crosshair.HorizontalLine.PositionLabelOppositeAxis = false; this.chart1Crosshair.HorizontalLine.PositionFormatter = position => position.ToString("#,##0.00"); this.chart1Crosshair.Color = Color.Red; this.chart1Crosshair.IsVisible = false; #endregion #region 차트 컨트롤 2를 설정한다. this.chartControl2.Plot.XAxis.DateTimeFormat(true); this.chartControl2.Plot.XAxis.TickDensity(5); this.chartControl2.Plot.XAxis.TickLabelStyle(rotation : 90); #endregion #region 차트 컨트롤 2에 막대 플롯을 추가한다. BarPlot barPlot = this.chartControl2.Plot.AddBar(chart2YArray, chart2XArray); #endregion #region 차트 컨트롤 2에 수직선을 추가한다. this.chart2VLine = this.chartControl2.Plot.AddVerticalLine(0, Color.Red, 1f, LineStyle.Solid); this.chart2VLine.LineWidth = 1; this.chart2VLine.IsVisible = false; #endregion #region 차트 컨트롤 2의 축을 차트 컨트롤 1의 축과 동기화한다. this.chartControl2.Plot.SetAxisLimitsX(this.chartControl1.Plot.XAxis.Dims.Min, this.chartControl1.Plot.XAxis.Dims.Max); this.chartControl2.Plot.AxisAutoY(); #endregion #region 이벤트를 설정한다. this.chartControl1.AxesChanged += chartControl1_AxesChanged; this.chartControl1.MouseEnter += chartControl1_MouseEnter; this.chartControl1.MouseMove += chartControl1_MouseMove; this.chartControl1.MouseLeave += chartControl1_MouseLeave; this.chartControl2.AxesChanged += chartControl2_AxesChanged; this.chartControl2.MouseEnter += chartControl2_MouseEnter; this.chartControl2.MouseMove += chartControl2_MouseMove; this.chartControl2.MouseLeave += chartControl2_MouseLeave; #endregion this.chartControl1.Plot.YAxis.SetSizeLimit(min : 50, max : 50); this.chartControl2.Plot.YAxis.SetSizeLimit(min : 50, max : 50); this.chartControl1.Refresh(); this.chartControl2.Refresh(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 차트 컨트롤 1 축 변경시 처리하기 - chartControl1_AxesChanged(sender, e) /// <summary> /// 차트 컨트롤 1 축 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void chartControl1_AxesChanged(object sender, EventArgs e) { AxisLimits chart1AxisLimits = this.chartControl1.Plot.GetAxisLimits(); AxisLimits chart2AxisLimits = this.chartControl2.Plot.GetAxisLimits(); AxisLimits newChart2AxisLimits = new AxisLimits ( chart1AxisLimits.XMin, chart1AxisLimits.XMax, chart2AxisLimits.YMin, chart2AxisLimits.YMax ); this.chartControl2.Configuration.AxesChangedEventEnabled = false; this.chartControl2.Plot.SetAxisLimits(newChart2AxisLimits); this.chartControl2.Render(); this.chartControl2.Configuration.AxesChangedEventEnabled = true; } #endregion #region 차트 컨트롤 1 마우스 진입시 처리하기 - chartControl1_MouseEnter(sender, e) /// <summary> /// 차트 컨트롤 1 마우스 진입시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void chartControl1_MouseEnter(object sender, EventArgs e) { if(this.chart1Crosshair != null) { this.chart1Crosshair.IsVisible = true; this.chart2VLine.IsVisible = true; this.chartControl1.Refresh(); this.chartControl2.Refresh(); } } #endregion #region 차트 컨트롤 1 마우스 이동시 처리하기 - chartControl1_MouseMove(sender, e) /// <summary> /// 차트 컨트롤 1 마우스 이동시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void chartControl1_MouseMove(object sender, MouseEventArgs e) { if(this.chart1Crosshair != null) { (double coordinateX, double coordinateY) = this.chartControl1.GetMouseCoordinates(0, 0); this.chart1Crosshair.X = coordinateX; this.chart1Crosshair.Y = coordinateY; this.chart2VLine.X = coordinateX; this.chartControl1.Refresh(lowQuality : true, skipIfCurrentlyRendering : true); this.chartControl2.Refresh(lowQuality : true, skipIfCurrentlyRendering : true); } } #endregion #region 차트 컨트롤 1 마우스 이탈시 처리하기 - chartControl1_MouseLeave(sender, e) /// <summary> /// 차트 컨트롤 1 마우스 이탈시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void chartControl1_MouseLeave(object sender, EventArgs e) { if(this.chart1Crosshair != null) { this.chart1Crosshair.IsVisible = false; this.chart2VLine.IsVisible = false; this.chartControl1.Refresh(); this.chartControl2.Refresh(); } } #endregion #region 차트 컨트롤 2 축 변경시 처리하기 - chartControl2_AxesChanged(sender, e) /// <summary> /// 차트 컨트롤 2 축 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void chartControl2_AxesChanged(object sender, EventArgs e) { AxisLimits chart1AxisLimits = this.chartControl1.Plot.GetAxisLimits(); AxisLimits chart2AxisLimits = this.chartControl2.Plot.GetAxisLimits(); AxisLimits newChart1AxisLimits = new AxisLimits ( chart2AxisLimits.XMin, chart2AxisLimits.XMax, chart1AxisLimits.YMin, chart1AxisLimits.YMax ); this.chartControl1.Configuration.AxesChangedEventEnabled = false; this.chartControl1.Plot.SetAxisLimits(newChart1AxisLimits); this.chartControl1.Render(); this.chartControl1.Configuration.AxesChangedEventEnabled = true; } #endregion #region 차트 컨트롤 2 마우스 진입시 처리하기 - chartControl2_MouseEnter(sender, e) /// <summary> /// 차트 컨트롤 2 마우스 진입시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void chartControl2_MouseEnter(object sender, EventArgs e) { if(this.chart2VLine != null) { this.chart1Crosshair.IsVisible = true; this.chart2VLine.IsVisible = true; this.chartControl1.Refresh(); this.chartControl2.Refresh(); } } #endregion #region 차트 컨트롤 2 마우스 이동시 처리하기 - chartControl2_MouseMove(sender, e) /// <summary> /// 차트 컨트롤 2 마우스 이동시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void chartControl2_MouseMove(object sender, MouseEventArgs e) { if(this.chart2VLine != null) { (double coordinateX, double _) = this.chartControl2.GetMouseCoordinates(0, 0); this.chart1Crosshair.X = coordinateX; this.chart2VLine.X = coordinateX; this.chartControl1.Refresh(lowQuality : true, skipIfCurrentlyRendering : true); this.chartControl2.Refresh(lowQuality : true, skipIfCurrentlyRendering : true); } } #endregion #region 차트 컨트롤 2 마우스 이탈시 처리하기 - chartControl2_MouseLeave(sender, e) /// <summary> /// 차트 컨트롤 2 마우스 이탈시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void chartControl2_MouseLeave(object sender, EventArgs e) { if(this.chart2VLine != null) { this.chart1Crosshair.IsVisible = false; this.chart2VLine.IsVisible = false; this.chartControl1.Refresh(); this.chartControl2.Refresh(); } } #endregion } } |