我是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