Vladislav Vazhenin
Vladislav Vazhenin

Reputation: 349

How to draw minor gridlines between major gridlines?

I'm trying to draw minor gridlines in the center between major gridlines.

chart1.ChartAreas[0].AxisX.MajorGrid.Interval = 4;
chart1.ChartAreas[0].AxisX.MinorGrid.Enabled = true;
chart1.ChartAreas[0].AxisX.MinorGrid.Interval = 3;
chart1.ChartAreas[0].AxisX.MinorGrid.LineColor = Color.Cornsilk;
chart1.ChartAreas[0].AxisX.MinorGrid.LineDashStyle= ChartDashStyle.Solid;

I tried to set different values of chart1.ChartAreas[0].AxisX.MinorGrid.Interval but didn't get expected result.Here what I got so far. Arrow points to right located minor gridline Arrow points to right located minor gridline I tried to change chart1.ChartAreas[0].AxisX.MinorGrid.IntervalOffset property but it does not change anything. Has someone any suggestions? Thank you in advance.

EDIT Based on TaW's answer tried to set intervals

chart1.ChartAreas[0].AxisX.MajorGrid.Interval = 4;
chart1.ChartAreas[0].AxisX.MinorGrid.Interval = 2;

got enter image description here

EDIT 2

It was a good idea proposed by TaW, but since for me was not important LineDashStyle and custom labels are used, I decided to abandon minor lines and instead use major lines and custom labels, plotted for each second line. Issue resolved

enter image description here

Upvotes: 1

Views: 3597

Answers (1)

TaW
TaW

Reputation: 54433

If you want the MinorGrid lines centered between the MajorGrid their Interval ought to be half their value:

Axix ax = chart1.ChartAreas[0].AxisX;
ax.MajorGrid.Interval = 4;
ax.MinorGrid.Interval = ax.MajorGrid.Interval / 2;

If you want more MinorGrid lines the MajorGrid.Interval still should be divisible by MinorGrid.Interval.

In case you really want to set an Offset, both should have the same !

Since your X-Values are DateTimes, you also will want to control the IntervalTypes:

ax.MajorGrid.IntervalType = DateTimeIntervalType.Days;
ax.MinorGrid.IntervalType = DateTimeIntervalType.Days;

Note that, as usual, every other major line overwrites a minor one. If this is a problem you can make the Intervals the same and offset one by half an Interval; but normally it will not matter. enter image description here

Upvotes: 3

Related Questions