Reputation: 115
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.
Unfortunately, using ChartControl.ChartAreas[0].AxisY.Maximum returns the double NaN because I am using autoscale.
Is it possible to get the maximum displayed value of the Y-Axis when using an autoscaling axis?
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.
Upvotes: 3
Views: 21056
Reputation: 1878
I needed to set these before my RecalculateAxesScale call because I had set them while displaying another data set on the same chart control.
chart.ChartAreas[0].AxisY.ScaleView.Size = double.NaN;
chart.ChartAreas[0].AxisY2.ScaleView.Size = double.NaN;
EDIT: To clarify, I was reusing the same chart control to display charts of different data sets depending upon user selection. One of the selections set the ScaleView.Size to something other than the default (NaN). So I needed to set it back to default to allow RecalculateAxesScale to work like it should.
Upvotes: 1
Reputation: 18031
Call chart.ChartAreas[0].RecalculateAxesScale();
Then chart1.ChartAreas[0].AxisY.Maximum
and Minimum
will be set properly.
Upvotes: 6
Reputation: 9407
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
}
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
}
Upvotes: 3