Add legend to my PieChart

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

    Posted 27 Jul 2015 Link to this post

    Please my PieChart in the attach file.

    This is my collection:

    Dictionary<string, double> seriesSource = new Dictionary<string, double>();

        this.doughnutSries.ValueBinding = new GenericDataPointBinding<KeyValuePair<string, double>, double>()
        {
            ValueSelector = x => x.Value
        };
     
        this.doughnutSries.ItemsSource = seriesSource;
        this.doughnutSries.Loaded += doughnutSries_Loaded;
     
    void doughnutSries_Loaded(object sender, RoutedEventArgs e)
    {
        if (this.doughnutSries.DataPoints.Count > 0)
        {
            this.doughnutSries.DataPoints[0].IsSelected = true;
        }
    }

     

     

    XAML:

    <telerik:RadPieChart Palette="Windows8">
        <telerik:RadPieChart.Behaviors>
            <telerik:ChartSelectionBehavior />
            <telerik:ChartTooltipBehavior />
        </telerik:RadPieChart.Behaviors>
     
        <telerik:DoughnutSeries x:Name="doughnutSries" InnerRadiusFactor="0.25" SelectedPointOffset="0.07">
            <telerik:DoughnutSeries.TooltipTemplate>
                <DataTemplate>
                    <Border Background="#B1E154" CornerRadius="3" TextElement.Foreground="#FFFFFF" Padding="5">
                        <StackPanel>
                            <TextBlock>
                                <TextBlock Text="IP Address: " />
                                <TextBlock Text="{Binding DataItem.Key}" />                                   
                            </TextBlock>
                            <TextBlock>
                                <TextBlock Text="Value: " />
                                <TextBlock Text="{Binding Value}" />                                   
                            </TextBlock>
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </telerik:DoughnutSeries.TooltipTemplate>
        </telerik:DoughnutSeries>
    </telerik:RadPieChart>

     So i want to add:

     1. Legend that will show me the IP Address and when mouse over i want also to highlight the pie section:

    Please see this: http://docs.telerik.com/devtools/wpf/controls/radchart/features/chart-legend

    2 I want to be able to copy the IP Address with mouse right click. 

  2. Martin
    Admin
    Martin avatar
    1101 posts

    Posted 27 Jul 2015 Link to this post

    Hello berry,

    Let me get straight to your requirements:
    • I want to add a legend
      RadChartView doesn't have a built in legend support. However, it supports an integration with the RadLegend control which you can use to achieve the desired effect. You can highlight the pie slices corresponding to the legend item under the mouse you can use the chart's HoverMode property and set it to FadeOtherItems.
      <telerik:RadPieChart HoverMode="FadeOtherItems">
      About displaying a specific piece of information in the legend item, you can use the legend settings' TitleBinding property. Here is an example:
      DataPointLegendSettings legendSettings = new DataPointLegendSettings();
      legendSettings.TitleBinding = new GenericDataPointBinding<KeyValuePair<string, double>, string>()
      {
          ValueSelector = x => x.Key
      };
       
      this.doughnutSries.LegendSettings = legendSettings;
    • I want to be able to copy the IP address with mouse right click
      To achieve this you can subscribe for the MouseRightButtonDown event of the RadLegend control. For example:
      this.legend.AddHandler(LegendItemControl.MouseRightButtonDownEvent, new MouseButtonEventHandler(OnLegendRightButtonDown));
      //.............
      private void OnLegendRightButtonDown(object sender, MouseButtonEventArgs e)
      {
          var hoveredItem = this.legend.Items.FirstOrDefault(x => x.IsHovered);
      }

    Please, try the suggested approaches and let me know if they work for you.

    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 27 Jul 2015 in reply to Martin Link to this post

    Thanks you for your quick reply.

    Currently legend is not define in my project, should i define it in XAML file ?

    Can i have a shot code example ?

  5. berry
    berry avatar
    27 posts
    Member since:
    Jun 2015

    Posted 27 Jul 2015 Link to this post

    Update:

    I added this code:

    <telerik:RadLegend
                   Name="legend"
                   Foreground="Red"
                   Background="White"
                   BorderBrush="Black"
                   BorderThickness="1"
                   Items="{Binding LegendItems, ElementName=chart1}" Margin="698,271,21,166"
                   />

    And add your code and the Legend is empty.

  6. Martin
    Admin
    Martin avatar
    1101 posts

    Posted 27 Jul 2015 Link to this post

    Hello Berry,

    I attached a project based on your code snippets. Can you please give it a try and let me know if I am missing something? Thank you for the cooperation.

    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 28 Jul 2015 in reply to Martin Link to this post

    Thanks a lot this works great !

    BTW i still try to cope the value (the IP Address) via Mouse Right Click --> Copy.

    Is it possible to simple code example ?

  8. Martin
    Admin
    Martin avatar
    1101 posts

    Posted 28 Jul 2015 Link to this post

    Hi Berry,

    You can use the Clipboard class to copy the IP Address.
    private void OnLegendRightButtonDown(object sender, MouseButtonEventArgs e)
    {
        var hoveredItem = this.legend.Items.FirstOrDefault(x => x.IsHovered);
        Clipboard.SetText(hoveredItem.Title);
    }

    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
Back to Top
UI for WPF is Visual Studio 2017 Ready