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 successfulif (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.
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
If you have use cases for more simply obtaining the clicked datapoint on this type of chart you can make a feature request here: https://ko.infragistics.com/community/ideas
private void XamWebChart_DataItemMouseLeftButtonDown(object sender, Infragistics.Silverlight.Chart.DataItemMouseEventArgs e) { DataPointTemplate shape = e.Element as DataPointTemplate; if (shape != null) { DataPoint point = shape.DataPoint; MessageBox.Show(point.Value.ToString()); } }
This work for you?
How i can do the same for the column type chart. casting it to shape is returning a null.
Excellent Graham. It solves my problem. Thanks a lot.
for comparison, here is the handler for a doughnut chart:
Shape s = e.Element as Shape;
if (s != null && s.Tag != null && s.Tag is Dictionary<Type, object>)
Dictionary<Type, object> dict = (Dictionary<Type, object>)s.Tag;
MessageBox.Show(((DataPoint)dict[typeof(DataPoint)]).Label);