srikanth | 116e6e8 | 2014-08-19 07:22:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | Copyright (c) 2013 Sencha Inc. - Author: Nicolas Garcia Belmonte (http://philogb.github.com/) |
| 3 | |
| 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | of this software and associated documentation files (the "Software"), to deal |
| 6 | in the Software without restriction, including without limitation the rights |
| 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | copies of the Software, and to permit persons to whom the Software is |
| 9 | furnished to do so, subject to the following conditions: |
| 10 | |
| 11 | The above copyright notice and this permission notice shall be included in |
| 12 | all copies or substantial portions of the Software. |
| 13 | |
| 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | THE SOFTWARE. |
| 21 | |
| 22 | */ |
| 23 | /* |
| 24 | # |
| 25 | # Copyright (c) 2013 Big Switch Networks, Inc. |
| 26 | # |
| 27 | # Licensed under the Eclipse Public License, Version 1.0 (the |
| 28 | # "License"); you may not use this file except in compliance with the |
| 29 | # License. You may obtain a copy of the License at |
| 30 | # |
| 31 | # http://www.eclipse.org/legal/epl-v10.html |
| 32 | # |
| 33 | # Unless required by applicable law or agreed to in writing, software |
| 34 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 35 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 36 | # implied. See the License for the specific language governing |
| 37 | # permissions and limitations under the License. |
| 38 | # |
| 39 | */ |
| 40 | |
| 41 | // |
| 42 | // ForceDirected example1.js Javascript |
| 43 | // |
| 44 | // Slightly modified for use in rendering a simple network diagram topology |
| 45 | // |
| 46 | |
| 47 | var labelType, useGradients, nativeTextSupport, animate; |
| 48 | |
| 49 | (function() { |
| 50 | var ua = navigator.userAgent, |
| 51 | iStuff = ua.match(/iPhone/i) || ua.match(/iPad/i), |
| 52 | typeOfCanvas = typeof HTMLCanvasElement, |
| 53 | nativeCanvasSupport = (typeOfCanvas == 'object' || typeOfCanvas == 'function'), |
| 54 | textSupport = nativeCanvasSupport |
| 55 | && (typeof document.createElement('canvas').getContext('2d').fillText == 'function'); |
| 56 | //I'm setting this based on the fact that ExCanvas provides text support for IE |
| 57 | //and that as of today iPhone/iPad current text support is lame |
| 58 | labelType = (!nativeCanvasSupport || (textSupport && !iStuff))? 'Native' : 'HTML'; |
| 59 | nativeTextSupport = labelType == 'Native'; |
| 60 | useGradients = nativeCanvasSupport; |
| 61 | animate = !(iStuff || !nativeCanvasSupport); |
| 62 | })(); |
| 63 | |
| 64 | var Log = { |
| 65 | elem: false, |
| 66 | write: function(text){ |
| 67 | if (!this.elem) |
| 68 | this.elem = document.getElementById('log'); |
| 69 | this.elem.innerHTML = text; |
| 70 | this.elem.style.left = (500 - this.elem.offsetWidth / 2) + 'px'; |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | |
| 75 | // init data |
| 76 | var json = loadTopologyJSON(); |
| 77 | // end |
| 78 | // init ForceDirected |
| 79 | var fd = new $jit.ForceDirected({ |
| 80 | //id of the visualization container |
| 81 | injectInto: 'infovis', |
| 82 | //force height |
| 83 | height: 900, |
| 84 | //Enable zooming and panning |
| 85 | //by scrolling and DnD |
| 86 | Navigation: { |
| 87 | enable: true, |
| 88 | //Enable panning events only if we're dragging the empty |
| 89 | //canvas (and not a node). |
| 90 | panning: 'avoid nodes', |
| 91 | zooming: 10 //zoom speed. higher is more sensible |
| 92 | }, |
| 93 | // Change node and edge styles such as |
| 94 | // color and width. |
| 95 | // These properties are also set per node |
| 96 | // with dollar prefixed data-properties in the |
| 97 | // JSON structure. |
| 98 | Node: { |
| 99 | overridable: true |
| 100 | }, |
| 101 | Edge: { |
| 102 | overridable: true, |
| 103 | color: '#23A4FF', |
| 104 | lineWidth: 0.4 |
| 105 | }, |
| 106 | //Native canvas text styling |
| 107 | Label: { |
| 108 | type: labelType, //Native or HTML |
| 109 | size: 10, |
| 110 | color: '#000000', |
| 111 | style: 'bold' |
| 112 | }, |
| 113 | //Add Tips |
| 114 | Tips: { |
| 115 | enable: true, |
| 116 | onShow: function(tip, node) { |
| 117 | if(!node) return; |
| 118 | // Build the relations list as a tooltip. |
| 119 | // This is done by traversing the clicked node connections. |
| 120 | var html = "<div class=\"dynamictable2\"> <table> <tr><td>" + node.name + " links:</td></tr><tr><td>", |
| 121 | list = []; |
| 122 | node.eachAdjacency(function(adj){ |
| 123 | list.push(adj.nodeTo.name); |
| 124 | }); |
| 125 | //append connections information |
| 126 | tip.innerHTML = html + list.join("</td></tr><tr><td>") + "</td></tr></table></div>"; |
| 127 | } |
| 128 | }, |
| 129 | // Add node events |
| 130 | Events: { |
| 131 | enable: true, |
| 132 | type: 'Native', |
| 133 | //Change cursor style when hovering a node |
| 134 | onMouseEnter: function() { |
| 135 | fd.canvas.getElement().style.cursor = 'move'; |
| 136 | }, |
| 137 | onMouseLeave: function() { |
| 138 | fd.canvas.getElement().style.cursor = ''; |
| 139 | }, |
| 140 | //Update node positions when dragged |
| 141 | onDragMove: function(node, eventInfo, e) { |
| 142 | var pos = eventInfo.getPos(); |
| 143 | node.pos.setc(pos.x, pos.y); |
| 144 | fd.plot(); |
| 145 | }, |
| 146 | //Implement the same handler for touchscreens |
| 147 | onTouchMove: function(node, eventInfo, e) { |
| 148 | $jit.util.event.stop(e); //stop default touchmove event |
| 149 | this.onDragMove(node, eventInfo, e); |
| 150 | }, |
| 151 | //No node click-handler for now |
| 152 | //onClick: function(node) { |
| 153 | //} |
| 154 | }, |
| 155 | //Number of iterations for the FD algorithm |
| 156 | iterations: 10, |
| 157 | //Edge length |
| 158 | levelDistance: 130, |
| 159 | // Add text to the labels. This method is only triggered |
| 160 | // on label creation and only for DOM labels (not native canvas ones). |
| 161 | onCreateLabel: function(domElement, node){ |
| 162 | domElement.innerHTML = node.name; |
| 163 | var style = domElement.style; |
| 164 | style.fontSize = "0.8em"; |
| 165 | style.color = "#ddd"; |
| 166 | }, |
| 167 | // Change node styles when DOM labels are placed |
| 168 | // or moved. |
| 169 | onPlaceLabel: function(domElement, node){ |
| 170 | var style = domElement.style; |
| 171 | var left = parseInt(style.left); |
| 172 | var top = parseInt(style.top); |
| 173 | var w = domElement.offsetWidth; |
| 174 | style.left = (left - w / 2) + 'px'; |
| 175 | style.top = (top + 10) + 'px'; |
| 176 | style.display = ''; |
| 177 | } |
| 178 | }); |
| 179 | // load JSON data. |
| 180 | fd.loadJSON(json); |
| 181 | // compute positions incrementally and animate. |
| 182 | fd.computeIncremental({ |
| 183 | iter: 40, |
| 184 | property: 'end', |
| 185 | onStep: function(perc){ |
| 186 | Log.write(perc + '% loaded...'); |
| 187 | }, |
| 188 | onComplete: function(){ |
| 189 | Log.write('done'); |
| 190 | } |
| 191 | }); |
| 192 | // end |