0

I am developing an application that has multiple tabs (using jQuery UI). These can be opened and closed by the user. Within each tab is an rGraph visualisation from the JavaScript InfoVis Toolkit (http://philogb.github.io/jit/static/v20/Jit/Examples/RGraph/example1.html). (The code for both the tabs and the visualisations are very similar to the examples on their respective websites)

Whenever I create a new tab, the rGraph is generated correctly. This seems to scale quite nicely and I can click back and forth through multiple tabs without any problems. However, if I interact with an rGraph in any way (e.g. by clicking on a node to move it around), the existing rGraphs in the other tabs stop working. When I click on any of the existing rGraphs, it produces the error: TypeError: node is undefined. In other words, new instances work fine but clicking on the newest one breaks the previous ones.

Is there anything that can be done about this?

Note: There does appear to be some allowance for multiple instances in the docs - you can create each instance with a different variable name, e.g. var graph1 = new ... var graph2 = new ... etc. This does indeed work, but as the tabs (and therefore the graphs) are dynamically generated, I cannot assign specific variable names like this. I tried doing the following:

var graphs = {};
graphs[unique_id_i_pass_in] = new Graph();

...but this didn't seem to make any difference.

EDIT: Here is the code I am using to call the rGraphs (document_id is the unique variable I am passing in for each graph):

var doc_rgraph = {};

//init RGraph
doc_rgraph[document_id] = new $jit.RGraph({
//Where to append the visualization
injectInto: 'document_vis_'+document_id,  
//Optional: create a background canvas that plots
//concentric circles.
background: {
  CanvasStyles: {
    //Colour of the background circles
    strokeStyle: '#C5BE94'
  }
},
//Add navigation capabilities:
//zooming by scrolling and panning.
Navigation: {
  enable: true,
  panning: true,
  zooming: 10
},
//Set Node and Edge styles.
Node: {
    //Colour of the actual nodes
    color: '#660000'
},

Edge: {
  //Color of lines between nodes
  color: '#660000',
  lineWidth:1.5
},

onBeforeCompute: function(node){
    Log.write("Centering " + node.name + "...");
},

//Add the name of the node in the correponding label
//and a click handler to move the graph.
//This method is called once, on label creation.
onCreateLabel: function(domElement, node){
    domElement.innerHTML = node.name;
    domElement.onclick = function(){

        doc_rgraph[document_id].onClick(node.id, {
            onComplete: function() {
                Log.write("Done");
            }
        });
    };
},
//Change some label dom properties.
//This method is called each time a label is plotted.
onPlaceLabel: function(domElement, node){
    var style = domElement.style;
    style.display = '';
    style.cursor = 'pointer';

    if (node._depth == 0) {
        style.fontSize = "0.8em";
        //Text colour of ring 1 nodes
        style.color = "#000000";
        style.backgroundColor = "#F05322";
        style.padding = "1px 3px";          

    } else if (node._depth == 1) {
        style.fontSize = "0.8em";
        //Text colour of ring 1 nodes
        style.color = "#FFFFFF";
        style.backgroundColor = "#164557";
        style.padding = "1px 3px";

    } else if(node._depth == 2){
        style.fontSize = "0.7em";
        //Text colour of ring 2 nodes
        style.color = "#333333";
        style.backgroundColor = "#CAC399";

    } else {
        style.display = 'none';
    }

    var left = parseInt(style.left);
    var w = domElement.offsetWidth;
    style.left = (left - w / 2) + 'px';
},      
onComplete: function(){  
    Log.write("Done");  
}   
});
//load JSON data
doc_rgraph[document_id].loadJSON(document_data[document_id]);
//trigger small animation
doc_rgraph[document_id].graph.eachNode(function(n) {
  var pos = n.getPos();
  pos.setc(-200, -200);
});
doc_rgraph[document_id].compute('end');
doc_rgraph[document_id].fx.animate({
  modes:['polar'],
 duration: 1000
});
//end

0 Answers0