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
40
Get coordinates of the point in MouseButtonDown
posted

Hello! I'm evaluating xamDataChart for my company and have one question.

How to get 'real' coordinates of the point in MouseLeftButtonDown?

If I write something like

e.GetPosition(this.xmChart)

(as shown in sample project) I get double coordinates assosiated with current WindowRect. But I have DateTime X axis and want to get DateTime closest to the point I have clicked.

Is there a solution for my problem?

Thanks, Igor.

Parents
No Data
Reply
  • 30692
    Suggested Answer
    Offline posted

    This sample should show you the pieces you can use to put this together:

    The xaml:

     <UserControl.Resources>
            <local:TestData x:Key="data" />
        </UserControl.Resources>
        
        <Grid x:Name="LayoutRoot" Background="White">
            <igChart:XamDataChart x:Name="theChart" MouseLeftButtonDown="theChart_MouseLeftButtonDown">
                <igChart:XamDataChart.Axes>
                    <igChart:NumericYAxis x:Name="yAxis" />
                    <igChart:CategoryDateTimeXAxis 
                        x:Name="xAxis"
                        ItemsSource="{StaticResource data}"
                        DateTimeMemberPath="Date"
                        Label="{}{Date}"/>
                </igChart:XamDataChart.Axes>
                <igChart:XamDataChart.Series>
                    <igChart:LineSeries 
                        x:Name="series"
                        ItemsSource="{StaticResource data}"
                        XAxis="{Binding ElementName=xAxis}"
                        YAxis="{Binding ElementName=yAxis}"
                        ValueMemberPath="Value" />
                </igChart:XamDataChart.Series>
            </igChart:XamDataChart>
        </Grid>
    

    And the code behind:

    public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
            }
    
            private void theChart_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                var s = theChart.Series.FirstOrDefault();
                if (s == null)
                {
                    return;
                }
    
                var position = e.GetPosition(s);
    
                //we are only accounting for CategoryDateTimeXAxis in the following logic.
                if (!s.Chart.Axes.OfType<CategoryDateTimeXAxis>().Any())
                {
                    return;
                }
    
                var x = s.Chart.Axes.OfType<CategoryDateTimeXAxis>().First();
                var y = s.Chart.Axes.OfType<NumericYAxis>().First();
    
                var viewport = new Rect(0, 0, x.ActualWidth, y.ActualHeight);
                var window = s.Chart.WindowRect;
    
                var unscaledX = x.GetUnscaledValue(position.X, window, viewport);
                var unscaledY = y.GetUnscaledValue(position.Y, window, viewport);
    
                MessageBox.Show("x: " + new DateTime((long)unscaledX) + ", y:" + unscaledY);
            }
        }
    
        public class TestData
            : ObservableCollection<TestDataItem>
        {
            public TestData()
            {
                Add(new TestDataItem()
                {
                    Date = DateTime.Now.AddDays(-4),
                    Value = 1
                });
                Add(new TestDataItem()
                {
                    Date = DateTime.Now.AddDays(-3),
                    Value = 2
                });
                Add(new TestDataItem()
                {
                    Date = DateTime.Now.AddDays(-2),
                    Value = 3
                });
                Add(new TestDataItem()
                {
                    Date = DateTime.Now.AddDays(-1),
                    Value = 4
                });
            }
        }
    
        public class TestDataItem
        {
            public DateTime Date { get; set; }
            public double Value { get; set; }
        }
    

    Let me know if you have any questions. Hope this helps!

    -Graham

Children