How to draw minor gridlines between major gridlines? - c#

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
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
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

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.

Related

White Space in MS chart

I have a simple chart with 12 data points. The problem is that it shows a little white area before starting the trend lines.
Here is the code
for (int i = 0; i < 12; i++)
{
chart1.Series[0].Points.AddXY(DataTable.Rows[i].ItemArray[0], plotDataTable.Rows[i].ItemArray[1]);
chart1.Series[1].Points.AddXY(DataTable.Rows[i].ItemArray[0], plotDataTable.Rows[i].ItemArray[2]);
chart1.Series[2].Points.AddXY(DataTable.Rows[i].ItemArray[0], DataTable.Rows[i].ItemArray[3]);
chart1.Series[3].Points.AddXY(DataTable.Rows[i].ItemArray[0], DataTable.Rows[i].ItemArray[4]);
}
First column of DataTable is string and the other four are floats.
You need to set the AxisX.Minimum to a suitable value.
Usually this would be 0 or the x-value of the first DataPoint.
But the way you add the values this will not work.
You are adding the DataPoints in a rather unfortunte way, which sometimes is ok, but more often than not it will create all sorts of problems.
The recommended way is to add the x-values as numbers, or DateTimes, which internally will be converted to doubles.
But you add strings. This looks ok but the x-values contain neither those strings not anything else but 0s. Threfore you can't use them to set the range or tooltips or zoom ranges or to calculate stuff..
But if you want to you can still get the result you want by setting the minimum to 1:
ChartArea ca = yourChart.ChartAreas[0];
ca.AxisX.Minimum = 1;
I have added my x-values as string, too, but they look like numbers.
But the recommended way would be to convert your values to numbers so you can use them for all sorts of things..
A few notes:
This conversion is done by the chart, if at all possible for the y-values but not for the x-values! Maybe because a chart without numeric y-values makes no sense at all while sometimes x-values simply do not contain meaningful numeric data, like names, IDs, zip codes etc..
Don't let the visuals fool you: The strings are only copied into the axis labels; otherwise they are lost! (You should check this with the debugger!!)
You may notice that the number of Label changes in the screenshot. The number is calculated from the Interval of the x-axis. By default it is calculated automatically (Interval=double.NaN)to fit in a reasonable number. You can set it to any distance you like. Normaly it refers to the axis unit but in this case to the number of points. Set it to 2 to get one Label for every 2nd point; set it to 0.5 to get 2 Labels per DataPoint..
With real numbers (or DataTimes) as x-values you can also set a type for the interval like seconds or days..
By default, the ChartArea's IsStartedFromZero property is set true. This setting will force the chart to always start from zero. Try setting it to false:
chart1.ChartAreas[0].AxisX.IsStartedFromZero = false;
chart1.ChartAreas[0].AxisY.IsStartedFromZero = false;

zedgraph custom auto scale - but with minimum (of Min), and minimum of Max)

I use Zedgraph in order to plot, and I use AxisChange(); in order to auto-scale my graph.
My problem : I'd like to always see 0 to 10 on Y scale even After I go down or UP (auto-scale) , but I'd like always keep this value showed.
Here is an example of what I have now :
Here a screen shot of what I'd like to change (I want always see 0 to 10) even I have no value...
EDIT
Sorry I wasn't clear enough...
My goal is to have a graph that display AT least 0 to 10 in the Yaxis (always). but if the values goes down (0 to -100) or up (10 to 100), the graph auto scale the Y axis and show more values.
For example even I've the value oscillating around "100" during 2 mins, I want to see my scale 0 to 110.
I don't know if it's possible or not?
In fact as soon as I put :
myPane.YAxis.Scale.Min = 0;
myPane.YAxis.Scale.Max = 10;
The auto scale stop working ...
Thanks for your help
You can manually set 'Y' axis as follows,
myPane.YAxis.Scale.Min = 0;
myPane.YAxis.Scale.Max = 10;
myPane.AxisChange();
zedGraphControl1.Invalidate();
and if you want to reverse the scale set the 'IsReverse' property to 'True'
myPane.YAxis.Scale.IsReverse = true;

