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.

1170 lines
39 KiB

  1. /**
  2. *
  3. * Useful during debugging
  4. * =======================
  5. *
  6. * console.log(JSON.stringify(output, null, 2));
  7. *
  8. * for (let i in network.body.edges) {
  9. * let edge = network.body.edges[i];
  10. * console.log("" + i + ": from: " + edge.fromId + ", to: " + edge.toId);
  11. * }
  12. */
  13. var fs = require('fs');
  14. var assert = require('assert');
  15. var vis = require('../dist/vis');
  16. var Network = vis.network;
  17. var jsdom_global = require('jsdom-global');
  18. var stdout = require('test-console').stdout;
  19. var Validator = require("./../lib/shared/Validator").default;
  20. //var {printStyle} = require('./../lib/shared/Validator');
  21. /**
  22. * Merge all options of object b into object b
  23. * @param {Object} a
  24. * @param {Object} b
  25. * @return {Object} a
  26. *
  27. * Adapted merge() in dotparser.js
  28. */
  29. function merge (a, b) {
  30. if (!a) {
  31. a = {};
  32. }
  33. if (b) {
  34. for (var name in b) {
  35. if (b.hasOwnProperty(name)) {
  36. if (typeof b[name] === 'object') {
  37. a[name] = merge(a[name], b[name]);
  38. } else {
  39. a[name] = b[name];
  40. }
  41. }
  42. }
  43. }
  44. return a;
  45. }
  46. /**
  47. * Load legacy-style (i.e. not module) javascript files into the given context.
  48. */
  49. function include(list, context) {
  50. if (!(list instanceof Array)) {
  51. list = [list];
  52. }
  53. for (var n in list) {
  54. var path = list[n];
  55. var arr = [fs.readFileSync(path) + ''];
  56. eval.apply(context, arr);
  57. }
  58. }
  59. /**
  60. * Defined network consists of two sub-networks:
  61. *
  62. * - 1-2-3-4
  63. * - 11-12-13-14
  64. *
  65. * For reference, this is the sample network of issue #1218
  66. */
  67. function createSampleNetwork(options) {
  68. var NumInitialNodes = 8;
  69. var NumInitialEdges = 6;
  70. var nodes = new vis.DataSet([
  71. {id: 1, label: '1'},
  72. {id: 2, label: '2'},
  73. {id: 3, label: '3'},
  74. {id: 4, label: '4'},
  75. {id: 11, label: '11'},
  76. {id: 12, label: '12'},
  77. {id: 13, label: '13'},
  78. {id: 14, label: '14'},
  79. ]);
  80. var edges = new vis.DataSet([
  81. {from: 1, to: 2},
  82. {from: 2, to: 3},
  83. {from: 3, to: 4},
  84. {from: 11, to: 12},
  85. {from: 12, to: 13},
  86. {from: 13, to: 14},
  87. ]);
  88. // create a network
  89. var container = document.getElementById('mynetwork');
  90. var data = {
  91. nodes: nodes,
  92. edges: edges
  93. };
  94. var defaultOptions = {
  95. layout: {
  96. randomSeed: 8
  97. },
  98. edges: {
  99. smooth: {
  100. type: 'continuous' // avoid dynamic here, it adds extra hidden nodes
  101. }
  102. }
  103. };
  104. options = merge(defaultOptions, options);
  105. var network = new vis.Network(container, data, options);
  106. assertNumNodes(network, NumInitialNodes);
  107. assertNumEdges(network, NumInitialEdges);
  108. return [network, data, NumInitialNodes, NumInitialEdges];
  109. };
  110. /**
  111. * Create a cluster for the dynamic data change cases.
  112. *
  113. * Works on the network created by createSampleNetwork().
  114. *
  115. * This is actually a pathological case; there are two separate sub-networks and
  116. * a cluster is made of two nodes, each from one of the sub-networks.
  117. */
  118. function createCluster(network) {
  119. var clusterOptionsByData = {
  120. joinCondition: function(node) {
  121. if (node.id == 1 || node.id == 11) return true;
  122. return false;
  123. },
  124. clusterNodeProperties: {id:"c1", label:'c1'}
  125. }
  126. network.cluster(clusterOptionsByData);
  127. }
  128. /**
  129. * Display node/edge state, useful during debugging
  130. */
  131. function log(network) {
  132. console.log(Object.keys(network.body.nodes));
  133. console.log(network.body.nodeIndices);
  134. console.log(Object.keys(network.body.edges));
  135. console.log(network.body.edgeIndices);
  136. };
  137. /**
  138. * Note that only the node and edges counts are asserted.
  139. * This might be done more thoroughly by explicitly checking the id's
  140. */
  141. function assertNumNodes(network, expectedPresent, expectedVisible) {
  142. if (expectedVisible === undefined) expectedVisible = expectedPresent;
  143. assert.equal(Object.keys(network.body.nodes).length, expectedPresent, "Total number of nodes does not match");
  144. assert.equal(network.body.nodeIndices.length, expectedVisible, "Number of visible nodes does not match");
  145. };
  146. /**
  147. * Comment at assertNumNodes() also applies.
  148. */
  149. function assertNumEdges(network, expectedPresent, expectedVisible) {
  150. if (expectedVisible === undefined) expectedVisible = expectedPresent;
  151. assert.equal(Object.keys(network.body.edges).length, expectedPresent, "Total number of edges does not match");
  152. assert.equal(network.body.edgeIndices.length, expectedVisible, "Number of visible edges does not match");
  153. };
  154. describe('Network', function () {
  155. before(function() {
  156. this.jsdom_global = jsdom_global(
  157. "<div id='mynetwork'></div>",
  158. { skipWindowCheck: true}
  159. );
  160. this.container = document.getElementById('mynetwork');
  161. });
  162. after(function() {
  163. this.jsdom_global();
  164. });
  165. /////////////////////////////////////////////////////
  166. // Local helper methods for Edge and Node testing
  167. /////////////////////////////////////////////////////
  168. /**
  169. * Simplify network creation for local tests
  170. */
  171. function createNetwork(options) {
  172. var [network, data, numNodes, numEdges] = createSampleNetwork(options);
  173. return network;
  174. }
  175. function firstNode(network) {
  176. for (var id in network.body.nodes) {
  177. return network.body.nodes[id];
  178. }
  179. return undefined;
  180. }
  181. function firstEdge(network) {
  182. for (var id in network.body.edges) {
  183. return network.body.edges[id];
  184. }
  185. return undefined;
  186. }
  187. function checkChooserValues(item, chooser, labelChooser) {
  188. if (chooser === 'function') {
  189. assert.equal(typeof item.chooser, 'function');
  190. } else {
  191. assert.equal(item.chooser, chooser);
  192. }
  193. if (labelChooser === 'function') {
  194. assert.equal(typeof item.labelModule.fontOptions.chooser, 'function');
  195. } else {
  196. assert.equal(item.labelModule.fontOptions.chooser, labelChooser);
  197. }
  198. }
  199. describe('Node', function () {
  200. /**
  201. * NOTE: choosify tests of Node and Edge are parallel
  202. * TODO: consolidate this is necessary
  203. */
  204. it('properly handles choosify input', function () {
  205. // check defaults
  206. var options = {};
  207. var network = createNetwork(options);
  208. checkChooserValues(firstNode(network), true, true);
  209. // There's no point in checking invalid values here; these are detected by the options parser
  210. // and subsequently handled as missing input, thus assigned defaults
  211. // check various combinations of valid input
  212. options = {nodes: {chosen: false}};
  213. network = createNetwork(options);
  214. checkChooserValues(firstNode(network), false, false);
  215. options = {nodes: {chosen: { node:true, label:false}}};
  216. network = createNetwork(options);
  217. checkChooserValues(firstNode(network), true, false);
  218. options = {nodes: {chosen: {
  219. node:true,
  220. label: function(value, id, selected, hovering) {}
  221. }}};
  222. network = createNetwork(options);
  223. checkChooserValues(firstNode(network), true, 'function');
  224. options = {nodes: {chosen: {
  225. node: function(value, id, selected, hovering) {},
  226. label:false,
  227. }}};
  228. network = createNetwork(options);
  229. checkChooserValues(firstNode(network), 'function', false);
  230. });
  231. }); // Node
  232. describe('Edge', function () {
  233. /**
  234. * NOTE: choosify tests of Node and Edge are parallel
  235. * TODO: consolidate this is necessary
  236. */
  237. it('properly handles choosify input', function () {
  238. // check defaults
  239. var options = {};
  240. var network = createNetwork(options);
  241. checkChooserValues(firstEdge(network), true, true);
  242. // There's no point in checking invalid values here; these are detected by the options parser
  243. // and subsequently handled as missing input, thus assigned defaults
  244. // check various combinations of valid input
  245. options = {edges: {chosen: false}};
  246. network = createNetwork(options);
  247. checkChooserValues(firstEdge(network), false, false);
  248. options = {edges: {chosen: { edge:true, label:false}}};
  249. network = createNetwork(options);
  250. checkChooserValues(firstEdge(network), true, false);
  251. options = {edges: {chosen: {
  252. edge:true,
  253. label: function(value, id, selected, hovering) {}
  254. }}};
  255. network = createNetwork(options);
  256. checkChooserValues(firstEdge(network), true, 'function');
  257. options = {edges: {chosen: {
  258. edge: function(value, id, selected, hovering) {},
  259. label:false,
  260. }}};
  261. network = createNetwork(options);
  262. checkChooserValues(firstEdge(network), 'function', false);
  263. });
  264. /**
  265. * Support routine for next unit test
  266. */
  267. function createDataforColorChange() {
  268. var nodes = new vis.DataSet([
  269. {id: 1, label: 'Node 1' }, // group:'Group1'},
  270. {id: 2, label: 'Node 2', group:'Group2'},
  271. {id: 3, label: 'Node 3'},
  272. ]);
  273. // create an array with edges
  274. var edges = new vis.DataSet([
  275. {id: 1, from: 1, to: 2},
  276. {id: 2, from: 1, to: 3, color: { inherit: 'to'}},
  277. {id: 3, from: 3, to: 3, color: { color: '#00FF00'}},
  278. {id: 4, from: 2, to: 3, color: { inherit: 'from'}},
  279. ]);
  280. var data = {
  281. nodes: nodes,
  282. edges: edges
  283. };
  284. return data;
  285. }
  286. /**
  287. * Unit test for fix of #3350
  288. *
  289. * The issue is that changing color options is not registered in the nodes.
  290. * We test the updates the color options in the general edges options here.
  291. */
  292. it('sets inherit color option for edges on call to Network.setOptions()', function () {
  293. var container = document.getElementById('mynetwork');
  294. var data = createDataforColorChange();
  295. var options = {
  296. "edges" : { "color" : { "inherit" : "to" } },
  297. };
  298. // Test passing options on init.
  299. var network = new vis.Network(container, data, options);
  300. var edges = network.body.edges;
  301. assert.equal(edges[1].options.color.inherit, 'to'); // new default
  302. assert.equal(edges[2].options.color.inherit, 'to'); // set in edge
  303. assert.equal(edges[3].options.color.inherit, false); // has explicit color
  304. assert.equal(edges[4].options.color.inherit, 'from'); // set in edge
  305. // Sanity check: colors should still be defaults
  306. assert.equal(edges[1].options.color.color, network.edgesHandler.options.color.color);
  307. // Override the color value - inherit returns to default
  308. network.setOptions({ edges:{color: {}}});
  309. assert.equal(edges[1].options.color.inherit, 'from'); // default
  310. assert.equal(edges[2].options.color.inherit, 'to'); // set in edge
  311. assert.equal(edges[3].options.color.inherit, false); // has explicit color
  312. assert.equal(edges[4].options.color.inherit, 'from'); // set in edge
  313. // Check no options
  314. network = new vis.Network(container, data, {});
  315. edges = network.body.edges;
  316. assert.equal(edges[1].options.color.inherit, 'from'); // default
  317. assert.equal(edges[2].options.color.inherit, 'to'); // set in edge
  318. assert.equal(edges[3].options.color.inherit, false); // has explicit color
  319. assert.equal(edges[4].options.color.inherit, 'from'); // set in edge
  320. // Set new value
  321. network.setOptions(options);
  322. assert.equal(edges[1].options.color.inherit, 'to');
  323. assert.equal(edges[2].options.color.inherit, 'to'); // set in edge
  324. assert.equal(edges[3].options.color.inherit, false); // has explicit color
  325. assert.equal(edges[4].options.color.inherit, 'from'); // set in edge
  326. /*
  327. // Useful for debugging
  328. console.log('===================================');
  329. console.log(edges[1].options.color);
  330. console.log(edges[1].options.color.__proto__);
  331. console.log(edges[1].options);
  332. console.log(edges[1].options.__proto__);
  333. console.log(edges[1].edgeOptions);
  334. */
  335. });
  336. it('sets inherit color option for specific edge', function () {
  337. var container = document.getElementById('mynetwork');
  338. var data = createDataforColorChange();
  339. // Check no options
  340. var network = new vis.Network(container, data, {});
  341. var edges = network.body.edges;
  342. assert.equal(edges[1].options.color.inherit, 'from'); // default
  343. assert.equal(edges[2].options.color.inherit, 'to'); // set in edge
  344. assert.equal(edges[3].options.color.inherit, false); // has explicit color
  345. assert.equal(edges[4].options.color.inherit, 'from'); // set in edge
  346. // Set new value
  347. data.edges.update({id: 1, color: { inherit: 'to'}});
  348. assert.equal(edges[1].options.color.inherit, 'to'); // Only this changed
  349. assert.equal(edges[2].options.color.inherit, 'to');
  350. assert.equal(edges[3].options.color.inherit, false); // has explicit color
  351. assert.equal(edges[4].options.color.inherit, 'from');
  352. });
  353. /**
  354. * Perhaps TODO: add unit test for passing string value for color option
  355. */
  356. it('sets color value for edges on call to Network.setOptions()', function () {
  357. var container = document.getElementById('mynetwork');
  358. var data = createDataforColorChange();
  359. var defaultColor = '#848484'; // From defaults
  360. var color = '#FF0000';
  361. var options = {
  362. "edges" : { "color" : { "color" : color } },
  363. };
  364. // Test passing options on init.
  365. var network = new vis.Network(container, data, options);
  366. var edges = network.body.edges;
  367. assert.equal(edges[1].options.color.color, color);
  368. assert.equal(edges[1].options.color.inherit, false); // Explicit color, so no inherit
  369. assert.equal(edges[2].options.color.color, color);
  370. assert.equal(edges[2].options.color.inherit, 'to'); // Local value overrides! (bug according to docs)
  371. assert.notEqual(edges[3].options.color.color, color); // Has own value
  372. assert.equal(edges[3].options.color.inherit, false); // Explicit color, so no inherit
  373. assert.equal(edges[4].options.color.color, color);
  374. // Override the color value - all should return to default
  375. network.setOptions({ edges:{color: {}}});
  376. assert.equal(edges[1].options.color.color, defaultColor);
  377. assert.equal(edges[1].options.color.inherit, 'from');
  378. assert.equal(edges[2].options.color.color, defaultColor);
  379. assert.notEqual(edges[3].options.color.color, color); // Has own value
  380. assert.equal(edges[4].options.color.color, defaultColor);
  381. // Check no options
  382. network = new vis.Network(container, data, {});
  383. edges = network.body.edges;
  384. // At this point, color has not changed yet
  385. assert.equal(edges[1].options.color.color, defaultColor);
  386. assert.equal(edges[1].options.color.highlight, defaultColor);
  387. assert.equal(edges[1].options.color.inherit, 'from');
  388. assert.notEqual(edges[3].options.color.color, color); // Has own value
  389. // Set new Value
  390. network.setOptions(options);
  391. assert.equal(edges[1].options.color.color, color);
  392. assert.equal(edges[1].options.color.highlight, defaultColor); // Should not be changed
  393. assert.equal(edges[1].options.color.inherit, false); // Explicit color, so no inherit
  394. assert.equal(edges[2].options.color.color, color);
  395. assert.notEqual(edges[3].options.color.color, color); // Has own value
  396. assert.equal(edges[4].options.color.color, color);
  397. });
  398. }); // Edge
  399. describe('Clustering', function () {
  400. /**
  401. * Helper function for clustering
  402. */
  403. function clusterTo(network, clusterId, nodeList, allowSingle) {
  404. var options = {
  405. joinCondition: function(node) {
  406. return nodeList.indexOf(node.id) !== -1;
  407. },
  408. clusterNodeProperties: {
  409. id: clusterId,
  410. label: clusterId
  411. }
  412. }
  413. if (allowSingle === true) {
  414. options.clusterNodeProperties.allowSingleNodeCluster = true
  415. }
  416. network.cluster(options);
  417. }
  418. it('properly handles options allowSingleNodeCluster', function() {
  419. var [network, data, numNodes, numEdges] = createSampleNetwork();
  420. data.edges.update({from: 1, to: 11,});
  421. numEdges += 1;
  422. assertNumNodes(network, numNodes);
  423. assertNumEdges(network, numEdges);
  424. clusterTo(network, 'c1', [3,4]);
  425. numNodes += 1; // A clustering node is now hiding two nodes
  426. numEdges += 1; // One clustering edges now hiding two edges
  427. assertNumNodes(network, numNodes, numNodes - 2);
  428. assertNumEdges(network, numEdges, numEdges - 2);
  429. // Cluster of single node should fail, because by default allowSingleNodeCluster == false
  430. clusterTo(network, 'c2', [14]);
  431. assertNumNodes(network, numNodes, numNodes - 2); // Nothing changed
  432. assertNumEdges(network, numEdges, numEdges - 2);
  433. assert(network.body.nodes['c2'] === undefined); // Cluster not created
  434. // Redo with allowSingleNodeCluster == true
  435. clusterTo(network, 'c2', [14], true);
  436. numNodes += 1;
  437. numEdges += 1;
  438. assertNumNodes(network, numNodes, numNodes - 3);
  439. assertNumEdges(network, numEdges, numEdges - 3);
  440. assert(network.body.nodes['c2'] !== undefined); // Cluster created
  441. // allowSingleNodeCluster: true with two nodes
  442. // removing one clustered node should retain cluster
  443. clusterTo(network, 'c3', [11, 12], true);
  444. numNodes += 1; // Added cluster
  445. numEdges += 2;
  446. assertNumNodes(network, numNodes, 6);
  447. assertNumEdges(network, numEdges, 5);
  448. data.nodes.remove(12);
  449. assert(network.body.nodes['c3'] !== undefined); // Cluster should still be present
  450. numNodes -= 1; // removed node
  451. numEdges -= 3; // cluster edge C3-13 should be removed
  452. assertNumNodes(network, numNodes, 6);
  453. assertNumEdges(network, numEdges, 4);
  454. });
  455. it('removes nested clusters with allowSingleNodeCluster === true', function() {
  456. var [network, data, numNodes, numEdges] = createSampleNetwork();
  457. // Create a chain of nested clusters, three deep
  458. clusterTo(network, 'c1', [4], true);
  459. clusterTo(network, 'c2', ['c1'], true);
  460. clusterTo(network, 'c3', ['c2'], true);
  461. numNodes += 3;
  462. numEdges += 3;
  463. assertNumNodes(network, numNodes, numNodes - 3);
  464. assertNumEdges(network, numEdges, numEdges - 3);
  465. assert(network.body.nodes['c1'] !== undefined);
  466. assert(network.body.nodes['c2'] !== undefined);
  467. assert(network.body.nodes['c3'] !== undefined);
  468. // The whole chain should be removed when the bottom-most node is deleted
  469. data.nodes.remove(4);
  470. numNodes -= 4;
  471. numEdges -= 4;
  472. assertNumNodes(network, numNodes);
  473. assertNumEdges(network, numEdges);
  474. assert(network.body.nodes['c1'] === undefined);
  475. assert(network.body.nodes['c2'] === undefined);
  476. assert(network.body.nodes['c3'] === undefined);
  477. });
  478. /**
  479. * Check on fix for #1218
  480. */
  481. it('connects a new edge to a clustering node instead of the clustered node', function () {
  482. var [network, data, numNodes, numEdges] = createSampleNetwork();
  483. createCluster(network);
  484. numNodes += 1; // A clustering node is now hiding two nodes
  485. numEdges += 2; // Two clustering edges now hide two edges
  486. assertNumNodes(network, numNodes, numNodes - 2);
  487. assertNumEdges(network, numEdges, numEdges - 2);
  488. //console.log("Creating node 21")
  489. data.nodes.update([{id: 21, label: '21'}]);
  490. numNodes += 1; // New unconnected node added
  491. assertNumNodes(network, numNodes, numNodes - 2);
  492. assertNumEdges(network, numEdges, numEdges - 2); // edges unchanged
  493. //console.log("Creating edge 21 pointing to 1");
  494. // '1' is part of the cluster so should
  495. // connect to cluster instead
  496. data.edges.update([{from: 21, to: 1}]);
  497. numEdges += 2; // A new clustering edge is hiding a new edge
  498. assertNumNodes(network, numNodes, numNodes - 2); // nodes unchanged
  499. assertNumEdges(network, numEdges, numEdges - 3);
  500. });
  501. /**
  502. * Check on fix for #1315
  503. */
  504. it('can uncluster a clustered node when a node is removed that has an edge to that cluster', function () {
  505. // NOTE: this block is same as previous test
  506. var [network, data, numNodes, numEdges] = createSampleNetwork();
  507. createCluster(network);
  508. numNodes += 1; // A clustering node is now hiding two nodes
  509. numEdges += 2; // Two clustering edges now hide two edges
  510. assertNumNodes(network, numNodes, numNodes - 2);
  511. assertNumEdges(network, numEdges, numEdges - 2);
  512. // End block same as previous test
  513. //console.log("removing 12");
  514. data.nodes.remove(12);
  515. // NOTE:
  516. // At this particular point, there are still the two edges for node 12 in the edges DataSet.
  517. // If you want to do the delete correctly, these should also be deleted explictly from
  518. // the edges DataSet. In the Network instance, however, this.body.nodes and this.body.edges
  519. // should be correct, with the edges of 12 all cleared out.
  520. // 12 was connected to 11, which is clustered
  521. numNodes -= 1; // 12 removed, one less node
  522. numEdges -= 3; // clustering edge c1-12 and 2 edges of 12 gone
  523. assertNumNodes(network, numNodes, numNodes - 2);
  524. assertNumEdges(network, numEdges, numEdges - 1);
  525. //console.log("Unclustering c1");
  526. network.openCluster("c1");
  527. numNodes -= 1; // cluster node removed, one less node
  528. numEdges -= 1; // clustering edge gone, regular edge visible
  529. assertNumNodes(network, numNodes, numNodes); // all are visible again
  530. assertNumEdges(network, numEdges, numEdges); // all are visible again
  531. });
  532. /**
  533. * Check on fix for #1291
  534. */
  535. it('can remove a node inside a cluster and then open that cluster', function () {
  536. var [network, data, numNodes, numEdges] = createSampleNetwork();
  537. var clusterOptionsByData = {
  538. joinCondition: function(node) {
  539. if (node.id == 1 || node.id == 2 || node.id == 3) return true;
  540. return false;
  541. },
  542. clusterNodeProperties: {id:"c1", label:'c1'}
  543. }
  544. network.cluster(clusterOptionsByData);
  545. numNodes += 1; // new cluster node
  546. numEdges += 1; // 1 cluster edge expected
  547. assertNumNodes(network, numNodes, numNodes - 3); // 3 clustered nodes
  548. assertNumEdges(network, numEdges, numEdges - 3); // 3 edges hidden
  549. //console.log("removing node 2, which is inside the cluster");
  550. data.nodes.remove(2);
  551. numNodes -= 1; // clustered node removed
  552. numEdges -= 2; // edges removed hidden in cluster
  553. assertNumNodes(network, numNodes, numNodes - 2); // view doesn't change
  554. assertNumEdges(network, numEdges, numEdges - 1); // view doesn't change
  555. //console.log("Unclustering c1");
  556. network.openCluster("c1")
  557. numNodes -= 1; // cluster node gone
  558. numEdges -= 1; // cluster edge gone
  559. assertNumNodes(network, numNodes, numNodes); // all visible
  560. assertNumEdges(network, numEdges, numEdges); // all visible
  561. //log(network);
  562. });
  563. /**
  564. * Helper function for setting up a graph for testing clusterByEdgeCount()
  565. */
  566. function createOutlierGraph() {
  567. // create an array with nodes
  568. var nodes = new vis.DataSet([
  569. {id: 1, label: '1', group:'Group1'},
  570. {id: 2, label: '2', group:'Group2'},
  571. {id: 3, label: '3', group:'Group3'},
  572. {id: 4, label: '4', group:'Group4'},
  573. {id: 5, label: '5', group:'Group4'}
  574. ]);
  575. // create an array with edges
  576. var edges = new vis.DataSet([
  577. {from: 1, to: 3},
  578. {from: 1, to: 2},
  579. {from: 2, to: 4},
  580. {from: 2, to: 5}
  581. ]);
  582. // create a network
  583. var container = document.getElementById('mynetwork');
  584. var data = {
  585. nodes: nodes,
  586. edges: edges
  587. };
  588. var options = {
  589. "groups" : {
  590. "Group1" : { level:1 },
  591. "Group2" : { level:2 },
  592. "Group3" : { level:3 },
  593. "Group4" : { level:4 }
  594. }
  595. };
  596. var network = new vis.Network (container, data, options);
  597. return network;
  598. }
  599. /**
  600. * Check on fix for #3367
  601. */
  602. it('correctly handles edge cases of clusterByEdgeCount()', function () {
  603. /**
  604. * Collect clustered id's
  605. *
  606. * All node id's in clustering nodes are collected into an array;
  607. * The results for all clusters are returned as an array.
  608. *
  609. * Ordering of output depends on the order in which they are defined
  610. * within nodes.clustering; strictly, speaking, the array and its items
  611. * are collections, so order should not matter.
  612. */
  613. var collectClusters = function(network) {
  614. var clusters = [];
  615. for(var n in network.body.nodes) {
  616. var node = network.body.nodes[n];
  617. if (node.containedNodes === undefined) continue; // clusters only
  618. // Collect id's of nodes in the cluster
  619. var nodes = [];
  620. for(var m in node.containedNodes) {
  621. nodes.push(m);
  622. }
  623. clusters.push(nodes);
  624. }
  625. return clusters;
  626. }
  627. /**
  628. * Compare cluster data
  629. *
  630. * params are arrays of arrays of id's, e.g:
  631. *
  632. * [[1,3],[2,4]]
  633. *
  634. * Item arrays are the id's of nodes in a given cluster
  635. *
  636. * This comparison depends on the ordering; better
  637. * would be to treat the items and values as collections.
  638. */
  639. var compareClusterInfo = function(recieved, expected) {
  640. if (recieved.length !== expected.length) return false;
  641. for (var n = 0; n < recieved.length; ++n) {
  642. var itema = recieved[n];
  643. var itemb = expected[n];
  644. if (itema.length !== itemb.length) return false;
  645. for (var m = 0; m < itema.length; ++m) {
  646. if (itema[m] != itemb[m]) return false; // != because values can be string or number
  647. }
  648. }
  649. return true;
  650. }
  651. var assertJoinCondition = function(joinCondition, expected) {
  652. var network = createOutlierGraph();
  653. network.clusterOutliers({joinCondition: joinCondition});
  654. var recieved = collectClusters(network);
  655. //console.log(recieved);
  656. assert(compareClusterInfo(recieved, expected),
  657. 'recieved:' + JSON.stringify(recieved) + '; '
  658. + 'expected: ' + JSON.stringify(expected));
  659. };
  660. // Should cluster 3,4,5:
  661. var joinAll_ = function(n) { return true ; }
  662. // Should cluster none:
  663. var joinNone_ = function(n) { return false ; }
  664. // Should cluster 4 & 5:
  665. var joinLevel_ = function(n) { return n.level > 3 ; }
  666. assertJoinCondition(undefined , [[1,3],[2,4,5]]);
  667. assertJoinCondition(null , [[1,3],[2,4,5]]);
  668. assertJoinCondition(joinNone_ , []);
  669. assertJoinCondition(joinLevel_ , [[2,4,5]]);
  670. });
  671. ///////////////////////////////////////////////////////////////
  672. // Automatic opening of clusters due to dynamic data change
  673. ///////////////////////////////////////////////////////////////
  674. /**
  675. * Helper function, created nested clusters, three deep
  676. */
  677. function createNetwork1() {
  678. var [network, data, numNodes, numEdges] = createSampleNetwork();
  679. clusterTo(network, 'c1', [3,4]);
  680. numNodes += 1; // new cluster node
  681. numEdges += 1; // 1 cluster edge expected
  682. assertNumNodes(network, numNodes, numNodes - 2); // 2 clustered nodes
  683. assertNumEdges(network, numEdges, numEdges - 2); // 2 edges hidden
  684. clusterTo(network, 'c2', [2,'c1']);
  685. numNodes += 1; // new cluster node
  686. numEdges += 1; // 2 cluster edges expected
  687. assertNumNodes(network, numNodes, numNodes - 4); // 4 clustered nodes, including c1
  688. assertNumEdges(network, numEdges, numEdges - 4); // 4 edges hidden, including edge for c1
  689. clusterTo(network, 'c3', [1,'c2']);
  690. // Attempt at visualization: parentheses belong to the cluster one level above
  691. // c3
  692. // ( -c2 )
  693. // ( -c1 )
  694. // 14-13-12-11 1 -2 (-3-4)
  695. numNodes += 1; // new cluster node
  696. numEdges += 0; // No new cluster edge expected
  697. assertNumNodes(network, numNodes, numNodes - 6); // 6 clustered nodes, including c1 and c2
  698. assertNumEdges(network, numEdges, numEdges - 5); // 5 edges hidden, including edges for c1 and c2
  699. return [network, data, numNodes, numEdges];
  700. }
  701. it('opens clusters automatically when nodes deleted', function () {
  702. var [network, data, numNodes, numEdges] = createSampleNetwork();
  703. // Simple case: cluster of two nodes, delete one node
  704. clusterTo(network, 'c1', [3,4]);
  705. numNodes += 1; // new cluster node
  706. numEdges += 1; // 1 cluster edge expected
  707. assertNumNodes(network, numNodes, numNodes - 2); // 2 clustered nodes
  708. assertNumEdges(network, numEdges, numEdges - 2); // 2 edges hidden
  709. data.nodes.remove(4);
  710. numNodes -= 2; // deleting clustered node also removes cluster node
  711. numEdges -= 2; // cluster edge should also be removed
  712. assertNumNodes(network, numNodes, numNodes);
  713. assertNumEdges(network, numEdges, numEdges);
  714. // Extended case: nested nodes, three deep
  715. [network, data, numNodes, numEdges] = createNetwork1();
  716. data.nodes.remove(4);
  717. // c3
  718. // ( -c2 )
  719. // 14-13-12-11 1 (-2 -3)
  720. numNodes -= 2; // node removed, c1 also gone
  721. numEdges -= 2;
  722. assertNumNodes(network, numNodes, numNodes - 4);
  723. assertNumEdges(network, numEdges, numEdges - 3);
  724. data.nodes.remove(1);
  725. // c2
  726. // 14-13-12-11 (2 -3)
  727. numNodes -= 2; // node removed, c3 also gone
  728. numEdges -= 2;
  729. assertNumNodes(network, numNodes, numNodes - 2);
  730. assertNumEdges(network, numEdges, numEdges - 1);
  731. data.nodes.remove(2);
  732. // 14-13-12-11 3
  733. numNodes -= 2; // node removed, c2 also gone
  734. numEdges -= 1;
  735. assertNumNodes(network, numNodes); // All visible again
  736. assertNumEdges(network, numEdges);
  737. // Same as previous step, but remove all the given nodes in one go
  738. // The result should be the same.
  739. [network, data, numNodes, numEdges] = createNetwork1(); // nested nodes, three deep
  740. data.nodes.remove([1,2,4]);
  741. // 14-13-12-11 3
  742. assertNumNodes(network, 5);
  743. assertNumEdges(network, 3);
  744. });
  745. ///////////////////////////////////////////////////////////////
  746. // Opening of clusters at various clustering depths
  747. ///////////////////////////////////////////////////////////////
  748. /**
  749. * Check correct opening of a single cluster.
  750. * This is the 'simple' case.
  751. */
  752. it('properly opens 1-level clusters', function () {
  753. var [network, data, numNodes, numEdges] = createSampleNetwork();
  754. // Pedantic: make a cluster of everything
  755. clusterTo(network, 'c1', [1,2,3,4,11, 12, 13, 14]);
  756. // c1(14-13-12-11 1-2-3-4)
  757. numNodes += 1;
  758. assertNumNodes(network, numNodes, 1); // Just the clustering node visible
  759. assertNumEdges(network, numEdges, 0); // No extra edges!
  760. network.clustering.openCluster('c1', {});
  761. numNodes -= 1;
  762. assertNumNodes(network, numNodes, numNodes); // Expecting same as original
  763. assertNumEdges(network, numEdges, numEdges);
  764. // One external connection
  765. [network, data, numNodes, numEdges] = createSampleNetwork();
  766. // 14-13-12-11 1-2-3-4
  767. clusterTo(network, 'c1', [3,4]);
  768. network.clustering.openCluster('c1', {});
  769. assertNumNodes(network, numNodes, numNodes); // Expecting same as original
  770. assertNumEdges(network, numEdges, numEdges);
  771. // Two external connections
  772. clusterTo(network, 'c1', [2,3]);
  773. network.clustering.openCluster('c1', {});
  774. assertNumNodes(network, numNodes, numNodes); // Expecting same as original
  775. assertNumEdges(network, numEdges, numEdges);
  776. // One external connection to cluster
  777. clusterTo(network, 'c1', [1,2]);
  778. clusterTo(network, 'c2', [3,4]);
  779. // 14-13-12-11 c1(1-2-)-c2(-3-4)
  780. network.clustering.openCluster('c1', {});
  781. // 14-13-12-11 1-2-c2(-3-4)
  782. numNodes += 1;
  783. numEdges += 1;
  784. assertNumNodes(network, numNodes, numNodes - 2);
  785. assertNumEdges(network, numEdges, numEdges - 2);
  786. // two external connections to clusters
  787. [network, data, numNodes, numEdges] = createSampleNetwork();
  788. data.edges.update({
  789. from: 1,
  790. to: 11,
  791. });
  792. numEdges += 1;
  793. assertNumNodes(network, numNodes, numNodes);
  794. assertNumEdges(network, numEdges, numEdges);
  795. clusterTo(network, 'c1', [1,2]);
  796. // 14-13-12-11-c1(-1-2-)-3-4
  797. numNodes += 1;
  798. numEdges += 2;
  799. clusterTo(network, 'c2', [3,4]);
  800. // 14-13-12-11-c1(-1-2-)-c2(-3-4)
  801. // NOTE: clustering edges are hidden by clustering here!
  802. numNodes += 1;
  803. numEdges += 1;
  804. clusterTo(network, 'c3', [11,12]);
  805. // 14-13-c3(-12-11-)-c1(-1-2-)-c2(-3-4)
  806. numNodes += 1;
  807. numEdges += 2;
  808. assertNumNodes(network, numNodes, numNodes - 6);
  809. assertNumEdges(network, numEdges, numEdges - 8); // 6 regular edges hidden; also 2 clustering!!!!!
  810. network.clustering.openCluster('c1', {});
  811. numNodes -= 1;
  812. numEdges -= 2;
  813. // 14-13-c3(-12-11-)-1-2-c2(-3-4)
  814. assertNumNodes(network, numNodes, numNodes - 4);
  815. assertNumEdges(network, numEdges, numEdges - 5);
  816. });
  817. /**
  818. * Check correct opening of nested clusters.
  819. * The test uses clustering three levels deep and opens the middle one.
  820. */
  821. it('properly opens clustered clusters', function () {
  822. var [network, data, numNodes, numEdges] = createSampleNetwork();
  823. data.edges.update({from: 1, to: 11,});
  824. numEdges += 1;
  825. clusterTo(network, 'c1', [3,4]);
  826. clusterTo(network, 'c2', [2,'c1']);
  827. clusterTo(network, 'c3', [1,'c2']);
  828. // Attempt at visualization: parentheses belong to the cluster one level above
  829. // -c3
  830. // ( -c2 )
  831. // ( -c1 )
  832. // 14-13-12-11 -1 -2 (-3-4)
  833. numNodes += 3;
  834. numEdges += 3;
  835. //console.log("numNodes: " + numNodes + "; numEdges: " + numEdges);
  836. assertNumNodes(network, numNodes, numNodes - 6);
  837. assertNumEdges(network, numEdges, numEdges - 6);
  838. // Open the middle cluster
  839. network.clustering.openCluster('c2', {});
  840. // -c3
  841. // ( -c1 )
  842. // 14-13-12-11 -1 -2 (-3-4)
  843. numNodes -= 1;
  844. numEdges -= 1;
  845. assertNumNodes(network, numNodes, numNodes - 5);
  846. assertNumEdges(network, numEdges, numEdges - 5);
  847. //
  848. // Same, with one external connection to cluster
  849. //
  850. var [network, data, numNodes, numEdges] = createSampleNetwork();
  851. data.edges.update({from: 1, to: 11,});
  852. data.edges.update({from: 2, to: 12,});
  853. numEdges += 2;
  854. // 14-13-12-11-1-2-3-4
  855. // |------|
  856. assertNumNodes(network, numNodes);
  857. assertNumEdges(network, numEdges);
  858. clusterTo(network, 'c0', [11,12]);
  859. clusterTo(network, 'c1', [3,4]);
  860. clusterTo(network, 'c2', [2,'c1']);
  861. clusterTo(network, 'c3', [1,'c2']);
  862. // +----------------+
  863. // | c3 |
  864. // | +----------+ |
  865. // | | c2 | |
  866. // +-------+ | | +----+ | |
  867. // | c0 | | | | c1 | | |
  868. // 14-13-|-12-11-|-|-1-|-2-|-3-4| | |
  869. // | | | | | | +----+ | |
  870. // +-------+ | | | | |
  871. // | | +----------+ |
  872. // | | | |
  873. // | +----------------+
  874. // |------------|
  875. // (I)
  876. numNodes += 4;
  877. numEdges = 15;
  878. assertNumNodes(network, numNodes, 4);
  879. assertNumEdges(network, numEdges, 3); // (I) link 2-12 is combined into cluster edge for 11-1
  880. // Open the middle cluster
  881. network.clustering.openCluster('c2', {});
  882. // +--------------+
  883. // | c3 |
  884. // | |
  885. // +-------+ | +----+ |
  886. // | c0 | | | c1 | |
  887. // 14-13-|-12-11-|-|-1--2-|-3-4| |
  888. // | | | | | +----+ |
  889. // +-------+ | | |
  890. // | | | |
  891. // | +--------------+
  892. // |-----------|
  893. // (I)
  894. numNodes -= 1;
  895. numEdges -= 2;
  896. assertNumNodes(network, numNodes, 4); // visibility doesn't change, cluster opened within cluster
  897. assertNumEdges(network, numEdges, 3); // (I)
  898. // Open the top cluster
  899. network.clustering.openCluster('c3', {});
  900. //
  901. // +-------+ +----+
  902. // | c0 | | c1 |
  903. // 14-13-|-12-11-|-1-2-|-3-4|
  904. // | | | | +----+
  905. // +-------+ |
  906. // | |
  907. // |--------|
  908. // (II)
  909. numNodes -= 1;
  910. numEdges = 12;
  911. assertNumNodes(network, numNodes, 6); // visibility doesn't change, cluster opened within cluster
  912. assertNumEdges(network, numEdges, 6); // (II) link 2-12 visible again
  913. });
  914. }); // Clustering
  915. describe('on node.js', function () {
  916. it('should be running', function () {
  917. assert(this.container !== null, 'Container div not found');
  918. // The following should now just plain succeed
  919. var [network, data] = createSampleNetwork();
  920. assert.equal(Object.keys(network.body.nodes).length, 8);
  921. assert.equal(Object.keys(network.body.edges).length, 6);
  922. });
  923. describe('runs example ', function () {
  924. function loadExample(path, noPhysics) {
  925. include(path, this);
  926. var container = document.getElementById('mynetwork');
  927. // create a network
  928. var data = {
  929. nodes: new vis.DataSet(nodes),
  930. edges: new vis.DataSet(edges)
  931. };
  932. if (noPhysics) {
  933. // Avoid excessive processor time due to load.
  934. // We're just interested that the load itself is good
  935. options.physics = false;
  936. }
  937. var network = new vis.Network(container, data, options);
  938. return network;
  939. };
  940. it('basicUsage', function () {
  941. var network = loadExample('./test/network/basicUsage.js');
  942. //console.log(Object.keys(network.body.edges));
  943. // Count in following also contains the helper nodes for dynamic edges
  944. assert.equal(Object.keys(network.body.nodes).length, 10);
  945. assert.equal(Object.keys(network.body.edges).length, 5);
  946. });
  947. it('WorlCup2014', function () {
  948. // This is a huge example (which is why it's tested here!), so it takes a long time to load.
  949. this.timeout(10000);
  950. var network = loadExample('./examples/network/datasources/WorldCup2014.js', true);
  951. // Count in following also contains the helper nodes for dynamic edges
  952. assert.equal(Object.keys(network.body.nodes).length, 9964);
  953. assert.equal(Object.keys(network.body.edges).length, 9228);
  954. });
  955. // This actually failed to load, added for this reason
  956. it('disassemblerExample', function () {
  957. var network = loadExample('./examples/network/exampleApplications/disassemblerExample.js');
  958. // console.log(Object.keys(network.body.nodes));
  959. // console.log(Object.keys(network.body.edges));
  960. // Count in following also contains the helper nodes for dynamic edges
  961. assert.equal(Object.keys(network.body.nodes).length, 9);
  962. assert.equal(Object.keys(network.body.edges).length, 14 - 3); // NB 3 edges in data not displayed
  963. });
  964. }); // runs example
  965. }); // on node.js
  966. }); // Network