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

Custom legend colors

1 Answer 106 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Eirik
Top achievements
Rank 1
Eirik asked on 07 Jun 2013, 07:17 AM
Hi

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:
<telerik:RadLegend.ItemTemplate>
                                    <DataTemplate>
                                        <Grid Margin="10,5,0,5">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="Auto" />
                                                <ColumnDefinition Width="*" />
                                            </Grid.ColumnDefinitions>
                                            <Border Grid.Column="0"
                                                    CornerRadius="3"
                                                    Height="15"
                                                    Width="15"
                                                    Background="{Binding Path=MarkerFill}" />
                                            <TextBlock Grid.Column="1"
                                                       Text="{Binding Path=Title}"
                                                       Foreground="{Binding Path=MarkerStroke}"
                                                       Margin="3,0,0,0" />
                                        </Grid>
                                    </DataTemplate>
                                </telerik:RadLegend.ItemTemplate>

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)
{
    chart.LegendItems.Add(legendItem);
}

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?

1 Answer, 1 is accepted

Sort by
0
Petar Kirov
Telerik team
answered on 11 Jun 2013, 03:26 PM
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.
 
Regards,
Petar Kirov
Telerik

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
ChartView
Asked by
Eirik
Top achievements
Rank 1
Answers by
Petar Kirov
Telerik team
Share this question
or