■ Chart 클래스 : PostPaint 이벤트 사용하기

----------------------------------------------------------------------------------------------------

 

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

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요