Back to Devexpress

Series Colorizer

windowsforms-120046-controls-and-libraries-chart-control-series-series-colorizer.md

latest5.7 KB
Original Source

Series Colorizer

  • Aug 13, 2021
  • 6 minutes to read

The Series Colorizer provides the capability to assign a color to automatically generated series using one of the predefined algorithms.

This guide consists of the following sections:

The Chart Control ships with Series Point Colorizers that apply colors to series points and markers based on a condition. Refer to the following topic for more information: Series Point Colorizers.

You can also paint line and area series by segments. See the following topic to learn more: Segment Colorizers.

To manually assign a color to a series, specify the series view’s Color property.

Series Colorizer Setting at Design-Time

To change the series colorizer at design-time, select the chart, expand the ChartControl.SeriesTemplate property in the Properties window, locate the SeriesTemplate.SeriesColorizer property, and select the colorizer from the drop-down list.

You can configure the selected colorizer, for example, specify keys that the Key-Color Colorizer should use to assign colors to series. Refer to the Predefined Series Colorizers section to learn more about the predefined colorizer options.

Series Colorizer Setting at Runtime

The following code snippet demonstrates how to assign the series colorizer at runtime:

csharp
chartControl.SeriesTemplate.SeriesDataMember = "Region";
chartControl.SeriesTemplate.ArgumentDataMember = "Year";
chartControl.SeriesTemplate.ValueDataMembers.AddRange("Value");
chartControl.SeriesTemplate.SeriesColorizer = new SeriesKeyColorColorizer() {
    // Colorizer customization is here.
};
vb
chartControl.SeriesTemplate.SeriesDataMember ="Region"
chartControl.SeriesTemplate.ArgumentDataMember = "Year"
chartControl.SeriesTemplate.ValueDataMembers.AddRange("Value")
chartControl.SeriesTemplate.SeriesColorizer = New SeriesKeyColorColorizer With { _
    ' Colorizer customization is here.
}

The code above uses the following classes and properties:

SymbolDescription
SeriesTemplate.SeriesColorizerGets or sets the Series Colorizer that automatically paints series.
SeriesColorizerBaseThe base class for all colorizers.

Refer to the Predefined Series Colorizers section to learn more about the predefined colorizers and their options.

Predefined Series Colorizers

The chart control has the following predefined colorizers:

Custom Series Colorizers Implementation

The following code demonstrates how to implement a custom colorizer that should paint series using non-predefined algorithm.:

csharp
class CustomSeriesColorizer : SeriesColorizerBase {
    List<object> metColors = new List<object>();

    public override Color GetSeriesColor(object seriesKey, Palette palette) {
        int keyIndex;
        if (metColors.Contains(seriesKey)) {
            keyIndex = metColors.IndexOf(seriesKey);
        } else {
            keyIndex = metColors.Count;
            metColors.Add(seriesKey);
        }
        return palette[keyIndex % palette.Count].Color;
    }

    protected override ChartElement CreateObjectForClone() {
        return new CustomSeriesColorizer();
    }
}
vb
Class CustomSeriesColorizer Inherits SeriesColorizerBase
    Private metColors As List(Of Object) = New List(Of Object)

    Public Overrides Function GetSeriesColor(ByVal seriesKey As Object, ByVal palette As Palette) As Color
        Dim keyIndex As Integer
        If Me.metColors.Contains(seriesKey) Then
            keyIndex = Me.metColors.IndexOf(seriesKey)
        Else
            keyIndex = Me.metColors.Count
            Me.metColors.Add(seriesKey)
        End If
        Return palette((keyIndex Mod palette.Count)).Color
    End Function

    Protected Overrides Function CreateObjectForClone() As ChartElement
        Return New CustomSeriesColorizer
    End Function
End Class

Each series colorizes should inherit the abstract SeriesColorizerBase class that declares the SeriesColorizerBase.GetSeriesColor method that returns a color that corresponds to the specified series key (the series data member value).