How to conditionally colour a chart?

How to conditionally colour a chart?

Scenario: In a bar chart, you want each bar colour depending on its value.

Solution: Add the chart to the Report, and with the chart selected go to scripts.

Add the following script:


// Apply different colors to the bars depending on their value

private void ApplyTresholdColors(object sender, DevExpress.XtraCharts.CustomDrawSeriesPointEventArgs e) {

var value = Convert.ToDouble(e.SeriesPoint.Values[0]);

if(value<60)

e.SeriesDrawOptions.Color = Color.FromArgb(239, 32, 32);

else if (value>=60 && value<80)

e.SeriesDrawOptions.Color = Color.FromArgb(254, 204, 5);

else

e.SeriesDrawOptions.Color = Color.FromArgb(4, 110, 4);

}


In the Property grid of the chart go to Behavior – Scripts – Custom draw a series and select ApplyTresholdColors from the list of functions in the dropdown menu.


The output chart will be like this: