Winforms 图表不按 ascending/descending 顺序显示 x 轴标签

Winforms Chart not displaying x-axis labels in ascending/descending order

.net 桌面应用程序接收来自微控制器的响应,并按时间间隔将数据绘制到图形中。如果您查看红圈 x 轴标签,您将无法按顺序找到它们。 例如。 -20.0 应该在 -20.4 之前。 问题是,如果您查看代码:

   chart1.Series[0].Points.AddXY(rLabData[0], rLabData[1]);

'AddXY' 根据 msdn 文档:将具有指定 X 值和 Y 值的 DataPoint 对象添加到集合的末尾。 这就是问题所在,我不希望将数据点添加到上一个结果的末尾,而是添加到 scale 中定义的 X 轴的最大和最小值范围内 如果输入点在同一实例中给出,它工作正常,但在定期间隔的基础上,winform 中的图表不会显示所需的结果。

你的第一个问题可以回答:如果你不希望新的DataPoints添加Points集合的末尾,你需要插入它们在正确的位置。

这是一个将 DataPoint(yourXValue, yourYValue) 插入 Series S 的示例:

    // we try to find the DataPoint right after our X-Value:
    DataPoint dp = S.Points.FirstOrDefault(p => p.XValue > yourXValue);
    if (dp != null)
    {
        // got it, so we insert before it..
        int index = S.Points.IndexOf(dp);
        if (index >= 0) S.Points.InsertXY(index, yourXValue, yourYValue);
    }
    // no, so we add to the end
    else S.Points.AddXY(yourXValue, yourYValue);

不幸的是,我怀疑你的 Label 问题真的是由添加引起的..

  • 您的图表看起来像 ChartType.Point。对于这种类型,DataPoints 的顺序不显示,与其他类型相反,如 Line..

  • 并且 Labels 不应该与任何类型乱序,除非您添加(错误)CustomLabels..?