单击dagre-d3中的节点

时间:2014-09-09 10:45:47

标签: javascript d3.js dagre-d3

我正在使用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

我不确定我应该用这段代码覆盖的方式和位置......

1 个答案:

答案 0 :(得分:0)

来自他们Wiki的这个例子可能有所帮助 http://cpettitt.github.io/project/dagre-d3/latest/demo/hover.html