ACM Algorithm 463提供了三個簡單的功能,以產生具有輸出xminp,xmaxp和DIST用於在規模和在標尺刻度標記之間的距離的最大值和最小值良好軸秤,給出n
間隔的請求包括數據點xmin
和xmax
:
Scale1()
給出的線性標尺與大約n
間隔和dist
爲10倍1,2或5 的整數冪
Scale2()
給出了一個精確到n
間隔的線性標度(xminp和xmaxp之間的間隙往往比Scale1()
產生的間隙大)。
Scale3()
給出了一個對數尺度。
原始的1973年論文在線here,它提供了比上面鏈接的代碼更多的解釋。
代碼位於Fortran中,但它只是一組算術計算,因此解釋並轉換爲其他語言非常簡單。我自己並沒有編寫任何PHP,但它看起來很像C,所以你可能需要通過f2c運行代碼開始,它應該給你一些接近於在PHP中運行的東西。
還有更復雜的功能可以提供更漂亮的比例(例如gnuplot
中的那些),但Scale1()
可能會以最少的代碼爲您完成工作。
(這個答案建立在我的回答上一個問題Graph axis calibration 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";
}
}
沒有人之間?至少有一些提示? – michi 2013-02-17 20:47:12
是否有一定數量的標籤要顯示在每張圖片上? – Kyle 2013-02-19 17:37:59
@凱爾:沒有固定數量的標籤,它必須符合數值。考慮到300px的寬度,有一種自然限制。 – michi 2013-02-19 19:03:39