Custom legend colors

2 posts, 0 answers
  1. Eirik
    Eirik avatar
    1 posts
    Member since:
    May 2013

    Posted 07 Jun 2013 Link to this post


    My team are in the progress of converting an existing solution from using RadChart to RadChartView due to performance issues. I'm currently working on the legend items, which is handled in a special way. Basically, each legend item can have 2 colors. One color for the box/icon and one color for the legend item text. 

    The way I've set this up at the moment is like this:

    - As the chart series is created in code (not xaml) I set the LegendSettings to be a SeriesLegendSetting and set the Title accordingly. All series are of type LineSeries.
    - In XAML I build a new ItemTemplate:
                                            <Grid Margin="10,5,0,5">
                                                    <ColumnDefinition Width="Auto" />
                                                    <ColumnDefinition Width="*" />
                                                <Border Grid.Column="0"
                                                        Background="{Binding Path=MarkerFill}" />
                                                <TextBlock Grid.Column="1"
                                                           Text="{Binding Path=Title}"
                                                           Foreground="{Binding Path=MarkerStroke}"
                                                           Margin="3,0,0,0" />

    As you can see, the border's background is set to the legend item's MarkerFill property, and the text color is set to the legend item's MarkerStroke property.

    In code, I've been trying various ways of setting the MarkerFill and MarkerStroke property. Currently, my code looks like this:

    private void SetLegendItem(GraphAxisYDTO axisDTO, GraphVariableDTO variableDTO)
        if (_legendItems == null)
            _legendItems = new LegendItemCollection();
        _legendItems.Add(new LegendItem
                MarkerFill = new SolidColorBrush(Colors.Blue),
                MarkerStroke = new SolidColorBrush(Colors.Green),
                Title = variableDTO.Legend

    The Blue and Green color being for illustrative purposes, but the main concept being I'm trying to set this manually. When preparing to show a chart on the screen, I add items from the _legendItems collection to the chart's LegendItems collection:

    foreach (var legendItem in _legendItems)

    When debugging at this point, the chart's LegendItems collection has the correct amount of items with the correct colors.

    However, when the chart is rendered live, the series' color is completely overwriting my custom MarkerStroke/Fill properties. 

    Is there any way I can force the chart to use my legend items instead of auto-generating them from the series?

  2. Petar Kirov
    Petar Kirov avatar
    425 posts

    Posted 11 Jun 2013 Link to this post

    Hi Eirik,

    If you are creating the chart legend  items manually, then you should leave the series.LegendSettings to null to prevent the chart from generating legend items that override yours.

    I hope this helps.
    Petar Kirov

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top