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

How to Display Legend from Data

4 Answers 51 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Michel
Top achievements
Rank 1
Michel asked on 08 Nov 2011, 09:43 PM
I am trying to display the MaterialName values in the Legend...

The Chart controls creates a line for each MaterialName in the list but I cannot get the Legend to display those names...

All the examples I found use static Legend captions.

I am using this ItemSource:

public class ForecastValue
{
    public string MaterialName { get; set; }
    public double Period { get; set; }
    public double Quantity { get; set; }
}

This is the XAML code:

<telerik:RadChart x:Name="EquipmentDistribution" ItemsSource="{Binding ForecastValueList}" >
    <telerik:RadChart.DefaultView >
        <telerik:ChartDefaultView >
            <telerik:ChartDefaultView.ChartLegend>
                <telerik:ChartLegend DisplayMemberPath="MaterialName" />
            </telerik:ChartDefaultView.ChartLegend>
            <telerik:ChartDefaultView.ChartArea>
                <telerik:ChartArea OverridesDefaultStyle="False" EnableAnimations="False">
                    <telerik:ChartArea.AxisY>
                        <telerik:AxisY Title="Quantity"/>
                    </telerik:ChartArea.AxisY>
                    <telerik:ChartArea.AxisX>
                        <telerik:AxisX Title="Periods"/>
                    </telerik:ChartArea.AxisX>
                </telerik:ChartArea>
            </telerik:ChartDefaultView.ChartArea>
        </telerik:ChartDefaultView>
    </telerik:RadChart.DefaultView>
    <telerik:RadChart.SeriesMappings>
        <telerik:SeriesMapping LegendLabel="MaterialName">
            <telerik:SeriesMapping.SeriesDefinition>
                <telerik:LineSeriesDefinition LegendDisplayMode="SeriesLabel" />
            </telerik:SeriesMapping.SeriesDefinition>
            <telerik:ItemMapping DataPointMember="LegendLabel" FieldName="MaterialName"/>
            <telerik:ItemMapping DataPointMember="XValue" FieldName="Period"/>
            <telerik:ItemMapping DataPointMember="YValue" FieldName="Quantity" />
        </telerik:SeriesMapping>
    </telerik:RadChart.SeriesMappings>
</telerik:RadChart>

4 Answers, 1 is accepted

Sort by
0
Nikolay
Telerik team
answered on 09 Nov 2011, 09:00 AM
Hello Michel,

It appears that the Legend has not been associated with the ChartArea, which is why you do not get the items displayed. Please, find attached a sample application, that we have prepared to simulate a scenario similar to yours.

Hope this helps.

Regards,
Nikolay
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
0
Michel
Top achievements
Rank 1
answered on 09 Nov 2011, 06:46 PM
Thanks !

It is now displaying the Material names. Great, but if I have 12 values for one Material name, I see 12 times the same Material name in the legend...

My scenario is that I may have 2 - 5 Materials with multiple values.

Do I have to present the data in another way ? Or is it just a different setting in the Chart control ?

Thanks again,

Michel
0
Nikolay
Telerik team
answered on 10 Nov 2011, 08:59 AM
Hello Michel,

In case you need to have each material as a separately defined series, then you would need one series mapping for each material. Please, find attached a slightly modified version of the original application, which demonstrates how you can achieve the desired behavior.

Greetings,
Nikolay
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
0
Michel
Top achievements
Rank 1
answered on 10 Nov 2011, 05:21 PM
Hi !

Your second example is really different but it gave me a hint about grouping and I was able to map everything on the XAML side.

Now I get a single line and a single Legend entry for each MaterialName...  Thanks !

<telerik:RadChart ItemsSource="{Binding ForecastValueList}">
    <telerik:RadChart.DefaultView >
        <telerik:ChartDefaultView >
            <telerik:ChartDefaultView.ChartLegend>
                <telerik:ChartLegend x:Name="legend" Header="Materials"/>
            </telerik:ChartDefaultView.ChartLegend>
            <telerik:ChartDefaultView.ChartArea>
                <telerik:ChartArea LegendName="legend" EnableAnimations="False">
                    <telerik:ChartArea.AxisY>
                        <telerik:AxisY Title="Quantity" DefaultLabelFormat="#.0" />
                    </telerik:ChartArea.AxisY>
                    <telerik:ChartArea.AxisX>
                        <telerik:AxisX Title="Periods" DefaultLabelFormat="MMM-dd"/>
                    </telerik:ChartArea.AxisX>
                </telerik:ChartArea>
            </telerik:ChartDefaultView.ChartArea>
        </telerik:ChartDefaultView>
    </telerik:RadChart.DefaultView>
    <telerik:RadChart.SeriesMappings>
        <telerik:SeriesMapping>
            <telerik:SeriesMapping.SeriesDefinition>
                <telerik:LineSeriesDefinition LegendDisplayMode="SeriesLabel"/>
            </telerik:SeriesMapping.SeriesDefinition>
            <telerik:SeriesMapping.GroupingSettings>
                <telerik:GroupingSettings>
                    <telerik:GroupingSettings.GroupDescriptors>
                        <telerik:ChartGroupDescriptor Member="MaterialName" />
                    </telerik:GroupingSettings.GroupDescriptors>
                </telerik:GroupingSettings>
            </telerik:SeriesMapping.GroupingSettings>
            <telerik:ItemMapping DataPointMember="LegendLabel" FieldName="MaterialName"/>
            <telerik:ItemMapping DataPointMember="XValue" FieldName="Period" />
            <telerik:ItemMapping DataPointMember="YValue" FieldName="Quantity" />
        </telerik:SeriesMapping>
    </telerik:RadChart.SeriesMappings>
</telerik:RadChart>





Tags
Chart
Asked by
Michel
Top achievements
Rank 1
Answers by
Nikolay
Telerik team
Michel
Top achievements
Rank 1
Share this question
or