WinForms Chart: Set minimum Y Axis display range

匿名 (未验证) 提交于 2019-12-03 00:58:01

问题:

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:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!