Change legend labels and colors at runtime

2 posts, 0 answers
  1. Wil
    Wil avatar
    11 posts
    Member since:
    Aug 2010

    Posted 04 Jun 2013 Link to this post


    in our application, the user can build different kind of charts, that are created using default colors and properties. We want to allow the user to customize the appearance of the chart,  specifically, series names and colors.

    This is what i'm doing now:

    var legendItems = radChart.DefaultView.ChartLegend.ChildrenOfType<ChartLegendItem>();
    for (int i = 0; i < frm.ViewModel.Items.Count; i++)
        var item = frm.ViewModel.Items[i]; // this is the item that describe the new label - color
        var mapping = mappings[i]; // here i iterate through the series mappings
        var legend = legendItems[i];
        mapping.SeriesDefinition.Appearance.Fill = item.LegendColor;
        mapping.LegendLabel = item.LegendText; // this seems not to have any effect
        legend.MarkerFill = item.LegendColor; // no effect
        legend.Label = item.LegendText;

    and this is how the chart is declared:

    <telerik:RadChart x:Name="radChart" DataBound="RadChartDataBound">                               
                    <telerik:ChartLegend x:Name="chartLegend" UseAutoGeneratedItems="True"
                                            Style="{StaticResource MyCustomLegendStyle}"
                                            Visibility="{Binding ShowLegend, Converter={StaticResource boolToVisibilityConverter}">
                    <telerik:ChartArea SelectionChanged="ChartAreaOnSelectionChanged" LegendName="chartLegend"  >


    Everything works, except the color of the legend marker, that stays unchanged. I attached a screenshot of the problem, after i changed color and label. As you can see, the color of the legend item is not in sync anymore with the series color.

    Is there any way to force the refresh of these items? How should i proceed?

    Thanks a lot
  2. Wil
    Wil avatar
    11 posts
    Member since:
    Aug 2010

    Posted 05 Jun 2013 Link to this post

    Never mind.

    Solved using manual generation of the legend, and rebuilding the legend items every time i make a change, instead of changing in-place.
  3. DevCraft banner
Back to Top