Custom Painting

RadSparkline provides you with two event that allows to style any of the painted elements. The events are PaintSparkStroke and PaintSparkFill. The events will be fired for each element and allow you to change its properties or directly paint on the sparkline surface. The event arguments are providing the following properties:

  • Cancel: Allows you to cancel the current element painting.
  • Graphics: The Graphics object that allows you to paint.
  • Path: The path of the current element.
  • Context: The currently painted element.
  • StrokePen: The default Pen used for the current element (PaintSparkStroke event only).
  • FillBrush: The default Brush used for the current element (PaintSparkFill event only).

The following examples demonstrates how you can use the above events.

Using the Paint Events

Font labelFont = new Font("Segoe UI", 8, FontStyle.Regular);
Image pointIcon = Image.FromFile(@"C:\img\pin.png").GetThumbnailImage(16, 16, null, IntPtr.Zero);
private void RadSparkline1_PaintSparkStroke(object sender, PaintSparkStrokeEventArgs e)
{
    if (e.Context is CategoricalSparkDataPoint)
    {
        e.Cancel = true;
        var point = new PointF(e.Path.PathPoints[0].X - 16, e.Path.PathPoints[0].Y - 16);
        e.Graphics.DrawImage(pointIcon, point);
    }
}
private void RadSparkline1_PaintSparkFill(object sender, PaintSparkFillEventArgs e)
{
    if (e.Context is CategoricalSparkDataPoint)
    {
        e.Cancel = true;
        var value = (e.Context as CategoricalSparkDataPoint).Value;
        e.Graphics.DrawString(value.ToString(), labelFont, Brushes.Blue, e.Path.PathPoints[0].X, e.Path.PathPoints[0].Y);
    }
}

Private labelFont As New Font("Segoe UI", 8, FontStyle.Regular)
Private pointIcon As Image = Image.FromFile("C:\img\pin.png").GetThumbnailImage(16, 16, Nothing, IntPtr.Zero)
Private Sub RadSparkline1_PaintSparkStroke(ByVal sender As Object, ByVal e As PaintSparkStrokeEventArgs)
    If TypeOf e.Context Is CategoricalSparkDataPoint Then
        e.Cancel = True
        Dim point = New PointF(e.Path.PathPoints(0).X - 16, e.Path.PathPoints(0).Y - 16)
        e.Graphics.DrawImage(pointIcon, point)
    End If
End Sub
Private Sub RadSparkline1_PaintSparkFill(ByVal sender As Object, ByVal e As PaintSparkFillEventArgs)
    If TypeOf e.Context Is CategoricalSparkDataPoint Then
        e.Cancel = True
        Dim value = (TryCast(e.Context, CategoricalSparkDataPoint)).Value
        e.Graphics.DrawString(value.ToString(), labelFont, Brushes.Blue, e.Path.PathPoints(0).X, e.Path.PathPoints(0).Y)
    End If
End Sub

Figure 1: Custom Paint

In this article
Not finding the help you need? Improve this article