[英]Misplaced year label on mschart axis
I have a chart where the x-axis is composed of dates, with yearly intervals. 我有一个图表,其中x轴由日期组成,每隔一年。 The problem is, depending on the minimum date, the minimum label is or is not shown.
问题是,取决于最小日期,是否显示了最小标签。 Check below:
检查以下内容:
var s = new Series();
s.ChartType = SeriesChartType.Line;
var d = new DateTime(2013, 04, 01);
s.Points.AddXY(d.AddYears(-1), 3);
s.Points.AddXY(d, 3);
s.Points.AddXY(d.AddYears(1), 2);
s.Points.AddXY(d.AddYears(2), 1);
s.Points.AddXY(d.AddYears(3), 4);
chart1.Series.Clear();
chart1.Series.Add(s);
chart1.Series[0].XValueType = ChartValueType.DateTime;
chart1.ChartAreas[0].AxisX.LabelStyle.Format = "yyyy-MM-dd";
chart1.ChartAreas[0].AxisX.Interval = 2;
chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Years;
chart1.Series[0].XValueType = ChartValueType.DateTime;
DateTime minDate = new DateTime(2011, 01, 01);
DateTime maxDate = new DateTime(2022, 01, 01);
chart1.ChartAreas[0].AxisX.Minimum = minDate.ToOADate();
When running the code above, the minimum date has no label (the first label is 2012), however, if you use 2012 as the minimum year, it will be shown. 当运行上面的代码时,最小日期没有标签(第一个标签是2012),但是,如果您使用2012作为最小年份,则会显示该日期。 This only happens on a higher than 1 interval, and becomes worse the greater the value.
这仅在大于1的时间间隔上发生,并且值越大,变得越差。 I have no idea as to why this happens.
我不知道为什么会这样。 I have also tried setting the interval properties (minus offset) for the LabelStyle, but to no avail.
我也尝试过为LabelStyle设置时间间隔属性(减去偏移量),但无济于事。
PS: Sample code extracted form this answer , with some modifications Tested on .NET 4.5.1 and 3.5 PS: 从此答案中提取了示例代码,并进行了一些修改。在.NET 4.5.1和3.5中进行了测试
The first label to be shown is the one higher or equal to the minvalue which is a multiple of the interval. 要显示的第一个标签是大于或等于最小值的一个标签,最小值是间隔的倍数。 So, for an interval of 3: [2011,2012] ->2013;
因此,间隔为3:[2011,2012]-> 2013; [2014,2015]->2016.
[2014,2015] - > 2016。 This way the offset can be dynamically calculated with some simple math.
这样,可以使用一些简单的数学方法动态计算偏移量。
var offset = -(interval - (minYear % interval));
Although the problem is solved, it still makes no sense to me. 尽管问题已解决,但对我来说仍然没有意义。 Why do this on a date axis and not a numerical one?
为什么要在日期轴上而不是数字轴上呢? Sounds more like a bug.
听起来更像是个错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.