如何从自动缩放图表控件中获得最大的Y轴值 [英] How to get maximum Y-axis value from autoscaling chart control
问题描述
所以我在.NET中使用图表控制,使用内部自动缩放算法为Y轴。这一切的伟大工程,但我现在想获得Y轴的最大显示值保存为一个双重用于进一步的格式。
So I am using a Chart control in .NET which is using the internal autoscale algorithm for the Y-Axis. This all works great, but I am now trying to get the maximum displayed value of the Y-Axis to store as a double to use for further formatting.
不幸的是,使用ChartControl.ChartAreas [0] .AxisY.Maximum返回double NaN的,因为我使用的自动缩放。
Unfortunately, using ChartControl.ChartAreas[0].AxisY.Maximum returns the double NaN because I am using autoscale.
是否有可能使用自动缩放轴时,你得到的Y轴的最大显示值?
Is it possible to get the maximum displayed value of the Y-Axis when using an autoscaling axis?
修改 在我执行操作的顺序是建立基本格式为条形图,使用AddXY(加入数据点),最后试图让显示的Y轴的最大值。使用ChartControl.ChartAreas [0] .AxisY.Maximum仍然即使加入大量的数据点返回NaN。
EDIT The order in which I am performing operations is to establish basic formatting for the bar chart, adding datapoints using AddXY(), and then finally trying to get the maximum value of the displayed Y-Axis. Using ChartControl.ChartAreas[0].AxisY.Maximum still returns NaN even after adding numerous datapoints.
推荐答案
这不计算最大值,直至显示图表所以下面code显示楠:
It doesn't compute the max value until the chart is displayed so the following code displays NaN:
public Form1()
{
InitializeComponent();
this.chart1.Series.Clear();
this.chart1.Series.Add("My Data");
this.chart1.Series[0].Points.AddXY(1, 1);
this.chart1.Series[0].Points.AddXY(2, 2);
this.chart1.Series[0].Points.AddXY(3, 6);
MessageBox.Show(this.chart1.ChartAreas[0].AxisY.Maximum.ToString()); // returns NaN
}
但检查显示图表后,会给出正确的值:
But checking after the chart is displayed will give the correct value:
public Form1()
{
InitializeComponent();
this.chart1.Series.Clear();
this.chart1.Series.Add("My Data");
this.chart1.Series[0].Points.AddXY(1, 1);
this.chart1.Series[0].Points.AddXY(2, 2);
this.chart1.Series[0].Points.AddXY(3, 6);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(this.chart1.ChartAreas[0].AxisY.Maximum.ToString()); // returns 8
}
另外,您可以执行后立即设置你的数据的更新(但由于尚未显示在图表的形式构造这是不行的):
Alternatively you can perform an update right after you set your data (but this won't work in the form constructor because the chart isn't yet displayed):
private void button1_Click(object sender, EventArgs e)
{
this.chart1.Series.Clear();
this.chart1.Series.Add("My Data");
this.chart1.Series[0].Points.AddXY(1, 1);
this.chart1.Series[0].Points.AddXY(2, 2);
this.chart1.Series[0].Points.AddXY(3, 6);
this.chart1.Update();
MessageBox.Show(this.chart1.ChartAreas[0].AxisY.Maximum.ToString()); // returns 8
}
下面是另一种方式使用OnShown表事件,并做两个数据系列:
Here's another way to do it using the OnShown Form event and two data series:
public Form1()
{
InitializeComponent();
this.chart1.Series.Clear();
this.chart1.Series.Add("My Data");
this.chart1.Series[0].Points.AddXY(1, 1);
this.chart1.Series[0].Points.AddXY(2, 2);
this.chart1.Series[0].Points.AddXY(3, 6);
this.chart1.Series.Add("My Data2");
this.chart1.Series[1].Points.AddXY(1, 1);
this.chart1.Series[1].Points.AddXY(2, 9);
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
this.chart1.Update();
MessageBox.Show(this.chart1.ChartAreas[0].AxisY.Maximum.ToString()); // returns 10
}
这篇关于如何从自动缩放图表控件中获得最大的Y轴值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!