We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on : November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to get a datapoint on mouse click

Thread ID:

Created:

Updated:

Platform:

Replies:

119355 Jun 10,2015 09:59 AM UTC Jun 18,2015 10:26 AM UTC WPF 5
loading
Tags: SfChart
Senthil Ramanathan
Asked On June 10, 2015 09:59 AM UTC

[SF version is 13.1]
I have a Fast Line Series in a chart with a category axis as Xaxis populated with Datetime values mapped to a Numerical axis as Yaxis. 
When I click on the chart , I want to get the corresponding DateTime,Numeric (X,Y) values..?

How can this be done..?

Thanks. 

Suresh S [Syncfusion]
Replied On June 11, 2015 01:04 PM UTC

Hi Senthil,

Thanks for update.

We have analyzed your query and achieved your requirement for getting point info as shown in the below code snippet. Also, we have prepared a sample based on this, please download from below location,

CodeSnippet:

var chart = sender as SfChart;

FrameworkElement element = e.OriginalSource as FrameworkElement;

 

            if (element != null && element.Tag is ChartSegment)

            {

                FastLineSegment segment = element.Tag as FastLineSegment;

                var series = segment.Series as FastLineSeries;

                double x, y, stackedValue;

 

                segment.Series.FindNearestChartPoint((Point)e.GetPosition(sfchart), out x, out y, out stackedValue);

 

                MessageBox.Show("X:" + x);

           }

Sample: TrackBallBehavior

Please let us know in case of any query.

Regards,

Suresh S


Senthil Ramanathan
Replied On June 17, 2015 05:21 AM UTC

Not sure it's working correctly. I modified the sample slightly to show the dates on click and the very first point in the series I am able to click seems to off by a few days. 

Could you please check..?
private void sfchart_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var chart = sender as SfChart;
            FrameworkElement element = e.OriginalSource as FrameworkElement;
 
            if (element != null && element.Tag is ChartSegment)
            {
                FastLineSegment segment = element.Tag as FastLineSegment;
                var series = segment.Series as FastLineSeries;
                double x, y, stackedValue;
 
                segment.Series.FindNearestChartPoint((Point)e.GetPosition(sfchart), out x, out y, out stackedValue);
 
                //MessageBox.Show("X:" + x);
 
                var items = segment.Item as List<object>;
                if (items != null && items.First() is Model && items.Count > x)
                {
                    var key = ((Model)items[(int)x]).Year;
                    MessageBox.Show(key + ":" + y + ":" + stackedValue);
                }
           }
        }

Suresh S [Syncfusion]
Replied On June 17, 2015 01:10 PM UTC

Hi Senthil,

Thanks for update.

We have analyzed your query and achieved your requirement for getting data point as shown in below code snippet. Also, we have modified the sample based on this, please download from below location.

Code Snippet:

private void sfchart_MouseDown(object sender, MouseButtonEventArgs e)

        {

            var chart = sender as SfChart;

            FrameworkElement element = e.OriginalSource as FrameworkElement;

 

            if (element != null && element.Tag is ChartSegment)

            {

                FastLineSegment segment = element.Tag as FastLineSegment;

                var series = segment.Series as FastLineSeries;

                double x, y, stackedValue;

                segment.Series.FindNearestChartPoint((Point)e.GetPosition(sfchart), out x, out y, out stackedValue);

                Point dataPoint = e.GetPosition(sfchart);

                var data = series.GetDataPointIndex(dataPoint.X, dataPoint.Y);

               

                if (data == -1) return;

 

                var items = segment.Item as List<object>;

                if (items != null && items.First() is Model && items.Count > data)

                {

                    var key = ((Model)items[(int)data]).Year;

                    var valueY = ((Model)items[(int)data]).India;

                    MessageBox.Show(key + ":" + valueY + ":" + stackedValue);

                }

           }

        }

Sample: TrackBallBehavior

Please let us know in case of any query.

Regards,

Suresh S


Senthil Ramanathan
Replied On June 18, 2015 03:24 AM UTC

That solved the offset problem. Thanks. 

Suresh S [Syncfusion]
Replied On June 18, 2015 10:26 AM UTC

Hi Senthil,

Thanks for update.
We are glad to know the issue is resolved.
Please let us know in case of any query.

Regards.
Suresh S

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;