WinForms Chart: Set Minimum Y-Axis Display Range

I have a Winforms chart in which I have temperature readings popping up and showing every second. I like the way the chart works automatically, handling the display of values, but I want to change one simple thing.

I want to increase the minimum displayable range of the y-axis, so it displays a range of 20. At the moment, it only displays around 5. I've 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 functions work:

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 missed something simple. I hope I don't care.

+3


source to share


1 answer


The "Minimum Display Range" is not built into the MSChart control.

But you can easily fake it:

Add a dummy Series

that only contains two to make sure the display range is not below their y range ..:

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

      

Place the y-axis as you wish:

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, when you add data points with a larger range , the y-axis will grow in its display range to accommodate them. And if you delete , then the larger dots will shrink back, but not below the range required for the dummy series ..:

enter image description here

Note that since it Chart

automatically adds some weak points , I decrease the range on both sides by 1

; with others Intervals

, etc. need other numbers.

Code to remove large values, btw:

var toRemove = sData.Points.Cast<DataPoint>()
                    .Where(x => x.YValues[0] >= rangeMax).ToList();
foreach (var dp in toRemove) sData.Points.Remove(dp);

      

+4


source







All Articles