RadLegend Support

This topic will give you an overview of the RadLegend support that RadChartView provides.

Read more about the RadLegend control in its documentation section.

The chart exposes a LegendItems property which is to be passed to the RadLegend's Items property. The LegendItems collection is empty by default and no legend items will be generated for a series unless the LegendSettings property of the series is set. This will generate and add LegendItem objects in the LegendItems collection of the chart. The chart will automatically set the Title, MarkerStroke, MarkerFill and MarkerGeometry properties of the LegendItem object, based on the LegendSettings and the default visuals of the chart series.

LegendSetting

Each chart series expose a LegendSettings property that allows you to determine the legend items appearance. There are two types of legend settings supported by the chart series.

  • SeriesLegendSettings: This is supported by all chart series. A single LegendItem will be generated for the series that uses the settings.

    The settings object allows you to set the title of the LegendItem via its Title property (see Example 2).

  • DataPointLegendSettings: This is supported only by the BarSeries and BubbleSeries. A LegendItem will be generated for each data point in the series that uses the settings.

    The settings allow you to bind the title to a property from the data point model. You can do that via the TitleBinding property. The property expects a string value that corresponds to the name of a property in the DataPoint object.

    Example 1: Binding legend items title to the Category property of the CategoricalDataPoint object

        <Grid> 
            <telerik:RadCartesianChart x:Name="chart" Palette="Windows8"> 
                <telerik:RadCartesianChart.HorizontalAxis> 
                    <telerik:CategoricalAxis /> 
                </telerik:RadCartesianChart.HorizontalAxis> 
                <telerik:RadCartesianChart.VerticalAxis> 
                    <telerik:LinearAxis /> 
                </telerik:RadCartesianChart.VerticalAxis> 
                <telerik:RadCartesianChart.Series> 
                    <telerik:BarSeries PaletteMode="DataPoint"> 
                        <telerik:BarSeries.DataPoints> 
                            <telerik:CategoricalDataPoint Category="X1" Value="11" /> 
                            <telerik:CategoricalDataPoint Category="X2" Value="7" /> 
                            <telerik:CategoricalDataPoint Category="X3" Value="3" /> 
                        </telerik:BarSeries.DataPoints> 
                        <telerik:BarSeries.LegendSettings> 
                            <telerik:DataPointLegendSettings TitleBinding="Category" /> 
                        </telerik:BarSeries.LegendSettings> 
                    </telerik:BarSeries>              
                </telerik:RadCartesianChart.Series> 
            </telerik:RadCartesianChart> 
     
            <telerik:RadLegend Items="{Binding LegendItems, ElementName=chart}"  
                               BorderBrush="Black"  
                               BorderThickness="1" 
                               HorizontalAlignment="Right"  
                               VerticalAlignment="Top" /> 
        </Grid> 
    

    Rad Chart View-features-radlegend-support-0

Both legend settings allow you to set the geometry of the legend item's marker. You can do that via the MarkerGeometry property of the settings.

Code Example

The example below demonstrates the cooperation between the RadLegend and RadCartesianChart. In this example the chart uses a palette to color its series and it uses the same palette to color the legend items. Note that no legend item is present for the last series as the LegendSettings property has not been set. The BarSeries is set up to use DataPointLegendSettings and this is why a legend item is created for each DataPoint.If a SeriesLegendSettings was used instead - only a single item would have been generated for the BarSeries.

Example 2: Legend Settings Code Example

<Grid> 
    <telerik:RadCartesianChart x:Name="chart1" Palette="Summer"> 
        <telerik:RadCartesianChart.HorizontalAxis> 
            <telerik:CategoricalAxis /> 
        </telerik:RadCartesianChart.HorizontalAxis> 
        <telerik:RadCartesianChart.VerticalAxis> 
            <telerik:LinearAxis /> 
        </telerik:RadCartesianChart.VerticalAxis> 
        <telerik:RadCartesianChart.Series> 
            <telerik:BarSeries x:Name="series1"> 
                <telerik:BarSeries.DataPoints> 
                    <telerik:CategoricalDataPoint Category="X1" Value="11" /> 
                    <telerik:CategoricalDataPoint Category="X2" Value="7" /> 
                    <telerik:CategoricalDataPoint Category="X3" Value="3" /> 
                </telerik:BarSeries.DataPoints> 
                <telerik:BarSeries.LegendSettings> 
                    <telerik:DataPointLegendSettings /> 
                </telerik:BarSeries.LegendSettings> 
            </telerik:BarSeries> 
            <telerik:LineSeries x:Name="series2"> 
                <telerik:LineSeries.DataPoints> 
                    <telerik:CategoricalDataPoint Category="X1" Value="15" /> 
                    <telerik:CategoricalDataPoint Category="X2" Value="14" /> 
                    <telerik:CategoricalDataPoint Category="X3" Value="13" /> 
                </telerik:LineSeries.DataPoints> 
                <telerik:LineSeries.LegendSettings> 
                    <telerik:SeriesLegendSettings Title="Line Series" /> 
                </telerik:LineSeries.LegendSettings> 
            </telerik:LineSeries> 
            <telerik:LineSeries x:Name="series3"> 
                <telerik:LineSeries.DataPoints> 
                    <telerik:CategoricalDataPoint Category="X1" Value="14" /> 
                    <telerik:CategoricalDataPoint Category="X2" Value="13" /> 
                    <telerik:CategoricalDataPoint Category="X3" Value="10" /> 
                </telerik:LineSeries.DataPoints> 
            </telerik:LineSeries> 
        </telerik:RadCartesianChart.Series> 
    </telerik:RadCartesianChart> 
 
    <telerik:RadLegend Background="White"  
                       BorderBrush="Black"  
                       BorderThickness="1"  
                       Items="{Binding LegendItems, ElementName=chart1}"  
                       HorizontalAlignment="Right"  
                       VerticalAlignment="Top" /> 
</Grid> 
Rad Chart View-features-radlegend-support-0

If the default LegendItem objects created by the chart doesn't work for your scenario, you can manually populate the legend instead of using the LegendItems collection. Read the Getting Started article of RadLegend to see how to populate the control with LegendItem objects manually.

In this article
Not finding the help you need? Improve this article