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.

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