This is a migrated thread and some comments may be shown as answers.

[Solved] Y axis: fixed minimum, variable maximum

1 Answer 199 Views
Chart
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Edward
Top achievements
Rank 1
Edward asked on 02 Dec 2010, 12:43 PM
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

1 Answer, 1 is accepted

Sort by
0
Evgeni "Zammy" Petrov
Telerik team
answered on 07 Dec 2010, 09:41 AM
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
Tags
Chart
Asked by
Edward
Top achievements
Rank 1
Answers by
Evgeni "Zammy" Petrov
Telerik team
Share this question
or