我正在使用d3中的散點圖。圖表上的點代表一張紙。點擊右鍵點擊我有一個上下文菜單,其中有2個選項:1)將該論文添加到庫(將類型更改爲In_library)和2)從庫中刪除(完全從數據中刪除紙張)。未正確調用刷新函數d3
在每次這些更新之後,我會調用refreshGraph()函數,這些更新會使用更新的數據重新繪製圖形。但是,我認爲沒有任何反應,因爲refreshGraph()沒有被正確調用?或者對於選項1,類型庫沒有正確設置?當在選項1之後調用refreshGraph時,點應該變爲藍色,並在調用選項2時,點將從顯示中消失,因爲它已從用於繪製圓圈的數據的alldata中刪除。這裏是相關的代碼:
allData = [];
var menu = [{
title: 'Add to Library',
action: function addToLibrary(elem, d, i) {
d3.json("connection6.php?paperID="+d.ID, function(error, dataJson) {
for(i=0;i<allData.length;i++){
if (d.type === "In_library")
{
alert("The paper: " + d.TITLE + " is already in your Library!");
return;
}
}
d.type = "In_library"; // is this the correct way to change the type if the input has a different type??
refreshGraph();
})
refreshGraph();
}
},
{
title: 'Remove from Library',
action: function removeFromLibrary (elem, d, i) {
d3.json("connection9.php?paperID="+d.ID, function(error, dataJson) {
//loop through allData and if selected ID has type In_library, remove from allData
for(i=0;i<allData.length;i++){
if (d.type == "In_library"){
allData.splice(i--,1);
}
}
refreshGraph();
})
}
}
]
function refreshGraph() {
// draw dots
var circles = svg.selectAll("circle")
.data(allData)
circles.transition()
.attr("cx", function(d) {return x(YearFn(d))})
.attr("cy", function(d) {return y(Num_citationsFn(d))})
circles.enter()
.append("circle")
.attr("class", "dot")
.attr("r", 3.5)
.attr("cx", function(d) {return x(YearFn(d))})
.attr("cy", function(d) {return y(Num_citationsFn(d))})
.style("fill",function(d){
var colour = "black"
switch(d.type){
case "In_library":
colour = "blue";
break;
case "cited by":
colour = "red";
break;
case "cites":
colour = "green";
break;
case "selected":
colour = "magenta";
break;
default:
colour = "black";
}
return colour;
})
.on("mouseover", mouseHandler)
.on("mouseout", mouseoutHandler)
.on("click", clickHandler)
.on("contextmenu", rightClickHandler);
svg.select(".y.axis").call(yAxis);
svg.select(".x.axis").call(xAxis);
//don't want dots overlapping axis, so add in buffer to data domain
x.domain([d3.min(allData, YearFn)-1, d3.max(allData, YearFn)+1]);
y.domain([d3.min(allData, Num_citationsFn)-1, d3.max(allData, Num_citationsFn)+1]);
}
任何幫助非常感謝我是新的d3,所以在此先感謝!
你忘了在函數addTolibrary中更新'allData'嗎? – Quarter2Twelve
@ Quarter2Twelve是的好點你的意思是推(D)到allData?如果是這樣,不應該是這樣的情況,就好像一個點被顯示,並且可以被點擊,這意味着它已經在allData ..所以如果我將該紙張推入allData,它將在該文件的頂部繪製另一個實例,而不是我想要的該類型將被識別爲In_library並更改現有點的顏色。我確實嘗試過,看到一個藍點,但意識到它只是覆蓋另一個點。 –
當你調用refreshGraph時,你在用已經繪製的圓圈做什麼? – James