代码之家  ›  专栏  ›  技术社区  ›  Mahsa

php中基于点击的热图颜色生成

  •  0
  • Mahsa  · 技术社区  · 6 年前

    我想生成热图的基础上为我的网站在php点击。

    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

    但它没有生成正确的颜色,例如,对于normalCount为1的链接“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)";
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   Mahsa    6 年前

    我用电脑解决了我的问题 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)";