How do I force a chart to auto adjust Y Axis Maximum?

荒凉一梦 提交于 2019-12-03 23:50:47

问题


I have a .NET chart which I am populating at runtime

The chart appears within a report. For each band in my report, I clear all the series and add them back in using code

            Series s = new Series();
            s.Font = new Font("Verdana", 8f);                

            int i = 0;
            foreach (var month in line.Months)
            {
                DataPoint p = new DataPoint();

                p.XValue = i;
                p.YValues = new Double[] { month.LineValue ?? 0 };
                s.Points.Add(p);

                i++;
            }

When I populate the chart the second time, the Y Axis maximum stays on 2000, i.e. is not being recalculated

How do I force recalculation?

I have ScaleBreakStyle enabled on the Y Axis

If I try to set IsLogarithmic to true on the Y Axis I get a X instead of a chart

I am using Visual Studio 2010 with the System.Windows.forms.DataVisualization.Charting.Chart

Paul


回答1:


chart.ChartAreas[0].RecalculateAxesScale();




回答2:


The docs say the default for the Axis.Maximum property is NaN (not a number), so you should be able to re-enable the automatic scaling functionality by setting it back to that value.

Something like this...

chart.ChartAreas[0].AxisY.Maximum = Double.NaN;

UPDATE / CORRECTION

Anton's answer is correct; you should be using:

ChartArea.RecalculateAxesScale();

According to the RecalculateAxesScale() docs:

... it is sometimes necessary to recalculate chart area properties so that the chart is rendered correctly. For example, if an axis range is changed, the labels for that axis must be recalculated.

Apparently, it's has been available since .NET 4.0.




回答3:


you need to run this sequence:

AxisY.Maximum = Double.NaN; // sets the Maximum to NaN
AxisY.Minimum = Double.NaN; // sets the Minimum to NaN
enter code herechart.ChartAreas[0].RecalculateAxesScale(); // recalculates the Maximum and Minimum values, since they are set to NaN



回答4:


First, initialize this:

chart.ChartAreas[0].AxisY.IsStartedFromZero = false;

Whenever data point is added, do this please.

pinChart.ChartAreas[0].RecalculateAxesScale();


来源:https://stackoverflow.com/questions/8788801/how-do-i-force-a-chart-to-auto-adjust-y-axis-maximum

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