Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8208793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:29:49+00:00 2026-06-07T09:29:49+00:00

I’m graphing some statistics which can be percentages, currency values or plain numbers. I

  • 0

I’m graphing some statistics which can be percentages, currency values or plain numbers.

I need to set the maximum value of the graph control’s axis to a nice, round number just a bit above the maximum value in the data set. (The graph control’s default value is not what I want).

Two things to note:

  1. The value I set for the axis maximum should be minimum 5% above the dataset’s maximum value (the less above this the better).

  2. I have 4 horizontal lines above the 0 Y-axis; so ideally the Y-axis maximum should divide nicely by 4.

Sample data might be:

200%, 100%, 100%, 100%, 75%, 50%, 9%

In this case, 220% would be acceptable as the maximum value.

$3500161, $1825223, $1671232, $110112

In this case, $3680000 might be ok. Or $3700000 I suppose.

Can anyone suggest a nice formula for doing this? I might need to adjust settings, like the 5% margin might be changed to 10%, or I might need to change the 4 horizontal lines to 5.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T09:29:50+00:00Added an answer on June 7, 2026 at 9:29 am

    Here is the code I use to create graph axes.

    /// <summary>
    /// Axis scales a min/max value appropriately for the purpose of graphs
    /// <remarks>Code taken and modified from http://peltiertech.com/WordPress/calculate-nice-axis-scales-in-excel-vba/</remarks>
    /// </summary>
    public struct Axis 
    {
        public readonly float min_value;
        public readonly float max_value;
        public readonly float major_step;
        public readonly float minor_step;
        public readonly int major_count;
        public readonly int minor_count;
    
        /// <summary>
        /// Initialize Axis from range of values. 
        /// </summary>
        /// <param name="x_min">Low end of range to be included</param>
        /// <param name="x_max">High end of range to be included</param>
        public Axis(float x_min, float x_max)
        {
            //Check if the max and min are the same
            if(x_min==x_max)
            {
                x_max*=1.01f;
                x_min/=1.01f;
            }
            //Check if dMax is bigger than dMin - swap them if not
            if(x_max<x_min)
            {
                float temp = x_min;
                x_min = x_max;
                x_max = temp;
            }
    
            //Make dMax a little bigger and dMin a little smaller (by 1% of their difference)
            float delta=(x_max-x_min)/2;
            float  x_mid=(x_max+x_min)/2;
    
            x_max=x_mid+1.01f*delta;
            x_min=x_mid-1.01f*delta;
    
            //What if they are both 0?
            if(x_max==0&&x_min==0)
            {
                x_max=1;
            }
    
            //This bit rounds the maximum and minimum values to reasonable values
            //to chart.  If not done, the axis numbers will look very silly
            //Find the range of values covered
            double pwr=Math.Log(x_max-x_min)/Math.Log(10);
            double scl=Math.Pow(10, pwr-Math.Floor(pwr));
            //Find the scaling factor
            if(scl>0&&scl<=2.5)
            {
                major_step=0.2f;
                minor_step=0.05f;
            }
            else if(scl>2.5&&scl<5)
            {
                major_step=0.5f;
                minor_step=0.1f;
            }
            else if(scl>5&&scl<7.5)
            {
                major_step=1f;
                minor_step=0.2f;
            }
            else
            {
                major_step=2f;
                minor_step=0.5f;
            }
            this.major_step=(float)(Math.Pow(10, Math.Floor(pwr))*major_step);
            this.minor_step=(float)(Math.Pow(10, Math.Floor(pwr))*minor_step);
            this.major_count=(int)Math.Ceiling((x_max-x_min)/major_step);
            this.minor_count=(int)Math.Ceiling((x_max-x_min)/minor_step);
            int i_1=(int)Math.Floor(x_min/major_step);
            int i_2=(int)Math.Ceiling(x_max/major_step);
            this.min_value=i_1*major_step;
            this.max_value=i_2*major_step;
        }
        public float[] MajorRange
        {
            get
            {
                float[] res=new float[major_count+1];
                for(int i=0; i<res.Length; i++)
                {
                    res[i]=min_value+major_step*i;
                }
                return res;
            }
        }
        public float[] MinorRange
        {
            get
            {
                float[] res=new float[minor_count+1];
                for(int i=0; i<res.Length; i++)
                {
                    res[i]=min_value+minor_step*i;
                }
                return res;
            }
        }
    }
    

    You can the nice max_value and min_value as calculated from the initialized for Axis given the mathematical min. max. values in x_min and x_max.

    Example:

    1. new Axis(0,3500161) calculates max_value = 4000000.0
    2. new Axis(0,1825223) calculates max_value = 2000000.0
    3. new Axis(0,1671232) calculates max_value = 1800000.0
    4. new Axis(0, 110112) calculates max_value = 120000.0
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.