Search code examples
c#chartsdundas

C# Dundas Charts Auto-Scale Y Axis with no values over 100%


I am using Dundas Charts in Visual Studio using C#.

My charts are displaying percentage values, if I do not use a maximum y axis value the chart will automatically scale the axis - however, sometimes it will display values over 100% (even though none of the values actually exceed 100%).

How can I allow the axis to change in size depending on chart values, but never allow it to exceed 100%?


Solution

  • For anyonewho is interested, I decided to programmatically set the maximum value for the Y Axis for each chart.

    The following code is used to get the max value from the given array and return 100 or the max value rounded up to the next multiple of 10:

        int GetYAxisMaxValue(decimal[] yValues)
        {
            var max = yValues.Max();
            var output = 0m;
    
            if (yValues.Max() % 10 == 0) output = max;
            else output = max + (10 - max % 10);
    
            if (output > 100) return 100;
            else return Convert.ToInt32(output);
        }
    

    This is then used to create the chart, setting:

            chart.ChartAreas["Default"].AxisY.Maximum = GetYAxisMaxValue(...);
    

    Hope this helps.