How to display logarithmic x-axis minor label?

I set x-axis as logarithmic scale, but it only displays the major labels, exp : 1E000, 1E001, 1E002, etc... Now i also want to display the minor values, for example : 2E000, 3E000, 4E000,..., 2E001, 3E001, .....
it should look like the graph below :
It seems a simple question but i can't find the way to solve this problem. Can anyone help me ?
Thanks in advance.
This question is similar to this one.
It took me a long time to solve this one, but:
If you if you set the MinorGrid Interval to 1, then you get the traditional Logarithmic grid marks, ten per decade:
aChart.ChartAreas[0].AxisX.IsLogarithmic = true;
aChart.ChartAreas[0].AxisX.MinorGrid.Interval = 1;
aChart.ChartAreas[0].AxisX.MinorGrid.Enabled = true;
As far as I know, the only way to do this is by setting the interval property of the axis.LabelStyle e.g.:
this.chart1.ChartAreas[0].AxisX.LabelStyle.Interval = 0.1;
For example, setting 0.1 in a logaritmic scale, the labels show will be 10^0.1, 10^0.2, 10^0.3 ...
If you need something more particular, I suggest you to create the labels manually using the property axis.CustomLabels, even if is not so intuitive...

MS Chart Control Zoom MinSize issue

I'm implementing a scatter plot using the MS Chart Control .NET 3.5, WinForms, C#. My x-axis data is DateTime and noticed I couldn't zoom in smaller than a resolution of 1 day, despite setting the ScaleView as follows:
chart1.ChartAreas["MyChart"].AxisX.ScaleView.MinSize = 4;
chart1.ChartAreas["MyChart"].AxisX.ScaleView.MinSizeType = DateTimeIntervalType.Hours;
Has anyone else had this issue? Any ideas?
Figured this out... perhaps I didn't RTFM close enough, but it wasn't obvious from the interactive demo.
Set
chart1.ChartAreas["MyChart"].CursorX.Interval = 0;
and then it allowed me to zoom along the x-axis just fine.
Works Great !
Very handy and mandatory if you want to have smooth Zooming behavior.
Didn't stumble upon it, though I did RTFM :-)
However, if you handle doubles or floats instead of integer based types (such as hours or days), setting the interval to Zero may be a little bit extreme : While zooming, you will end up having overly precise labels such as 2,907343253253235
A good combination is to use these two properties :
chartArea1.AxisY.ScaleView.MinSize = 0;
chartArea1.CursorY.Interval = 0.001;
this way you can zoom as much as you want, while still controlling precision at a reasonable level

Stacked Chart using MSChart

I am attempting to create a stacked chart using the relatively new Microsoft Chart Controls. I am sure that I am missing something obvious but a bit of help will go a long way. The below code creates a chart with two columns. I'd like the columns to be stacked on top of each other. Further, I'd like the total of the two to be displayed on the chart. Any help would be much appreciated.
Series activeSeries = new Series("Active");
activeSeries.ChartType = SeriesChartType.StackedColumn;
activeSeries.BorderWidth = 3;
activeSeries.ShadowOffset = 2;
activeSeries.Points.AddY(3000);
LaptopChart.Series.Add(activeSeries);
Series inactiveSeries = new Series("Inactive");
inactiveSeries.ChartType = SeriesChartType.StackedColumn;
inactiveSeries.BorderWidth = 3;
inactiveSeries.ShadowOffset = 2;
activeSeries.Points.AddY(987);
LaptopChart.Series.Add(inactiveSeries);
Bone head move when creating the second series I added the inactive points to the active series. Sometimes no matter how often you walk through your own code it takes a second set of eyes to find things. Sorry for wasting anyone's time looking at this. The second reference to activeSeries.Points.AddY(987); should be inactiveSeries.Points.AddY(987);

Categories

Resources