Code for Force Directed Static Graph

These are ordered fragments for the Force Directed Static Graph example code. I trimmed parts that seemed uninteresting and that are not related to the library code. You can still see the full code here. Or go back to the example.

data

  1. var json = [  
  2.     {  
  3.       "adjacencies": [  
  4.           "graphnode21",   
  5.           {  
  6.             "nodeTo""graphnode1",  
  7.             "nodeFrom""graphnode0",  
  8.             "data": {  
  9.               "$color""#557EAA"  
  10.             }  
  11.           }, {  
  12.             "nodeTo""graphnode13",  
  13.             "nodeFrom""graphnode0",  
  14.             "data": {  
  15.               "$color""#909291"  
  16.             }  
  17.           }, {  
  18.             "nodeTo""graphnode14",  
  19.             "nodeFrom""graphnode0",  
  20.             "data": {  
  21.               "$color""#557EAA"  
  22.             }  
  23.           }, {  
  24.             "nodeTo""graphnode15",  
  25.             "nodeFrom""graphnode0",  
  26.             "data": {  
  27.               "$color""#557EAA"  
  28.             }  
  29.           }, {  
  30.             "nodeTo""graphnode16",  
  31.             "nodeFrom""graphnode0",  
  32.             "data": {  
  33.               "$color""#557EAA"  
  34.             }  
  35.           }, {  
  36.             "nodeTo""graphnode17",  
  37.             "nodeFrom""graphnode0",  
  38.             "data": {  
  39.               "$color""#557EAA"  
  40.             }  
  41.           }  
  42.       ],  
  43.       "data": {  
  44.         "$color""#83548B",  
  45.         "$type""circle",  
  46.         "$dim": 10  
  47.       },  
  48.       "id""graphnode0",  
  49.       "name""graphnode0"  
  50.     }, {  
  51.       "adjacencies": [  
  52.           {  
  53.             "nodeTo""graphnode2",  
  54.             "nodeFrom""graphnode1",  
  55.             "data": {  
  56.               "$color""#557EAA"  
  57.             }  
  58.           }, {  
  59.             "nodeTo""graphnode4",  
  60.             "nodeFrom""graphnode1",  
  61.             "data": {  
  62.               "$color""#909291"  
  63.             }  
  64.           }, {  
  65.             "nodeTo""graphnode5",  
  66.             "nodeFrom""graphnode1",  
  67.             "data": {  
  68.               "$color""#909291"  
  69.             }  
  70.           }, {  
  71.             "nodeTo""graphnode6",  
  72.             "nodeFrom""graphnode1",  
  73.             "data": {  
  74.               "$color""#909291"  
  75.             }  
  76.           }, {  
  77.             "nodeTo""graphnode7",  
  78.             "nodeFrom""graphnode1",  
  79.             "data": {  
  80.               "$color""#909291"  
  81.             }  
  82.           }, {  
  83.             "nodeTo""graphnode8",  
  84.             "nodeFrom""graphnode1",  
  85.             "data": {  
  86.               "$color""#909291"  
  87.             }  
  88.           }, {  
  89.             "nodeTo""graphnode10",  
  90.             "nodeFrom""graphnode1",  
  91.             "data": {  
  92.               "$color""#557EAA"  
  93.             }  
  94.           }, {  
  95.             "nodeTo""graphnode11",  
  96.             "nodeFrom""graphnode1",  
  97.             "data": {  
  98.               "$color""#909291"  
  99.             }  
  100.           }, {  
  101.             "nodeTo""graphnode12",  
  102.             "nodeFrom""graphnode1",  
  103.             "data": {  
  104.               "$color""#909291"  
  105.             }  
  106.           }, {  
  107.             "nodeTo""graphnode13",  
  108.             "nodeFrom""graphnode1",  
  109.             "data": {  
  110.               "$color""#557EAA"  
  111.             }  
  112.           }, {  
  113.             "nodeTo""graphnode14",  
  114.             "nodeFrom""graphnode1",  
  115.             "data": {  
  116.               "$color""#557EAA"  
  117.             }  
  118.           }, {  
  119.             "nodeTo""graphnode15",  
  120.             "nodeFrom""graphnode1",  
  121.             "data": {  
  122.               "$color""#557EAA"  
  123.             }  
  124.           }, {  
  125.             "nodeTo""graphnode16",  
  126.             "nodeFrom""graphnode1",  
  127.             "data": {  
  128.               "$color""#909291"  
  129.             }  
  130.           }, {  
  131.             "nodeTo""graphnode17",  
  132.             "nodeFrom""graphnode1",  
  133.             "data": {  
  134.               "$color""#557EAA"  
  135.             }  
  136.           }  
  137.       ],  
  138.       "data": {  
  139.         "$color""#EBB056",  
  140.         "$type""circle",  
  141.         "$dim": 11  
  142.       },  
  143.       "id""graphnode1",  
  144.       "name""graphnode1"  
  145.     }, {  
  146.       "adjacencies": [  
  147.           {  
  148.             "nodeTo""graphnode5",  
  149.             "nodeFrom""graphnode2",  
  150.             "data": {  
  151.               "$color""#909291"  
  152.             }  
  153.           }, {  
  154.             "nodeTo""graphnode9",  
  155.             "nodeFrom""graphnode2",  
  156.             "data": {  
  157.               "$color""#557EAA"  
  158.             }  
  159.           }, {  
  160.             "nodeTo""graphnode18",  
  161.             "nodeFrom""graphnode2",  
  162.             "data": {  
  163.               "$color""#557EAA"  
  164.             }  
  165.           }  
  166.       ],  
  167.       "data": {  
  168.         "$color""#416D9C",  
  169.         "$type""circle",  
  170.         "$dim": 7  
  171.       },  
  172.       "id""graphnode2",  
  173.       "name""graphnode2"  
  174.     }, {  
  175.       "adjacencies": [  
  176.           {  
  177.             "nodeTo""graphnode5",  
  178.             "nodeFrom""graphnode3",  
  179.             "data": {  
  180.               "$color""#909291"  
  181.             }  
  182.           }, {  
  183.             "nodeTo""graphnode9",  
  184.             "nodeFrom""graphnode3",  
  185.             "data": {  
  186.               "$color""#557EAA"  
  187.             }  
  188.           }, {  
  189.             "nodeTo""graphnode10",  
  190.             "nodeFrom""graphnode3",  
  191.             "data": {  
  192.               "$color""#557EAA"  
  193.             }  
  194.           }, {  
  195.             "nodeTo""graphnode12",  
  196.             "nodeFrom""graphnode3",  
  197.             "data": {  
  198.               "$color""#557EAA"  
  199.             }  
  200.           }  
  201.       ],  
  202.       "data": {  
  203.         "$color""#416D9C",  
  204.         "$type""square",  
  205.         "$dim": 10  
  206.       },  
  207.       "id""graphnode3",  
  208.       "name""graphnode3"  
  209.     }, {  
  210.       "adjacencies": [],  
  211.       "data": {  
  212.         "$color""#83548B",  
  213.         "$type""square",  
  214.         "$dim": 11  
  215.       },  
  216.       "id""graphnode4",  
  217.       "name""graphnode4"  
  218.     }, {  
  219.       "adjacencies": [  
  220.         {  
  221.           "nodeTo""graphnode9",  
  222.           "nodeFrom""graphnode5",  
  223.           "data": {  
  224.             "$color""#909291"  
  225.           }  
  226.         }  
  227.       ],  
  228.       "data": {  
  229.         "$color""#C74243",  
  230.         "$type""triangle",  
  231.         "$dim": 8  
  232.       },  
  233.       "id""graphnode5",  
  234.       "name""graphnode5"  
  235.     }, {  
  236.       "adjacencies": [  
  237.           {  
  238.             "nodeTo""graphnode10",  
  239.             "nodeFrom""graphnode6",  
  240.             "data": {  
  241.               "$color""#557EAA"  
  242.             }  
  243.           }, {  
  244.             "nodeTo""graphnode11",  
  245.             "nodeFrom""graphnode6",  
  246.             "data": {  
  247.               "$color""#557EAA"  
  248.             }  
  249.           }  
  250.       ],  
  251.       "data": {  
  252.         "$color""#83548B",  
  253.         "$type""circle",  
  254.         "$dim": 11  
  255.       },  
  256.       "id""graphnode6",  
  257.       "name""graphnode6"  
  258.     }, {  
  259.       "adjacencies": [],  
  260.       "data": {  
  261.         "$color""#EBB056",  
  262.         "$type""triangle",  
  263.         "$dim": 12  
  264.       },  
  265.       "id""graphnode7",  
  266.       "name""graphnode7"  
  267.     }, {  
  268.       "adjacencies": [],  
  269.       "data": {  
  270.         "$color""#C74243",  
  271.         "$type""star",  
  272.         "$dim": 10  
  273.       },  
  274.       "id""graphnode8",  
  275.       "name""graphnode8"  
  276.     }, {  
  277.       "adjacencies": [],  
  278.       "data": {  
  279.         "$color""#83548B",  
  280.         "$type""circle",  
  281.         "$dim": 12  
  282.       },  
  283.       "id""graphnode9",  
  284.       "name""graphnode9"  
  285.     }, {  
  286.       "adjacencies": [  
  287.         {  
  288.           "nodeTo""graphnode11",  
  289.           "nodeFrom""graphnode10",  
  290.           "data": {  
  291.             "$color""#909291"  
  292.           }  
  293.         }  
  294.       ],  
  295.       "data": {  
  296.         "$color""#70A35E",  
  297.         "$type""triangle",  
  298.         "$dim": 11  
  299.       },  
  300.       "id""graphnode10",  
  301.       "name""graphnode10"  
  302.     }, {  
  303.       "adjacencies": [],  
  304.       "data": {  
  305.         "$color""#70A35E",  
  306.         "$type""circle",  
  307.         "$dim": 11  
  308.       },  
  309.       "id""graphnode11",  
  310.       "name""graphnode11"  
  311.     }, {  
  312.       "adjacencies": [],  
  313.       "data": {  
  314.         "$color""#83548B",  
  315.         "$type""triangle",  
  316.         "$dim": 10  
  317.       },  
  318.       "id""graphnode12",  
  319.       "name""graphnode12"  
  320.     }, {  
  321.       "adjacencies": [  
  322.         {  
  323.           "nodeTo""graphnode14",  
  324.           "nodeFrom""graphnode13",  
  325.           "data": {  
  326.             "$color""#557EAA"  
  327.           }  
  328.         }  
  329.       ],  
  330.       "data": {  
  331.         "$color""#EBB056",  
  332.         "$type""star",  
  333.         "$dim": 7  
  334.       },  
  335.       "id""graphnode13",  
  336.       "name""graphnode13"  
  337.     }, {  
  338.       "adjacencies": [],  
  339.       "data": {  
  340.         "$color""#EBB056",  
  341.         "$type""triangle",  
  342.         "$dim": 12  
  343.       },  
  344.       "id""graphnode14",  
  345.       "name""graphnode14"  
  346.     }, {  
  347.       "adjacencies": [  
  348.           {  
  349.             "nodeTo""graphnode16",  
  350.             "nodeFrom""graphnode15",  
  351.             "data": {  
  352.               "$color""#557EAA"  
  353.             }  
  354.           }, {  
  355.             "nodeTo""graphnode17",  
  356.             "nodeFrom""graphnode15",  
  357.             "data": {  
  358.               "$color""#557EAA"  
  359.             }  
  360.           }  
  361.       ],  
  362.       "data": {  
  363.         "$color""#83548B",  
  364.         "$type""triangle",  
  365.         "$dim": 11  
  366.       },  
  367.       "id""graphnode15",  
  368.       "name""graphnode15"  
  369.     }, {  
  370.       "adjacencies": [  
  371.         {  
  372.           "nodeTo""graphnode17",  
  373.           "nodeFrom""graphnode16",  
  374.           "data": {  
  375.             "$color""#557EAA"  
  376.           }  
  377.         }  
  378.       ],  
  379.       "data": {  
  380.         "$color""#C74243",  
  381.         "$type""star",  
  382.         "$dim": 7  
  383.       },  
  384.       "id""graphnode16",  
  385.       "name""graphnode16"  
  386.     }, {  
  387.       "adjacencies": [],  
  388.       "data": {  
  389.         "$color""#416D9C",  
  390.         "$type""circle",  
  391.         "$dim": 7  
  392.       },  
  393.       "id""graphnode17",  
  394.       "name""graphnode17"  
  395.     }, {  
  396.       "adjacencies": [  
  397.           {  
  398.             "nodeTo""graphnode19",  
  399.             "nodeFrom""graphnode18",  
  400.             "data": {  
  401.               "$color""#557EAA"  
  402.             }  
  403.           }, {  
  404.             "nodeTo""graphnode20",  
  405.             "nodeFrom""graphnode18",  
  406.             "data": {  
  407.               "$color""#557EAA"  
  408.             }  
  409.           }  
  410.       ],  
  411.       "data": {  
  412.         "$color""#EBB056",  
  413.         "$type""triangle",  
  414.         "$dim": 9  
  415.       },  
  416.       "id""graphnode18",  
  417.       "name""graphnode18"  
  418.     }, {  
  419.       "adjacencies": [],  
  420.       "data": {  
  421.         "$color""#70A35E",  
  422.         "$type""circle",  
  423.         "$dim": 8  
  424.       },  
  425.       "id""graphnode19",  
  426.       "name""graphnode19"  
  427.     }, {  
  428.       "adjacencies": [],  
  429.       "data": {  
  430.         "$color""#C74243",  
  431.         "$type""star",  
  432.         "$dim": 8  
  433.       },  
  434.       "id""graphnode20",  
  435.       "name""graphnode20"  
  436.     }  
  437. ];  

