I have a Winforms chart in which I have temperature readings arriving and displaying every second. I like the way the chart works automatically handling the display of the values, but I want to change one simple thing.
I want to increase the minimum displayed y axis range, so it displays a range of 20. At the moment it only displays around 5. I have tried a few things:
//(when new data arrives...) //Does not work, I think because by default, Size is always NaN? if (chart1.ChartAreas[0].AxisY.ScaleView.Size < 20) { chart1.ChartAreas[0].AxisY.ScaleView.Size = 20; }
None of these work either:
chart1.ChartAreas[0].AxisY.ScaleView.SmallScrollMinSize = 20; chart1.ChartAreas[0].AxisY.ScaleView.SmallScrollSize = 20; chart1.ChartAreas[0].AxisY.ScaleView.MinSize = 20; chart1.ChartAreas[0].AxisY.Minimum //doesn't seem to have any effect chart1.ChartAreas[0].AxisY.Maximum //doesn't seem to have any effect
I'm sure I've missed something simple. I hope I have anyway.
The 'minimum display range' is not something built-in in the MSChart control.
But you can easily fake it:
Add a dummy Series
which contains only two points to make sure the display range will not go below the range of their y-values..:
int rangeMin = -10; int rangeMax = 20; sDummy = chart.Series.Add("dummy"); sDummy.Color = Color.Transparent; sDummy.IsVisibleInLegend = false; sDummy.ChartType = SeriesChartType.Point; sDummy.Points.AddXY(0, rangeMin + 1); sDummy.Points.AddXY(0, rangeMax - 1);
Style your y-axis as you like:
Axis ay = chart.ChartAreas[0].AxisY; ay.MajorGrid.Interval = 5;
And add one or more data Series
:
sData = chart.Series.Add("data"); sData.LegendText = "Temperature"; sData.ChartType = SeriesChartType.Line;
Now as you add data points with a larger range of values the y-axis will grow its display range to accommodate them. And if you remove the larger points it will shrink back, but not below the range needed for the dummy series..:

Note that since the Chart
automatically adds some slack I reduce the range on both sides by 1
; with other Intervals
etc other numbers are needed..
The code to remove the larger values, btw:
var toRemove = sData.Points.Cast<DataPoint>() .Where(x => x.YValues[0] >= rangeMax).ToList(); foreach (var dp in toRemove) sData.Points.Remove(dp);