我想基于php中网站的点击次数生成热图。
我制作了一个如下所示的数组,并将归一化的点击计数定在0,1之间(1是具有最高点击计数的链接):
0 => [
'count' => 290
'normalCount' => 1 //normalized count between 0,1
'link' => 'page/252'
'color' => 'rgb(255, 255, 0)'
]
1 => [
'count' => 277
'normalCount' => 0.95501730103806 //normalized count between 0,1
'link' => '/page/255'
'color' => 'rgb(255, 243.52941176471, 0)'
]
]
2 => [
'count' => 200
'normalCount' => 0.68858131487889
'link' => '/fa/page/253'
'color' => 'rgb(255, 175.58823529412, 0)'
]
]
我在this question中使用了算法,并通过array的normalCount索引来获取每个链接的颜色。
但是它没有生成正确的颜色,例如,链接“ page / 252”具有normalCount 1,我希望颜色是红色(最暖),但现在是黄色。
下面是我生成颜色的功能:
$value = $myArray[$i]['normalCount']
$ratio = $value;
if ($min > 0 || $max < 1) {
if ($value < $min) {
$ratio = 1;
} else if ($value > $max) {
$ratio = 0;
} else {
$range = $min - $max;
$ratio = ($value - $max) / $range;
}
}
$hue = ($ratio * 1.2) / 3.60;
$rgb = $this->ColorHSLToRGB($hue, 1, .5);
$r = round($rgb['r'], 0);
$g = round($rgb['g'], 0);
$b = round($rgb['b'], 0);
return "rgb($r,$g,$b)";
答案 0 :(得分:1)
我使用this link解决了我的问题。
这是我的功能
$color = [
[0, 0, 255, '0.0f'], // Blue.
[0, 255, 255, '0.25f'], // Cyan.
[0, 128, 0, '0.5f'], // Green.
[255, 255, 0, '0.75f'], // Yellow.
[255, 0, 0, '1.0f'],
];
for ($i=0; $i <count($color) ; $i++) {
$currC = $color[$i];
if($value < $currC[3]) {
$prevC = $color[ max(0,$i-1)];
$valueDiff = ($prevC[3] - $currC[3]);
$fractBetween = ($valueDiff==0) ? 0 : ($value - $currC[3]) / $valueDiff;
$red = ($prevC[0] - $currC[0])*$fractBetween + $currC[0];
$green = ($prevC[1] - $currC[1])*$fractBetween + $currC[1];
$blue = ($prevC[2] - $currC[2])*$fractBetween + $currC[2];
return "rgb($red,$green,$blue)";
}
}
return "rgb(255, 0, 0)";