WinForms图表:设置最小Y轴显示范围 [英] WinForms Chart: Set minimum Y Axis display range

查看:74
本文介绍了WinForms图表:设置最小Y轴显示范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Winforms图表,其中每秒都有温度读数到达并显示。我喜欢图表自动处理值显示的方式,但是我想更改一件简单的事情

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.

我想增加最小显示的y轴范围,因此它显示的范围为20。目前,它仅显示5。我已经尝试了一些方法:

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.

推荐答案

最小显示范围不是MSChart控件中内置的。

The 'minimum display range' is not something built-in in the MSChart control.

但是您可以轻松地伪造它:

But you can easily fake it:

添加虚拟 系列仅包含两个点,以确保显示范围不会低于其y值范围。.:

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

根据需要设置y轴的样式:

Style your y-axis as you like:

Axis ay = chart.ChartAreas[0].AxisY;
ay.MajorGrid.Interval = 5;

并添加一个或多个数据 系列

sData = chart.Series.Add("data");
sData.LegendText = "Temperature";
sData.ChartType = SeriesChartType.Line;

现在,当您添加具有更大值范围的数据点y -axis将增加其显示范围以适应它们。而且,如果您删除较大的点,它将缩小,但不低于虚拟系列所需的范围。:

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

请注意,由于图表自动添加一些松弛,我将双方的范围都减小了 1

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

删除较大值的代码,btw:

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

这篇关于WinForms图表:设置最小Y轴显示范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