Events in Windows Forms Chart

18 Nov 201811 minutes to read

Chart region events

Chart region events are raised when the user interacts with regions such as data points, axis labels, or custom chart regions.

The following chart region events are supported:

These events provide a ChartRegionMouseEventArgs instance with the following properties:

  • Point gets the client coordinate at which the event occurred.
  • Region gets information about the chart region associated with the event.
  • Button gets the mouse button actions.

The Region provides useful information about the chart region with which the user is currently interacting.

The ChartRegion object provides the following properties:

The SeriesIndex and PointIndex properties are applicable when the region Type is SeriesPoint.

The following code example demonstrates how to handle the ChartRegionDoubleClick and ChartRegionMouseDown events.

private Label regionInfoLabel;
this.chartControl.ChartRegionDoubleClick += chartControl_ChartRegionDoubleClick;

private void chartControl_ChartRegionDoubleClick(object sender, ChartRegionMouseEventArgs e)
{
    if (e.Region.IsChartPoint)
    {
        int seriesIndex = e.Region.SeriesIndex;
        int pointIndex = e.Region.PointIndex;

        ChartSeries series = chartControl.Series[seriesIndex];
        ChartPoint point = series.Points[pointIndex];

        regionInfoLabel.Text = string.Format("Double-clicked series: {0}, point index: {1}, " + "X value: {2}, Y value: {3}", series.Name, pointIndex, point.X, point.YValues[0]);
    }
    else
    {
        regionInfoLabel.Text = string.Format("Double-clicked region: {0}, location: {1}", e.Region.Description.ToString(), e.Point);
    }
}

this.chartControl.ChartRegionMouseDown += chartControl_ChartRegionMouseDown;

private void chartControl_ChartRegionMouseDown(object sender, ChartRegionMouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        Console.WriteLine("Chart region mouse down: " + e.Point);
    }
}
Private regionInfoLabel As Label

'ChartRegionDoubleClick Event
AddHandler chartControl.ChartRegionDoubleClick, AddressOf chartControl_ChartRegionDoubleClick

Private Sub chartControl_ChartRegionDoubleClick(ByVal sender As Object, ByVal e As ChartRegionMouseEventArgs)
    If e.Region.IsChartPoint Then
        Dim seriesIndex As Integer = e.Region.SeriesIndex
        Dim pointIndex As Integer = e.Region.PointIndex

        Dim series As ChartSeries = chartControl.Series(seriesIndex)
        Dim point As ChartPoint = series.Points(pointIndex)

        regionInfoLabel.Text = String.Format("Double-clicked series: {0}, point index: {1}, X value: {2}, Y value: {3}", series.Name, pointIndex, point.X, point.YValues(0))
    Else
        regionInfoLabel.Text = String.Format("Double-clicked region: {0}, location: {1}", e.Region.Description.ToString(), e.Point)
    End If
End Sub

'Usage of Button property in ChartRegionMouseDown Event
AddHandler chartControl.ChartRegionMouseDown, AddressOf chartControl_ChartRegionMouseDown

Private Sub chartControl_ChartRegionMouseDown(ByVal sender As Object, ByVal e As ChartRegionMouseEventArgs)
    If e.Button = MouseButtons.Right Then
        Console.WriteLine("Chart region mouse down: " & e.Point.ToString())
    End If
End Sub

Visible range changed event

The VisibleRangeChanged event is raised when the visible range of the chart changes during zooming.

The following code example demonstrates how to handle the VisibleRangeChanged event.

this.chartControl.VisibleRangeChanged += chartControl_VisibleRangeChanged;

private void chartControl_VisibleRangeChanged(object sender, EventArgs e)
{
    Console.WriteLine("Visible range changed event is raised.");
}
AddHandler Me.chartControl.VisibleRangeChanged, AddressOf chartControl_VisibleRangeChanged

