我正在使用 degre-d3 并且在获取单击节点的节点数据时遇到问题。单击时我有一个事件,但 d3.select(this) 似乎没有获得相关数据(如果可能,我希望获得节点名称、标签和其他对象数据。
app.controller "DashboardElementShowController", ($scope, DashboardElementIndexService) ->
init_app = ->
# Create a new directed graph
g = new dagreD3.Digraph()
g.addNode "bpitt",
label: "html Brad Pitt <b>test</b>"
g.addNode "hford",
label: "Harrison Ford"
g.addNode "lwilson",
label: "Luke Wilson"
g.addNode "kbacon",
label: "Kevin Bacon"
g.addEdge null, "bpitt", "kbacon",
g.addEdge null, "hford", "lwilson",
g.addEdge null, "lwilson", "kbacon",
renderer = new dagreD3.Renderer().edgeInterpolate("cardinal")
.edgeTension(0.8)
renderer.edgeInterpolate('linear');
renderer.run g, d3.select("svg g")
d3.select("svg g").on("click", (d, i, k) ->
console.log d3.select(this) # doesn't seem to have relevant info I need
)
init_app()
编辑:10.09.2014:这里说我必须覆盖 renderer.drawNodes 方法: https ://github.com/cpettitt/dagre-d3/issues/67
这是指定的代码:
constructor:
@renderer.drawNodes @_drawNodes().bind @
_drawNodes: ->
oldDrawNodes = @renderer.drawNodes()
(graph, svg) ->
# catch drawed nodes
svgNodes = oldDrawNodes graph, svg
svgNodes.on 'click', (nodeId) -> console.log 'node clicked', nodeId
我不确定我应该如何以及在哪里用这段代码覆盖......