Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get maximum Y-axis value from autoscaling chart control

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.

like image 417
SRTie4k Avatar asked Jan 25 '26 04:01

SRTie4k


2 Answers

Call chart.ChartAreas[0].RecalculateAxesScale();

Then chart1.ChartAreas[0].AxisY.Maximum and Minimum will be set properly.

like image 191
Larry Avatar answered Jan 26 '26 16:01

Larry


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
}
like image 24
Nick Gotch Avatar answered Jan 26 '26 17:01

Nick Gotch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!