Mouse position -> data -> position are not the same values

2 posts, 0 answers
  1. Martin
    Martin avatar
    3 posts
    Member since:
    Jul 2013

    Posted 22 Jul 2013 Link to this post


    I have used your cross-hair example but trying to extend it and having issues

    I boiled it down to the code example below

    1) Get a mouse position
    2) Get the data representing that position
    3) For that data, get the mouse position

    how can these return different values?


                var plotAreaPanel = sender as ClipPanel;
                var mousePosition = e.GetPosition(plotAreaPanel);
                var dataAtMouse = _Chart.DefaultView.ChartArea.AxisX.ConvertPhysicalUnitsToData(mousePosition.X);
                var physicalAtMouse = _Chart.DefaultView.ChartArea.AxisX.ConvertDataUnitsToPhysical(dataAtMouse);

                if (mousePosition.X != physicalAtMouse)
                    Debug.Assert(false, "Should never happen");

  2. Petar Kirov
    Petar Kirov avatar
    425 posts

    Posted 24 Jul 2013 Link to this post

    Hi Martin,

    This is a known issue. It is logged here in our Public Issue Tracker. Presently you can can workaround it the following way: 
    var dataX = this.chart.DefaultView.ChartArea.AxisX.ConvertPhysicalUnitsToData(position.X);
    var dataY = this.chart.DefaultView.ChartArea.AxisY.ConvertPhysicalUnitsToData(position.Y);
    double xMin = _Chart.DefaultView.ChartArea.AxisX.ActualMinValue;
    double yMax = _Chart.DefaultView.ChartArea.AxisY.ActualMaxValue;
    var physicalX = this.chart.DefaultView.ChartArea.AxisX.ConvertDataUnitsToPhysical(dataX - xMin);
    var physicalY = this.chart.DefaultView.ChartArea.AxisY.ConvertDataUnitsToPhysical(yMax - dataY);

    Please note that, due to the nature of floating-point calculations,  physicalX/Y will be very close to poistion.X/Y but probably will not be equal. The error should be smaller than 10^-3. 

    On as side note, you may be interested in reviewing our newer charting solution - RadChartView (represented by the RadCartesianChart, RadPolarChart and RadPieChart classes). The new control was built from the ground-up to provide significantly better performance, easy to use API and support for implicit styles. Additionally RadCartesianChart provides a built-in customizable Crosshair behavior which you can see in this online demo. A sample project demonstrating the feature is also available here in our XAML SDK on GitHub.

    I hope you will find the RadChartView suitable for your project.

    Petar Kirov
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top