gpt4 book ai didi

c# - MS Chart 控件上的日期时间类型 X 轴不显示周末

转载 作者:行者123 更新时间:2023-11-30 21:47:12 27 4
gpt4 key购买 nike

我在 .Net 4 中使用 MS Chart 控件显示 4 个数据系列,X 轴设置为日期时间类型。

如果将时间间隔设置为天数,即使数据中不包含周末日期,图表也会包含周末。

除一个系列外,所有系列都具有相同的日期点,异常(exception)是从最后日期开始并持续多个工作日(不包括周末)的投影线。

如何从所有系列的图表中删除周末(或没有值的日期)的显示?

这是一个 winforms .Net 4.0 应用程序。

 foreach (var series in chart2.Series)
{
series.ChartType = SeriesChartType.Line;
series.BorderWidth = 5;
series.XValueType = ChartValueType.Date;
// I Tried this to remove weekends but it results in the graph showing a big red X
series.IsXValueIndexed = true;
}

使用代码编辑以重现我的问题:

        DateTime dt = DateTime.Now;
chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Days;

Series test1 = new Series("Test1");
test1.XValueType = ChartValueType.Date;
for (int i = 1; i < 7; i++)
{
//skip the weekend
if (dt.DayOfWeek.ToString() == "Saturday") dt = dt.AddDays(1);
if (dt.DayOfWeek.ToString() == "Sunday") dt = dt.AddDays(1);

test1.Points.AddXY(dt.Date, i);
dt = dt.AddDays(1);
i++;
}

chart1.Series.Add(test1);

Series test2 = new Series("Test2");
test1.XValueType = ChartValueType.Date;
for (int i = 1; i < 7; i++)
{
//skip the weekend
if (dt.DayOfWeek.ToString() == "Saturday") dt = dt.AddDays(1);
if (dt.DayOfWeek.ToString() == "Sunday") dt = dt.AddDays(1);

test2.Points.AddXY(dt.Date, i);
dt = dt.AddDays(1);
i++;
}

chart1.Series.Add(test2);

foreach (var series in chart1.Series)
{
series.ChartType = SeriesChartType.Line;
series.BorderWidth = 5;
series.IsXValueIndexed=true;
}
chart1.ChartAreas[0].AxisX.LabelStyle.Angle = 45;
chart1.ChartAreas[0].AxisX.LabelStyle.Interval = 1;

最佳答案

您正在将 x 值添加为 DateTime,这是您应该做的。但默认情况下,这意味着所有 x 值都是按比例定位的,正如人们通常希望的那样。

但是你的情况是一个异常(exception),甚至在 Series.IsXValueIndexed 属性的文档中也提到了这一点:

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.

For example, assume there are three (3) data points in a series having X-values of 1, 2 and 4. If this property was false, there would be a data point missing at the X-axis location labeled "3". However, if you set this property to true, the three data points will be plotted at points 1, 2, and 4, sequentially, and there will be no missing data point. The X-axis location labeled "3" will not appear on the chart.

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.

但是请注意:

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.

所以您使用此属性是正确的,但是,很可能您的系列没有完全对齐

要确保它们经常是一个,可以使用 Chart.DataManipulator.InsertEmptyPoints 重载之一。不幸的是,它们都使用固定的 Interval,而不是模板 Series

因此,您需要确保在 Series 中数据缺失的地方添加一个 Empty DataPoint。如果数据是数据绑定(bind)的,则必须在 DataSource 中执行此操作!

关于c# - MS Chart 控件上的日期时间类型 X 轴不显示周末,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38655802/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com