How can I pass a path to open.window() onclick event in d3plus / javascript?

╄→гoц情女王★ 提交于 2019-12-12 01:44:43

问题


I am trying to build a small application via d3plus.js. The aim is to use network visualisation to show a series of nodes representing pdf files. When the node is clicked a window showing the pdf should occur.

I figured out how to use the window.open() function and it works if I write the path directly into the window.open() function (fx "docs/somepdf.pdf").

My problem is now to pass the path string from the sample_data to the window.open function.

Can anyone pleas tell me what I am doing wrong here?

<script src="//d3plus.org/js/d3.js"></script>
<script src="//d3plus.org/js/d3plus.js"></script>

<div id="viz"></div>

<script>
  // create list of node positions
  var sample_data = [
    {"name": "alpha", "size": 10, "path": "docs/Tan - 1999 - Text mining The state of the art and the challeng.pdf"},
    {"name": "beta", "size": 12, "path": ""},
    {"name": "gamma", "size": 30, "path": ""},
    {"name": "delta", "size": 26, "path": ""},
    {"name": "epsilon", "size": 12, "path": ""},
    {"name": "zeta", "size": 26, "path": ""},
    {"name": "theta", "size": 11, "path": ""},
    {"name": "eta", "size": 24, "path": ""}
  ]
  var connections = [
    {"source": "alpha", "target": "beta"},
    {"source": "alpha", "target": "gamma"},
    {"source": "beta", "target": "delta"},
    {"source": "beta", "target": "epsilon"},
    {"source": "zeta", "target": "gamma"},
    {"source": "theta", "target": "gamma"},
    {"source": "eta", "target": "gamma"}
  ]
  // instantiate d3plus
  var visualization = d3plus.viz()
    .container("#viz")
    .type("network")
    .edges(connections)
    .size("size")
    .id("name")
    .tooltip(["name", "size"]).mouse({                
      "move": false,                        // key will also take custom function
      "click": function(){window.open("path", '_blank', 'fullscreen=yes')}    
    })
    .draw()
</script>

回答1:


The problem is that you provide "path" as a string and not as an attribute of the node.

Try to change your mouse method to:

.mouse({                
  "move": false,
  "click": function(node){window.open(node.path, '_blank', 'fullscreen=yes')}    
})

Take a look at the similar working example: http://jsfiddle.net/v1fvhpvx/14/



来源:https://stackoverflow.com/questions/41153348/how-can-i-pass-a-path-to-open-window-onclick-event-in-d3plus-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!