根据 php 中的点击生成热图颜色

Generating heatmap color base on clicks in php

我想根据 php 中我网站的点击次数生成热图。

我制作了一个如下所示的数组,并将点击次数标准化为 0,1(1 表示 link 的点击次数最高):

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)'
    ]
]

我在 中使用算法并传递数组的 normalCount 索引来获取每个 link 的颜色。

但它没有生成正确的颜色,例如对于 normalCount 为 1 的 link "page/252",我希望是红色(最暖),但现在是黄色。

下面是我生成颜色的函数:

$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)";

我使用 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)";