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

Add legend to my PieChart

6 Answers 421 Views
Chart
This is a migrated thread and some comments may be shown as answers.
berry
Top achievements
Rank 1
berry asked on 27 Jul 2015, 05:30 AM

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. 

6 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 27 Jul 2015, 06:40 AM
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
0
berry
Top achievements
Rank 1
answered on 27 Jul 2015, 06:58 AM

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 ?

0
berry
Top achievements
Rank 1
answered on 27 Jul 2015, 07:14 AM

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.

0
Martin Ivanov
Telerik team
answered on 27 Jul 2015, 09:15 PM
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
0
berry
Top achievements
Rank 1
answered on 28 Jul 2015, 08:02 AM

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 ?

0
Martin Ivanov
Telerik team
answered on 28 Jul 2015, 12:31 PM
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
Tags
Chart
Asked by
berry
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
berry
Top achievements
Rank 1
Share this question
or