0
有可能針對具有特定鍵值的節點嗎?我可以針對d3中的特定節點嗎?
有一個「類別」:數據集的節點,我試圖給它分配一個不同的顏色。我想這是可能的。我想知道如果我能寫某種形式的if語句像
if(d.node.category ==dataset){
d3.select(this).style("fill", "yellow");
}
這是我怎麼現在
var z = d3.scale.category10();
var node = svg.selectAll("circle.node")
.data(nodes)
.enter().append("svg:circle")
.attr("r", function(d){return d.size*2})
.attr('class', 'generalClass')
.style("fill", function(d) { return z(d.parent); })
.style("stroke", "#000")
.style("stroke-width", "4px")
.call(force.drag);
我的JSON看起來像這樣
function getNodes() {
var inNodes = {
"name": "APP",
"dept": "NYC",
"children": [
{
"name": "API 1",
"dept": "Third Party",
"size": 15,
"url": "http://www.nytimes.com/"
},
{
"name": "API 2",
"dept": "NYC",
"size": 15,
"url": "https://www.google.com/"
},
{
"name": "Dataset",
"category": "Dataset", // this one I am trying to assign a diff color fill
"dept": "Third Party",
"size": 15,
"url": "http://www.wired.com/",
}
],
"size":20,
"url": "http://www.nyc.gov/html/index.html"
};
return inNodes;
}
@ Thach--我只是添加了一些代碼來顯示我現在正在添加顏色。我認爲這可能需要一些解釋。再次感謝你的幫助 – soum