【问题标题】:Find the maximum distance between two series in a chart找出图表中两个系列之间的最大距离
【发布时间】:2014-06-20 16:02:49
【问题描述】:

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

我有以下代码:

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 值来计算距离。

【问题讨论】:

  • 听起来像线性插值,只是通过点连接线。你可以做更多花哨的抛物线(样条拟合),但很有可能线性插值就足够了。
  • 据我所知,您希望在给定两个 X 值的情况下找到 Y 值的最大差异。我认为这意味着您正在检查的两个点必须具有相同的 X 值。因此,比较每个系列中的点数,并遍历系列中点数最少的点。还要确保您比较的两个点具有相同的 X 值。 // 如果你想变得花哨,你可以使用回归技术从点数较少的系列中找到“缺失”的 X 值,然后使用回归中的函数生成 Y 值,然后进行比较。
  • 您是说要处理两个系列之间数据点数量不同的情况吗?如果需要,您想在两点之间进行插值,以得出一个与其他系列进行比较的值?
  • 我没有得到 "Points[i].YValues[0]" 一个点如何有多个 y 值?
  • @Dennis_E 在这种情况下,每个点 i 都有一个 y 值,并且有两个系列,所以你有两个可以减去的 y 值

标签: c# charts distance mschart series


【解决方案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
    }
}

这是一个简单的算法示例。你会想要清理它,做一些错误检查,让它更有效率,等等。

【讨论】:

  • 抱歉,您的示例在很多方面都不起作用(例如,您无法将 Charting.DataPoint 转换为 Drawing.Point point1 = series1.Points[i];
  • 我上面给出的代码是伪代码——不是简单的复制粘贴和编译。您需要将其转换为可编译的实际 c# 代码。 point1point2DataPoint 对象;我已经编辑了答案以使其更加清晰。
  • 好吧,它看起来更像 c# 而不是伪代码,这就是我写它的原因。
【解决方案2】:

如果 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++;
        }
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    • 2017-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多