ChartControl - X 轴上的重复值

ChartControl - Repeated Values on X Axis

我正在尝试在 C# windows 表单应用程序中绘制一个简单的图表。 绘制此图表的数据将被输入 运行 次。 问题是它没有根据 X 轴值在图表上绘制唯一点。例如对于两个点 (5,1) 和 (5,3),它在 X 轴上两次显示 '5' 值。

我尝试寻找类似问题的解决方案,但 none 帮了忙。 这是我的代码的样子: (为了便于参考,我特意将声明移到了点击事件处理程序中)

    private void button1_Click(object sender, EventArgs e)
    {
        Series series3 = new Series
        {
            Name = "Series3",
            Color = System.Drawing.Color.Red,
            IsVisibleInLegend = false,
            IsXValueIndexed = true,
            ChartType = SeriesChartType.Line
        };

        double startPosition = 2;
        double endPosition = 4;

        double startForceLow = 1;
        double startForceHigh = 3;
        double endForceLow = 5;
        double endForceHigh = 7;

        chart1.Series.Clear();
        this.chart1.Series.Add(series3);

        double[] xPoints = { startPosition, startPosition, endPosition, endPosition, startPosition };
        double[] yPoints = { startForceLow, startForceHigh, endForceHigh, endForceLow, startForceLow };

        //Adding individual points too does not work
        //series3.Points.AddXY(startPosition, startForceLow);
        //series3.Points.AddXY(startPosition, startForceHigh);
        //series3.Points.AddXY(endPosition, endForceHigh);
        //series3.Points.AddXY(endPosition, endForceLow);
        //series3.Points.AddXY(startPosition, startForceLow);

        chart1.Series["Series3"].XValueType = ChartValueType.Double;
        chart1.Series["Series3"].YValueType = ChartValueType.Double;

        chart1.Series["Series3"].Points.DataBindXY(xPoints, yPoints);

        chart1.Invalidate();
    }

图表如下:

原因是您告诉图表忽略X-Values并使用他们的 indices 用于放置它们:

IsXValueIndexed = true

删除该行或将 属性 设置为其 默认值 false

结果:

解释见here on MSDN

True if the indices of data points that belong to the series will be used for X-values; false if they will not. The default value is false.

在极少数情况下这很有用,但您的情况不是其中之一。事实上 Line 图表几乎从来没有;然而,一些图表很好地利用了它..:[=​​18=]

All data points in a series use sequential indices, and if this property is true then data points will be plotted sequentially, regardless of their associated X-values. This means that there will be no "missing" data points.
...
...
This is useful when you do not want to have missing data points for intervals that you know you will not have data for, such as weekends.

Important

If you are displaying multiple series and at least one series uses indexed X-values, then all series must be aligned—that is, have the same number of data points—and the corresponding points must have the same X-values.