[]
        
(Showing Draft Content)

Custom Rendering and Custom Symbols

Rendering and Rendered events

FlexChart provides two events for custom rendering: Rendering and Rendered. The Rendering event fires before the chart renders its elements. The Rendered event fires after the chart completes rendering.

Both events expose the chart drawing surface through RenderEventArgs so you can draw custom shapes, text, and images during the rendering cycle.

public event EventHandler<RenderEventArgs> Rendered
public event EventHandler<RenderEventArgs> Rendering

Draw a custom zone using the Rendering event

The Rendering event enables drawing on the chart surface before series elements are rendered. The following example draws a threshold band behind the chart data by handling the Rendering event and using the DataToPoint method to convert data coordinates to pixel coordinates.

  1. Create a custom class to record the data.

public class ScoreItem
{
    public string Label { get; set; }
    public double Score { get; set; }

    public ScoreItem(string label, double score)
    {
        Label = label;
        Score = score;
    }
}

2. Set the data source on the FlexChart control.

var data = new List<ScoreItem>
{
  new ScoreItem("Q1", 55),
  new ScoreItem("Q2", 62),
  new ScoreItem("Q3", 80),
  new ScoreItem("Q4", 74),
  new ScoreItem("Q5", 90),
};

flexChart1.DataSource = data;
flexChart1.BindingX = "Label";
flexChart1.ChartType = ChartType.Line;
flexChart1.Header.Content = "Custom Rendering: Threshold Band";

3. Add a series and attach the Rendering event handler.

var series = new Series { Name = "Score", Binding = "Score" };
flexChart1.Series.Add(series);

// Handle Rendering to draw a custom band behind the series.
flexChart1.Rendering += FlexChart1_Rendering;

4. Handle the Rendering event to draw the zone.

private void FlexChart1_Rendering(object sender, RenderEventArgs e)
{
    var chart = sender as FlexChart;

    var pt1 = chart.DataToPoint(new PointF((float)chart.AxisX.ActualMin, 70f));
    var pt2 = chart.DataToPoint(new PointF((float)chart.AxisX.ActualMax, (float)chart.AxisY.ActualMax));

    using var fillBrush = new SolidBrush(Color.FromArgb(40, Color.Red));
    using var strokeBrush = new SolidBrush(Color.Transparent);
    e.Engine.SetFill(fillBrush);
    e.Engine.SetStroke(strokeBrush);
    e.Engine.DrawRect(pt1.X, pt2.Y, Math.Abs(pt2.X - pt1.X), Math.Abs(pt2.Y - pt1.Y));
}

A line chart titled "Custom Rendering: Threshold Band" with a semi-transparent red zone drawn behind the data points above a score of 70.


Custom symbols

FlexChart provides a pair of events for customizing how the marker for each data point is drawn: SymbolRendering and SymbolRendered.

Both events receive RenderSymbolEventArgs, which expose the drawing engine, the data point index, and the point coordinates.

public event EventHandler<RenderSymbolEventArgs> SymbolRendered 
public event EventHandler<RenderSymbolEventArgs> SymbolRendering

The SymbolRendering event fires before the default symbol is drawn. The SymbolRendered event fires after the default symbol is drawn. Both events enable custom drawing on top of or in place of the default rendering.

Customize line segment colors

The SymbolRendered event can customize the line segment drawn between the current data point and the previous data point.

The handler compares the current Y value with the previous Y value and draws the connecting line in red when the value increases, green when it decreases, and orange with a dashed stroke when it stays the same.

  1. Attach the SymbolRendered event to the series.

var series = new Series()            
{        
  Name = "Trend",   
  DataSource = dataList,
  Binding = "Y",
  BindingX = "X",
};
// Call the rendering event from the series instance
flexChart1.Series.Add(series);
series.SymbolRendered += Series_SymbolRendered;

2. Handle the SymbolRendered event.