ForceDirected

  1. var fd = new $jit.ForceDirected({  
  2.   //id of the visualization container  
  3.   injectInto: 'infovis',  
  4.   //Enable zooming and panning  
  5.   //by scrolling and DnD  
  6.   Navigation: {  
  7.     enable: true,  
  8.     //Enable panning events only if we're dragging the empty  
  9.     //canvas (and not a node).  
  10.     panning: 'avoid nodes',  
  11.     zooming: 10 //zoom speed. higher is more sensible  
  12.   },  
  13.   // Change node and edge styles such as  
  14.   // color and width.  
  15.   // These properties are also set per node  
  16.   // with dollar prefixed data-properties in the  
  17.   // JSON structure.  
  18.   Node: {  
  19.     overridable: true  
  20.   },  
  21.   Edge: {  
  22.     overridable: true,  
  23.     color: '#23A4FF',  
  24.     lineWidth: 0.4  
  25.   },  
  26.   //Native canvas text styling  
  27.   Label: {  
  28.     type: labelType, //Native or HTML  
  29.     size: 10,  
  30.     style: 'bold'  
  31.   },  
  32.   //Add Tips  
  33.   Tips: {  
  34.     enable: true,  
  35.     onShow: function(tip, node) {  
  36.       //count connections  
  37.       var count = 0;  
  38.       node.eachAdjacency(function() { count++; });  
  39.       //display node info in tooltip  
  40.       tip.innerHTML = "<div class=\"tip-title\">" + node.name + "</div>"  
  41.         + "<div class=\"tip-text\"><b>connections:</b> " + count + "</div>";  
  42.     }  
  43.   },  
  44.   // Add node events  
  45.   Events: {  
  46.     enable: true,  
  47.     type: 'Native',  
  48.     //Change cursor style when hovering a node  
  49.     onMouseEnter: function() {  
  50.       fd.canvas.getElement().style.cursor = 'move';  
  51.     },  
  52.     onMouseLeave: function() {  
  53.       fd.canvas.getElement().style.cursor = '';  
  54.     },  
  55.     //Update node positions when dragged  
  56.     onDragMove: function(node, eventInfo, e) {  
  57.         var pos = eventInfo.getPos();  
  58.         node.pos.setc(pos.x, pos.y);  
  59.         fd.plot();  
  60.     },  
  61.     //Implement the same handler for touchscreens  
  62.     onTouchMove: function(node, eventInfo, e) {  
  63.       $jit.util.event.stop(e); //stop default touchmove event  
  64.       this.onDragMove(node, eventInfo, e);  
  65.     },  
  66.     //Add also a click handler to nodes  
  67.     onClick: function(node) {  
  68.       if(!node) return;  
  69.       // Build the right column relations list.  
  70.       // This is done by traversing the clicked node connections.  
  71.       var html = "<h4>" + node.name + "</h4><b> connections:</b><ul><li>",  
  72.           list = [];  
  73.       node.eachAdjacency(function(adj){  
  74.         list.push(adj.nodeTo.name);  
  75.       });  
  76.       //append connections information  
  77.       $jit.id('inner-details').innerHTML = html + list.join("</li><li>") + "</li></ul>";  
  78.     }  
  79.   },  
  80.   //Number of iterations for the FD algorithm  
  81.   iterations: 200,  
  82.   //Edge length  
  83.   levelDistance: 130,  
  84.   // Add text to the labels. This method is only triggered  
  85.   // on label creation and only for DOM labels (not native canvas ones).  
  86.   onCreateLabel: function(domElement, node){  
  87.     domElement.innerHTML = node.name;  
  88.     var style = domElement.style;  
  89.     style.fontSize = "0.8em";  
  90.     style.color = "#ddd";  
  91.   },  
  92.   // Change node styles when DOM labels are placed  
  93.   // or moved.  
  94.   onPlaceLabel: function(domElement, node){  
  95.     var style = domElement.style;  
  96.     var left = parseInt(style.left);  
  97.     var top = parseInt(style.top);  
  98.     var w = domElement.offsetWidth;  
  99.     style.left = (left - w / 2) + 'px';  
  100.     style.top = (top + 10) + 'px';  
  101.     style.display = '';  
  102.   }  
  103. });  
  104. // load JSON data.  
  105. fd.loadJSON(json);  
  106. // compute positions incrementally and animate.  
  107. fd.computeIncremental({  
  108.   iter: 40,  
  109.   property: 'end',  
  110.   onStep: function(perc){  
  111.     Log.write(perc + '% loaded...');  
  112.   },  
  113.   onComplete: function(){  
  114.     Log.write('done');  
  115.     fd.animate({  
  116.       modes: ['linear'],  
  117.       transition: $jit.Trans.Elastic.easeOut,  
  118.       duration: 2500  
  119.     });  
  120.   }  
  121. });