【发布时间】:2015-09-09 17:27:26
【问题描述】:
我正在尝试使用通过 AJAX 和 JSON 获得的值来修改矢量图。
用这个数组键值完美的映射函数:
var visitorsData = {
"tac": 564,
"moq": 400,
"lim": 1000,
"apu": 800,
"caj": 760,
"ama": 300,
"lib": 700,
"lam": 600,
};
接受这个值的函数是这样的:
$('#world-map').vectorMap({
map: 'peru',
backgroundColor: '#fff',
regionStyle: {
initial: {
fill: "#c6c6c6",
stroke: "#204d6f",
"stroke-width": 1,
"stroke-opacity": 1
},
hover: {
fill: "#ed0000",
"fill-opacity": "1"
}
},
series: {
regions: [{
values: visitorsData,
scale: ["#3c8dbc", "#2D79A6"], //['#3E5E6B', '#A6BAC2'],
normalizeFunction: 'polynomial'
}]
},
onRegionLabelShow: function(e, el, code) {
if (typeof visitorsData[code] != "undefined")
el.html(el.html() + ': ' + visitorsData[code] + ' new visitors');
}
})
现在我想从 JSON 中获取值,并以此构建一个新的 visitorsData 但没有函数。这是我的代码:
$.ajax({
url: globalMapUrl, //obtain json
})
.done(function(data) {
var visitorsData = new Array();
for (var i = 0; i < data.length; i++) {
var item = data[i];
visitorsData[item.nombre] = item.numeroDeAnuncios;
}
})
.fail(function() {
alert("Ajax failed to fetch data")
})
【问题讨论】:
-
whats inside data 你能提供一个来自 ajax 的数据样本
-
所以现在在您的 ajax 调用中,您正在向地图添加更多项目,但您想创建新地图以便清除旧数据?
标签: javascript jquery json ajax