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

How to add RadLegend in code behind

9 Answers 481 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
berry
Top achievements
Rank 1
berry asked on 01 Jul 2015, 11:42 AM

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

Sort by
0
Martin Ivanov
Telerik team
answered on 03 Jul 2015, 07:24 AM
Hello Berry,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
berry
Top achievements
Rank 1
answered on 04 Jul 2015, 08:53 AM
Thanks a lot this works fine, just another thing: currently the Legend is in different place then i want it to be, how can i put it over the right side of my chart ?
0
berry
Top achievements
Rank 1
answered on 04 Jul 2015, 01:58 PM

And another question:

I want every time the mouse is over specific legend to highlight the LineSeries over my chart, is it possible ?

 

 

0
Martin Ivanov
Telerik team
answered on 07 Jul 2015, 04:02 PM
Hello Berry,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
berry
Top achievements
Rank 1
answered on 07 Jul 2015, 07:40 PM

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 !

0
Martin Ivanov
Telerik team
answered on 09 Jul 2015, 03:57 PM
Hello Berry,

Let me get straight to your questions:
  1. 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);
  2. 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>
  3. 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>
I hope this helps.

Regards,
Martin
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
berry
Top achievements
Rank 1
answered on 09 Jul 2015, 07:34 PM

this relay helped, thanks you very much.

0
rohith
Top achievements
Rank 1
answered on 09 Nov 2017, 08:48 AM

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

0
Martin Ivanov
Telerik team
answered on 10 Nov 2017, 09:38 AM
Hello Rohith,

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
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
ChartView
Asked by
berry
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
berry
Top achievements
Rank 1
rohith
Top achievements
Rank 1
Share this question
or