How to reuse Legend definition

3 posts, 1 answers
  1. Cedric
    Cedric avatar
    64 posts
    Member since:
    Oct 2010

    Posted 22 Jul 2011 Link to this post

    I want to use the same legend on several graph without duplicate xaml everywhere but when i do this the legend is displayed only in one chart :

    the definition of my legend:
    <telerik:ChartLegend x:Uid="ClassificationLegend"
            <telerik:ChartLegendItem x:Uid="telerik:ChartLegendItem_1"
                                     MarkerFill="{x:Static sim:ClassificationStatusColors.SuccessBrush}" />
            <telerik:ChartLegendItem x:Uid="telerik:ChartLegendItem_2"
                                     MarkerFill="{x:Static sim:ClassificationStatusColors.HesitationBrush}" />
            <telerik:ChartLegendItem x:Uid="telerik:ChartLegendItem_3"
                                     MarkerFill="{x:Static sim:ClassificationStatusColors.UnknownBrush}" />
            <telerik:ChartLegendItem x:Uid="telerik:ChartLegendItem_4"
                                     MarkerFill="{x:Static sim:ClassificationStatusColors.MisrecognitionBrush}" />

    The way i use it:
    <telerik:RadChart Name="chartStrategyPerformanceMax"
    DataContext="{Binding RelativeSource={RelativeSource AncestorType=telerik:RadFluidContentControl, Mode=FindAncestor},  Path=DataContext}"
    ItemsSource="{Binding StatusGroup}"
    PaletteBrushes="{Binding BrushPalette}">
    <!-- snip -->                
    <telerik:ChartDefaultView ChartLegend="{StaticResource ClassificationLegend}">
    <telerik:ChartArea LegendName="ClassificationLegend"
             SmartLabelsEnabled="True" />
    and same in another chart:
    <telerik:RadChart ItemsSource="{Binding DocumentsClassificationScore}"
                PaletteBrushes="{x:Static sim:ClassificationStatusColors.ClassificationPalette}">
                <telerik:ChartDefaultView ChartLegend="{StaticResource ClassificationLegend}">
                    <telerik:ChartArea LegendName="ClassificationLegend"/>

    I think because of the staticResource only one instance of the legend is created and this instance can only be owned by one chartarea (am i right ?).
    But there is no LegendStyle and even i don't know a way to add LegendItem by style so, is there a way to not copy/paste the same lines in all my graph. A way in declarative xaml of course and without code behind...

    - Cedric -
  2. Answer
    Yavor avatar
    401 posts

    Posted 27 Jul 2011 Link to this post

    Hello Cedric,

    RadChart support setting style for your legend through the LegendStyle property as explained in this topic in our help system. Currently you can't add items directly in the style using a setter, but you can add an EventSetter and attach the Loaded event of the legend to a method in your code behind that can add the items. Here is some sample code:

        <Style x:Key="legendStyle" TargetType="telerik:ChartLegend">
            <Setter Property="Padding" Value="0,0,5,0" />
            <Setter Property="HorizontalContentAlignment" Value="Right" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="UseAutoGeneratedItems" Value="False" />
            <EventSetter Event="Loaded" Handler="ChartLegend_Loaded" />
            <ColumnDefinition />
            <ColumnDefinition />
        <telerik:RadChart Name="chart1" LegendStyle="{StaticResource legendStyle}" Grid.Column="0" />
        <telerik:RadChart Name="chart2" LegendStyle="{StaticResource legendStyle}" Grid.Column="1" />

    And the ChartLegend_Loaded method:
    void ChartLegend_Loaded(object sender, RoutedEventArgs e)
        ChartLegend legend = sender as ChartLegend;
        legend.Items.Add(new ChartLegendItem() { Label = "Success" });
        legend.Items.Add(new ChartLegendItem() { Label = "Hesitation" });
        legend.Items.Add(new ChartLegendItem() { Label = "Unknown" });
        legend.Items.Add(new ChartLegendItem() { Label = "Misrecognition" });

    I have attached a sample project that demonstrates the mentioned approach. Feel free to modify it as you please.

    Hope this helps!

    All the best,
    Yavor Ivanov
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

  3. DevCraft banner
  4. Cedric
    Cedric avatar
    64 posts
    Member since:
    Oct 2010

    Posted 27 Jul 2011 Link to this post

    Thanks a lot !!!

    - Cedric -
Back to Top