2

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

我不确定我应该如何以及在哪里用这段代码覆盖......

4

1 回答 1

1

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

于 2014-09-17T10:42:26.427 回答