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.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Change custom brush depending on bar value

Thread ID:

Created:

Updated:

Platform:

Replies:

132927 Sep 28,2017 01:11 PM UTC Oct 2,2017 04:14 AM UTC Xamarin.Forms 4
loading
Tags: SfChart
Novel Alex
Asked On September 28, 2017 01:21 PM UTC

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

Novel Alex
Replied On 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?


Parthiban Sundaram [Syncfusion]
Replied On 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

Novel Alex
Replied On September 29, 2017 12:14 PM UTC

Hello, Parthiban. 

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


Parthiban Sundaram [Syncfusion]
Replied On 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


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;