Back to Devexpress

ColorObjectColorizer Class

corelibraries-devexpress-dot-xtracharts-e5df53a3.md

latest7.6 KB
Original Source

ColorObjectColorizer Class

A colorizer that uses SeriesBase.ColorDataMember field values to paint series points.

Namespace : DevExpress.XtraCharts

Assembly : DevExpress.XtraCharts.v25.2.dll

NuGet Package : DevExpress.Charts

Declaration

csharp
public class ColorObjectColorizer :
    ChartColorizerBase
vb
Public Class ColorObjectColorizer
    Inherits ChartColorizerBase

Remarks

The ColorObjectColorizer is a default colorizer. You need to specify the SeriesBase.ColorDataMember property to use it. The ColorDataMember field can contain the following values:

  • an integer ARGB color value (431493885);
  • three or six digit hex code (#fc0, #ff005d) ;
  • a string color name (SkyBlue);
  • a Color object.

Use the SeriesViewBase.Colorizer property to assign the colorizer to a series view. Refer to the Series Point Colorizer topic for more information about colorizers.

Example

Follow the steps below to use the Color Object Colorizer:

Note

The complete sample project is available in the following repository: https://github.com/DevExpress-Examples/winforms-chart-colorize-charts-using-the-color-object-colorizer.

csharp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using DevExpress.XtraCharts;

namespace ColorObjectColorizerExample {
    public partial class Form1 : Form {
        const string filepath = "..\\..\\Data\\GDP.xml";

        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            #region #BarSeries
            // Create and customize a bar series.
            Series barSeries = new Series() {
                DataSource = LoadData(filepath),
                ArgumentDataMember = "Country",
                ColorDataMember = "NationalColor",
                View = new SideBySideBarSeriesView()
            };
            barSeries.View.Colorizer = new ColorObjectColorizer();
            barSeries.ValueDataMembers.AddRange(new string[] { "Product" });
            #endregion #BarSeries

            // Add the series to the ChartControl's Series collection.
            chartControl1.Series.Add(barSeries);

            // Show a title for the values axis.
            ((XYDiagram)chartControl1.Diagram).AxisY.Title.Text = "GDP per capita, $";
            ((XYDiagram)chartControl1.Diagram).AxisY.Title.Visibility = DevExpress.Utils.DefaultBoolean.True;
        }

        #region #DataLoad
        class HpiPoint {
            public string Country { get; set; }
            public double Product { get; set; }
            public string NationalColor { get; set; }
        }

        // Loads data from an XML data source.
        static List<HpiPoint> LoadData(string filepath) {
            XDocument doc = XDocument.Load(filepath);
            List<HpiPoint> points = new List<HpiPoint>();
            foreach (XElement element in doc.Element("G20HPIs").Elements("CountryStatistics")) {
                points.Add(new HpiPoint() {
                    Country = element.Element("Country").Value,
                    Product = Convert.ToDouble(element.Element("Product").Value),
                    NationalColor = element.Element("NationalColor").Value
                });
            }
            return points;
        }
        #endregion #DataLoad
    }
}
vb
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms
Imports System.Xml.Linq
Imports DevExpress.XtraCharts

Namespace ColorObjectColorizerExample
    Partial Public Class Form1
        Inherits Form

        Private Const filepath As String = "..\..\Data\GDP.xml"

        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
' #Region "#BarSeries"
            ' Create and customize a bar series.
            Dim barSeries As New Series() With {
                .DataSource = LoadData(filepath),
                .ArgumentDataMember = "Country",
                .ColorDataMember = "NationalColor",
                .View = New SideBySideBarSeriesView()
            }
            barSeries.View.Colorizer = New ColorObjectColorizer()
            barSeries.ValueDataMembers.AddRange(New String() { "Product" })
' #End Region ' #BarSeries

            ' Add the series to the ChartControl's Series collection.
            chartControl1.Series.Add(barSeries)

            ' Show a title for the values axis.
            CType(chartControl1.Diagram, XYDiagram).AxisY.Title.Text = "GDP per capita, $"
            CType(chartControl1.Diagram, XYDiagram).AxisY.Title.Visibility = DevExpress.Utils.DefaultBoolean.True
        End Sub

        #Region "#DataLoad"
        Private Class HpiPoint
            Public Property Country() As String
            Public Property Product() As Double
            Public Property NationalColor() As String
        End Class

        ' Loads data from an XML data source.
        Private Shared Function LoadData(ByVal filepath As String) As List(Of HpiPoint)
            Dim doc As XDocument = XDocument.Load(filepath)
            Dim points As New List(Of HpiPoint)()
            For Each element As XElement In doc.Element("G20HPIs").Elements("CountryStatistics")
                points.Add(New HpiPoint() With {.Country = element.Element("Country").Value, .Product = Convert.ToDouble(element.Element("Product").Value), .NationalColor = element.Element("NationalColor").Value})
            Next element
            Return points
        End Function
        #End Region ' #DataLoad
    End Class
End Namespace

Implements

IColorizer

Inheritance

Object ChartElement ChartColorizerBase ColorObjectColorizer

See Also

ColorObjectColorizer Members

Series Point Colorizer

DevExpress.XtraCharts Namespace