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

How to get series name from clicking Bar Element Label

4 Answers 316 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Kyle
Top achievements
Rank 1
Kyle asked on 08 Dec 2017, 05:16 PM

I have a bar chart and I'm trying to get the name of the series when I click on the Label Element next to the bar.

However, I can't find any documentation on how to add a click event to the label element (in the image attached, I'm talking about the $10000 label).

I have a SelectedPointChanged event registered to my RadChartView control and that works great, but only works when the bar is clicked not the label next to it.

private void selectionController_SelectedPointChanged(object sender, ChartViewSelectedPointChangedEventArgs args)
{
 var series = args.NewSelectedSeries;
}

 

 

 

 

4 Answers, 1 is accepted

Sort by
0
Kyle
Top achievements
Rank 1
answered on 08 Dec 2017, 05:17 PM
Also, in my example image above I would expect the series name to be "Test".
0
Accepted
Hristo
Telerik team
answered on 11 Dec 2017, 02:04 PM
Hello Kyle,

Thank you for writing.

RadChartView does not provide means for directly attaching a click event to the label elements. You can achieve your task by handling the MouseClick event of the control and after checking for the mouse and the labels locations: 
public partial class RadForm1 : Telerik.WinControls.UI.RadForm
{
    public RadForm1()
    {
        InitializeComponent();
 
        BarSeries barSeries = new BarSeries("Performance", "RepresentativeName");
        barSeries.Name = "Series1";
        barSeries.LegendTitle = "Series1";
        barSeries.ShowLabels = true;
        barSeries.DataPoints.Add(new CategoricalDataPoint(177, "Harley"));
        this.radChartView1.Series.Add(barSeries);
 
        BarSeries barSeries2 = new BarSeries("Performance", "RepresentativeName");
        barSeries2.Name = "Series2";
        barSeries2.LegendTitle = "Series2";
        barSeries2.ShowLabels = true;
        barSeries2.DataPoints.Add(new CategoricalDataPoint(141, "White"));
        this.radChartView1.Series.Add(barSeries2);
 
        this.radChartView1.ShowLegend = true;
        this.radChartView1.MouseClick += RadChartView1_MouseClick;
    }
     
    private void RadChartView1_MouseClick(object sender, MouseEventArgs e)
    {
        foreach (ChartSeries series in this.radChartView1.Series)
        {
            foreach (BarPointElement pointElement in series.Children)
            {
                BarLabelElement labelElement = pointElement.Children[0] as BarLabelElement;
                Rectangle rect = CartesianRenderer.ToRectangle(labelElement.GetLayoutSlot());
                rect.Offset(this.radChartView1.ChartElement.View.Margin.Left, this.radChartView1.ChartElement.View.Margin.Top);
                if (rect.Contains(this.radChartView1.ChartElement.Wrapper.PointFromControl(e.Location)))
                {  
                    RadMessageBox.Show(series.Name);
                }
            }
        }
    }
}

I am also attaching a short video showing the result on my end.

I hope this helps. Let me know if you need further assistance.

Regards,
Hristo
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Kyle
Top achievements
Rank 1
answered on 11 Dec 2017, 02:05 PM
Perfect, thank you!
0
James
Top achievements
Rank 1
Veteran
answered on 29 Feb 2020, 04:55 PM

For Pie Charts:

 

foreach (Telerik.WinControls.UI.PieSeries objChartSeries in this.chartPieChart.Series)
{
    foreach (Telerik.WinControls.UI.PiePointElement objPiePointElement in objChartSeries.Children)
    {
        if (objPiePointElement.Children[0] is Telerik.WinControls.UI.PieLabelElement)
        {
            Telerik.WinControls.UI.PieLabelElement objLabelElement = objPiePointElement.Children[0] as Telerik.WinControls.UI.PieLabelElement;
            if (objLabelElement.Parent is Telerik.WinControls.UI.PiePointElement)
            {
                Telerik.WinControls.UI.PiePointElement objPointElement = (Telerik.WinControls.UI.PiePointElement)objLabelElement.Parent;
                if (objLabelElement.SmartRectangle.Contains(e.X, e.Y))
                {
                    Debug.WriteLine(String.Format("User clicked: {0} Label Text: ", objPiePointElement.LegendTitle, objLabelElement.Text));
                }
            }
        }
    }
}

Tags
ChartView
Asked by
Kyle
Top achievements
Rank 1
Answers by
Kyle
Top achievements
Rank 1
Hristo
Telerik team
James
Top achievements
Rank 1
Veteran
Share this question
or