New to Telerik UI for WinFormsStart a free 30-day trial

How to offset PieSeries slice by clicking on a LegendItemElement

Updated over 6 months ago

Environment

Product VersionProductAuthor
2022.2.510RadChartView for WinFormsDinko Krastev

Description

In the following topic, we will demonstrate how we can offset PieSeries slices by clicking on its corresponding legend item.

Solution

This behavior can be achieved by handling the MouseDown event of the chart. In the event handler, we will need to find the associated data point with the clicked LegendItemElement.

C#

public Form1()
{
	InitializeComponent();

	this.radChartView1.AreaType = ChartAreaType.Pie;
	PieSeries series = new PieSeries();
	series.DataPoints.Add(new PieDataPoint(50, "Germany"));
	series.DataPoints.Add(new PieDataPoint(70, "United States"));
	series.DataPoints.Add(new PieDataPoint(40, "France"));
	series.DataPoints.Add(new PieDataPoint(25, "United Kingdom"));
	series.ShowLabels = true;

	this.radChartView1.Series.Add(series);
	this.radChartView1.ShowLegend = true;
	this.radChartView1.MouseDown += radChartView1_MouseDown;
}

private void radChartView1_MouseDown(object sender, MouseEventArgs e)
{
	RadChartView chart = (RadChartView)sender;
	RadElement element = chart.ElementTree.GetElementAtPoint(e.Location);
	LegendItemElement itemElement = chart.ElementTree.GetElementAtPoint(e.Location) as LegendItemElement;
	if (itemElement == null)
	{
		RadElement parent = element.Parent;
		while (parent != null)
		{
			if (parent is LegendItemElement)
			{
				itemElement = (LegendItemElement)parent;
				break;
			}

			parent = parent.Parent;
		}
	}

	if (itemElement != null)
	{
		PieDataPoint dataPoint = ((PiePointElement)itemElement.LegendItem.Element).DataPoint as PieDataPoint;
		if (dataPoint.OffsetFromCenter > 0)
		{
			dataPoint.OffsetFromCenter = 0;
		}
		else
		{ 
			dataPoint.OffsetFromCenter = 0.1;
		}
	}
}
In this article
EnvironmentDescriptionSolution
Not finding the help you need?
Contact Support