graph.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. var labelType, useGradients, nativeTextSupport, animate;
  2. (function() {
  3. var ua = navigator.userAgent,
  4. iStuff = ua.match(/iPhone/i) || ua.match(/iPad/i),
  5. typeOfCanvas = typeof HTMLCanvasElement,
  6. nativeCanvasSupport = (typeOfCanvas == 'object' || typeOfCanvas == 'function'),
  7. textSupport = nativeCanvasSupport
  8. && (typeof document.createElement('canvas').getContext('2d').fillText == 'function');
  9. //I'm setting this based on the fact that ExCanvas provides text support for IE
  10. //and that as of today iPhone/iPad current text support is lame
  11. labelType = (!nativeCanvasSupport || (textSupport && !iStuff))? 'Native' : 'HTML';
  12. nativeTextSupport = labelType == 'Native';
  13. useGradients = nativeCanvasSupport;
  14. animate = !(iStuff || !nativeCanvasSupport);
  15. })();
  16. var Log = {
  17. elem: false,
  18. write: function(text){
  19. if (!this.elem)
  20. this.elem = document.getElementById('log');
  21. this.elem.innerHTML = text;
  22. this.elem.style.left = (500 - this.elem.offsetWidth / 2) + 'px';
  23. }
  24. };
  25. function init(){
  26. // init data
  27. var json = [
  28. {
  29. "adjacencies": [
  30. "graphnode0",
  31. {
  32. "nodeTo": "graphnode1",
  33. "nodeFrom": "graphnode0",
  34. "data": {
  35. "$color": "#557EAA"
  36. }
  37. }, {
  38. "nodeTo": "graphnode5",
  39. "nodeFrom": "graphnode0",
  40. "data": {
  41. "$color": "#909291"
  42. }
  43. }, {
  44. "nodeTo": "graphnode9",
  45. "nodeFrom": "graphnode0",
  46. "data": {
  47. "$color": "#557EAA"
  48. }
  49. }
  50. ],
  51. "data": {
  52. "$color": "#83548B",
  53. "$type": "circle",
  54. "$dim": 8
  55. },
  56. "id": "graphnode0",
  57. "name": "graphnode0"
  58. }, {
  59. "adjacencies": [
  60. "graphnode1",
  61. {
  62. "nodeTo": "graphnode8",
  63. "nodeFrom": "graphnode1",
  64. "data": {
  65. "$color": "#557EAA"
  66. }
  67. }, {
  68. "nodeTo": "graphnode0",
  69. "nodeFrom": "graphnode1",
  70. "data": {
  71. "$color": "#909291"
  72. }
  73. }, {
  74. "nodeTo": "graphnode2",
  75. "nodeFrom": "graphnode1",
  76. "data": {
  77. "$color": "#557EAA"
  78. }
  79. }
  80. ],
  81. "data": {
  82. "$color": "#83548B",
  83. "$type": "circle",
  84. "$dim": 8
  85. },
  86. "id": "graphnode1",
  87. "name": "graphnode1"
  88. }, {
  89. "adjacencies": [
  90. "graphnode2",
  91. {
  92. "nodeTo": "graphnode1",
  93. "nodeFrom": "graphnode2",
  94. "data": {
  95. "$color": "#557EAA"
  96. }
  97. }, {
  98. "nodeTo": "graphnode3",
  99. "nodeFrom": "graphnode2",
  100. "data": {
  101. "$color": "#909291"
  102. }
  103. }, {
  104. "nodeTo": "graphnode6",
  105. "nodeFrom": "graphnode2",
  106. "data": {
  107. "$color": "#557EAA"
  108. }
  109. }
  110. ],
  111. "data": {
  112. "$color": "#83548B",
  113. "$type": "circle",
  114. "$dim": 8
  115. },
  116. "id": "graphnode2",
  117. "name": "graphnode2"
  118. }, {
  119. "adjacencies": [
  120. "graphnode3",
  121. {
  122. "nodeTo": "graphnode2",
  123. "nodeFrom": "graphnode3",
  124. "data": {
  125. "$color": "#557EAA"
  126. }
  127. }, {
  128. "nodeTo": "graphnode4",
  129. "nodeFrom": "graphnode3",
  130. "data": {
  131. "$color": "#909291"
  132. }
  133. }, {
  134. "nodeTo": "graphnode9",
  135. "nodeFrom": "graphnode3",
  136. "data": {
  137. "$color": "#557EAA"
  138. }
  139. }
  140. ],
  141. "data": {
  142. "$color": "#83548B",
  143. "$type": "circle",
  144. "$dim": 8
  145. },
  146. "id": "graphnode3",
  147. "name": "graphnode3"
  148. }, {
  149. "adjacencies": [
  150. "graphnode4",
  151. {
  152. "nodeTo": "graphnode3",
  153. "nodeFrom": "graphnode4",
  154. "data": {
  155. "$color": "#557EAA"
  156. }
  157. }, {
  158. "nodeTo": "graphnode5",
  159. "nodeFrom": "graphnode4",
  160. "data": {
  161. "$color": "#909291"
  162. }
  163. }, {
  164. "nodeTo": "graphnode8",
  165. "nodeFrom": "graphnode4",
  166. "data": {
  167. "$color": "#557EAA"
  168. }
  169. }
  170. ],
  171. "data": {
  172. "$color": "#83548B",
  173. "$type": "circle",
  174. "$dim": 8
  175. },
  176. "id": "graphnode4",
  177. "name": "graphnode4"
  178. }, {
  179. "adjacencies": [
  180. "graphnode5",
  181. {
  182. "nodeTo": "graphnode4",
  183. "nodeFrom": "graphnode5",
  184. "data": {
  185. "$color": "#557EAA"
  186. }
  187. }, {
  188. "nodeTo": "graphnode0",
  189. "nodeFrom": "graphnode5",
  190. "data": {
  191. "$color": "#909291"
  192. }
  193. }, {
  194. "nodeTo": "graphnode6",
  195. "nodeFrom": "graphnode5",
  196. "data": {
  197. "$color": "#557EAA"
  198. }
  199. }
  200. ],
  201. "data": {
  202. "$color": "#83548B",
  203. "$type": "circle",
  204. "$dim": 8
  205. },
  206. "id": "graphnode5",
  207. "name": "graphnode5"
  208. }, {
  209. "adjacencies": [
  210. "graphnode6",
  211. {
  212. "nodeTo": "graphnode5",
  213. "nodeFrom": "graphnode6",
  214. "data": {
  215. "$color": "#557EAA"
  216. }
  217. }, {
  218. "nodeTo": "graphnode2",
  219. "nodeFrom": "graphnode6",
  220. "data": {
  221. "$color": "#909291"
  222. }
  223. }, {
  224. "nodeTo": "graphnode7",
  225. "nodeFrom": "graphnode6",
  226. "data": {
  227. "$color": "#557EAA"
  228. }
  229. }
  230. ],
  231. "data": {
  232. "$color": "#83548B",
  233. "$type": "circle",
  234. "$dim": 8
  235. },
  236. "id": "graphnode6",
  237. "name": "graphnode6"
  238. }, {
  239. "adjacencies": [
  240. "graphnode7",
  241. {
  242. "nodeTo": "graphnode6",
  243. "nodeFrom": "graphnode7",
  244. "data": {
  245. "$color": "#557EAA"
  246. }
  247. }, {
  248. "nodeTo": "graphnode8",
  249. "nodeFrom": "graphnode7",
  250. "data": {
  251. "$color": "#909291"
  252. }
  253. }, {
  254. "nodeTo": "graphnode9",
  255. "nodeFrom": "graphnode7",
  256. "data": {
  257. "$color": "#557EAA"
  258. }
  259. }
  260. ],
  261. "data": {
  262. "$color": "#83548B",
  263. "$type": "circle",
  264. "$dim": 8
  265. },
  266. "id": "graphnode7",
  267. "name": "graphnode7"
  268. }, {
  269. "adjacencies": [
  270. "graphnode8",
  271. {
  272. "nodeTo": "graphnode1",
  273. "nodeFrom": "graphnode8",
  274. "data": {
  275. "$color": "#557EAA"
  276. }
  277. }, {
  278. "nodeTo": "graphnode4",
  279. "nodeFrom": "graphnode8",
  280. "data": {
  281. "$color": "#909291"
  282. }
  283. }, {
  284. "nodeTo": "graphnode7",
  285. "nodeFrom": "graphnode8",
  286. "data": {
  287. "$color": "#557EAA"
  288. }
  289. }
  290. ],
  291. "data": {
  292. "$color": "#83548B",
  293. "$type": "circle",
  294. "$dim": 8
  295. },
  296. "id": "graphnode8",
  297. "name": "graphnode8"
  298. }, {
  299. "adjacencies": [
  300. "graphnode9",
  301. {
  302. "nodeTo": "graphnode0",
  303. "nodeFrom": "graphnode9",
  304. "data": {
  305. "$color": "#557EAA"
  306. }
  307. }, {
  308. "nodeTo": "graphnode3",
  309. "nodeFrom": "graphnode9",
  310. "data": {
  311. "$color": "#909291"
  312. }
  313. }, {
  314. "nodeTo": "graphnode7",
  315. "nodeFrom": "graphnode9",
  316. "data": {
  317. "$color": "#557EAA"
  318. }
  319. }
  320. ],
  321. "data": {
  322. "$color": "#83548B",
  323. "$type": "circle",
  324. "$dim": 8
  325. },
  326. "id": "graphnode9",
  327. "name": "graphnode9"
  328. }
  329. ];
  330. // end
  331. // init ForceDirected
  332. var fd = new $jit.ForceDirected({
  333. //id of the visualization container
  334. injectInto: 'infovis',
  335. //Enable zooming and panning
  336. //by scrolling and DnD
  337. Navigation: {
  338. enable: true,
  339. //Enable panning events only if we're dragging the empty
  340. //canvas (and not a node).
  341. panning: 'avoid nodes',
  342. zooming: 10 //zoom speed. higher is more sensible
  343. },
  344. // Change node and edge styles such as
  345. // color and width.
  346. // These properties are also set per node
  347. // with dollar prefixed data-properties in the
  348. // JSON structure.
  349. Node: {
  350. overridable: true
  351. },
  352. Edge: {
  353. overridable: true,
  354. color: '#23A4FF',
  355. lineWidth: 0.4
  356. },
  357. //Native canvas text styling
  358. Label: {
  359. type: labelType, //Native or HTML
  360. size: 10,
  361. style: 'bold'
  362. },
  363. //Add Tips
  364. Tips: {
  365. enable: true,
  366. onShow: function(tip, node) {
  367. //count connections
  368. var count = 0;
  369. node.eachAdjacency(function() { count++; });
  370. //display node info in tooltip
  371. tip.innerHTML = "<div class=\"tip-title\">" + node.name + "</div>"
  372. + "<div class=\"tip-text\"><b>connections:</b> " + count + "</div>";
  373. }
  374. },
  375. // Add node events
  376. Events: {
  377. enable: true,
  378. type: 'Native',
  379. //Change cursor style when hovering a node
  380. onMouseEnter: function() {
  381. fd.canvas.getElement().style.cursor = 'move';
  382. },
  383. onMouseLeave: function() {
  384. fd.canvas.getElement().style.cursor = '';
  385. },
  386. //Update node positions when dragged
  387. onDragMove: function(node, eventInfo, e) {
  388. var pos = eventInfo.getPos();
  389. node.pos.setc(pos.x, pos.y);
  390. fd.plot();
  391. },
  392. //Implement the same handler for touchscreens
  393. onTouchMove: function(node, eventInfo, e) {
  394. $jit.util.event.stop(e); //stop default touchmove event
  395. this.onDragMove(node, eventInfo, e);
  396. },
  397. //Add also a click handler to nodes
  398. onClick: function(node) {
  399. if(!node) return;
  400. // Build the right column relations list.
  401. // This is done by traversing the clicked node connections.
  402. var html = "<h4>" + node.name + "</h4><b> connections:</b><ul><li>",
  403. list = [];
  404. node.eachAdjacency(function(adj){
  405. list.push(adj.nodeTo.name);
  406. });
  407. //append connections information
  408. $jit.id('inner-details').innerHTML = html + list.join("</li><li>") + "</li></ul>";
  409. }
  410. },
  411. //Number of iterations for the FD algorithm
  412. iterations: 200,
  413. //Edge length
  414. levelDistance: 130,
  415. // Add text to the labels. This method is only triggered
  416. // on label creation and only for DOM labels (not native canvas ones).
  417. onCreateLabel: function(domElement, node){
  418. domElement.innerHTML = node.name;
  419. var style = domElement.style;
  420. style.fontSize = "0.8em";
  421. style.color = "#ddd";
  422. },
  423. // Change node styles when DOM labels are placed
  424. // or moved.
  425. onPlaceLabel: function(domElement, node){
  426. var style = domElement.style;
  427. var left = parseInt(style.left);
  428. var top = parseInt(style.top);
  429. var w = domElement.offsetWidth;
  430. style.left = (left - w / 2) + 'px';
  431. style.top = (top + 10) + 'px';
  432. style.display = '';
  433. }
  434. });
  435. // load JSON data.
  436. fd.loadJSON(json);
  437. // compute positions incrementally and animate.
  438. fd.computeIncremental({
  439. iter: 40,
  440. property: 'end',
  441. onStep: function(perc){
  442. Log.write(perc + '% loaded...');
  443. },
  444. onComplete: function(){
  445. Log.write('done');
  446. fd.animate({
  447. modes: ['linear'],
  448. transition: $jit.Trans.Elastic.easeOut,
  449. duration: 2500
  450. });
  451. }
  452. });
  453. // end
  454. }