var COLOR = 'blue';
var map = new Datamap({
element: document.getElementById('map'),
responsive: false,
});
function changeColor(countryCode) {
map.updateChoropleth({
countryCode: COLOR
});
}
setInterval(function() {
changeColor('USA');
}, 2000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.9/topojson.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/datamaps/0.5.8/datamaps.world.min.js"></script>
<div id="map"></div>
我有以下javascript,它在HTML上呈现世界地图。
var map = new Datamap({
element: document.getElementById('map'),
responsive: true,
});
function changeColor(countryCode) {
map.updateChoropleth({
countryCode: 'blue'
});
console.log(countryCode + '--> colorChange');
}
setInterval(function() {
changeColor('USA');
}, 2000);
我希望在2秒后更新特定国家/地区的颜色。当我运行上面的代码时,每隔2秒调用一次函数 changeColor ,但函数 map.updateChoropleth 似乎没有任何效果。如果我改变了以下代码段:
function changeColor(countryCode) {
map.updateChoropleth({
countryCode: 'blue'
});
console.log(countryCode + '--> colorChange');
}
到
function changeColor(countryCode) {
map.updateChoropleth({
"USA": 'blue'
});
console.log(countryCode + '--> colorChange');
}
该功能按预期工作。为什么这不起作用?
答案 0 :(得分:3)
updateChoropleth
函数正在查找名为countryCode
的属性键。
解决方案是填充对象并将其传递给函数:
var COLOR = 'blue';
var map = new Datamap({
element: document.getElementById('map'),
responsive: false,
});
function changeColor(countryCode) {
var obj = {};
obj[countryCode] = COLOR;
map.updateChoropleth(obj);
}
setInterval(function() {
changeColor('USA');
}, 2000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.9/topojson.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/datamaps/0.5.8/datamaps.world.min.js"></script>
<div id="map"></div>
答案 1 :(得分:0)
这似乎对我有用
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.9/topojson.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/datamaps/0.5.8/datamaps.world.min.js"></script>
<div id="map" class="world-map"></div>
<script>
// https://github.com/markmarkoh/datamaps
// https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
var defaultColor = '#c3c3c3';
var activeColor = '#00b3c6';
var highlightColorHover = '#037582';
var highlightBorderColor = '#037582';
var map = new Datamap({
element: document.getElementById('map'),
responsive: false,
fills: {
defaultFill: defaultColor
},
geographyConfig: {
highlightFillColor: highlightColorHover,
highlightBorderColor: highlightBorderColor,
},
});
map.updateChoropleth({
USA: activeColor,
MEX: activeColor,
PAN: activeColor,
ARG: activeColor,
BRA: activeColor,
CHL: activeColor,
COL: activeColor,
AUT: activeColor,
BEL: activeColor,
BGR: activeColor,
DNK: activeColor,
FIN: activeColor,
FRA: activeColor,
DEU: activeColor,
HUN: activeColor,
IRL: activeColor,
ITA: activeColor,
LTU: activeColor,
NLD: activeColor,
POL: activeColor,
PRT: activeColor,
ROU: activeColor,
RUS: activeColor,
ESP: activeColor,
SWE: activeColor,
CHE: activeColor,
TUR: activeColor,
GBR: activeColor,
CHN: activeColor,
IND: activeColor,
JPN: activeColor,
MYS: activeColor,
PAK: activeColor,
SGP: activeColor,
AUS: activeColor,
ARE: activeColor,
ZAF: activeColor,
});
</script>