Winforms Chart不按升序/降序显示x轴标签

时间:2015-05-22 12:16:22

标签: c# .net winforms visual-studio charts

enter image description here

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

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

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

1 个答案:

答案 0 :(得分:1)

您的第一个问题可以回答:如果您不想在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不应出现任何类型的错误。