I’m trying to implement a global heat map of where our international projects are located in the world. I’m using JQVmap for this purpose.
$(document).ready(function() {
var data = {
"af": 16.63,
"al": 11.58,
"dz": 158.97,
"ca": 146.9
};
var max = 0,
min = Number.MAX_VALUE,
cc,
startColor = [200, 238, 255],
endColor = [0, 100, 145],
colors = {},
hex;
// Find max and min values
for(cc in data) {
if(parseFloat(data[cc]) > max) {
max = parseFloat(data[cc]);
}
if(parseFloat(data[cc]) < min) {
min = parseFloat(data[cc]);
}
}
// Set colors according to data values
for(cc in data) {
if(data[cc] > 0) {
colors[cc] = '#';
for(var i = 0; i < 3; i++) {
hex = Math.round(
startColor[i] + (endColor[i] - startColor[i]) * (data[cc] / (max - min))
).toString(16);
if(hex.length == 1) {
hex = '0' + hex;
}
colors[cc] += (hex.length == 1 ? '0' : '') + hex;
}
}
}
$("#vmap").vectorMap({
map: 'world_en',
backgroundColor: '#f5f5f5',
borderColor: '#888888',
borderOpacity: 0.25,
borderWidth: 1,
colors: colors,
enableZoom: true,
hoverColor: false,
hoverOpacity: 0.7,
normalizeFunction: 'linear',
selectedColor: false,
selectedRegions: null,
showTooltip: true
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqvmap/1.5.1/jqvmap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqvmap/1.5.1/jquery.vmap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqvmap/1.5.1/maps/jquery.vmap.world.js"></script>
<div id="vmap" style="width: 100%; height: 400px;"></div>
As you’ll notice, there’s a country filled with black color and the resulting hex is #-105988
. My only guess is that the problem lies within the formula
startColor[i] + (endColor[i] - startColor[i]) * (data[cc] / (max - min))
I tried grouping the first operation by enclosing them in a parenthesis but that didn't solve my problem. I also tried using Math.abs((endColor[i] - startColor[i]))
but that also didn’t fix it.
To be honest this isn’t my formula and I don’t really know how to calculate the different hues to denote the density of a value so I have no idea how to adjust the formula. The formula was taken directly from the library’s official documentation.
Apparently I don't need to calculate the colors myself as the library can do that for you already - just supply the data.
$(document).ready(function() {
var data = {
"af": 16.63,
"al": 11.58,
"dz": 158.97,
"ca": 146.9
};
$("#vmap").vectorMap({
map: 'world_en',
backgroundColor: '#f5f5f5',
borderColor: '#888888',
borderOpacity: 0.25,
borderWidth: 1,
// colors: colors,
enableZoom: true,
hoverColor: false,
hoverOpacity: 0.7,
normalizeFunction: 'linear',
scaleColors: ['#C8EEFF', '#006491'],
selectedColor: false,
selectedRegions: null,
showTooltip: true,
values: data
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqvmap/1.5.1/jqvmap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqvmap/1.5.1/jquery.vmap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqvmap/1.5.1/maps/jquery.vmap.world.js"></script>
<div id="vmap" style="width: 100%; height: 400px;"></div>