■ Chart 클래스의 PostPaint 이벤트를 사용하는 방법을 보여준다.
▶ 예제 코드 (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 |
using System; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; Chart chart = new Chart(); chart.Dock = DockStyle.Fill; chart.PostPaint += chart_PostPaint; Controls.Add(chart); chart.ChartAreas.Add("ChartArea1"); Series series = chart.Series.Add("Series1"); series.ChartType = SeriesChartType.Column; Random random = new Random(DateTime.Now.Millisecond); for(int i = 0; i < 10; i++) { series.Points.Add(random.Next(500)); } ... #region 차트 페인트 후 그리기 - chart_PostPaint(sender, e) /// <summary> /// 차트 페인트 후 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void chart_PostPaint(object sender, ChartPaintEventArgs e) { ChartGraphics chartGraphics = e.ChartGraphics; double x1 = chartGraphics.GetPositionFromAxis("ChartArea1", AxisName.X, 1); double x2 = chartGraphics.GetPositionFromAxis("ChartArea1", AxisName.X, 9); double y1 = chartGraphics.GetPositionFromAxis("ChartArea1", AxisName.Y, 200); double y2 = chartGraphics.GetPositionFromAxis("ChartArea1", AxisName.Y, 100); RectangleF rect = chartGraphics.GetAbsoluteRectangle(new RectangleF((float)x1, (float)y1, (float)x2 - (float)x1, (float)y2 - (float)y1)); Pen pen = new Pen(Brushes.Red, 2); chartGraphics.Graphics.DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height); } #endregion |