Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://www.netlib.org/toms/463" rel="nofollow noreferrer">ACM Algorithm 463</a> provides three simple functions to produce good axis scales with outputs xminp, xmaxp and dist for the minimum and maximum values on the scale and the distance between tick marks on the scale, given a request for <code>n</code> intervals that include the data points <code>xmin</code> and <code>xmax</code>:</p> <ol> <li><code>Scale1()</code> gives a linear scale with approximately <code>n</code> intervals and <code>dist</code> being an integer power of 10 times 1, 2 or 5.</li> <li><code>Scale2()</code> gives a linear scale with exactly <code>n</code> intervals (the gap between xminp and xmaxp tends to be larger than the gap produced by <code>Scale1()</code>).</li> <li><code>Scale3()</code> gives a logarithmic scale.</li> </ol> <p>The original 1973 paper is online <a href="http://lists.gnu.org/archive/html/octave-bug-tracker/2011-09/pdfJd5VVqNUGE.pdf" rel="nofollow noreferrer">here</a>, which provides more explanation than the code linked to above.</p> <p>The code is in Fortran but it is just a set of arithmetical calculations so it is very straightforward to interpret and convert into other languages. I haven't written any PHP myself, but it looks a lot like C so you might want to start by running the code through <a href="http://www.netlib.org/f2c/" rel="nofollow noreferrer">f2c</a> which should give you something close to runnable in PHP. </p> <p>There are more complicated functions that give prettier scales (e.g. the ones in <code>gnuplot</code>), but <code>Scale1()</code> would likely do the job for you with minimal code. </p> <p>(This answer builds on my answer to a previous question <a href="https://stackoverflow.com/questions/14948693/graph-axis-calibration-in-c/14949049#14949049">Graph axis calibration in C++</a>)</p> <p>(EDIT -- I've found an implementation of <code>Scale1()</code> that I did in Perl):</p> <pre><code>use strict; sub scale1 ($$$) { # from TOMS 463 # returns a suitable scale ($xMinp, $xMaxp, $dist), when called with # the minimum and maximum x values, and an approximate number of intervals # to divide into. $dist is the size of each interval that results. # @vInt is an array of acceptable values for $dist. # @sqr is an array of geometric means of adjacent values of @vInt, which # is used as break points to determine which @vInt value to use. # my ($xMin, $xMax, $n) = @_; @vInt = {1, 2, 5, 10}; @sqr = {1.414214, 3.162278, 7.071068 } if ($xMin &gt; $xMax) { my ($tmp) = $xMin; $xMin = $xMax; $xMax = $tmp; } my ($del) = 0.0002; # accounts for computer round-off my ($fn) = $n; # find approximate interval size $a my ($a) = ($xMax - $xMin) / $fn; my ($al) = log10($a); my ($nal) = int($al); if ($a &lt; 1) { $nal = $nal - 1; } # $a is scaled into a variable named $b, between 1 and 10 my ($b) = $a / 10^$nal; # the closest permissable value for $b is found) my ($i); for ($i = 0; $i &lt; $_sqr; $i++) { if ($b &lt; $sqr[$i]) last; } # the interval size is computed $dist = $vInt[$i] * 10^$nal; $fm1 = $xMin / $dist; $m1 = int($fm1); if ($fm1 &lt; 0) $m1--; if (abs(($m1 + 1.0) - $fm1) &lt; $del) $m1++; # the new minimum and maximum limits are found $xMinp = $dist * $m1; $fm2 = $xMax / $dist; $m2 = $fm2 + 1; if ($fm2 &lt; -1) $m2--; if (abs ($fm2 + 1 - $m2) &lt; $del) $m2--; $xMaxp = $dist * $m2; # adjust limits to account for round-off if necessary if ($xMinp &gt; $xMin) $xMinp = $xMin; if ($xMaxp &lt; $xMax) $xMaxp = $xMax; return ($xMinp, $xMaxp, $dist); } sub scale1_Test { $par = (-3.1, 11.1, 5, 5.2, 10.1, 5, -12000, -100, 9); print "xMin\txMax\tn\txMinp\txMaxp,dist\n"; for ($i = 0; $i &lt; $_par/3; $i++) { ($xMinp, $xMaxp, $dist) = scale1($par[3*$i+0], $par[3*$i+1], $par[3*$i+2]); print "$par[3*$i+0]\t$par[3*$i+1]\t$par[3*$i+2]\t$xMinp\t$xMaxp,$dist\n"; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload