doudi1449 2018-09-27 10:21
浏览 96
已采纳

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

I want to generate heatmap based on clicks for my website in php.

I made an array like below and normalized click counts between 0,1 (1 is for link with the highest click counts):

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

I used algorithm in this question and passed normalCount index of array to get color of each link.

But It's not generating right color, for example for link "page/252" with normalCount 1, I expect red color(warmest) but it's yellow now.

Below is my function to generate color:

$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)";
  • 写回答

1条回答 默认 最新

  • doulieyu0881 2018-09-27 18:01
    关注

    I solved my problem using this link.

    this is my function:

        $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)";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大