Search code examples
javascriptd3plus

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


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>

Solution

  • 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/