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

根据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)";
    
    点赞 打赏 评论

相关推荐 更多相似问题