1

.net 4 vs2010 winform c#

使用添加了一些点

chart1.Series[0].Points.AddXY(x,y);

当我点击图表时,光标可能不会落在任何点上。有没有返回最近点的函数?(忘记y,只是x距离。)或者我必须编写自己的二进制搜索函数?

4

4 回答 4

2
private void Chart_MouseClick(object sender, MouseButtonEventArgs e)
{
    LineSeries line = (LineSeries)mychart.Series[0];
    Point point = e.GetPosition(line);
    Int32? selectIndex = FindNearestPointIndex(line.Points, point);

    // ...
}

private Int32? FindNearestPointIndex(PointCollection points, Point point)
{
    if ((points == null || (points.Count == 0))
        return null;

    Func<Point, Point, Double> getLength = (p1, p2) => Math.Sqrt(Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2)); // C^2 = A^2 + B^2
    List<Points> results = points.Select((p,i) => new { Point = p, Length = getLength(p, point), Index = i }).ToList();
    Int32 minLength = results.Min(i => i.Length);

    return results.First(i => (i.Length == minLength)).Index;
}
于 2013-01-22T09:40:36.500 回答
1

要在一组无序点中找到最近的点,您必须遍历它们并跟踪最小距离。这具有 O(n) 的时间复杂度。

您可以通过在更有条理的数据结构(例如R-tree)中维护点来显着改善这一点。如果您不想实现自己的,可以使用第三方库。许多数据库已经支持空间索引的 R 树。

如果您真的只想搜索具有最近 X 坐标的点,可以通过将点存储在排序集合(例如 a SortedList<TKey, TValue>)中并执行二进制搜索(SortedList<TKey, TValue>.IndexOfKey已经实现)来进一步简化。

于 2013-01-22T19:58:55.360 回答
0
/*My Fuzzy Binary Search*/
private int FindNearestId(System.Windows.Forms.DataVisualization.Charting.DataPointCollection p, uint ClickedX)
{
    int ret = 0;
    int low = 0;
    int high = p.Count - 1;
    bool bLoop = true;

    while (bLoop)
    {
        ret = (low + high) / 2;
        switch (FindNearestId_Match(p, ClickedX, ret))
        {
        case 0:
            high = ret+1;
            break;
        case 1:
            bLoop = false;
            break;
        case 2:
            low = ret-1;
            break;
        }
    }

    return ret+1;
}

private int FindNearestId_Match(System.Windows.Forms.DataVisualization.Charting.DataPointCollection p, uint ClickedX, int id)
{
    uint id0 = Convert.ToUInt32(p[id].XValue);
    uint id1 = Convert.ToUInt32(p[id+1].XValue);

    if ( (id0 <= ClickedX) && (ClickedX < id1) )
    {
        return 1;
    }
    else if ((id0 < ClickedX) && (ClickedX > id1))
    {
        return 2;
    }
    else
    {
        return 0;
    }
}
于 2013-01-22T12:04:53.813 回答
0

灵魂可以更清楚。(如上所述,您应该使用日志复杂性来访问项目)

双 x 值解决方案:

double FindNearestPointYValueInSeries( System::Windows::Forms::DataVisualization::Charting::Series ^pxSeries, double dSearchedPosition )
{
    int i_min = 0;
    int i_max = pxSeries->Points->Count - 1;
    int i_mean = 0;
    double d ;

    if ( i_max < 0 ) // not defined - minimum one point required
        return Double::NaN;

    while ( i_min <= i_max )
    {
        i_mean = (i_max + i_min ) / 2; // index of compared value in series
        d = pxSeries->Points[ i_mean ]->XValue; // compared value 

        if ( d > dSearchedPosition ) // greater - search in right part
            i_max = i_mean - 1;
        else if ( d < dSearchedPosition ) // lower - search in left part
            i_min = i_mean + 1;
        else // equal ?
            return d;
    }

    // delta is dSearchedPosition - pxSeries->Points[ i_mean ]->YValues[0]

    // get Y value ( on index 0 ) 
    return pxSeries->Points[ i_mean ]->YValues[0];
}
于 2014-11-26T16:26:07.640 回答