ACM 算法 463提供了三个简单的函数来生成良好的轴刻度,输出 xminp、xmaxp 和 dist 用于刻度上的最小值和最大值以及刻度上刻度线之间的距离,给定n
包含数据点xmin
和的间隔请求xmax
:
Scale1()
给出一个具有近似n
间隔的线性标度,dist
是 10 乘以 1、2 或 5 的整数幂.
Scale2()
给出具有精确n
间隔的线性标度(xminp 和 xmaxp 之间的差距往往大于由 产生的差距Scale1()
).
Scale3()
给出一个对数刻度。
1973 年的原始论文在这里在线,它提供了比上面链接的代码更多的解释。
该代码在 Fortran 中,但它只是一组算术计算,因此解释和转换为其他语言非常简单。我自己没有写过任何 PHP,但它看起来很像 C,所以你可能想从通过f2c运行代码开始,这应该会给你一些接近 PHP 中可运行的东西。
有更复杂的函数可以提供更漂亮的比例(例如 中的那些gnuplot
),但Scale1()
可能会用最少的代码为您完成这项工作。
(这个答案建立在我对上一个问题Graph axiscalibration in C++的回答之上)
(编辑——我找到了Scale1()
我在 Perl 中所做的实现):
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 > $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 < 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 < $_sqr; $i++) {
if ($b < $sqr[$i]) last;
}
# the interval size is computed
$dist = $vInt[$i] * 10^$nal;
$fm1 = $xMin / $dist;
$m1 = int($fm1);
if ($fm1 < 0) $m1--;
if (abs(($m1 + 1.0) - $fm1) < $del) $m1++;
# the new minimum and maximum limits are found
$xMinp = $dist * $m1;
$fm2 = $xMax / $dist;
$m2 = $fm2 + 1;
if ($fm2 < -1) $m2--;
if (abs ($fm2 + 1 - $m2) < $del) $m2--;
$xMaxp = $dist * $m2;
# adjust limits to account for round-off if necessary
if ($xMinp > $xMin) $xMinp = $xMin;
if ($xMaxp < $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 < $_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";
}
}