教程:使用事件自定义图表
此教程演示如何使用事件来自定义图表。您将在最大的数据点处添加自定义标记,以突出显示当年度销售额最高的销售人员。
若要完成此教程,必须先完成教程:将图表与数据库进行数据绑定中的教程
添加图表命名空间
若要开始,请打开 ASP.NET 应用程序中的代码隐藏文件,或者切换到 Windows 窗体应用程序中的代码视图。
在代码文件的顶部,添加如下所示的图表命名空间。
' For Windows Forms
Imports System.Windows.Forms.DataVisualization.Charting
' For ASP.NET
Imports System.Web.UI.DataVisualization.Charting
// For Windows Forms
using System.Windows.Forms.DataVisualization.Charting;
// For ASP.NET
using System.Web.UI.DataVisualization.Charting;
添加 PostPaint 事件
为 Chart.PostPaint 事件添加事件处理程序。
在 Windows 窗体中,在窗体的构造函数中紧接在 InitializeComponent 方法之后的位置执行此操作。在 ASP.NET 中,则在自动创建的 Page_Load 事件处理程序方法中执行此操作。
AddHandler Me.Chart1.PostPaint, AddressOf Chart1_PostPaint
this.Chart1.PostPaint += new EventHandler<ChartPaintEventArgs>(Chart1_PostPaint);
然后,为刚才注册的事件处理程序添加方法定义。为此,将下面的代码添加到类定义中。
Private Sub Chart1_PostPaint(ByVal sender As Object, ByVal e As ChartPaintEventArgs)
End Sub
void Chart1_PostPaint(object sender, ChartPaintEventArgs e)
{
}
执行自定义绘制
现在将添加自定义代码,用于突出显示当年度销售额最高的销售人员。为此,将代码添加到刚才创建的 Chart1_PostPaint 事件处理程序之下,保存该文件,然后运行该应用程序。
' Make sure all series have been drawn before proceeding
If TypeOf e.ChartElement Is Series AndAlso DirectCast(e.ChartElement, Series).Name = "Series2" Then
Dim s As Series = e.Chart.Series(0)
Dim cg As ChartGraphics = e.ChartGraphics
Dim max As Double = s.Points.FindMaxByValue().YValues(0)
' Highlight the maximum sales this year
For i As Integer = 0 To s.Points.Count - 1
If s.Points(i).YValues(0) = max Then
' Get relative coordinates of the data point
Dim pos As System.Drawing.PointF = System.Drawing.PointF.Empty
pos.X = CSng(cg.GetPositionFromAxis("ChartArea1", AxisName.X, i+1))
pos.Y = CSng(cg.GetPositionFromAxis("ChartArea1", AxisName.Y, max))
' Convert relative coordinates to absolute coordinates.
pos = cg.GetAbsolutePoint(pos)
' Draw concentric circles at the data point
For radius As Integer = 10 To 39 Step 10
cg.Graphics.DrawEllipse(System.Drawing.Pens.Red, _
CSng(pos.X - radius / 2), _
CSng(pos.Y - radius / 2), radius, radius)
Next
End If
Next
End If
// Make sure all series have been drawn before proceeding
if (e.ChartElement is Series && ((Series)e.ChartElement).Name == "Series2" )
{
Series s = e.Chart.Series[0];
ChartGraphics cg = e.ChartGraphics;
double max = s.Points.FindMaxByValue().YValues[0];
// Highlight the maximum sales this year
for(int i = 0; i < s.Points.Count; i++)
{
if(s.Points[i].YValues[0] == max)
{
// Get relative coordinates of the data point
System.Drawing.PointF pos = System.Drawing.PointF.Empty;
pos.X = (float)cg.GetPositionFromAxis("ChartArea1", AxisName.X, i);
pos.Y = (float)cg.GetPositionFromAxis("ChartArea1", AxisName.Y, max);
// Convert relative coordinates to absolute coordinates.
pos = cg.GetAbsolutePoint(pos);
// Draw concentric circles at the data point
for (int radius = 10; radius < 40; radius += 10)
{
cg.Graphics.DrawEllipse(
System.Drawing.Pens.Red,
pos.X - radius / 2,
pos.Y - radius / 2,
radius, radius);
}
}
}
}
请参阅
参考
System.Windows.Forms.DataVisualization.Charting
System.Web.UI.DataVisualization.Charting