This is a migrated thread and some comments may be shown as answers.

Setting LegendLabel from Query result set

5 Answers 77 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Tony
Top achievements
Rank 1
Tony asked on 25 Feb 2015, 10:37 PM
Hello,

I've been trying to solve the riddle of setting my chart's LegendLabel items. What I have is a query that provides a result set from a database. Two of the items are mapped (see code below) and I'm trying to figure out how to get the LegendLabel to display the appropriate info.

Here is my code segment:

            radChart.DefaultView.ChartLegend.Header = "Series";
                
            radChart.DefaultView.ChartArea.DataSeries.Clear();
            radChart.SeriesMappings.Clear();
            radChart.ItemsSource = this.getChartData();
            radChart.DefaultView.ChartTitle.Content = _chartTitle;
            //radChart.DefaultSeriesDefinition.LegendDisplayMode = LegendDisplayMode.DataPointLabel;
            //radChart.DefaultView.ChartLegend.Items.Add(new ItemMapping("Series"));
            radChart.DefaultView.ChartLegend.UseAutoGeneratedItems = false;

            SeriesMapping seriesMapping = new SeriesMapping();
            //seriesMapping.LegendLabel = "Series";
            seriesMapping.SeriesDefinition = new BarSeriesDefinition();
            //seriesMapping.ItemMappings.Add(new ItemMapping("Series", DataPointMember.LegendLabel));
            seriesMapping.ItemMappings.Add(new ItemMapping("Scenario_name", DataPointMember.XCategory));
            seriesMapping.ItemMappings.Add(new ItemMapping("Overall_RC0_Pct", DataPointMember.YValue)); 
            radChart.SeriesMappings.Add(seriesMapping);


I scoured numerous simliar posts to my query but haven't been successful yet. I was a bit confused as to what property I needed but as you can see I've set my Legend Header to the word "Series"

I want the items below to display the actual content of the underlying result set that comes from a column named "Series". So what the only time I see anything generated is when I set the "UseAutoGeneratedItems" to true and all I see is 'Series 0'

I tried mapping to the LegendLabel but I am not sure if that is correct or if I need to map to the ChartLegend.Items? If that is how I need to go what is the actual syntax to do that. 

Thanks again for your help!

Tony

5 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 02 Mar 2015, 09:16 AM
Hi Tony,

Yes, you can use the SeriesMapping'sLegendLabel property to set the label's content. However, this property won't be respected when the legend's UseAutoGeneratedItems property is set to False. In order to use the LegendLabel you will need to set the automatically generation of the legend items to True.
radChart.DefaultView.ChartLegend.UseAutoGeneratedItems = true;
seriesMapping.LegendLabel = "Series";
Note that the default value of the UseAutoGeneratedItems is True.

You can also consider manually creating the legend items. The Chart Legend help article demonstrates such approach.

Please try those options and let me know if they help.

Regards,
Martin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Tony
Top achievements
Rank 1
answered on 04 Mar 2015, 12:56 AM
I'm dynamically setting the datasource for the chart and am able to correctly map the X and Y axis to the appropriate fields. I am expecting that I should be able to read the specific field labeled 'Series' as the legend items but all I get is Series 0. I'm still confused on exactly what properties I should be calling. Don't get it :(

            radChart.DefaultView.ChartLegend.UseAutoGeneratedItems = true;
            
            SeriesMapping seriesMapping = new SeriesMapping();
            seriesMapping.SeriesDefinition = new BarSeriesDefinition();
            radChart.DefaultSeriesDefinition.LegendDisplayMode = LegendDisplayMode.SeriesLabel;
            seriesMapping.ItemMappings.Add(new ItemMapping("Series", DataPointMember.LegendLabel));


This is how it's set. I am expecting to get the underlying data that resides in the 'series' field in the dataset for the chart but all I get is Series 0

Thanks for your help.

Tony
0
Martin Ivanov
Telerik team
answered on 04 Mar 2015, 04:00 PM
Hello Tony,

The ItemMapping to the LegendLabel property will work only if the LegendDisplayMode of the series definition is set to DataPointLabel. This will display a legend item for each data point in the series and the LegendLabel mapping determines the content of this legend item. You can read more on the matter in the Legend Display Modes section of the Chart Legend help article.

The code snippet from your reply expects that you have a Series property in the view model of the chart's data points. In order to make this mapping to work you will need to change the LegendDisplayMode:
this.chart.DefaultSeriesDefinition.LegendDisplayMode = LegendDisplayMode.DataPointLabel;

However, as I understand you want to show a legend item not for each data point but only for the entire series. If so, you can use the LegendLabel property of the SeriesMapping and set the label get from your view models. Here is an example:
SeriesMapping seriesMapping = new SeriesMapping() { LegendLabel = "Series Legend Label"};
For your convenience I prepared a sample project demonstrating this approach. Please give it a try and let me know if it works for you. 

Regards,
Martin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Tony
Top achievements
Rank 1
answered on 04 Mar 2015, 06:20 PM
Hi Martin,

Thank you for the example you provided however I think it's not touching on exactly what I think I need.

I've attached an image to describe what I'm looking for an where I'm getting lost. I understand setting the label text but I think what I'm looking for is setting the actual legend item text based on the content in the field. Scenario_name and Overall_RC0_Pct are binded appropriately but I am not able to bind to Series. It is that value that I am trying to retrieve. 

Does that make sense?

Thanks,

Tony
0
Tony
Top achievements
Rank 1
answered on 04 Mar 2015, 11:19 PM
I figured this out by just attaching the datasource field to the legendlabel. I was assuming there was some feature I was missing that did that since the chart allows mapping to the X and Y axis points. I just assumed there was something I was missing for the legend. Anyway it reads as I was looking for now.
Tags
Chart
Asked by
Tony
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Tony
Top achievements
Rank 1
Share this question
or