找到图表中两个系列之间的最大距离

时间:2014-06-20 16:02:49

标签: c# charts distance mschart series

我有两个系列的图表。现在我想找到给定间隔内沿x轴的聊天之间的最大距离。为了解决这个问题,在x = 50的图像中计算给定x点上的干扰就足够了。

distance between two datapoints

我有以下代码:

public void MaxSpacing(object chart, int series1, int series2) 
{
    Chart tmpChart = (Chart)chart;
    double distance = 0;
    int positon = 0;
    for (int i = 0; i < tmpChart.Series[series1].Points.Count(); i++)
    {
        if ((Math.Abs(tmpChart.Series[series1].Points[i].YValues[0] - tmpChart.Series[series2].Points[i].YValues[0])) > distance)
        {
            distance = tmpChart.Series[series1].Points[i].YValues[0] - tmpChart.Series[series2].Points[i].YValues[0];
        }
}

关于此代码的问题是,它使用两个系列的数据点。如果series1和series2中的点数/间隔不同,则计算不起作用。所以我正在寻找给定X值的Y值来计算距离。

2 个答案:

答案 0 :(得分:1)

如果两个系列的点数不同,则假定您想在点之间进行某种插值。一个简单的线性插值应该适用于足够多的点,因此整个算法看起来像这样(在伪代码中):

double distance = 0;
Series series1 = tmpChart.Series[series1];
Series series2 = tmpChart.Series[series2];
Series seriesToEnumerate = series1.Points.Count() >= series2.Points.Count() ? series1 : series2;

for (int i = 0; i < series1.Count(); ++i)
{
    DataPoint point1 = series1.Points[i];
    DataPoint point2 = series2.Points[i];

    if (point1.X == point2.X)
    {
        distance = Math.Abs(point1.Y - point2.Y) // if greater than previous distance
    }
    else
    {
        // find two points in series2 whose X values surround point1.X, call them point3 and point4

        // Interpolate between point3 and point4 to find the y value at the x of point1
        double slope = (point4.Y - point3.Y) / (point4.X - point3.X);
        double intercept = point4.Y - slope * point4.X;
        double y2 = slope * point1.X + intercept;

        distance = Math.Abs(point1.Y - y2); // if this is greater than previous distance
    }
}

这是一个简单的算法示例。您将要清理它,进行一些错误检查,使其更有效等等。

答案 1 :(得分:0)

如果x值不相等,则增加较小的值。 (这可能不是最有效的方式;它只是解释原理)

public void MaxSpacing(object chart, int series1, int series2)
{
    Chart tmpChart = (Chart)chart;
    double distance = 0;
    int position = 0;
    for (int i = 0; i < tmpChart.Series[series1].Points.Count(); i++) {
        if ((Math.Abs(tmpChart.Series[series1].Points[i].YValues[0] - tmpChart.Series[series2].Points[i].YValues[0])) > distance) {
            distance = tmpChart.Series[series1].Points[i].YValues[0] - tmpChart.Series[series2].Points[i].YValues[0];
        }
    }

    int len1 = tmpChart.Series[series1].Points.Count(), len2 = tmpChart.Series[series2].Points.Count();
    for (int i1 = 0, i2 = 0; i1 < len1 && i2 < len2;) {
        var x1 = tmpChart.Series[series1].Points[i1].XValue;
        var x2 = tmpChart.Series[series2].Points[i2].XValue;
        if (x1 < x2) {
            i1++;
        } else if (x2 < x1) {
            i2++;
        } else {
            double d = Math.Abs(tmpChart.Series[series1].Points[i1].YValues[0] - tmpChart.Series[series2].Points[i2].YValues[0]);
            if (d > distance) {
                distance = d;
                position = i1; //I'm guessing here
            }
            i1++;
            i2++;
        }
    }

}