2011-11-23 61 views
9

我正在使用D3 JavaScript庫將數據顯示爲強制定向標記。它工作正常。但我無法將點擊事件添加到圈子中。所以當我點擊圓圈時,我會得到圓圈的詳細分析並將其顯示在模態框中。無法獲取D3 JavaScript庫中的單擊事件

var links = [{source: "x", target:"y", type: "paid"},......]'; 

var nodes = {}; 

// Compute the distinct nodes from the links. 
links.forEach(function(link) { 
    link.source = nodes[link.source] || (nodes[link.source] = {name: link.source}); 
    link.target = nodes[link.target] || (nodes[link.target] = {name: link.target}); 
}); 

var w = 950, 
    h = 500; 

var force = d3.layout.force() 
    .nodes(d3.values(nodes)) 
    .links(links) 
    .size([w, h]) 
    .linkDistance(60) 
    .charge(-300) 
    .on("tick", tick) 
    .start(); 

var svg = d3.select("#graph").append("svg:svg") 
    .attr("width", w) 
    .attr("height", h); 

// Per-type markers, as they don't inherit styles. 
svg.append("svg:defs").selectAll("marker") 
    .data(["suit", "licensing", "resolved"]) 
    .enter().append("svg:marker") 
    .attr("id", String) 
    .attr("viewBox", "0 -5 10 10") 
    .attr("refX", 15) 
    .attr("refY", -1.5) 
    .attr("markerWidth", 6) 
    .attr("markerHeight", 6) 
    .attr("orient", "auto") 
    .append("svg:path") 
    .attr("d", "M0,-5L10,0L0,5"); 

var path = svg.append("svg:g").selectAll("path") 
    .data(force.links()) 
    .enter().append("svg:path") 
    .attr("class", function(d) { return "link " + d.type; }) 
    .attr("marker-end", function(d) { return "url(#" + d.type + ")"; }); 

var circle = svg.append("svg:g").selectAll("circle") 
    .data(force.nodes()) 
    .enter().append("svg:circle") 
    .attr("r", 6) 
    .call(force.drag); 

var text = svg.append("svg:g").selectAll("g") 
    .data(force.nodes()) 
    .enter().append("svg:g"); 

// A copy of the text with a thick white stroke for legibility. 
text.append("svg:text") 
    .attr("x", 8) 
    .attr("y", ".31em") 
    .attr("class", "shadow") 
    .text(function(d) { return d.name; }); 

text.append("svg:text") 
    .attr("x", 8) 
    .attr("y", ".31em") 
    .text(function(d) { return d.name; }); 

// Use elliptical arc path segments to doubly-encode directionality. 
function tick() { 
    path.attr("d", function(d) { 
    var dx = d.target.x - d.source.x, 
     dy = d.target.y - d.source.y, 
     dr = Math.sqrt(dx * dx + dy * dy); 
    return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y; 
    }); 

    circle.attr("transform", function(d) { 
    return "translate(" + d.x + "," + d.y + ")"; 
    }); 

    text.attr("transform", function(d) { 
    return "translate(" + d.x + "," + d.y + ")"; 
    }); 
} 

我嘗試添加.on("click", 'alert(\'Hello world\')')var circle。它沒有按預期工作。它會在加載時發出警報而不是點擊。

我感謝所有幫助

+0

你究竟在哪裏放置.on(「click」等)代碼? –

回答

19

試試這個:

var circle = svg.append("svg:g").selectAll("circle") 
    .data(force.nodes()) 
    .enter().append("svg:circle") 
    .attr("r", 6) 
    .on("click", function(d,i) { alert("Hello world"); }) 
    .call(force.drag); 
+0

@Antony Blake謝謝你的回答。最後一件事,如果我想獲得圓的屬性,我應該使用d.source,但它不起作用。它說未定義。我感謝你的幫助。 –

3

試試這個,如果你想包含在圓圈內的節點(假設你的節點映射一鍵叫做憤怒的對象和值34:

var circle = svg.append("svg:g").selectAll("circle") 
.data(force.nodes()) 
.enter().append("svg:circle") 
.attr("r", 6) 
.on("click", function(d,i) { alert(d.anger); }) // this will alert 34 
.call(force.drag); 

或者嘗試這對SVG的屬性(獲取SVG的半徑,例如):

var circle = svg.append("svg:g").selectAll("circle") 
.data(force.nodes()) 
.enter().append("svg:circle") 
.attr("r", 6) 
.on("click", function(d,i) { alert(d3.select(this).r; }) // this will print out the radius }) 
.call(force.drag); 

對不起,如果我的帖子是像上面的那個,但我認爲澄清可能是有用的。

+0

有一個錯字:alert(d3.select(this).r); – tuned