How to Display Legend from Data

5 posts, 0 answers
  1. Michel
    Michel avatar
    39 posts
    Member since:
    Jul 2011

    Posted 08 Nov 2011 Link to this post

    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>
  2. Nikolay
    Admin
    Nikolay avatar
    385 posts

    Posted 09 Nov 2011 Link to this post

    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 >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Michel
    Michel avatar
    39 posts
    Member since:
    Jul 2011

    Posted 09 Nov 2011 Link to this post

    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
  5. Nikolay
    Admin
    Nikolay avatar
    385 posts

    Posted 10 Nov 2011 Link to this post

    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 >>
  6. Michel
    Michel avatar
    39 posts
    Member since:
    Jul 2011

    Posted 10 Nov 2011 Link to this post

    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>





Back to Top
UI for WPF is Visual Studio 2017 Ready