[C#/WINFORM/MSCHART] Chart 클래스 사용하기
■ Chart 클래스를 사용하는 방법을 보여준다. ▶ 예제 코드 (C#)
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 |
using System; using System.Threading; using System.Windows.Forms.DataVisualization.Charting; #region 시리즈 구하기 - GetSeries(chart, series, seriesName, chartAreaNam, seriesChartType, seriesColor) /// <summary> /// 시리즈 구하기 /// </summary> /// <param name="chart">차트</param> /// <param name="series">시리즈</param> /// <param name="seriesName">시리즈명</param> /// <param name="chartAreaNam">차트 영역명</param> /// <param name="seriesChartType">시리즈 차트 타입</param> /// <param name="seriesColor">시리즈 색상</param> /// <returns>시리즈</returns> private Series GetSeries(Chart chart, Series series, string seriesName, string chartAreaNam, SeriesChartType seriesChartType, Color seriesColor) { series = new Series(); series.Name = seriesName; series.ChartArea = chartAreaNam; series.ChartType = seriesChartType; series.Color = seriesColor; chart.Series.Add(series); return series; } #endregion #region XY 배열 구하기 - GetXYArray() /// <summary> /// XY 배열 구하기 /// </summary> /// <returns>XY 배열</returns> private int[] GetXYArray() { int[] xyArray = new int[2]; Random randomX = new Random(unchecked((int)DateTime.Now.Ticks)); Thread.Sleep(10); Random randomY = new Random(unchecked((int)DateTime.Now.Ticks)); xyArray[0] = randomX.Next(10); xyArray[1] = randomY.Next(10); return xyArray; } #endregion #region 데이터 생성하기 - CreateData(series) /// <summary> /// 데이터 생성하기 /// </summary> /// <param name="series">시리즈</param> private void CreateData(Series series) { for(int i = 0; i < 5; i++) { int[] xyArray = GetXYArray(); DataPoint dataPoint = new DataPoint(); dataPoint.SetValueXY(series, xyArray[0], xyArray[1]); series.Points.Add(dataPoint); } } #endregion this.chart1.Series.Clear(); this.chart1.ChartAreas.Clear(); this.chart1.ChartAreas.Add("ChartArea1"); this.chart2.Series.Clear(); this.chart2.ChartAreas.Clear(); this.chart2.ChartAreas.Add("ChartArea1"); this.chart2.ChartAreas.Add("ChartArea2"); this.chart2.ChartAreas.Add("ChartArea3"); Series seriesA = null; Series seriesB = null; Series seriesC = null; Series seriesD = null; Series seriesE = null; Series seriesF = null; CreateData(GetSeries(this.chart1, seriesA, "A", "ChartArea1", SeriesChartType.Column, Color.Green)); CreateData(GetSeries(this.chart1, seriesB, "B", "ChartArea1", SeriesChartType.Column, Color.Red )); CreateData(GetSeries(this.chart1, seriesC, "C", "ChartArea1", SeriesChartType.Column, Color.Blue )); CreateData(GetSeries(chart2, seriesD, "D", "ChartArea1", SeriesChartType.Line , Color.Green)); CreateData(GetSeries(chart2, seriesE, "E", "ChartArea2", SeriesChartType.Spline, Color.Red )); CreateData(GetSeries(chart2, seriesF, "F", "ChartArea3", SeriesChartType.Point , Color.Blue )); |