选择 Silverlight Toolkit 图表中最近的点

发布于 2024-11-02 05:25:19 字数 191 浏览 4 评论 0原文

我有一个 LineSeries 图表。通过series.IsSelectionEnabled = true;,当我将鼠标移到这些点上时,我可以选择该节点。但是,当鼠标未完全位于该点上方而是接近该点(上方或下方)时,我该怎么办?谢谢。

附: 还有一件事。当鼠标悬停在列上时,如何更改列的颜色,以便用户可以知道他/她要选择哪一列。

I have a LineSeries chart. By series.IsSelectionEnabled = true; when I move the mouse over the points, I can select that node. But how can I do it when the mouse is not exactly over the point but when it's near it (above or under)? Thanks.

PS:
One more thing. How can I change the color of the column when the mouse is over it so the user can tell which one of the columns he/she is going to select.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老娘不死你永远是小三 2024-11-09 05:25:19

我使用单个 LineSeries 创建了图表示例。您可以单击绘图上的任意位置,然后将选择最近的点。

XAML(将 ItemsSource 属性和其他属性更改为您的属性):

    <Charting:Chart MouseLeftButtonDown="Chart_MouseLeftButtonDown">
        <Charting:Chart.Series>
            <Charting:LineSeries IsSelectionEnabled="True" ItemsSource="..." ... />
        </Charting:Chart.Series>
    </Charting:Chart>

隐藏代码:

    private void Chart_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var chart = sender as Chart;
        //In my example the line series is the first item of the chart series
        var line = (LineSeries)chart.Series[0];

        //Find the nearest point on the LineSeries
        var newPoint = e.GetPosition(line);
        var selectIndex = this.FindNearestPointIndex(line.Points, newPoint);

        if (selectIndex != null)
        {
            //Select a real item from the items source
            var source = line.ItemsSource as IList;
            line.SelectedItem = source[selectIndex.Value];
        }
    }

    private int? FindNearestPointIndex(PointCollection points, Point newPoint)
    {
        if (points == null || !points.Any())
            return null;

        //c^2 = a^2+b^2
        Func<Point, Point, double> getLength = (p1, p2) => Math.Sqrt(Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2));

        //Create the collection of points with more information
        var items = points.Select((p,i) => new { Point = p, Length = getLength(p, newPoint), Index = i });
        var minLength = items.Min(item => item.Length);

        //Uncomment if it is necessary to have some kind of sensitive area
        //if (minLength > 50)
        //    return null;

        //The index of the point with min distance to the new point
        return items.First(item => item.Length == minLength).Index;
    }

正如我所说,即使您在距离任何图表点很远的地方单击,该图表也会选择最近的点。如果这不是预期的行为,您可以取消注释这些行并以像素为单位设置任何数字:

//Uncomment if it is necessary to have some kind of sensitive area
if (minLength > 50)
    return null;

我已经写了注释,但如果有不清楚的地方,您可以询问,我会解释。

I have created the example of the chart with the single LineSeries. You can click anywhere at the plot and the nearest point will be selected.

XAML (Change the ItemsSource property and other properties to yours):

    <Charting:Chart MouseLeftButtonDown="Chart_MouseLeftButtonDown">
        <Charting:Chart.Series>
            <Charting:LineSeries IsSelectionEnabled="True" ItemsSource="..." ... />
        </Charting:Chart.Series>
    </Charting:Chart>

Code-behind:

    private void Chart_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var chart = sender as Chart;
        //In my example the line series is the first item of the chart series
        var line = (LineSeries)chart.Series[0];

        //Find the nearest point on the LineSeries
        var newPoint = e.GetPosition(line);
        var selectIndex = this.FindNearestPointIndex(line.Points, newPoint);

        if (selectIndex != null)
        {
            //Select a real item from the items source
            var source = line.ItemsSource as IList;
            line.SelectedItem = source[selectIndex.Value];
        }
    }

    private int? FindNearestPointIndex(PointCollection points, Point newPoint)
    {
        if (points == null || !points.Any())
            return null;

        //c^2 = a^2+b^2
        Func<Point, Point, double> getLength = (p1, p2) => Math.Sqrt(Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2));

        //Create the collection of points with more information
        var items = points.Select((p,i) => new { Point = p, Length = getLength(p, newPoint), Index = i });
        var minLength = items.Min(item => item.Length);

        //Uncomment if it is necessary to have some kind of sensitive area
        //if (minLength > 50)
        //    return null;

        //The index of the point with min distance to the new point
        return items.First(item => item.Length == minLength).Index;
    }

As I said this chart will select the nearest point even if you click at a great distance away from any chart point. If it isn't intended behavior, you can uncomment these lines and set any number in pixels:

//Uncomment if it is necessary to have some kind of sensitive area
if (minLength > 50)
    return null;

I have written comments, but if something isn't clear you can ask and I will explain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文