Private Sub chartControl_VisibleRangeChanged(ByVal sender As Object, ByVal e As EventArgs)
    Console.WriteLine("Visible range changed event is raised.")
End Sub

Prepare style event

The PrepareStyle event is raised before a series point is rendered. Use the event arguments to customize the style of an individual data point.

The following code example changes the appearance of points that meet the specified sales quota.

series.PrepareStyle += new ChartPrepareStyleInfoHandler(series_PrepareStyle);

private void series_PrepareStyle(object sender, ChartPrepareStyleInfoEventArgs args)
{
    ChartSeries series = sender as ChartSeries ;
        if(series != null)    
    {
        //Condition to select members (data points) who made 100 % quota in sales
        if(((series.Points[args.Index].YValues[0] / 150) * 100) >= 100)
            {
                args.Style.Interior = new Syncfusion.Drawing.BrushInfo(Syncfusion.Drawing.GradientStyle.Horizontal, System.Drawing.Color.DarkGreen, System.Drawing.Color.LightYellow);
            }
    }
}
AddHandler series.PrepareStyle, AddressOf series_PrepareStyle

Private Sub series_PrepareStyle(ByVal sender As Object, ByVal args As ChartPrepareStyleInfoEventArgs)
    Dim series As ChartSeries = TryCast(sender, ChartSeries)
    If series IsNot Nothing Then
        'Condition to select members (data points) who made 100 % quota in sales
        If ((series.Points(args.Index).YValues(0) / 150) * 100) >= 100 Then
            args.Style.Interior = New Syncfusion.Drawing.BrushInfo(Syncfusion.Drawing.GradientStyle.Horizontal, System.Drawing.Color.DarkGreen, System.Drawing.Color.LightYellow)
        End If
    End If
End Sub

Chart events

Series incompatible event

The SeriesIncompatible event is raised when the Chart control detects that one or more series cannot be rendered together because their chart types or configurations are incompatible.

The following code example demonstrates how to handle the SeriesIncompatible event.

this.chartControl.SeriesIncompatible += chartControl_SeriesIncompatible;

private void chartControl_SeriesIncompatible(object sender, EventArgs e)
{
    Console.WriteLine("An incompatible series combination was detected.");
}
AddHandler Me.chartControl.SeriesIncompatible, AddressOf chartControl_SeriesIncompatible

Private Sub chartControl_SeriesIncompatible(ByVal sender As Object, ByVal e As EventArgs)
    Console.WriteLine("An incompatible series combination was detected.")
End Sub

Layout completed event

The LayoutCompleted event is raised when the chart is resized or re-rendered. This event is useful for rendering custom images or positioning custom controls over the chart after the layout is completed.

The following code example demonstrates how to handle the LayoutCompleted event.

this.chartControl.LayoutCompleted += chartControl_LayoutCompleted;

private void chartControl_LayoutCompleted(object sender, EventArgs e)
{
    Console.WriteLine("Layout Completed event is raised.");
}
AddHandler Me.chartControl.LayoutCompleted, AddressOf chartControl_LayoutCompleted

Private Sub chartControl_LayoutCompleted(ByVal sender As Object, ByVal e As EventArgs)
    Console.WriteLine("Layout Completed event is raised.")
End Sub

PreChart area paint event

The PreChartAreaPaint event is raised before the chart area is painted.

The following code example changes the background color before the chart area is painted.

this.chartControl.PreChartAreaPaint += new System.Windows.Forms.PaintEventHandler(this.chartControl_PreChartAreaPaint);;

private void chartControl_PreChartAreaPaint(object sender, PaintEventArgs e)
{
    this.chartControl.BackColor = Color.Yellow;
}
AddHandler Me.chartControl.PreChartAreaPaint, AddressOf chartControl_PreChartAreaPaint

Private Sub chartControl_PreChartAreaPaint(ByVal sender As Object, ByVal e As PaintEventArgs)
    Me.chartControl.BackColor = Color.Yellow
End Sub

See also