private void Series_SymbolRendered(object sender, RenderSymbolEventArgs e)
{
    if (e.Index > 0)
    {
        Color lineColor;
        if (e.Point.Y == previousY)
        {
            lineColor = Color.Orange;
            e.Engine.SetStrokePattern(new double[] { 2, 1 });
        }
        else if (e.Point.Y > previousY)
        {
            lineColor = Color.Red;
            e.Engine.SetStrokePattern(null);
        }
        else
        {
            lineColor = Color.Green;
            e.Engine.SetStrokePattern(null);
        }
        using var brush = new SolidBrush(lineColor);
        e.Engine.SetStroke(brush);
        e.Engine.SetStrokeThickness(3);
        e.Engine.DrawLine(previousX, previousY, e.Point.X, e.Point.Y);
    }
}

A line chart with connecting segments colored to indicate trend direction: red for increasing values, green for decreasing, and orange dashed for unchanged.

Customize colors for a bar chart

The SymbolRendering event customizes the fill and stroke colors of individual columns in a bar chart. The handler sets the color based on the data item at the current index.

  1. Attach the SymbolRendering event to the series.

customSeries.Name = "Shipments";

// Call the rendering event from the series instance
customSeries.SymbolRendering += VendorSeries_SymbolRendering;
flexChart1.Series.Add(customSeries);

2. Handle the SymbolRendering event.

private void VendorSeries_SymbolRendering(object sender, RenderSymbolEventArgs e)
{
    using var fillBrush = new SolidBrush(vendors.ElementAt(e.Index).Color);
    using var strokeBrush = new SolidBrush(Color.FromArgb(200, vendors.ElementAt(e.Index).Color));
    e.Engine.SetFill(fillBrush);
    e.Engine.SetStroke(strokeBrush);
}

3. Define the data class and sample data.

// Sample data used for the column 
List<SmartPhoneVendor> vendors = new List<SmartPhoneVendor>();

vendors.Add(new SmartPhoneVendor()
{
    Name = "Apple",
    Color = Color.FromArgb(136, 189, 230),
    Shipments = 215.2,
});
vendors.Add(new SmartPhoneVendor()
{
    Name = "Huawei",
    Color = Color.FromArgb(251, 178, 88),
    Shipments = 139,
});

A bar chart showing smartphone vendor shipments, with each column filled in a distinct custom color.

Customize colors for a scatter plot

The SymbolRendering event customizes the stroke and fill colors of points in a scatter plot.

The handler selects a color from a gradient based on the temperature value at the current index.

  1. Attach the SymbolRendering event to the series.

flexChart1.DataSource = records;
flexChart1.ChartType = ChartType.Scatter;
var recordsSeries = new SeriesWithPointLegendItems();

// Call the rendering event from the series instance
recordsSeries.SymbolRendering += RecordsSeries_SymbolRendering;
flexChart1.Series.Add(recordsSeries);

2. Handle the SymbolRendering event.

// Symbol Rendering method
private void RecordsSeries_SymbolRendering(object sender, RenderSymbolEventArgs e)
{
  int idx = (((TemperatureRecord)e.Item).High - 95) / 5;
  using var fillBrush = new SolidBrush(GradientRedColors[idx]);
  using var strokeBrush = new SolidBrush(GradientRedColors[idx]);
  e.Engine.SetFill(fillBrush);
  e.Engine.SetStroke(strokeBrush);
}

3. Define the data class, sample data, and color gradient.

// Defines the red color gradient
private static readonly Color[] GradientRedColors = 
{
  Color.FromArgb(254, 224, 232),
  Color.FromArgb(252, 173, 193),
  Color.FromArgb(251, 121, 154),
  Color.FromArgb(249, 70, 115)
};

// Represents a temperature record.
public class TemperatureRecord
{
    public string Place { get; set; }
    public int High { get; set; }
}

A scatter plot with points colored on a red gradient based on temperature values.

See also

Create Alarm Zone