I am ad my charts and several Series in code behind:
RadCartesianChart chart =
new
RadCartesianChart();
chart.Margin =
new
Thickness(0, 78, 0, -13);
chart.HorizontalAxis =
new
CategoricalAxis();
chart.VerticalAxis =
new
LinearAxis();
chart.HorizontalAxis.Visibility = System.Windows.Visibility.Collapsed;
chart.VerticalAxis.Visibility = System.Windows.Visibility.Collapsed;
LineSeries line =
new
LineSeries();
LineSeries line2 =
new
LineSeries();
line.Stroke =
new
SolidColorBrush(Colors.Blue);
line2.Stroke =
new
SolidColorBrush(Colors.Red);
chart.Series.Add(line);
chart.Series.Add(line2);
his.LayoutRoot.Children.Add(chart);
And i want to add Legend for each Series in code behind.
9 Answers, 1 is accepted
You can take a look at the RadLegend Support help article to see how to add a legend for the Cartesian chart. As for your specific code snippet you can use the following syntax to add legend settings and a legend for the chart's series:
RadCartesianChart chart =
new
RadCartesianChart();
chart.Margin =
new
Thickness(0, 78, 0, -13);
chart.HorizontalAxis =
new
CategoricalAxis();
chart.VerticalAxis =
new
LinearAxis();
chart.HorizontalAxis.Visibility = System.Windows.Visibility.Collapsed;
chart.VerticalAxis.Visibility = System.Windows.Visibility.Collapsed;
SeriesLegendSettings series1LegendSettings =
new
SeriesLegendSettings() { Title =
"Series 1"
};
SeriesLegendSettings series2LegendSettings =
new
SeriesLegendSettings() { Title =
"Series 2"
};
LineSeries line =
new
LineSeries() { LegendSettings = series1LegendSettings };
LineSeries line2 =
new
LineSeries() { LegendSettings = series2LegendSettings };
line.Stroke =
new
SolidColorBrush(Colors.Blue);
line2.Stroke =
new
SolidColorBrush(Colors.Red);
chart.Series.Add(line);
chart.Series.Add(line2);
this
.LayoutRoot.Children.Add(chart);
RadLegend legendControl =
new
RadLegend();
legendControl.Items = chart.LegendItems;
this
.LayoutRoot.Children.Add(legendControl);
I hope this is useful.
Regards,
Martin
Telerik
And another question:
I want every time the mouse is over specific legend to highlight the LineSeries over my chart, is it possible ?
The legend control is placed over the chart because you are adding both controls in a Grid panel without any rows defined.
//.....
RadLegend legendControl =
new
RadLegend();
legendControl.Items = chart.LegendItems;
this
.LayoutRoot.ColumnDefinitions.Add(
new
ColumnDefinition());
this
.LayoutRoot.ColumnDefinitions.Add(
new
ColumnDefinition() { Width =
new
GridLength(
double
.NaN) });
Grid.SetColumn(legendControl, 1);
this
.LayoutRoot.Children.Add(legendControl);
this
.LayoutRoot.Children.Add(chart);
About highlighting the series, you can set the chart's HoverMode to FadeOtherSeries.
chart.HoverMode = Telerik.Windows.Controls.ChartView.ChartHoverMode.FadeOtherSeries
I hope this helps.
Regards,
Martin
Telerik
OK that works fine, i have another 3 questions:
1. I want to Add Border to my Legeng so i try:
RadLegend legendControl =
new
RadLegend();
....
legendControl.BorderBrush = System.Windows.Media.Brushes.Blue;
But it seemt that nothing happen.
2. This regard to the Legend Mouse Enter: is it possible to highlight also the Legend in the menu and not only the series ?
3. How to add vertial ScrollBar to my Legend ?
Thanks !
Let me get straight to your questions:
- I want to Add Border to my Legend
In order to display a brush around the RadLegend control you will need to set the BorderThickness property along with the Border:
legendControl.BorderBrush = System.Windows.Media.Brushes.Blue;
legendControl.BorderThickness = new Thickness(1);
- Is it possible to highlight also the Legend in the menu and not only the series ?
A possible approach for highlighting the legend items is if you use a DataTrigger. Basically, you can check if the IsHovered property of the LegendItem is through - then change the Background of the item, for example.
<
telerik:RadLegend
Items
=
"{Binding ElementName=chart, Path=LegendItems}"
>
<
telerik:RadLegend.Resources
>
<
Style
TargetType
=
"telerik:LegendItemControl"
>
<
Style.Triggers
>
<
DataTrigger
Binding
=
"{Binding IsHovered}"
Value
=
"True"
>
<
Setter
Property
=
"Background"
Value
=
"Red"
/>
</
DataTrigger
>
</
Style.Triggers
>
</
Style
>
</
telerik:RadLegend.Resources
>
</
telerik:RadLegend
>
- How to add vertical ScrollBar to my Legend ?
The RadLegend control give its children as much space as they need and it doesn't have a built-in scrollviewer. In order to display a vertical scrollbar you can wrap the legend control into a ScrollViewer element.
<
ScrollViewer
>
<
telerik:RadLegend
/>
</
ScrollViewer
>
Regards,
Martin
Telerik
this relay helped, thanks you very much.
I would like to add check boxes inside legend of RadCartesianChart, so that I can control the visibility of each data series.
Can we achieve this using only .xaml or some code behind will be required.
Any idea regarding this will be helpful.
Thanks,
Rohith Mathew
You can do that only in XAML. Basically, you can define an item template for the legend control with a CheckBox inside and bind its IsChecked to the Visibility of the chart series. See how to do this in the Large Data demo of RadChartView.
Regards,
Martin Ivanov
Progress Telerik