Skip to main content

SeriesKeyColorColorizer.KeyProvider Property

Gets or sets the object that converts series data member values into series color keys.

Namespace: DevExpress.XtraCharts

Assembly: DevExpress.XtraCharts.v24.1.dll

NuGet Package: DevExpress.Charts

Declaration

[Browsable(false)]
[TypeConverter(typeof(LocalizableExpandableObjectTypeConverter))]
public IColorizerKeyProvider KeyProvider { get; set; }

Property Value

Type Description
IColorizerKeyProvider

The object that converts series data member values into series keys.

Remarks

You can use custom logic to assign color keys to series. Create a custom key provider that inherits the IColorizerKeyProvider interface and implement the GetKey(Object) method for this purpose. Assign an instance of this provider to the SeriesKeyColorColorizer.KeyProvider property as follows:

using DevExpress.XtraCharts;
using System;
using System.Data;
using System.Windows.Forms;

namespace ColorKeyProvider {
   public partial class Form1 : Form {
       public Form1() {
           InitializeComponent();
       }
       private void Form1_Load(object sender, EventArgs e) {
           chartControl1.DataSource = GetData();
           chartControl1.SeriesDataMember = "Region";
           chartControl1.SeriesTemplate.ArgumentDataMember = "Year";
           chartControl1.SeriesTemplate.View = new LineSeriesView();
           chartControl1.SeriesTemplate.ValueDataMembers.AddRange(new string[] { "Sales" });
           SeriesKeyColorColorizer colorizer = new SeriesKeyColorColorizer() {
               PaletteName = "Slipstream",
               // Assign a provider instance to the KeyProvider property.
               KeyProvider = new CustomKeyProvider(),
           };
           colorizer.Keys.AddRange(new object[] { "key1", "key2", "key3" });
           chartControl1.SeriesTemplate.SeriesColorizer = colorizer;
       }
       internal static DataTable GetData() {
           DataTable table = new DataTable();
           table.Columns.AddRange(new DataColumn[] { new DataColumn("Region", typeof(string)),
                                                 new DataColumn("Year", typeof(int)),
                                                 new DataColumn("Sales", typeof(decimal)) });
           table.Rows.Add("Asia", 2017, 4.2372D);
           table.Rows.Add("Asia", 2018, 4.7685D);
           table.Rows.Add("Asia", 2019, 5.2890D);
           table.Rows.Add("Australia", 2017, 1.7871D);
           table.Rows.Add("Australia", 2018, 1.9576D);
           table.Rows.Add("Australia", 2019, 2.2727D);
           table.Rows.Add("Europe", 2017, 3.0884D);
           table.Rows.Add("Europe", 2018, 3.3579D);
           table.Rows.Add("Europe", 2019, 3.7257D);
           table.Rows.Add("North America", 2017, 3.4855D);
           table.Rows.Add("North America", 2018, 3.7477D);
           table.Rows.Add("North America", 2019, 4.1825D);
           table.Rows.Add("South America", 2017, 1.6027D);
           table.Rows.Add("South America", 2018, 1.8237D);
           table.Rows.Add("South America", 2019, 2.1172D);

           return table;
       }
   }
   //Implement a custom key provider class. 
   internal class CustomKeyProvider : IColorizerKeyProvider {
       public object GetKey(object colorKey) {
           string seriesName = colorKey.ToString();
           if (seriesName.StartsWith("A"))
               return "key1";
           if (seriesName.Contains("America")) {
               return "key2";
           }
           else
               return "key3";
       }
   }
}

Result:

Chart SeriesKeyColorColorizer Custom Key Provider

See Also