How to add RadLegend in code behind

10 posts, 0 answers
  1. berry
    berry avatar
    27 posts
    Member since:
    Jun 2015

    Posted 01 Jul 2015 Link to this post

    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.

  2. Martin Ivanov
    Admin
    Martin Ivanov avatar
    1452 posts

    Posted 03 Jul 2015 Link to this post

    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
  3. berry
    berry avatar
    27 posts
    Member since:
    Jun 2015

    Posted 04 Jul 2015 Link to this post

    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 ?
  4. berry
    berry avatar
    27 posts
    Member since:
    Jun 2015

    Posted 04 Jul 2015 Link to this post

    And another question:

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

     

     

  5. Martin Ivanov
    Admin
    Martin Ivanov avatar
    1452 posts

    Posted 07 Jul 2015 Link to this post

    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
  6. berry
    berry avatar
    27 posts
    Member since:
    Jun 2015

    Posted 07 Jul 2015 in reply to Martin Ivanov Link to this post

    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 !

  7. Martin Ivanov
    Admin
    Martin Ivanov avatar
    1452 posts

    Posted 09 Jul 2015 Link to this post

    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
  8. berry
    berry avatar
    27 posts
    Member since:
    Jun 2015

    Posted 09 Jul 2015 in reply to Martin Ivanov Link to this post

    this relay helped, thanks you very much.

  9. rohith
    rohith avatar
    2 posts
    Member since:
    Oct 2017

    Posted 09 Nov Link to this post

    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

  10. Martin Ivanov
    Admin
    Martin Ivanov avatar
    1452 posts

    Posted 10 Nov Link to this post

    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.
Back to Top