Back to Devexpress

Colorize Series Points

mobilecontrols-401980-xamarin-forms-charts-examples-color-series.md

latest14.5 KB
Original Source

Colorize Series Points

  • Jul 30, 2021
  • 14 minutes to read

A series point colorizer helps you color-code series points to enhance data visualization. Use a series point colorizer to specify one or more conditions based on which a chart applies colors to series points. For example, point colors indicate the Happy Planet Index (HPI for short) on the following chart:

You can use a predefined colorizer or implement a custom colorizer for series points.

Use Predefined Colorizers

ChartView ships with point and value range colorizers that use predefined algorithms to select point colors.

Point Colorizers

A predefined point colorizer uses colors from the default or custom palette to paint series points on a chart. Colors apply in the same order they appear in the palette.

To enable a point colorizer for a series, assign a ColorEachPointColorizer object to the series’s PointColorizer property.

  • Default Colors

  • Custom Colors

Value Range Colorizers

You can color series points based on their Y-axis values. To do this, use a value range colorizer:

  1. Assign a colorizer object to the series’s PointColorizer property.
  2. Populate the colorizer’s ColorStops collection with ColorStop objects. Each ColorStop object defines a value range (a pair of Y-axis values) and color. If a Y-axis value falls within the range, the point is painted with this color.

The following table lists series types and value range colorizers they support:

|

Series Type

|

Value Range Colorizer

| | | --- | --- | --- | |

Area, RangeArea, Point, Bar, Line,
StackedArea, StackedBar, and Bubble

|

ValueBandPointColorizer

| |

RangeBar

|

RangeValueBandPointColorizer

| |

StackedArea and StackedBar

|

StackedValueBandPointColorizer

| |

Bubble

|

WeightedValueBandPointColorizer

|

Example

In this example, the bar chart displays cryptocurrency portfolio yield. It colors series points red when their Y-axis values are between -100 and 0 , and green when values are between 0 and 100. Note that this chart diagram is rotated.

  1. Create a data source – a list of objects each of which has the Ticker property that returns a string (point argument) and the Profit property that returns a number (point value).

  2. Assign a ValueBandPointColorizer object to the BarSeries.PointColorizer property. Populate the colorizer’s ColorStops collection with two ColorStop objects that specify colors for ranges of point values.

Implement Custom Colorizers

You can also implement custom colorizers to paint series points.

Point Colorizers

The ChartView can color points of a series according to a custom algorithm based on point arguments/values and values of the chart’s bound data source.

Create a class that implements a custom colorizer interface and assign this class instance to the series’s PointColorizer property. The following table lists custom colorizer interfaces that ChartView series support:

|

Series Type

|

Colorizer Interface

|

Colorizer Description

| | | --- | --- | --- | --- | |

Area, RangeArea, Point, Bar, and Line

|

ICustomPointColorizer

|

The GetColor method’s parameter (ColoredPointInfo) allows you to obtain the following information about a series point and use it to create a color rule:

  • point argument
  • point value (aggregated value if data is aggregated)
  • index of an item in the data source (multiple indexes for an aggregated value)

| |

RangeBar

|

IRangeCustomPointColorizer

| |

StackedArea and StackedBar

|

IStackedCustomPointColorizer

| |

Bubble

|

IWeightedCustomPointColorizer

| |

All series, except for financial and pie series

|

IIndexBasedCustomPointColorizer

|

The GetColor method’s parameter is an index of the data source’s item that corresponds to the series point. If data aggregation is enabled, only the index of the first point’s item is passed.

|

Color Points by Their Arguments

In this example, the bar chart displays monthly values colored based on the season.

  1. Create a data source – a list of objects that define data points with date-time arguments and numeric values.

  2. Create a colorizer class (CustomColorizer in this example) that implements the ICustomPointColorizer interface.
    Implement the GetColor method to return a data point’s color based on a ColoredPointInfo object passed as the method’s parameter. Use the ColoredPointInfo.DateTimeArgument property to obtain the data point’s argument value.

  3. Assign a CustomColorizer object to the BarSeries.PointColorizer property.

Color Points by Data Source Values

In this example, the bar chart visualizes GDP values for the G20 and colors data points according to which part of the world the country belongs.

  1. Create a data source – a list of objects each of which stores a country’s name, GDP value, and the part of the world (region) where the country is located.

  2. Create a class (ColorizerByRegion, in this example) that implements the IIndexBasedCustomPointColorizer interface. Specify a color for each region and implement the IIndexBasedCustomPointColorizer.GetColor method that returns a color for each data point depending on the Region data member’s value.
    This class should also implement the IIndexBasedCustomPointColorizer.GetLegendItemProvider method and the ILegendItemProvider interface to specify legend items that show region colors.

  3. Assign a ColorizerByRegion object to the BarSeries.PointColorizer property.

Value Range Colorizers

A predefined value range colorizer colors points according to their Y-axis values. If you want a series point’s color to indicate the value of the specified data source field, implement a custom value range colorizer:

  1. Assign a CustomValueBandPointColorizer object to a series’s PointColorizer property.
  2. Create a class that implements the ICustomColorizerNumericValueProvider interface and its GetValueForColorizer method. This method returns a value from the data source that specifies how a series point should be colored. Assign this class value to the colorizer’s ValueProvider property.
  3. Populate the colorizer’s ColorStops collection with ColorStop objects. Each ColorStop object defines a value range and color. If a value that the GetValueForColorizer method returns for a series point falls within the range, the point is painted with this color.

You can use a custom value range colorizer for any series except for financial and pie series.

Example

In this example, the bubble chart displays GDP values for the G20. A point’s size indicates the country population and the color indicates the HPI.

  1. Create a data source – a list of objects each of which stores a country’s name, GDP value, population, and HPI.

  2. Create a class (HpiProvider, in this example) that implements the ICustomColorizerNumericValueProvider interface and the GetValueForColorizer method, which returns the Hpi value for a series point.

  3. Set the BubbleSeries.PointColorizer property to a CustomValueBandPointColorizer object and assign an HpiProvider object to the CustomValueBandPointColorizer.ValueProvider property.

  4. Populate the colorizer’s ColorStops collection to specify colors for ranges of HPI values.

Bind Colorizers to Data Source Fields

You can bind a colorizer to a data source that stores colors for series points. To do this, use a color data adapter:

  1. Assign a ColorDataAdapter object to a series’s PointColorizer property.
  2. Use the adapter’s DataSource, ColorDataMember, and LegendTextDataMember properties to specify the data source and its fields that contain colors and their descriptions.

Example

In this example, the bar chart visualizes GDP values for the G20 and paints bars with colors from the specified field of the data source. Legend items obtain descriptions for these colors from another field.

  1. Create a data source – a list of objects each of which stores a country’s name, GDP value, region (the part of the world where the country is located), and color for this region.

  2. Set the BarSeries.PointColorizer property to a ColorDataAdapter object with the following properties specified: