How to add different color for each data point (Bar)

1 Answer 98 Views
Chart Chart - Xamarin.Android
HV
Top achievements
Rank 1
HV asked on 04 Jul 2023, 04:30 PM | edited on 04 Jul 2023, 05:14 PM

Hi, I am Implementing the Bar series, I am able to draw the Bars, But I need to apply different colors to each bar. I've tried like below but no use.

        ChartPalette customPalette = chartView.Palette.ClonePalette();
            PaletteEntry barEntry = customPalette.GetEntry(ChartPalette.BarFamily);
            barEntry = customPalette.GetEntry(ChartPalette.BarFamily);
            barEntry.Fill = Color.Black;

            PaletteEntry barEntry1 = customPalette.GetEntry(ChartPalette.BarFamily);
            barEntry1= customPalette.GetEntry(ChartPalette.BarFamily);
            barEntry1.Fill = Color.Red;

            PaletteEntry barEntry2 = customPalette.GetEntry(ChartPalette.BarFamily);
            barEntry2  = customPalette.GetEntry(ChartPalette.BarFamily);
            barEntry2 .Fill = Color.Yellow;

            barSeries.CategoryBinding = new MonthResultDataBinding("Index");
            barSeries.ValueBinding = new MonthResultDataBinding("BarValue");
            barSeries.Data = (Java.Lang.IIterable)this.monthResults;
            chartView.Series.Add(barSeries);
            chartView.Palette = customPalette;

Any help on adding different color for each data point.

HV
Top achievements
Rank 1
commented on 04 Jul 2023, 04:30 PM

@Didi @Lance

1 Answer, 1 is accepted

Sort by
0
Accepted
Lance | Manager Technical Support
Telerik team
answered on 05 Jul 2023, 05:14 PM

Hello HV,

The chart uses a Palette approach, where each new series get its own color from the list of colors in the palette. In your case, I'm assuming these are data points in the same series, so each bar in the same series is going to get the same color form the palette.... because its per-series... not per-datapoint.

You have two options to move forward:

1) Recommended - Create separate series, each with 1 data point. This is the easiest approach that doesn't require custom styling code because each series gets a separate color, and since there's only one datapoint in each series, you can go through the entire palette.

2) Alternative - You can review the custom implementation approach here => https://docs.telerik.com/devtools/xamarin/nativecontrols/android/chart/chart-palettes#creating-custom-palettes You can also see the demo that is shipped with UI for Xamarin "C:\Program Files (x86)\Progress\Telerik UI for Xamarin R2 2023\Examples\Android\Fragments\Chart\Features\PalettesFragment.cs"

Regards,
Lance | Manager Technical Support
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.

Tags
Chart Chart - Xamarin.Android
Asked by
HV
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
Share this question
or