27

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

Paul
  • 2,773
  • 7
  • 41
  • 96

4 Answers4

53

chart.ChartAreas[0].RecalculateAxesScale();

Anton Kedrov
  • 1,767
  • 2
  • 13
  • 20
  • 4
    I also was trying to set `AxisY.Maximum = Double.NaN` (and same for AxisY2), but it's not working for me. – Anton Kedrov Mar 05 '13 at 15:18
  • This answer worked for me. I was enabling/disabling different Series (all series are initially disabled and controlled from a list) and noticed that only the first Series enabled would set the maximum for the y-axis. – Doc Dec 03 '15 at 11:48
14

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.

Molomby
  • 5,859
  • 2
  • 34
  • 27
  • 2
    I thought and tried the same thing but it didn't work. Anton Kedrov's answer works – yclkvnc May 29 '13 at 16:11
  • @yclkvnc, can you post some version info? I think this solution once worked but it sounds like most people are having better luck with Anton's answer these days. – Molomby Aug 02 '17 at 00:47
  • sorry, I don't even remember using .NET chart 5 years ago :) – yclkvnc Aug 02 '17 at 05:41
6

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
ehsanpro
  • 61
  • 1
  • 2
6

First, initialize this:

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

Whenever data point is added, do this please.

pinChart.ChartAreas[0].RecalculateAxesScale();
Lin
  • 497
  • 5
  • 15