.net 如何强制图表自动调整 Y 轴最大值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8788801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I force a chart to auto adjust Y Axis Maximum?
提问by Paul
I have a .NET chart which I am populating at runtime
我有一个在运行时填充的 .NET 图表
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
当我第二次填充图表时,Y 轴最大值保持在 2000,即没有被重新计算
How do I force recalculation?
如何强制重新计算?
I have ScaleBreakStyle enabled on the Y Axis
我在 Y 轴上启用了 ScaleBreakStyle
If I try to set IsLogarithmic to true on the Y Axis I get a X instead of a chart
如果我尝试在 Y 轴上将 IsLogarithmic 设置为 true,则会得到 X 而不是图表
I am using Visual Studio 2010 with the System.Windows.forms.DataVisualization.Charting.Chart
我将 Visual Studio 2010 与 System.Windows.forms.DataVisualization.Charting.Chart 一起使用
Paul
保罗
回答by Anton Kedrov
chart.ChartAreas[0].RecalculateAxesScale();
chart.ChartAreas[0].RecalculateAxesScale();
回答by Molomby
The docssay the default for the Axis.Maximumproperty is NaN (not a number), so you should be able to re-enable the automatic scaling functionality by setting it back to that value.
文档说该Axis.Maximum属性的默认值是 NaN(不是数字),因此您应该能够通过将其设置回该值来重新启用自动缩放功能。
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.
显然,它从 .NET 4.0 开始就可用了。
回答by ehsanpro
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
回答by Lin
First, initialize this:
首先,初始化这个:
chart.ChartAreas[0].AxisY.IsStartedFromZero = false;
Whenever data point is added, do this please.
每当添加数据点时,请执行此操作。
pinChart.ChartAreas[0].RecalculateAxesScale();

