简体   繁体   English

GetValueAtPosition 与 WPF 工具包图表的使用

[英]Use of GetValueAtPosition with WPF ToolKit Chart

I'm trying to get the X-coordinate position of the mouse on a Chart.我正在尝试在图表上获取鼠标的 X 坐标 position。 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.原点的 X 值为 0,但 GetValueAtPosition 正在返回,例如 15,并且所有返回的 x 值都比应有的值大 15。 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.我的解决方案是让鼠标 position 相对于系列而不是图表。 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.它并不完美(在 1366 x 720 的分辨率下相差一个或 2 个像素),但当四舍五入到 2 个位置时,它对我来说已经足够了。

In my xaml I gave the series a name (“Series”, as unoriginal as that is).在我的 xaml 中,我给这个系列起了一个名字(“系列”,虽然不是原创)。 I also have a TextBlock that's bound to the string Pos , so I can see the position of the cursor.我还有一个绑定到字符串Pos的 TextBlock,所以我可以看到 cursor 的 position。 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.我已将系列的ItemsSource绑定到List<KeyValuePair<Double, Double>> Points最后,不要忘记 y 轴相对于屏幕坐标翻转(y 轴随着您向上移动而增加,但屏幕坐标增加当你向下移动时),这就是为什么那里有一个1.0 – yAxisRange

Xaml: 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)})";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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