How to add RadLegend in code behind

8 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
    Admin
    Martin avatar
    1101 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. UI for WPF is Visual Studio 2017 Ready
  4. 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 ?
  5. 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 ?

     

     

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

    Posted 07 Jul 2015 in reply to Martin 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 !

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

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

    this relay helped, thanks you very much.

Back to Top
UI for WPF is Visual Studio 2017 Ready