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

Change legend labels and colors at runtime

1 Answer 183 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Wil
Top achievements
Rank 1
Wil asked on 04 Jun 2013, 12:55 PM
Hello

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:RadChart.DefaultView>
        <telerik:ChartDefaultView>
            <telerik:ChartDefaultView.ChartLegend>
                <telerik:ChartLegend x:Name="chartLegend" UseAutoGeneratedItems="True"
                                        Style="{StaticResource MyCustomLegendStyle}"
                                        Visibility="{Binding ShowLegend, Converter={StaticResource boolToVisibilityConverter}">
                </telerik:ChartLegend>
            </telerik:ChartDefaultView.ChartLegend>
            <telerik:ChartDefaultView.ChartArea>
                <telerik:ChartArea SelectionChanged="ChartAreaOnSelectionChanged" LegendName="chartLegend"  >
                </telerik:ChartArea>
            </telerik:ChartDefaultView.ChartArea>

       
</telerik:ChartDefaultView>
    </telerik:RadChart.DefaultView>
                                 
</telerik:RadChart>

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

1 Answer, 1 is accepted

Sort by
0
Wil
Top achievements
Rank 1
answered on 05 Jun 2013, 09:38 AM
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.
Tags
Chart
Asked by
Wil
Top achievements
Rank 1
Answers by
Wil
Top achievements
Rank 1
Share this question
or