<p>Does anyone have a decent algorithm for calculating axis minima and maxima? </p>

<p>When creating a chart for a given set of data items, I'd like to be able to give the algorithm: </p>

<ul>
<li>the maximum (y) value in the set </li>
<li>the minimum (y) value in the set </li>
<li>the number of tick marks to appear on the axis </li>
<li>an optional value that <strong>must</strong> appear as a tick (e.g. zero when showing +ve and -ve values)</li>
</ul>

<p>The algorithm should return</p>

<ul>
<li>the largest axis value </li>
<li>the smallest axis value (although that could be inferred from the largest, the interval size and the number of ticks)</li>
<li>the interval size </li>
</ul>

<p>The ticks should be at a regular interval should be of a "reasonable" size (e.g. 1, 3, 5, possibly even 2.5, but not any more sig figs). </p>

<p>The presence of the optional value will skew this, but without that value the largest item should appear between the top two tick marks, the lowest value between the bottom two. </p>

<p>This is a language-agnostic question, but if there's a C#/.NET library around, that would be smashing ;) </p>

[{'answer_id': 51860, 'author': 'Tom', 'author_id': 3715, 'author_profile': 'https://Stackoverflow.com/users/3715', 'pm_score': 1, 'selected': True, 'text': '<p>I\'ve been using the jQuery <a href="http://code.google.com/p/flot/" rel="nofollow noreferrer">flot</a> graph library.  It\'s open source and does axis/tick generation quite well.  I\'d suggest looking at it\'s code and pinching some ideas from there.</p>\n'}, {'answer_id': 152575, 'author': 'Andrew Edgecombe', 'author_id': 11694, 'author_profile': 'https://Stackoverflow.com/users/11694', 'pm_score': 0, 'selected': False, 'text': "<p>I can recommend the following:</p>\n\n<ul>\n<li>Set a visually appealing minimum number of major lines. This will depend on the nature of the data that you're presenting and the size of the plot you're doing, but 7 is a pretty good number</li>\n<li>Choose the exponent and the multiplier based on a progression of 1, 2, 5, 10, etc. that will give you at least the minimum number of major lines. (ie. (max-min)/(scale x 10^exponent) >= minimum_tick_marks)</li>\n<li>Find the minimum integer multiple of your exponent and multiplier that fits within your range. This will be the first major tick. The rest of the ticks are derived from this.</li>\n</ul>\n\n<p>This was used for an application that allowed arbitrary scaling of data an seemed to work well.</p>\n"}, {'answer_id': 152647, 'author': 'Bart Read', 'author_id': 17786, 'author_profile': 'https://Stackoverflow.com/users/17786', 'pm_score': 2, 'selected': False, 'text': '<p>OK, here\'s what I came up with for one of our applications. Note that it doesn\'t deal with the "optional value" scenario you mention, since our optional value is always 0, but it shouldn\'t be hard for you to modify.</p>\n\n<p>Data is continually added to the series so we just keep the range of y values up to date by inspecting each data point as its added; this is very inexpensive and easy to keep track of. Equal minimum and maximum values are special cased: a spacing of 0 indicates that no markers should be drawn.</p>\n\n<p>This solution isn\'t dissimilar to Andrew\'s suggestion above, except that it deals, in a slightly kludgy way with some arbitrary fractions of the exponent multiplier.</p>\n\n<p>Lastly, this sample is in C#. Hope it helps.</p>\n\n<pre><code>    private float GetYMarkerSpacing()\n    {\n        YValueRange range   = m_ScrollableCanvas.\n                    TimelineCanvas.DataModel.CurrentYRange;\n        if ( range.RealMinimum == range.RealMaximum )\n        {\n            return 0;\n        }\n\n        float   absolute    = Math.Max(\n                    Math.Abs( range.RealMinimum ),\n                    Math.Abs( range.RealMaximum ) ),\n            spacing     = 0;\n        for ( int power = 0; power &lt; 39; ++power )\n        {\n            float   temp    = ( float ) Math.Pow( 10, power );\n            if ( temp &lt;= absolute )\n            {\n                spacing = temp;\n            }\n            else if ( temp / 2 &lt;= absolute )\n            {\n                spacing = temp / 2;\n                break;\n            }\n            else if ( temp / 2.5 &lt;= absolute )\n            {\n                spacing = temp / 2.5F;\n                break;\n            }\n            else if ( temp / 4 &lt;= absolute )\n            {\n                spacing = temp / 4;\n                break;\n            }\n            else if ( temp / 5 &lt;= absolute )\n            {\n                spacing = temp / 5;\n                break;\n            }\n            else\n            {\n                break;\n            }\n        }\n\n        return spacing;\n    }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/51680', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2902/']