We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Change custom brush depending on bar value

Hello.
Using ColorModel with CustomBrushes works perfect if I set custom colors before using the chart. But I cannot set color dynamically during program execution (the simplest example is to paint bar in red/green based on some threshold). I tried the following:

private void SliderValueChanged(object sender, ValueChangedEventArgs e)
        {
            Random r = new Random();
            double newVal = 5 + r.Next(40); // here we obtain real bar value 
                        int index = (int)e.NewValue; // determine needed bar
            if (index > 4)
                return;
// determine color
            Color c = Color.LightGreen;
            if (newVal < 20)
                c = Color.Black;
            else if (newVal > 40)
                c = Color.LightSkyBlue;

            series.ColorModel.CustomBrushes[index] = c; // <------ here I try to use new color 
            datas[index].YValue = newVal;
        }
But it doesn't work. Can you advise, how to do it correctly?
Thanks, 
Vadim.
P.S. I maid a little sample to explain my question.

Attachment: cb2_39eec314.zip

4 Replies

NA Novel Alex September 28, 2017 03:07 PM UTC

Update. I can achive needed results with the next code modification (bold), it looks like some kind of brushes cashing is used.

        private void SliderValueChanged(object sender, ValueChangedEventArgs e)

        {

            Random r = new Random();

            double newVal = 5 + r.Next(40);

            int index = (int)e.NewValue;

            if (index > 4)

                return;

            Color c = Color.LightGreen;

            if (newVal < 20)

                c = Color.Black;

            else if (newVal > 40)

                c = Color.LightSkyBlue;

            var list = (from col in chart.Series[0].ColorModel.CustomBrushes select col).ToList();

            list[index] = c;

            series.ColorModel.CustomBrushes = list;

            datas[index].YValue = newVal;

        }

But it seems to be very expencive to make new list every time. Maybe, better way exists?



PS Parthiban Sundaram Syncfusion Team September 29, 2017 12:00 PM UTC

Hi Carlo,

Thanks for using Syncfusion products.

We would like to inform you that the given solution was the best possible way to change custom brush color dynamically.

Please let us know, if you need further assistance on this.

Regards,
Parthiban


NA Novel Alex September 29, 2017 12:14 PM UTC

Hello, Parthiban. 

Thank you for the information! This topic can be closed.



PS Parthiban Sundaram Syncfusion Team October 2, 2017 04:14 AM UTC

Hi Novel,

Thanks for the update. We are glad that the given solution has helped to achieve your requirement. Please let us know, if you need further assistance on this.

Regards,
Parthiban S


Loader.
Live Chat Icon For mobile
Up arrow icon