windowsforms-120046-controls-and-libraries-chart-control-series-series-colorizer.md
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.
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.
The following code snippet demonstrates how to assign the series colorizer at runtime:
chartControl.SeriesTemplate.SeriesDataMember = "Region";
chartControl.SeriesTemplate.ArgumentDataMember = "Year";
chartControl.SeriesTemplate.ValueDataMembers.AddRange("Value");
chartControl.SeriesTemplate.SeriesColorizer = new SeriesKeyColorColorizer() {
// Colorizer customization is here.
};
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:
| Symbol | Description |
|---|---|
| SeriesTemplate.SeriesColorizer | Gets or sets the Series Colorizer that automatically paints series. |
| SeriesColorizerBase | The base class for all colorizers. |
Refer to the Predefined Series Colorizers section to learn more about the predefined colorizers and their options.
The chart control has the following predefined colorizers:
The following code demonstrates how to implement a custom colorizer that should paint series using non-predefined algorithm.:
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();
}
}
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).