3
votes

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.

1
You are probably talking about ChartAreas > Axes > Y axis > Scale category, Minimum and Maximum property.Hans Passant
ScaleView is about zooming and scrolling. Use chart1.ChartAreas[0].AxisY.Minimum and chart1.ChartAreas[0].AxisY.MaximumTaW
@TaW I forgot to mention that one - doesn't work. Just tried again just to b sure.n00dles
Of course it works. But you may have done it wrong or have funny expectations. Show the (real) code and describe the results you got. What I see doesn't compile. Also do not set the ScaleView unless you know what you are doing. Leave as much on Auto (NaN) as possible..TaW
I thought would be self-explanatory A common mistake ;-) - and then one wonders both why it doesn't exist and why nobody understands.. - If think you can fake what you aim for: Add two transparent dummy points instead of setting fixed Minima/Maxima. Give them those y-values, maybe 0 and 20 . Then when larger values get added the displayed range will grow and if you remove them it'l shrink again but never below the dummy range. You may want to create a dummy series for this purpose.TaW

1 Answers

5
votes

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

enter image description here

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