我是D3.js的新手,我被要求接管其他人在工作中开始的项目。D3.js和jQuery - 多个地图点和点击事件
目标是有一个地图,为点拉入json数据,然后当点击点时,jQuery对话框会弹出每个点都有正确的json数据。
我已经能够让jQuery弹出窗口在地图上工作,但是每一个被点击的点都使用完全相同的文本填充。
我还测试了使用$ .getJSON在一个简单的html页面中加载和显示json数据,并且我能够通过所有json数据来获得循环。
下面是创建点的功能:
function addCirclesForArray(element,index,array) {
var coordinates =
projection([element.sendLocation.longitude,element.sendLocation.latitude]);
g.append("circle")
.attr("cx",coordinates[0])
.attr("cy",coordinates[1])
.attr("r",(index<array.length-1)?2:4)
.attr("r",4)
.style("fill",$colorScale(d3.round(element.profileReadings[0].psal)))
.attr("class","circle")
g.selectAll("circle")
.on("click",circleClicked)
}
这里是我通过了jQuery的弹出窗口的JSON数据循环方法:
function circleClicked(data, i) {
console.log(data) // undefined
console.log(i); // index #
$.getJSON("data/oc-readings3.json", function(data){
$.each(data, function(key, value){
//populate jQuery dialog
$('#floatID').text("Float ID: "+value.platformNumber);
$('#latitude').text("Latitude: "+value.sendLocation.latitude);
$('#longitude').text("Longitude: "+value.sendLocation.longitude);
// jQuery UI dialog and tabs
$("#tabs").tabs();
$("#dialog").dialog({ width: 400 })
});
});
}
我可能失去了一些东西简单在getJSON方法中使用循环,或者它可能与未定义的数据有关。如果您有任何提示,请告诉我。谢谢。
UPDATE /解决方案 我意识到,我并不需要使用$ .getJSON,因为我已经在addCirclesForArray方法获取JSON数据。我可以使用传入的数组参数中的索引。
我也摆脱了circleClicked方法,并将新的逻辑添加到addCirclesForArray方法中。
g.selectAll("circle")
.on("mouseover", increaseSize)
.on("mouseout", decreaseSize)
.on("click", function(d,i) {
//console.log(array[i]);
//jQuery popup
$("#tabs").tabs();
$("#dialog").dialog({
width: 418,
resizable: false
});
//populate tabs
$('#floatID').text("Float ID: "+array[i].platformNumber);
// etc.
你在哪里数据绑定的社交圈吗? – FernOfTheAndes
感谢您的回复。我只将数据绑定到jQuery对话框的html,而不是圆圈。也许这是我的问题? – NeilS
我会这么认为,因为您从圆圈的上下文中调用了click事件,但没有绑定到它们的数据......在侦听器开始时未定义数据的原因。 – FernOfTheAndes