2013-06-20 43 views
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; 
} 

回答

0

增色節點當類別存在時,您可以分配不同的填充?

d3.selectAll('path').data(inNodes.children).enter() 
    .append('path') 
    .doOtherStuffToCreateYourPath() 
    .attr('fill', function (d) { 
     return (d.category === 'DataSet' ? 'red' : 'defaultColor'); 
    }); 
+0

@ Thach--我只是添加了一些代碼來顯示我現在正在添加顏色。我認爲這可能需要一些解釋。再次感謝你的幫助 – soum

相關問題