Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
560
how to get the datapoint value in Line, Splinearea, doughnut charts
posted

Hi All,

I am using DataItemMouseLeftButtonDown event for capturing the click on the data point. My code as follows,

private void Chart_DataItemMouseLeftButtonDown(object sender, Infragistics.Silverlight.Chart.DataItemMouseEventArgs e)
{
DataPointTemplate shape = e.Element as DataPointTemplate;
//Check to see if cast was successful
if (shape != null)
{
//Try to cast the DataPointTemplate object to DataPoint
DataPoint point = shape.DataPoint;
}
}

This code works fine for Bar charts and Column charts. But in Line chart, the Shape returned is Ellipse, and in Doughnut chart it is Path. So the conversion to Datapointtemplate fails. I am unable to get the data point value, where I clicked. Kindly help me on this. Thanks.

Parents
  • 30692
    Suggested Answer
    Offline posted

    For the charts that don't use a datapointtemplate, you can still find information about which data point was clicked on by examining the dictionary that has been set on the Tag property of the shape. Its a Dictionary<Type,object> and if you lookup by typeof(DataPoint), you should find the datapoint object, or if you lookup by typeof(int) you should find the datapoint index. Unfortunately, things are a bit more complicated with a line chart, as each line in the chart only stores one of the data points that made it up in its tag. I've put together some code here that may help you compensate for this, if you want to give it a go. Charts that only encode one datapoint in a shape, should be more simple.

    Examine this handler, and let me know if you have any questions:

    private void theChart_DataItemMouseLeftButtonDown(object sender, Infragistics.Silverlight.Chart.DataItemMouseEventArgs e)

            {

                //dealing with a line chart

                Line l = e.Element as Line;

                //need to transform the clicked point from the

                //chart control coordinates to the coordinates on

                //which the line is plotted (an inner canvas)

                Canvas plottingPane = l.Parent as Canvas;

                if (plottingPane != null && l != null)

                {

                    //transform the clicked point to a point relative to the canvas the

                    //line is drawn on.

                    Point pointInChart = theChart.TransformToVisual(plottingPane).Transform(e.Position);

                    if (l.Tag != null && l.Tag is Dictionary<Type, object>)

                    {

                        //get the point index of the line. the line is really encompassing two points

                        //however.

                        int pointIndex = (int)(l.Tag as Dictionary<Type, object>)[typeof(int)];

     

                        double distToStartOfLine = Math.Pow((l.X1 - pointInChart.X), 2) +

                                                   Math.Pow((l.Y1 - pointInChart.Y), 2);

                        double distToEndOfLine = Math.Pow((l.X2 - pointInChart.X), 2) +

                                                 Math.Pow((l.Y2 - pointInChart.Y), 2);

     

                        //is the clicked point closer to the start of the line or the end of the line?

                        if (distToStartOfLine > distToEndOfLine)

                        {

                            pointIndex += 1;

                        }

     

                        //show the label from the datapoint that was clicked.

                        MessageBox.Show(theChart.Series[0].DataPoints[pointIndex].Label);

                    }

                }

            }

    -Graham

Reply Children