vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

660 lines
19 KiB

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <script src="../dist/vis.js"></script>
  5. <link href="http://visjs.org/dist/vis.css" rel="stylesheet" type="text/css" />
  6. <style type="text/css">
  7. .graph {
  8. width: 600px;
  9. height: 500px;
  10. } </style>
  11. </head>
  12. <body>
  13. Reset causes crash
  14. <pre>
  15. When you press the button, the nodes dataset will receive an update which shall reset the font and color properties, instead, vis crashes :(
  16. (The browser console shows the details)
  17. </pre>
  18. <button onclick="javascript:action()">Click me!</button>
  19. <div class="graph" id="visualization1"></div>
  20. <script type="text/javascript">
  21. var action = function() {
  22. nodes.update([
  23. {
  24. "id": "35b99ac8-aa07-4834-b508-6e9435f573cb",
  25. "label": "Adam",
  26. "x": -96,
  27. "y": -382,
  28. "fixed": {
  29. "x": true,
  30. "y": true
  31. },
  32. "color": null,
  33. "font": null
  34. },
  35. {
  36. "id": "38d37e10-3c8b-487c-9757-1061d2bbbd07",
  37. "label": "Betsy",
  38. "x": -19,
  39. "y": -548,
  40. "fixed": {
  41. "x": true,
  42. "y": true
  43. },
  44. "color": null,
  45. "font": null
  46. },
  47. {
  48. "id": "4762501a-6bcb-4d35-acfd-f2e9d6310389",
  49. "label": "Carol",
  50. "x": -243,
  51. "y": -378,
  52. "fixed": {
  53. "x": true,
  54. "y": true
  55. },
  56. "color": null,
  57. "font": null
  58. },
  59. {
  60. "id": "7e17b617-b7a4-4f70-937b-f45ec8dc0a9d",
  61. "label": "Catherine",
  62. "x": -551,
  63. "y": -214,
  64. "fixed": {
  65. "x": true,
  66. "y": true
  67. },
  68. "color": null,
  69. "font": null
  70. },
  71. {
  72. "id": "e017a09b-4526-4b4d-b097-f70e56f20ab4",
  73. "label": "Craig",
  74. "x": 155,
  75. "y": -381,
  76. "fixed": {
  77. "x": true,
  78. "y": true
  79. },
  80. "color": null,
  81. "font": null
  82. },
  83. {
  84. "id": "8ec7f3ee-35f9-4c56-a57b-12d91c0c372e",
  85. "label": "George",
  86. "x": -302,
  87. "y": -536,
  88. "fixed": {
  89. "x": true,
  90. "y": true
  91. },
  92. "color": null,
  93. "font": null
  94. },
  95. {
  96. "id": "8d2a0469-bb6a-4e40-ab1f-3d5edc620415",
  97. "label": "Jessica",
  98. "x": -4,
  99. "y": -379,
  100. "fixed": {
  101. "x": true,
  102. "y": true
  103. },
  104. "color": null,
  105. "font": null
  106. },
  107. {
  108. "id": "afd75e95-e45a-4dea-848a-e16ca735bc57",
  109. "label": "John",
  110. "x": -531,
  111. "y": -543,
  112. "fixed": {
  113. "x": true,
  114. "y": true
  115. },
  116. "color": null,
  117. "font": null
  118. },
  119. {
  120. "id": "9c050064-d805-48b3-80a6-181d3599daff",
  121. "label": "Mary",
  122. "x": -280,
  123. "y": -210,
  124. "fixed": {
  125. "x": true,
  126. "y": true
  127. },
  128. "color": null,
  129. "font": null
  130. },
  131. {
  132. "id": "f24ab062-62a1-4555-a57f-dfcd597c3515",
  133. "label": "Michael",
  134. "x": -98,
  135. "y": -211,
  136. "fixed": {
  137. "x": true,
  138. "y": true
  139. },
  140. "color": null,
  141. "font": null
  142. },
  143. {
  144. "id": "59d10b18-1c9e-4dcb-ba4f-988ebd7a3efb",
  145. "label": "Michelle",
  146. "x": -630,
  147. "y": -387,
  148. "fixed": {
  149. "x": true,
  150. "y": true
  151. },
  152. "color": null,
  153. "font": null
  154. },
  155. {
  156. "id": "b11149a2-9050-4632-b095-d9d233319b91",
  157. "label": "Robert",
  158. "x": -360,
  159. "y": -380,
  160. "fixed": {
  161. "x": true,
  162. "y": true
  163. },
  164. "color": null,
  165. "font": null
  166. },
  167. {
  168. "id": "a1485262-99fe-4b1e-a246-cc70fbb1859a",
  169. "label": "Sharon",
  170. "x": -532,
  171. "y": -384,
  172. "fixed": {
  173. "x": true,
  174. "y": true
  175. },
  176. "color": null,
  177. "font": null
  178. }
  179. ]);
  180. }
  181. var nodes = new vis.DataSet([
  182. {
  183. "id": "35b99ac8-aa07-4834-b508-6e9435f573cb",
  184. "label": "Adam",
  185. "color": {
  186. "background": "rgba(252,245,170,1)",
  187. "border": "rgba(183,148,134,1)"
  188. },
  189. "x": -96,
  190. "y": -382,
  191. "fixed": {
  192. "x": true,
  193. "y": true
  194. },
  195. "font": {
  196. "color": "#000000"
  197. }
  198. },
  199. {
  200. "id": "38d37e10-3c8b-487c-9757-1061d2bbbd07",
  201. "label": "Betsy",
  202. "color": {
  203. "background": "rgba(252,245,170,1)",
  204. "border": "rgba(183,148,134,1)"
  205. },
  206. "x": -19,
  207. "y": -548,
  208. "fixed": {
  209. "x": true,
  210. "y": true
  211. },
  212. "font": {
  213. "color": "#000000"
  214. }
  215. },
  216. {
  217. "id": "4762501a-6bcb-4d35-acfd-f2e9d6310389",
  218. "label": "Carol",
  219. "color": {
  220. "background": "rgba(252,245,170,1)",
  221. "border": "rgba(183,148,134,1)"
  222. },
  223. "x": -243,
  224. "y": -378,
  225. "fixed": {
  226. "x": true,
  227. "y": true
  228. },
  229. "font": {
  230. "color": "#000000"
  231. }
  232. },
  233. {
  234. "id": "7e17b617-b7a4-4f70-937b-f45ec8dc0a9d",
  235. "label": "Catherine",
  236. "color": {
  237. "background": "rgba(252,245,170,1)",
  238. "border": "rgba(183,148,134,1)"
  239. },
  240. "x": -551,
  241. "y": -214,
  242. "fixed": {
  243. "x": true,
  244. "y": true
  245. },
  246. "font": {
  247. "color": "#000000"
  248. }
  249. },
  250. {
  251. "id": "e017a09b-4526-4b4d-b097-f70e56f20ab4",
  252. "label": "Craig",
  253. "color": {
  254. "background": "rgba(252,245,170,1)",
  255. "border": "rgba(183,148,134,1)"
  256. },
  257. "x": 155,
  258. "y": -381,
  259. "fixed": {
  260. "x": true,
  261. "y": true
  262. },
  263. "font": {
  264. "color": "#000000"
  265. }
  266. },
  267. {
  268. "id": "8ec7f3ee-35f9-4c56-a57b-12d91c0c372e",
  269. "label": "George",
  270. "color": {
  271. "background": "rgba(252,245,170,1)",
  272. "border": "rgba(183,148,134,1)"
  273. },
  274. "x": -302,
  275. "y": -536,
  276. "fixed": {
  277. "x": true,
  278. "y": true
  279. },
  280. "font": {
  281. "color": "#000000"
  282. }
  283. },
  284. {
  285. "id": "8d2a0469-bb6a-4e40-ab1f-3d5edc620415",
  286. "label": "Jessica",
  287. "color": {
  288. "background": "rgba(252,245,170,1)",
  289. "border": "rgba(183,148,134,1)"
  290. },
  291. "x": -4,
  292. "y": -379,
  293. "fixed": {
  294. "x": true,
  295. "y": true
  296. },
  297. "font": {
  298. "color": "#000000"
  299. }
  300. },
  301. {
  302. "id": "afd75e95-e45a-4dea-848a-e16ca735bc57",
  303. "label": "John",
  304. "color": {
  305. "background": "rgba(252,245,170,1)",
  306. "border": "rgba(183,148,134,1)"
  307. },
  308. "x": -531,
  309. "y": -543,
  310. "fixed": {
  311. "x": true,
  312. "y": true
  313. },
  314. "font": {
  315. "color": "#000000"
  316. }
  317. },
  318. {
  319. "id": "9c050064-d805-48b3-80a6-181d3599daff",
  320. "label": "Mary",
  321. "color": {
  322. "background": "rgba(252,245,170,1)",
  323. "border": "rgba(183,148,134,1)"
  324. },
  325. "x": -280,
  326. "y": -210,
  327. "fixed": {
  328. "x": true,
  329. "y": true
  330. },
  331. "font": {
  332. "color": "#000000"
  333. }
  334. },
  335. {
  336. "id": "f24ab062-62a1-4555-a57f-dfcd597c3515",
  337. "label": "Michael",
  338. "color": {
  339. "background": "rgba(252,245,170,1)",
  340. "border": "rgba(183,148,134,1)"
  341. },
  342. "x": -98,
  343. "y": -211,
  344. "fixed": {
  345. "x": true,
  346. "y": true
  347. },
  348. "font": {
  349. "color": "#000000"
  350. }
  351. },
  352. {
  353. "id": "59d10b18-1c9e-4dcb-ba4f-988ebd7a3efb",
  354. "label": "Michelle",
  355. "color": {
  356. "background": "rgba(252,245,170,1)",
  357. "border": "rgba(183,148,134,1)"
  358. },
  359. "x": -630,
  360. "y": -387,
  361. "fixed": {
  362. "x": true,
  363. "y": true
  364. },
  365. "font": {
  366. "color": "#000000"
  367. }
  368. },
  369. {
  370. "id": "b11149a2-9050-4632-b095-d9d233319b91",
  371. "label": "Robert",
  372. "color": {
  373. "background": "rgba(252,245,170,1)",
  374. "border": "rgba(183,148,134,1)"
  375. },
  376. "x": -360,
  377. "y": -380,
  378. "fixed": {
  379. "x": true,
  380. "y": true
  381. },
  382. "font": {
  383. "color": "#000000"
  384. }
  385. },
  386. {
  387. "id": "a1485262-99fe-4b1e-a246-cc70fbb1859a",
  388. "label": "Sharon",
  389. "color": {
  390. "background": "rgba(252,245,170,1)",
  391. "border": "rgba(183,148,134,1)"
  392. },
  393. "x": -532,
  394. "y": -384,
  395. "fixed": {
  396. "x": true,
  397. "y": true
  398. },
  399. "font": {
  400. "color": "#000000"
  401. }
  402. }
  403. ]);
  404. var edges = new vis.DataSet([
  405. {
  406. "id": "62b28c2f-c6dc-4b6b-b108-722eb47d5972",
  407. "from": "35b99ac8-aa07-4834-b508-6e9435f573cb",
  408. "to": "f24ab062-62a1-4555-a57f-dfcd597c3515",
  409. "type": "family:father of",
  410. "label": "father of",
  411. "color": "#bc960d",
  412. "labelAlignment": "line-above"
  413. },
  414. {
  415. "id": "4ae8de41-ef1a-400a-b51b-05662ce24597",
  416. "from": "38d37e10-3c8b-487c-9757-1061d2bbbd07",
  417. "to": "e017a09b-4526-4b4d-b097-f70e56f20ab4",
  418. "type": "family:mother of",
  419. "label": "mother of",
  420. "color": "#eec75b",
  421. "labelAlignment": "line-above"
  422. },
  423. {
  424. "id": "70f6e36a-ce9a-47b4-97e1-14b2df17d204",
  425. "from": "38d37e10-3c8b-487c-9757-1061d2bbbd07",
  426. "to": "8d2a0469-bb6a-4e40-ab1f-3d5edc620415",
  427. "type": "family:mother of",
  428. "label": "mother of",
  429. "color": "#eec75b",
  430. "labelAlignment": "line-above"
  431. },
  432. {
  433. "id": "c32a86ce-ab9a-49b6-a202-e93a37265fda",
  434. "from": "38d37e10-3c8b-487c-9757-1061d2bbbd07",
  435. "to": "b11149a2-9050-4632-b095-d9d233319b91",
  436. "type": "family:mother of",
  437. "label": "mother of",
  438. "color": "#eec75b",
  439. "labelAlignment": "line-above"
  440. },
  441. {
  442. "id": "bbce9cc0-0353-45cd-bfa6-a13834ff7218",
  443. "from": "4762501a-6bcb-4d35-acfd-f2e9d6310389",
  444. "to": "f24ab062-62a1-4555-a57f-dfcd597c3515",
  445. "type": "family:mother of",
  446. "label": "mother of",
  447. "color": "#eec75b",
  448. "labelAlignment": "line-above"
  449. },
  450. {
  451. "id": "deb7eb05-f667-496b-aeda-205c37b2484b",
  452. "from": "8ec7f3ee-35f9-4c56-a57b-12d91c0c372e",
  453. "to": "afd75e95-e45a-4dea-848a-e16ca735bc57",
  454. "type": "family:brother of",
  455. "label": "brother of",
  456. "color": "green",
  457. "labelAlignment": "line-above"
  458. },
  459. {
  460. "id": "3c8c7947-6c82-4f98-978a-26bbb372bfbb",
  461. "from": "afd75e95-e45a-4dea-848a-e16ca735bc57",
  462. "to": "a1485262-99fe-4b1e-a246-cc70fbb1859a",
  463. "type": "family:father of",
  464. "label": "father of",
  465. "color": "#bc960d",
  466. "labelAlignment": "line-above"
  467. },
  468. {
  469. "id": "cd4e71e7-9735-4a42-8632-e88cc01ab64f",
  470. "from": "f24ab062-62a1-4555-a57f-dfcd597c3515",
  471. "to": "9c050064-d805-48b3-80a6-181d3599daff",
  472. "type": "family:brother of",
  473. "label": "brother of",
  474. "color": "green",
  475. "labelAlignment": "line-above"
  476. },
  477. {
  478. "id": "b4499f60-04f9-4306-9c57-3d73b72f37c0",
  479. "from": "59d10b18-1c9e-4dcb-ba4f-988ebd7a3efb",
  480. "to": "7e17b617-b7a4-4f70-937b-f45ec8dc0a9d",
  481. "type": "family:mother of",
  482. "label": "mother of",
  483. "color": "#eec75b",
  484. "labelAlignment": "line-above"
  485. },
  486. {
  487. "id": "16562853-b597-44f6-9c55-2ce8a1fe4f83",
  488. "from": "b11149a2-9050-4632-b095-d9d233319b91",
  489. "to": "7e17b617-b7a4-4f70-937b-f45ec8dc0a9d",
  490. "type": "family:father of",
  491. "label": "father of",
  492. "color": "#bc960d",
  493. "labelAlignment": "line-above"
  494. },
  495. {
  496. "id": "599dd644-e61f-401e-8b17-dfdd340c125e",
  497. "from": "b11149a2-9050-4632-b095-d9d233319b91",
  498. "to": "9c050064-d805-48b3-80a6-181d3599daff",
  499. "type": "family:father of",
  500. "label": "father of",
  501. "color": "#bc960d",
  502. "labelAlignment": "line-above"
  503. },
  504. {
  505. "id": "961f7f2b-5abe-4036-a155-aa9b30ae1260",
  506. "from": "b11149a2-9050-4632-b095-d9d233319b91",
  507. "to": "a1485262-99fe-4b1e-a246-cc70fbb1859a",
  508. "type": "family:married to",
  509. "title": "Does not need to be a christian marriage though...",
  510. "label": "married to",
  511. "color": "#17bba3",
  512. "labelAlignment": "line-above"
  513. }
  514. ]);
  515. var data = {
  516. nodes: nodes,
  517. edges: edges
  518. };
  519. var options = {
  520. "autoResize": true,
  521. "height": "100%",
  522. "width": "100%",
  523. "interaction": {
  524. "dragNodes":true,
  525. "dragView": true,
  526. "hideEdgesOnDrag": false,
  527. "hideNodesOnDrag": false,
  528. "hover": false,
  529. "navigationButtons": true,
  530. "selectable": true,
  531. "selectConnectedEdges": true,
  532. "tooltipDelay": 300,
  533. "zoomView": true,
  534. "keyboard": {
  535. "enabled": false,
  536. "speed": {
  537. "x": 10,
  538. "y": 10,
  539. "zoom": 0.02
  540. },
  541. "bindToWindow": true
  542. }
  543. },
  544. "nodes": {
  545. "shape": "box",
  546. font: {color:"#ff0000"},
  547. "color": {
  548. "border" : "#2B7CE9",
  549. "background" : "#97C2FC",
  550. "highlight": {
  551. "border": "#2B7CE9",
  552. "background": "#D2E5FF"
  553. },
  554. "hover": {
  555. "border": "white",
  556. "background": "white"
  557. }
  558. }
  559. },
  560. "physics": {
  561. "barnesHut": {
  562. "centralGravity": 0,
  563. "springLength": 240
  564. }
  565. },
  566. "groups": {
  567. "neighbours": {
  568. "color": "#E6E6E6"
  569. },
  570. "matches": {
  571. "color": "#97C2FC"
  572. }
  573. },
  574. "locale": "en_EN"
  575. }
  576. ;
  577. var container = document.getElementById("visualization1");
  578. var network = new vis.Network(container, data, options);
  579. var hasNetworkStabilized = false;
  580. network.on("stabilized", function(properties) {
  581. if(!hasNetworkStabilized) {
  582. hasNetworkStabilized = true;
  583. network.storePositions();
  584. setNodesMoveable(data.nodes.get(), false);
  585. network.fit();
  586. }
  587. });
  588. network.on("dragStart", function(properties) {
  589. if(properties.nodes.length) {
  590. var node = data.nodes.get(properties.nodes[0]);
  591. setNodesMoveable([ node ], true);
  592. }
  593. });
  594. network.on("dragEnd", function(properties) {
  595. if(properties.nodes.length) {
  596. var node = data.nodes.get(properties.nodes[0]);
  597. setNodesMoveable([ node ], false);
  598. }
  599. });
  600. setNodesMoveable = function(nodes, isMoveable) {
  601. // first store positions
  602. network.storePositions();
  603. var updates = [];
  604. var keys = Object.keys(nodes);
  605. for(var i = 0; i < keys.length; i++) {
  606. var id = nodes[keys[i]].id;
  607. var update = {
  608. id: id,
  609. fixed: { // v4: formerly allowToMoveX, allowToMoveY
  610. x: !isMoveable,
  611. y: !isMoveable
  612. }
  613. };
  614. updates.push(update);
  615. }
  616. data.nodes.update(updates);
  617. console.log(updates);
  618. };
  619. </script>
  620. </body>
  621. </html>