当方差高时,为热图生成颜色

时间:2017-09-11 17:21:17

标签: javascript colors heatmap

所以我有一个JSON数组,其中包含来自该国家/地区的国家/地区代码和点击次数

{c_code: "sg", count: 513}
{c_code: "bg", count: 198}
{c_code: "ua", count: 4463}
....
....

我想根据匹配和RGB颜色范围获取此阵列中每个国家/地区的颜色代码。所以我发现了一个执行此操作的算法,看起来像

let max = 0,
    min = Number.MAX_VALUE,
    startColor = [200, 238, 255],
    endColor = [45, 100, 145],
    colors = {},
    hex;

for (let i = 0; i < data.length; ++i) {

    if (parseFloat(data[i].count) > max) {
        max = parseFloat(data[i].count);
    }
    if (parseFloat(data[i].count) < min) {
        min = parseFloat(data[i].count);
    }
}
console.log(max, min);

for (let i = 0; i < data.length; ++i) {
    if (data[i].count > 0) {
        colors[data[i].c_code] = '#';

        for (let j = 0; j < 3; j++) {
            hex = Math.round(startColor[j] +
                (endColor[j] -
                    startColor[j]) *
                (parseFloat(data[i].count - min) / (max - min))).toString(16);

            if (hex.length === 1) {
                hex = '0' + hex;
            }

            colors[data[i].c_code] += (hex.length === 1 ? '0' : '') + hex;
        }
    }
}

但我面临的问题是,在我的阵列中,一个国家与其他国家相比有很大的点击率(比第二大国家大15倍),因此,其他郡的颜色看起来非常相似。有没有什么办法解决这一问题?有没有比上面更好的算法可以用来生成热图?

0 个答案:

没有答案