简体   繁体   中英

Misplaced year label on mschart axis

I have a chart where the x-axis is composed of dates, with yearly intervals. 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. This only happens on a higher than 1 interval, and becomes worse the greater the value. 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.

此图显示发生了什么

PS: Sample code extracted form this answer , with some modifications Tested on .NET 4.5.1 and 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; [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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM