简体   繁体   中英

Use of GetValueAtPosition with WPF ToolKit Chart

I'm trying to get the X-coordinate position of the mouse on a Chart. Looking at some other code on this site, I have

    private void Chart_MouseMove(object sender, MouseEventArgs e)
    {
       var chart = (Chart)sender;
        var xAxisRange = (IRangeAxis)chart.Axes[0];
        var mousePositionInPixels = e.GetPosition(chart);
        double mouseXPositionInChartUnits = (double)xAxisRange.GetValueAtPosition(new UnitValue(mousePositionInPixels.X, Unit.Pixels));
        TextBoxR0.Text = mouseXPositionInChartUnits.ToString(); //temp display textBox for debugging
        RaisePropertyChanged();
    }

The problem is that the values are shifted to the left by a constant. The X-value for the origin is 0, but GetValueAtPosition is returning, say 15, and all x values returned are 15 greater than they should be. If I expand the chart horizontally, the offset changes, becoming smaller. If I contract the chart horizontally, the offset becomes larger. I've done some extensive searching for an answer, and have looked for some member of Chart that may give me a left margin offset, but with no success.

I know this answer is late, but I had the same problem. My solution was to get the mouse position relative to the series instead of the chart. Its not perfect (it's off by a pixel or 2 at a resolution of 1366 x 720), but when rounded to 2 places it's good enough for me.

In my xaml I gave the series a name (“Series”, as unoriginal as that is). I also have a TextBlock that's bound to the string Pos , so I can see the position of the cursor. I've bound the ItemsSource of the series to List<KeyValuePair<Double, Double>> Points Lastly, don't forget that the y axis is flipped relative to screen coordinates (the y axis increases as you move up, but screen coordinates increase as you move down ), so that's why there's a 1.0 – yAxisRange in there.

Xaml:

<chartingToolkit:Chart Name="columnChart" MouseMove="Chart_MouseMove">
    <chartingToolkit:LineSeries Name="Series" ItemsSource="{Binding Points}" DependentValuePath="Value" IndependentValuePath="Key" />
</chartingToolkit:Chart>

Code:

private void Chart_MouseMove(object sender, MouseEventArgs e)
{
    var chart = (Chart)sender;
    var mousePositionInPixels = e.GetPosition(Series);

    var xAxisRange = (IRangeAxis)chart.ActualAxes[0];
    double mouseXPositionInChartUnits = (double)xAxisRange.GetValueAtPosition(new UnitValue(mousePositionInPixels.X, Unit.Pixels));

    var yAxisRange = (IRangeAxis)chart.ActualAxes[1];
    double mouseYPositionInChartUnits = 1.0 - (double)yAxisRange.GetValueAtPosition(new UnitValue(mousePositionInPixels.Y, Unit.Pixels));

    Pos = $"({Math.Round(mouseXPositionInChartUnits, 2)}, {Math.Round(mouseYPositionInChartUnits, 2)})";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM