ChartSeriesProvider Binding LegendSettings in code behind

2 Answers 198 Views
ChartView
Lior
Top achievements
Rank 1
Lior asked on 28 Dec 2021, 04:47 PM

I'm using a ChartSeriesProvider to display multiple LineSeries charts. Everything is working well except the legend items. I would like the Title property to display each series name, but instead I'm getting the same series name (the first one) for all of the legend titles.

If I do it via XAML it works, as shown in your example:

<Setter Property="LegendSettings">
  <Setter.Value>
    <telerik:SeriesLegendSettings x:Name="jobsLegendSettings" Title="{Binding SeriesName}" />
  </Setter.Value>
 </Setter>

However, I must do it via code behind. Here is my current code which doesn't work (I only pasted the relevant code, I hope it's enough):

Style style = new Style();
descriptor.Style = style;
jobsSeriesProvider.SeriesDescriptors.Add(descriptor);
style.TargetType = typeof(Telerik.Windows.Controls.ChartView.LineSeries);
Binding binding = new Binding();
binding.Path = new PropertyPath("SeriesName");
binding.Source = JobsData;
SeriesLegendSettings legendSettings = new SeriesLegendSettings();
BindingOperations.SetBinding(legendSettings, SeriesLegendSettings.TitleProperty, binding);

var legendSetter = new Setter
{
	Property = Telerik.Windows.Controls.ChartView.LineSeries.LegendSettingsProperty,
	Value = legendSettings,
};
style.Setters.Add(legendSetter);  
BTW JobsData is the ObservableCollection which contains all of the different series view models, which have a property called SeriesName.

 

2 Answers, 1 is accepted

Sort by
0
Accepted
Stenly
Telerik team
answered on 31 Dec 2021, 08:02 AM

Hello Lior,

You could utilize the Palette property of the RadChartView control, which allows for easier colorization of the elements present in the RadChartView control instance, via a predefined set of Fills and Strokes. You could refer to this article of our documentation for additional information on this topic. Furthermore, you could also create your own custom palettes, if the built-in ones are undesired, by following the steps from the Creating Custom palettes section of our documentation.

I hope the provided information is of help to you.

Regards,
Stenly
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Lior
Top achievements
Rank 1
commented on 31 Dec 2021, 08:50 AM

Actually I am already using the Palette property when initializing the chart. I hope I'm doing it right:

 

RadCartesianChart jobsChart = new()
        {
            Palette = new ChartPalette() { Name = "Windows8" },            
            //other properties...
        };

Stenly
Telerik team
commented on 31 Dec 2021, 09:54 AM

Set the Palette property when creating a new RadCartesianChart class instance, to one of the properties from the ChartPalettes static class. Doing this will apply the chosen palette to the chart view element.
RadCartesianChart jobsChart = new RadCartesianChart()
{
    Palette = ChartPalettes.Warm,
    //other properties...
};
Lior
Top achievements
Rank 1
commented on 31 Dec 2021, 10:10 AM

That did it, thanks.
0
Stenly
Telerik team
answered on 29 Dec 2021, 02:31 PM

Hello Lior,

I was able to reproduce the duplicated legend items (only the first one is shown) in a sample project when applying the second code snippet (code-behind definition). It appears that this behavior is present due to the Source property of the Binding class instance, so, in order for the legend items to show correctly, you would need to remove its setting. 

The following code snippet shows the provided code snippet, modified to include this logic:

Style style = new Style();
descriptor.Style = style;
jobsSeriesProvider.SeriesDescriptors.Add(descriptor);
style.TargetType = typeof(Telerik.Windows.Controls.ChartView.LineSeries);
Binding binding = new Binding();
binding.Path = new PropertyPath("SeriesName");
//binding.Source = JobsData;
SeriesLegendSettings legendSettings = new SeriesLegendSettings();
BindingOperations.SetBinding(legendSettings, SeriesLegendSettings.TitleProperty, binding);

var legendSetter = new Setter
{
	Property = Telerik.Windows.Controls.ChartView.LineSeries.LegendSettingsProperty,
	Value = legendSettings,
};
style.Setters.Add(legendSetter);  

Upon running the application with the upper code added, the result is as follows:

With that said, could you give this suggestion a try and let me know how it goes?

Regards,
Stenly
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Lior
Top achievements
Rank 1
commented on 30 Dec 2021, 01:32 PM

Hi Stenly,

Indeed, that solved it. Thanks.

However, all my series have the same color, unlike in your example. How do I make them each have a random color?

Tags
ChartView
Asked by
Lior
Top achievements
Rank 1
Answers by
Stenly
Telerik team
Share this question
or