[C#/WINFORM/LIGHTNINGCHART] FreeformPointLineSeries 클래스 : Scatter 차트 그리기
■ FreeformPointLineSeries 클래스를 사용해 Scatter 차트를 그리는 방법을 보여준다. ▶ ChartHelper.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 |
using System; using System.Collections.Generic; using System.Drawing; using Arction.WinForms.Charting; using Arction.WinForms.Charting.Axes; using Arction.WinForms.Charting.Views.ViewXY; namespace TestProject { /// <summary> /// 차트 헬퍼 /// </summary> public static class ChartHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 단일 색상 리스트 /// </summary> public static List<Color> SolidColorList = new List<Color> { Color.FromArgb(187, 28 , 0 ), Color.FromArgb(222, 54 , 11), Color.FromArgb(199, 86 , 10), Color.FromArgb(255, 160, 0 ), Color.FromArgb(255, 124, 0 ) }; /// <summary> /// 반투명 색상 리스트 /// </summary> public static List<Color> TranslucentColorList = new List<Color> { Color.FromArgb(180, SolidColorList[0]), Color.FromArgb(180, SolidColorList[1]), Color.FromArgb(180, SolidColorList[2]), Color.FromArgb(180, SolidColorList[3]), Color.FromArgb(180, SolidColorList[4]) }; #endregion //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 복구 정보 /// </summary> private static RestoreInfo _restoreInfo; /// <summary> /// 배경 색상 /// </summary> private static Color _backgroundColor = Color.FromArgb(255, 30, 30, 30); /// <summary> /// 제목 색상 /// </summary> private static Color _titleColor = Color.FromArgb(255, 249, 202, 3); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 리스트 데이터 지우기 - ClearListData<TItem>(sourceList) /// <summary> /// 리스트 데이터 지우기 /// </summary> /// <typeparam name="TItem">항목 타입</typeparam> /// <param name="sourceList">소스 리스트</param> public static void ClearListData<TItem>(List<TItem> sourceList) where TItem : IDisposable { if(sourceList == null) { return; } while(sourceList.Count > 0) { int lastIndex = sourceList.Count - 1; TItem item = sourceList[lastIndex]; sourceList.RemoveAt(lastIndex); if(item != null) { (item as IDisposable).Dispose(); } } } #endregion #region 다크/플랫 스타일 설정하기 - SetDarkFlatStyle(chart, minorTickOption) /// <summary> /// 다크/플랫 스타일 설정하기 /// </summary> /// <param name="chart">차트</param> /// <param name="minorTickOption">작은 눈금 옵션</param> public static void SetDarkFlatStyle(LightningChartUltimate chart, MinorTickOption minorTickOption = MinorTickOption.None) { _restoreInfo = new RestoreInfo(chart); chart.Background.Color = _backgroundColor; chart.Background.GradientFill = GradientFill.Solid; chart.ViewXY.GraphBackground.Color = Color.FromArgb(255, 20, 20, 20); chart.ViewXY.GraphBackground.GradientFill = GradientFill.Solid; chart.Title.Color = _titleColor; chart.Title.MouseHighlight = MouseOverHighlight.None; bool showXMinotTick = false; bool showYMinorTick = false; if(minorTickOption == MinorTickOption.Both) { showXMinotTick = showYMinorTick = true; } else if(minorTickOption == MinorTickOption.OnlyX) { showXMinotTick = true; } else if(minorTickOption == MinorTickOption.OnlyY) { showYMinorTick = true; } foreach(AxisY axisY in chart.ViewXY.YAxes) { axisY.Title.Color = _titleColor; axisY.Title.MouseHighlight = MouseOverHighlight.None; axisY.MajorGrid.Color = Color.FromArgb(35, 255, 255, 255); axisY.MajorGrid.Pattern = LinePattern.Solid; axisY.MinorDivTickStyle.Visible = showYMinorTick; } foreach(AxisX axisX in chart.ViewXY.XAxes) { axisX.Title.Color = _titleColor; axisX.Title.MouseHighlight = MouseOverHighlight.None; axisX.MajorGrid.Color = Color.FromArgb(35, 255, 255, 255); axisX.MajorGrid.Pattern = LinePattern.Solid; axisX.MinorDivTickStyle.Visible = showXMinotTick; } if(chart.ViewXY.LegendBoxes != null) { foreach(LegendBoxXY legendBox in chart.ViewXY.LegendBoxes) { legendBox.Shadow.Visible = false; } } } #endregion #region 다크 플랫 스타일 취소하기 - CancelDarkFlatStyle(chart) /// <summary> /// 다크 플랫 스타일 취소하기 /// </summary> /// <param name="chart">차트</param> public static void CancelDarkFlatStyle(LightningChartUltimate chart) { if(_restoreInfo != null) { if(chart != null) { chart.BeginUpdate(); chart.Background.Color = _restoreInfo.ChartColor; chart.Background.GradientFill = _restoreInfo.ChartGradientFill; chart.ViewXY.GraphBackground.Color = _restoreInfo.ViewColor; chart.ViewXY.GraphBackground.GradientFill = _restoreInfo.viewGradientFill; foreach(var yAxis in chart.ViewXY.YAxes) { yAxis.MajorGrid.Color = _restoreInfo.GridColor; yAxis.MajorGrid.Pattern = _restoreInfo.GridLinePattern; yAxis.MinorDivTickStyle.Visible = true; } foreach(var xAxis in chart.ViewXY.XAxes) { xAxis.MajorGrid.Color = _restoreInfo.GridColor; xAxis.MajorGrid.Pattern = _restoreInfo.GridLinePattern; xAxis.MinorDivTickStyle.Visible = true; } chart.EndUpdate(); } } } #endregion } } |
▶ 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 328 329 330 331 332 333 334 335 336 337 338 339 340 |
using System; using System.Drawing; using System.Windows.Forms; using Arction.WinForms.Charting; using Arction.WinForms.Charting.Axes; using Arction.WinForms.Charting.SeriesXY; using Arction.WinForms.Charting.Views.ViewXY; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 차트 /// </summary> private LightningChartUltimate chart = null; /// <summary> /// 현재 X 범위 /// </summary> private double currentXRange = 1000.0; /// <summary> /// 현재 Y 범위 /// </summary> private double currentYRange = 1000.0; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); InitializeChart(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region X축 범위 변경시 처리하기 - axisX_RangeChanged(sender, e) /// <summary> /// X축 범위 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void axisX_RangeChanged(object sender, RangeChangedEventArgs e) { if(e.NewMin < 0.0) { double newRange = e.NewMax - e.NewMin; if(newRange <= 1000.0) { this.chart.ViewXY.XAxes[0].SetRange(0.0, newRange); } else { this.chart.ViewXY.XAxes[0].SetRange(0.0, 1000.0); } } else if(e.NewMax > 1000.0) { double newRange = e.NewMax - e.NewMin; if(newRange <= 1000.0) { this.chart.ViewXY.XAxes[0].SetRange(1000.0 - newRange, 1000.0); } else { this.chart.ViewXY.XAxes[0].SetRange(0, 1000.0); } } else { double newRange = e.NewMax - e.NewMin; this.chart.BeginUpdate(); this.chart.HorizontalScrollBars[0].LargeChange = (ulong)(newRange + 0.5); this.chart.HorizontalScrollBars[0].Value = (ulong)(e.NewMin + 0.5); this.chart.EndUpdate(); this.currentXRange = newRange; } } #endregion #region Y축 범위 변경시 처리하기 - axisY_RangeChanged(sender, e) /// <summary> /// Y축 범위 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void axisY_RangeChanged(object sender, RangeChangedEventArgs e) { if(e.NewMin < 0.0) { double newRange = e.NewMax - e.NewMin; if(newRange <= 1000.0) { this.chart.ViewXY.YAxes[0].SetRange(0.0, newRange); } else { this.chart.ViewXY.YAxes[0].SetRange(0.0, 1000.0); } } else if(e.NewMax > 1000.0) { double newRange = e.NewMax - e.NewMin; if(newRange <= 1000.0) { this.chart.ViewXY.YAxes[0].SetRange(1000.0 - newRange, 1000.0); } else { this.chart.ViewXY.YAxes[0].SetRange(0, 1000.0); } } else { double newRange = e.NewMax - e.NewMin; this.chart.BeginUpdate(); this.chart.VerticalScrollBars[0].LargeChange = (ulong)(newRange + 0.5); this.chart.VerticalScrollBars[0].Value = (ulong)(1000.0 - e.NewMax); this.chart.EndUpdate(); this.currentYRange = newRange; } } #endregion #region 수평 스크롤바 스크롤시 처리하기 - horizontalScrollBar_Scroll(sender, e) /// <summary> /// 수평 스크롤바 스크롤시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void horizontalScrollBar_Scroll(object sender, Arction.WinForms.Charting.ScrollEventArgs e) { double newMin = e.NewValue; if(newMin < 0.0) { newMin = 0.0; } double newMax = newMin + this.currentXRange; if(newMax > 1000.0) { newMax = 1000.0; newMin = newMax - this.currentXRange; } this.chart.ViewXY.XAxes[0].SetRange(newMin, newMax); } #endregion #region 수직 스크롤바 스크롤시 처리하기 - verticalScrollBar_Scroll(sender, e) /// <summary> /// 수직 스크롤바 스크롤시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void verticalScrollBar_Scroll(object sender, Arction.WinForms.Charting.ScrollEventArgs e) { double newMin = 1000.0 - (double)e.NewValue - this.currentYRange; if(newMin < 0.0) { newMin = 0.0; } double newMax = newMin + this.currentYRange; if(newMax > 1000.0) { newMax = 1000.0; newMin = newMax - this.currentYRange; } this.chart.ViewXY.YAxes[0].SetRange(newMin, newMax); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 차트 초기화하기 - InitializeChart() /// <summary> /// 차트 초기화하기 /// </summary> private void InitializeChart() { this.chart = new LightningChartUltimate(); this.chart.BeginUpdate(); this.chart.Name = "chart"; this.chart.Parent = this; this.chart.Dock = DockStyle.Fill; this.chart.Title.Font = Font; this.chart.Title.Text = "Scatter 차트"; ViewXY viewXY = this.chart.ViewXY; viewXY.AxisLayout.AutoAdjustMargins = false; viewXY.AxisLayout.XAxisAutoPlacement = XAxisAutoPlacement.AllTop; viewXY.Margins = new Padding(80, 80, 30, 30); AxisX axisX = viewXY.XAxes[0]; axisX.Title.Font = Font; axisX.Title.Text = "X축"; axisX.ValueType = AxisValueType.Number; axisX.ScrollMode = XAxisScrollMode.None; axisX.Minimum = 0.0; axisX.Maximum = this.currentXRange; AxisY axisY = viewXY.YAxes[0]; axisY.Title.Font = Font; axisY.Title.Text = "Y축"; axisY.ValueType = AxisValueType.Number; axisY.Minimum = 0.0; axisY.Maximum = this.currentYRange; ChartHelper.SetDarkFlatStyle(this.chart); FreeformPointLineSeries series = new FreeformPointLineSeries(viewXY, axisX, axisY); series.Title.Visible = false; series.Title.Color = series.LineStyle.Color; series.LineVisible = false; series.PointsVisible = true; series.PointStyle.Shape = Shape.Rectangle; series.PointStyle.Color1 = Color.Yellow; series.PointStyle.Color2 = Color.Orange; series.PointStyle.Angle = 45; series.MouseInteraction = false; viewXY.FreeformPointLineSeries.Add(series); HorizontalScrollBar horizontalScrollBar = new HorizontalScrollBar(this.chart); horizontalScrollBar.Border.Style = BorderType.None; horizontalScrollBar.Maximum = (ulong)axisX.Maximum; horizontalScrollBar.Minimum = (ulong)axisX.Minimum; horizontalScrollBar.LargeChange = (ulong)axisX.Maximum; horizontalScrollBar.SmallChange = 1; this.chart.HorizontalScrollBars.Add(horizontalScrollBar); VerticalScrollBar verticalScrollBar = new VerticalScrollBar(this.chart); verticalScrollBar.Border.Style = BorderType.None; verticalScrollBar.Maximum = (ulong)axisY.Maximum; verticalScrollBar.Minimum = (ulong)axisY.Minimum; verticalScrollBar.LargeChange = (ulong)axisY.Maximum; verticalScrollBar.SmallChange = 1; this.chart.VerticalScrollBars.Add(verticalScrollBar); int pointCount = 10000; Random random = new Random(); SeriesPoint[] pointArray = new SeriesPoint[pointCount]; for(int i = 0; i < pointCount; i++) { pointArray[i].X = random.NextDouble() * 1000.0; pointArray[i].Y = random.NextDouble() * 1000.0; } this.chart.ViewXY.FreeformPointLineSeries[0].Points = pointArray; LegendBoxXY legendBoxXY = viewXY.LegendBoxes[0]; legendBoxXY.Visible = false; axisX.RangeChanged += axisX_RangeChanged; axisY.RangeChanged += axisY_RangeChanged; horizontalScrollBar.Scroll += horizontalScrollBar_Scroll; verticalScrollBar.Scroll += verticalScrollBar_Scroll; axisX.SetRange(300, 500); axisY.SetRange(300, 500); this.chart.EndUpdate(); } #endregion } } |
TestProject.zip