且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在图表中对齐索引系列

更新时间:2023-01-24 23:03:35

此例程首先收集所有 / code> $

This routine first collects all values in all Series in a collection of doubles.

然后它遍历所有系列和所有值,并插入缺少的空 DataPoints

Then it loops over all Series and over all values and inserts the missing empty DataPoints:

void AlignSeries(Chart chart)
{
    var allValues = chart.Series.SelectMany(s => s.Points)
                          .Select(x=>x.XValue).Distinct().ToList();
    foreach (Series series in chart.Series)
    {
        int px = 0;    //insertion index
        foreach(double d in allValues )
        {
            var p = series.Points.FirstOrDefault(x=> x.XValue == d);
            if (p == null)  // this value is missing
            {
                DataPoint dp = new DataPoint(d, double.NaN);
                dp.IsEmpty = true;
                series.Points.Insert(px, dp);
            }
            px++;
        }
    }
}

请注意, 。


  • 您的x值已正确设置,即它们被添加为 numbers DateTimes 。如果您将它们添加为 strings ,它们都是 0 ,索引是没有意义的。

  • that your x-values are correctly set, i.e. they were added as numbers or DateTimes. If you added them as strings they all are 0 and indexing makes no sense.

DataPoints 是以升序顺序添加的。这并不总是这样,特别是当绘制 LineCharts 时。

that the DataPoints were added in ascending order. This is not always the case, especially when plotting LineCharts. However indexing these makes no sense either.

另外请注意,您可以设置几个选项来处理通过设置系列中创建空数据点 de / library / system.windows.forms.datavisualization.charting.series.emptypointstyle(v = vs.110).aspxrel =nofollow> Series.EmptyPointStyle ,它源自 DataPointCustomProperties

Also note that you can set several options of how to treat Empty DataPoints in a Series by setting properties in the Series.EmptyPointStyle, which is derived from DataPointCustomProperties.

c> Color 像这样:

So you could set their Color like this:

 someSeries.EmptyPointStyle.Color = Color.Red;