Is there an easy way to use different brush color along a bar series? I have many different bar series on the same chart. For each series I want to set a different color for some chunks. For example:I seriesfrom x-value = 5000 to x-value = 5100 set BrushColor="green";from x-value = 5200 to x-value = 6000 set BrushColor="red";...
II seriesfrom x-value = 4000 to x-value = 4150 set BrushColor="yellow";....
If it's possible, I would like to see an example.
Thank you for your support.
Hello Giuseppe,
In order to achieve your requirement, I would recommend utilizing the AssigningCategoryStyle event of the BarSeries. In order to do this, you must first opt-in to handling this event by setting the IsCustomCategoryStyleAllowed property of the BarSeries to true.
This event will fire for each "bar" that is drawn, and the event arguments of this event carry some index information so that you can get the underlying data item to each one by utilizing the GetItems.Invoke() method along with the StartIndex and EndIndex properties of these arguments. From here, you can check the value path of your underlying data item and assign the Fill property of the event arguments accordingly to color the bars differently. Below is a code-snippet of this event's usage, where "SampleData" is a data item class:
private void BarSeries_AssigningCategoryStyle(object sender, AssigningCategoryStyleEventArgs args) { var x = args.GetItems.Invoke(args.StartIndex, args.EndIndex); if(x.Length > 0) { SampleData data = x[0] as SampleData; if(data.Value > 5000) { args.Fill = new SolidColorBrush(Color.Green); } else { args.Fill = new SolidColorBrush(Color.Red); } } }
I hope this helps. Please let me know if you have any other questions or concerns on this matter.
Sincerely,AndrewAssociate Developer
Hi Andrew,
thank you for your example but It doesn't solve my issue.
If I use your code, I achieve a reference to the x-value 6000 so the entire bar changes color to Green.
Imagine to have one dataItem, just one bar on the BarChart.
For example:this.Add(new DataItem { Name="Country", Oil=6000 });
I want that the x-value 6000 has many chunks with different colors.From 3000 to 3500 color is Yellow;From 3500 to 3800 color is Orange....Probably I cannot use StartIdex and EndIndex. Do you suggest anything else or can you explain how to use these indexes?Thank you for your patience.G.
From your most recent update, it sounds like you are looking to have multiple "ranges" of colors in a single bar on a BarSeries in this case. Unfortunately, this is not currently possible with the BarSeries in the XamDataChart in our Xamarin.Forms toolset as each bar can only take a single Fill color.
We do expose a LinearGradientBrush, but I don't believe this will really help you, as this will not cut off at a particular point - it is a gradient of multiple colors, and it is based on a 0 => 1 offset structure, much like the LinearGradientBrush in WPF.
The StartIndex and EndIndex properties won't really help you here either, as these are used to net the underlying data items, where the indices are the indices that those data items exist at in the underlying ItemsSource to the series.
If you would like to see the ability to color a single Bar with a sort of "range" coloring, I would recommend suggesting a new product idea for this by e-mailing ideas@infragistics.com. This will place you in direct communication with our product management teams who plan and prioritize upcoming features and development based on community and user feedback.
Please let me know if you have any other questions or concerns on this matter.
Ok Andrew,thank you.Regards,Giuseppe