Y axis: fixed minimum, variable maximum

2 posts, 0 answers
  1. Edward
    Edward avatar
    18 posts
    Member since:
    Oct 2010

    Posted 02 Dec 2010 Link to this post

    Hello,

    Just to share a solution for fixing the minimum value of the Y axis (in my case at 0) but allowing the maximum to vary according to the series values. As we know, when you set AutoRange = false then you have to set all of MinValue, MaxValue and Step manually.

    Basically you add a DataBinding event to the chart, and in the event handler you get the ItemsSource of the chart and find its maximum value. You then call the following routine, which is taken from the "nice numbers" algorithm here http://tinyurl.com/5gmk2c

    To be honest, we shouldn't really need to do this :-)  It should be possible to set MinValue and let RadChart work out MaxValue and Step using its own algorithm.

    private void SetYAxisToNiceNumbers(double fixedMinValue, double knownMaxValue,
                                      
    int maximumNumberOfTicks)
    {
        double range = GetNiceNumber(knownMaxValue - fixedMinValue, false);
        double step = GetNiceNumber(range / (maximumNumberOfTicks - 2), true);
        double axisMaxValue = Math.Ceiling(knownMaxValue / step) * step;
     
        // Add a step if knownMaxValue is too close to the edge of the graph
          // (i.e. is 90% or more of a step)
        if ((axisMaxValue - knownMaxValue) / step <= 0.1)
        {
            axisMaxValue += step;
        }
     
        AxisY axisY = chart.DefaultView.ChartArea.AxisY;
        axisY.AutoRange = false;
        axisY.MinValue = fixedMinValue;
        axisY.MaxValue = axisMaxValue;
        axisY.Step = step;
    }
     
    private double GetNiceNumber(double x, bool round)
    {
        double expt = Math.Floor(Math.Log10(x));
        double frac = x / Math.Pow(10, expt);
        double nice;
     
        if (round)
        {
            if (frac < 1.5) nice = 1.0;
            else if (frac < 3.0) nice = 2.0;
            else if (frac < 7.0) nice = 5.0;
            else nice = 10.0;
        }
        else
        {
            if (frac <= 1.0) nice = 1.0;
            else if (frac <= 2.0) nice = 2.0;
            else if (frac <= 5.0) nice = 5.0;
            else nice = 10.0;
        }
     
        return nice * Math.Pow(10, expt);
    }

    Regards

    Ed
  2. Evgeni "Zammy" Petrov
    Admin
    Evgeni "Zammy" Petrov avatar
    168 posts

    Posted 07 Dec 2010 Link to this post

    Hi Edward,

     Thank you for giving us a heads up on this. We will consider your suggestion.

    Best wishes,
    Evgeni "Zammy" Petrov
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
Back to Top