Set individual bar colors in a series

1 Answer 70 Views
ChartView
Tim
Top achievements
Rank 1
Tim asked on 24 Aug 2023, 08:44 PM
Is there a way to set each bar to an individual color within a barseries in a RadChartView?   I have done it with multiple series but the need to be able to sort the bars based on a property and I can not find how to sort across multiple series.  Right now it looks like I could put all the data in a single series and order it how I need it displayed but each group of datapoints will need to have an individual color.  The attached image shows a working chart but it has 4 series and I need to sort by value.

1 Answer, 1 is accepted

Sort by
0
Accepted
Dinko | Tech Support Engineer
Telerik team
answered on 28 Aug 2023, 11:57 AM

Hi Tim,

Thank you for the provided image.

You are in the right direction with combining the 4 series into one, sort it, and then show it in the chart. This way you can easily sort your data before showing it to the end user. Now to change the bar's colors, you need to iterate the point elements after the chart is initialized. Here is an example:

public Form1()
{
    InitializeComponent();
    BarSeries barSeries = new BarSeries("Performance", "RepresentativeName");
    barSeries.Name = "Q1";
    barSeries.DataPoints.Add(new CategoricalDataPoint(177, "Harley"));
    barSeries.DataPoints.Add(new CategoricalDataPoint(50, "White"));
    barSeries.DataPoints.Add(new CategoricalDataPoint(80, "Smith"));
    barSeries.DataPoints.Add(new CategoricalDataPoint(90, "Jones"));
    barSeries.DataPoints.Add(new CategoricalDataPoint(118, "Marshall"));
    this.RadChartView1.Series.Add(barSeries);
    foreach (BarPointElement item in RadChartView1.Series(0).Children)
    {
        var value = (CategoricalDataPoint)item.DataPoint.Value;
        if (value.Value < 100)
            item.BackColor = Color.Green;
        else
            item.BackColor = Color.Red;
    }
}

In the above example, points whose value is bigger than 100 will be marked in red.

I hope that this approach will work for you.

Regards,
Dinko | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tim
Top achievements
Rank 1
commented on 29 Aug 2023, 03:23 PM

Thank you that is exactly what I was looking for.  I could not find how to get the BarPointElement to change them individually.
Tags
ChartView
Asked by
Tim
Top achievements
Rank 1
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or