Back to Devexpress

XRChart.CustomPaint Event

xtrareports-devexpress-dot-xtrareports-dot-ui-dot-xrchart-390dd599.md

latest5.4 KB
Original Source

XRChart.CustomPaint Event

Allows you to draw custom graphics on top of the chart.

Namespace : DevExpress.XtraReports.UI

Assembly : DevExpress.XtraReports.v25.2.dll

NuGet Package : DevExpress.Reporting.Core

Declaration

csharp
public event CustomPaintEventHandler CustomPaint
vb
Public Event CustomPaint As CustomPaintEventHandler

Event Data

The CustomPaint event's data class is CustomPaintEventArgs. The following properties provide information specific to this event:

PropertyDescription
BoundsGets the bounds of a custom graphic object.
DXGraphics
GraphicsGets the custom graphic object.

Remarks

You can handle the CustomPaint event to draw a line or graphical object over the chart rendered from the XRChart control.

Example - Draw the Text in a Rectangle

The following code snippet draws a text string and a rectangle with rounded corners:

csharp
private void Chart_CustomPaint(object sender, CustomPaintEventArgs args) {
    GraphicsCache g = (args is DXCustomPaintEventArgs) ? ((DXCustomPaintEventArgs)args).Cache : new GraphicsCache(args.Graphics);
    g.DrawString("TEST", new DXFont("Arial", 14), new SolidBrush(DXColor.Red), new Point(100, 60));
    g.DrawRoundedRectangle(DXColor.Blue, 1, new Rectangle(50, 50, 150, 50), new CornerRadius(8));
}
vb
Private Sub Chart_CustomPaint(ByVal sender As Object, ByVal args As CustomPaintEventArgs)
    Dim g As GraphicsCache = If((TypeOf args Is DXCustomPaintEventArgs), (CType(args, DXCustomPaintEventArgs)).Cache, New GraphicsCache(args.Graphics))
    g.DrawString("TEST", New DXFont("Arial", 14), New SolidBrush(DXColor.Red), New Point(100, 60))
    g.DrawRoundedRectangle(DXColor.Blue, 1, New Rectangle(50, 50, 150, 50), New CornerRadius(8))
End Sub

Example - Encircle Axis Values

You can use the XYDiagram2D.DiagramToPoint or RadarDiagram.DiagramToPoint methods in the CustomPaint event handler to draw custom graphic objects tied to the chart elements (series or series points).

The following code snippet encircles axis values in the chart:

csharp
private void Chart_CustomPaint(object sender, CustomPaintEventArgs e) {
    XYDiagram diagram = ((XRChart)sender).Diagram as XYDiagram;
    int leftInternalValue = Convert.ToInt32(Math.Ceiling(diagram.AxisX.VisualRange.MinValueInternal));
    int rightInternalValue = Convert.ToInt32(Math.Floor(diagram.AxisX.VisualRange.MaxValueInternal));
    double bottomYValue = (double)diagram.AxisY.VisualRange.MinValue;

    for (int i = leftInternalValue; i <= rightInternalValue; i++) {
        var axisValue = diagram.AxisX.GetScaleValueFromInternal(i);
        Point axisLabelCoordinates = diagram.DiagramToPoint(axisValue.ToString(), bottomYValue).Point;
        axisLabelCoordinates.Offset(-10, 5);

        e.Graphics.DrawEllipse(Pens.Red, axisLabelCoordinates.X, axisLabelCoordinates.Y, 20, 20);
    }
}
vb
Private Sub Chart_CustomPaint(ByVal sender As Object, ByVal e As CustomPaintEventArgs)
    Dim diagram As XYDiagram = TryCast((CType(sender, XRChart)).Diagram, XYDiagram)
    Dim leftInternalValue As Integer = Convert.ToInt32(Math.Ceiling(diagram.AxisX.VisualRange.MinValueInternal))
    Dim rightInternalValue As Integer = Convert.ToInt32(Math.Floor(diagram.AxisX.VisualRange.MaxValueInternal))
    Dim bottomYValue As Double = CDbl(diagram.AxisY.VisualRange.MinValue)

    For i As Integer = leftInternalValue To rightInternalValue
        Dim axisValue = diagram.AxisX.GetScaleValueFromInternal(i)
        Dim axisLabelCoordinates As Point = diagram.DiagramToPoint(axisValue.ToString(), bottomYValue).Point
        axisLabelCoordinates.Offset(-10, 5)
        e.Graphics.DrawEllipse(Pens.Red, axisLabelCoordinates.X, axisLabelCoordinates.Y, 20, 20)
    Next
End Sub

Note

The graphics drawn in the CustomPaint event handler will not affect 3D charts.

See Also

XRChart Class

XRChart Members

DevExpress.XtraReports.UI Namespace