简体   繁体   中英

How do I force the chart X axis labels to intervals of 100? (C#, .NET)

I'm using a chart control in a C# .NET application (Visual Studio 2013). How do I force the X-axis grid lines and labels to multiples of 100? I've set every "Interval" property I can find to 100, but at runtime it always puts grid lines and labels at 198, 398, 598, etc. This is for a data set with 2048 points.

I'd prefer do to it in the designer, but I'll do it in code if I must.

I'm new to C#/.NET, so please let me know what crucial pieces of information I've omitted...

First set chart.ChartArea[0].AxisX.Minimum and chart.ChartArea[0].AxisX.Maximum . And then chart.ChartArea[0].AxisX.Interval = 100; and also adjust the IntervalOffset .

However, if Minimum is > 0: the grid will offset from the Minimum value. Which means the correct offset will be axis.IntervalOffset = axis.Interval - axis.Minimum;

But, if Minimum is < 0:

axis.IntervalOffset = axis.Interval - axis.Minimum

will be evaluate to something like (with Minimum = -4 as example) axis.IntervalOffset = 100 - (-4) = 104 which is problematic because if IntervalOffset > Interval the interval will start at IntervalOffset and skip any gridlines between Minimum and IntervalOffset . (With above example gridline at 0 will be skipped)

In another words the correct offset is: IntervalOffset = (axis.Interval - axis.Minimum) % axis.Interval .

Which gives the correct offset :

axis.IntervalOffset = (-axis.Minimum) % axis.Interval;

For instance:

var axis = chart.ChartAreas[0].AxisX;
var points = chart.Series[0].Points;

axis.Minimum = points.Min(p => p.XValue);
axis.Maximum = points.Max(p => p.XValue);

axis.Interval = 100;
axis.IntervalOffset = (-axis.Minimum) % axis.Interval;

Will give you a grid that intersects X = 0.

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