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.

1063 lines
37 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. var util = require('../util');
  2. /**
  3. * @class Node
  4. * A node. A node can be connected to other nodes via one or multiple edges.
  5. * @param {object} properties An object containing properties for the node. All
  6. * properties are optional, except for the id.
  7. * {number} id Id of the node. Required
  8. * {string} label Text label for the node
  9. * {number} x Horizontal position of the node
  10. * {number} y Vertical position of the node
  11. * {string} shape Node shape, available:
  12. * "database", "circle", "ellipse",
  13. * "box", "image", "text", "dot",
  14. * "star", "triangle", "triangleDown",
  15. * "square"
  16. * {string} image An image url
  17. * {string} title An title text, can be HTML
  18. * {anytype} group A group name or number
  19. * @param {Network.Images} imagelist A list with images. Only needed
  20. * when the node has an image
  21. * @param {Network.Groups} grouplist A list with groups. Needed for
  22. * retrieving group properties
  23. * @param {Object} constants An object with default values for
  24. * example for the color
  25. *
  26. */
  27. function Node(properties, imagelist, grouplist, networkConstants) {
  28. var constants = util.selectiveBridgeObject(['nodes'],networkConstants);
  29. this.options = constants.nodes;
  30. this.selected = false;
  31. this.hover = false;
  32. this.edges = []; // all edges connected to this node
  33. this.dynamicEdges = [];
  34. this.reroutedEdges = {};
  35. this.fontDrawThreshold = 3;
  36. // set defaults for the properties
  37. this.id = undefined;
  38. this.x = null;
  39. this.y = null;
  40. this.allowedToMoveX = false;
  41. this.allowedToMoveY = false;
  42. this.xFixed = false;
  43. this.yFixed = false;
  44. this.horizontalAlignLeft = true; // these are for the navigation controls
  45. this.verticalAlignTop = true; // these are for the navigation controls
  46. this.baseRadiusValue = networkConstants.nodes.radius;
  47. this.radiusFixed = false;
  48. this.level = -1;
  49. this.preassignedLevel = false;
  50. this.hierarchyEnumerated = false;
  51. this.labelDimensions = {top:0, left:0, width:0, height:0, yLine:0}; // could be cached
  52. this.boundingBox = {top:0, left:0, right:0, bottom:0};
  53. this.imagelist = imagelist;
  54. this.grouplist = grouplist;
  55. // physics properties
  56. this.fx = 0.0; // external force x
  57. this.fy = 0.0; // external force y
  58. this.vx = 0.0; // velocity x
  59. this.vy = 0.0; // velocity y
  60. this.damping = networkConstants.physics.damping; // written every time gravity is calculated
  61. this.fixedData = {x:null,y:null};
  62. this.setProperties(properties, constants);
  63. // creating the variables for clustering
  64. this.resetCluster();
  65. this.dynamicEdgesLength = 0;
  66. this.clusterSession = 0;
  67. this.clusterSizeWidthFactor = networkConstants.clustering.nodeScaling.width;
  68. this.clusterSizeHeightFactor = networkConstants.clustering.nodeScaling.height;
  69. this.clusterSizeRadiusFactor = networkConstants.clustering.nodeScaling.radius;
  70. this.maxNodeSizeIncrements = networkConstants.clustering.maxNodeSizeIncrements;
  71. this.growthIndicator = 0;
  72. // variables to tell the node about the network.
  73. this.networkScaleInv = 1;
  74. this.networkScale = 1;
  75. this.canvasTopLeft = {"x": -300, "y": -300};
  76. this.canvasBottomRight = {"x": 300, "y": 300};
  77. this.parentEdgeId = null;
  78. }
  79. /**
  80. * (re)setting the clustering variables and objects
  81. */
  82. Node.prototype.resetCluster = function() {
  83. // clustering variables
  84. this.formationScale = undefined; // this is used to determine when to open the cluster
  85. this.clusterSize = 1; // this signifies the total amount of nodes in this cluster
  86. this.containedNodes = {};
  87. this.containedEdges = {};
  88. this.clusterSessions = [];
  89. };
  90. /**
  91. * Attach a edge to the node
  92. * @param {Edge} edge
  93. */
  94. Node.prototype.attachEdge = function(edge) {
  95. if (this.edges.indexOf(edge) == -1) {
  96. this.edges.push(edge);
  97. }
  98. if (this.dynamicEdges.indexOf(edge) == -1) {
  99. this.dynamicEdges.push(edge);
  100. }
  101. this.dynamicEdgesLength = this.dynamicEdges.length;
  102. };
  103. /**
  104. * Detach a edge from the node
  105. * @param {Edge} edge
  106. */
  107. Node.prototype.detachEdge = function(edge) {
  108. var index = this.edges.indexOf(edge);
  109. if (index != -1) {
  110. this.edges.splice(index, 1);
  111. }
  112. index = this.dynamicEdges.indexOf(edge);
  113. if (index != -1) {
  114. this.dynamicEdges.splice(index, 1);
  115. }
  116. this.dynamicEdgesLength = this.dynamicEdges.length;
  117. };
  118. /**
  119. * Set or overwrite properties for the node
  120. * @param {Object} properties an object with properties
  121. * @param {Object} constants and object with default, global properties
  122. */
  123. Node.prototype.setProperties = function(properties, constants) {
  124. if (!properties) {
  125. return;
  126. }
  127. var fields = ['borderWidth','borderWidthSelected','shape','image','brokenImage','radius','fontColor',
  128. 'fontSize','fontFace','fontFill','group','mass'
  129. ];
  130. util.selectiveDeepExtend(fields, this.options, properties);
  131. // basic properties
  132. if (properties.id !== undefined) {this.id = properties.id;}
  133. if (properties.label !== undefined) {this.label = properties.label; this.originalLabel = properties.label;}
  134. if (properties.title !== undefined) {this.title = properties.title;}
  135. if (properties.x !== undefined) {this.x = properties.x;}
  136. if (properties.y !== undefined) {this.y = properties.y;}
  137. if (properties.value !== undefined) {this.value = properties.value;}
  138. if (properties.level !== undefined) {this.level = properties.level; this.preassignedLevel = true;}
  139. // navigation controls properties
  140. if (properties.horizontalAlignLeft !== undefined) {this.horizontalAlignLeft = properties.horizontalAlignLeft;}
  141. if (properties.verticalAlignTop !== undefined) {this.verticalAlignTop = properties.verticalAlignTop;}
  142. if (properties.triggerFunction !== undefined) {this.triggerFunction = properties.triggerFunction;}
  143. if (this.id === undefined) {
  144. throw "Node must have an id";
  145. }
  146. // copy group properties
  147. if (typeof this.options.group === 'number' || (typeof this.options.group === 'string' && this.options.group != '')) {
  148. var groupObj = this.grouplist.get(this.options.group);
  149. for (var prop in groupObj) {
  150. if (groupObj.hasOwnProperty(prop)) {
  151. this.options[prop] = groupObj[prop];
  152. }
  153. }
  154. }
  155. else if (properties.color === undefined) {
  156. this.options.color = constants.nodes.color;
  157. }
  158. // individual shape properties
  159. if (properties.radius !== undefined) {this.baseRadiusValue = this.options.radius;}
  160. if (properties.color !== undefined) {this.options.color = util.parseColor(properties.color);}
  161. if (this.options.image!== undefined && this.options.image!= "") {
  162. if (this.imagelist) {
  163. this.imageObj = this.imagelist.load(this.options.image, this.options.brokenImage);
  164. }
  165. else {
  166. throw "No imagelist provided";
  167. }
  168. }
  169. if (properties.allowedToMoveX !== undefined) {
  170. this.xFixed = !properties.allowedToMoveX;
  171. this.allowedToMoveX = properties.allowedToMoveX;
  172. }
  173. else if (properties.x !== undefined && this.allowedToMoveX == false) {
  174. this.xFixed = true;
  175. }
  176. if (properties.allowedToMoveY !== undefined) {
  177. this.yFixed = !properties.allowedToMoveY;
  178. this.allowedToMoveY = properties.allowedToMoveY;
  179. }
  180. else if (properties.y !== undefined && this.allowedToMoveY == false) {
  181. this.yFixed = true;
  182. }
  183. this.radiusFixed = this.radiusFixed || (properties.radius !== undefined);
  184. if (this.options.shape == 'image') {
  185. this.options.radiusMin = constants.nodes.widthMin;
  186. this.options.radiusMax = constants.nodes.widthMax;
  187. }
  188. // choose draw method depending on the shape
  189. switch (this.options.shape) {
  190. case 'database': this.draw = this._drawDatabase; this.resize = this._resizeDatabase; break;
  191. case 'box': this.draw = this._drawBox; this.resize = this._resizeBox; break;
  192. case 'circle': this.draw = this._drawCircle; this.resize = this._resizeCircle; break;
  193. case 'ellipse': this.draw = this._drawEllipse; this.resize = this._resizeEllipse; break;
  194. // TODO: add diamond shape
  195. case 'image': this.draw = this._drawImage; this.resize = this._resizeImage; break;
  196. case 'text': this.draw = this._drawText; this.resize = this._resizeText; break;
  197. case 'dot': this.draw = this._drawDot; this.resize = this._resizeShape; break;
  198. case 'square': this.draw = this._drawSquare; this.resize = this._resizeShape; break;
  199. case 'triangle': this.draw = this._drawTriangle; this.resize = this._resizeShape; break;
  200. case 'triangleDown': this.draw = this._drawTriangleDown; this.resize = this._resizeShape; break;
  201. case 'star': this.draw = this._drawStar; this.resize = this._resizeShape; break;
  202. default: this.draw = this._drawEllipse; this.resize = this._resizeEllipse; break;
  203. }
  204. // reset the size of the node, this can be changed
  205. this._reset();
  206. };
  207. /**
  208. * select this node
  209. */
  210. Node.prototype.select = function() {
  211. this.selected = true;
  212. this._reset();
  213. };
  214. /**
  215. * unselect this node
  216. */
  217. Node.prototype.unselect = function() {
  218. this.selected = false;
  219. this._reset();
  220. };
  221. /**
  222. * Reset the calculated size of the node, forces it to recalculate its size
  223. */
  224. Node.prototype.clearSizeCache = function() {
  225. this._reset();
  226. };
  227. /**
  228. * Reset the calculated size of the node, forces it to recalculate its size
  229. * @private
  230. */
  231. Node.prototype._reset = function() {
  232. this.width = undefined;
  233. this.height = undefined;
  234. };
  235. /**
  236. * get the title of this node.
  237. * @return {string} title The title of the node, or undefined when no title
  238. * has been set.
  239. */
  240. Node.prototype.getTitle = function() {
  241. return typeof this.title === "function" ? this.title() : this.title;
  242. };
  243. /**
  244. * Calculate the distance to the border of the Node
  245. * @param {CanvasRenderingContext2D} ctx
  246. * @param {Number} angle Angle in radians
  247. * @returns {number} distance Distance to the border in pixels
  248. */
  249. Node.prototype.distanceToBorder = function (ctx, angle) {
  250. var borderWidth = 1;
  251. if (!this.width) {
  252. this.resize(ctx);
  253. }
  254. switch (this.options.shape) {
  255. case 'circle':
  256. case 'dot':
  257. return this.options.radius+ borderWidth;
  258. case 'ellipse':
  259. var a = this.width / 2;
  260. var b = this.height / 2;
  261. var w = (Math.sin(angle) * a);
  262. var h = (Math.cos(angle) * b);
  263. return a * b / Math.sqrt(w * w + h * h);
  264. // TODO: implement distanceToBorder for database
  265. // TODO: implement distanceToBorder for triangle
  266. // TODO: implement distanceToBorder for triangleDown
  267. case 'box':
  268. case 'image':
  269. case 'text':
  270. default:
  271. if (this.width) {
  272. return Math.min(
  273. Math.abs(this.width / 2 / Math.cos(angle)),
  274. Math.abs(this.height / 2 / Math.sin(angle))) + borderWidth;
  275. // TODO: reckon with border radius too in case of box
  276. }
  277. else {
  278. return 0;
  279. }
  280. }
  281. // TODO: implement calculation of distance to border for all shapes
  282. };
  283. /**
  284. * Set forces acting on the node
  285. * @param {number} fx Force in horizontal direction
  286. * @param {number} fy Force in vertical direction
  287. */
  288. Node.prototype._setForce = function(fx, fy) {
  289. this.fx = fx;
  290. this.fy = fy;
  291. };
  292. /**
  293. * Add forces acting on the node
  294. * @param {number} fx Force in horizontal direction
  295. * @param {number} fy Force in vertical direction
  296. * @private
  297. */
  298. Node.prototype._addForce = function(fx, fy) {
  299. this.fx += fx;
  300. this.fy += fy;
  301. };
  302. /**
  303. * Perform one discrete step for the node
  304. * @param {number} interval Time interval in seconds
  305. */
  306. Node.prototype.discreteStep = function(interval) {
  307. if (!this.xFixed) {
  308. var dx = this.damping * this.vx; // damping force
  309. var ax = (this.fx - dx) / this.options.mass; // acceleration
  310. this.vx += ax * interval; // velocity
  311. this.x += this.vx * interval; // position
  312. }
  313. else {
  314. this.fx = 0;
  315. this.vx = 0;
  316. }
  317. if (!this.yFixed) {
  318. var dy = this.damping * this.vy; // damping force
  319. var ay = (this.fy - dy) / this.options.mass; // acceleration
  320. this.vy += ay * interval; // velocity
  321. this.y += this.vy * interval; // position
  322. }
  323. else {
  324. this.fy = 0;
  325. this.vy = 0;
  326. }
  327. };
  328. /**
  329. * Perform one discrete step for the node
  330. * @param {number} interval Time interval in seconds
  331. * @param {number} maxVelocity The speed limit imposed on the velocity
  332. */
  333. Node.prototype.discreteStepLimited = function(interval, maxVelocity) {
  334. if (!this.xFixed) {
  335. var dx = this.damping * this.vx; // damping force
  336. var ax = (this.fx - dx) / this.options.mass; // acceleration
  337. this.vx += ax * interval; // velocity
  338. this.vx = (Math.abs(this.vx) > maxVelocity) ? ((this.vx > 0) ? maxVelocity : -maxVelocity) : this.vx;
  339. this.x += this.vx * interval; // position
  340. }
  341. else {
  342. this.fx = 0;
  343. this.vx = 0;
  344. }
  345. if (!this.yFixed) {
  346. var dy = this.damping * this.vy; // damping force
  347. var ay = (this.fy - dy) / this.options.mass; // acceleration
  348. this.vy += ay * interval; // velocity
  349. this.vy = (Math.abs(this.vy) > maxVelocity) ? ((this.vy > 0) ? maxVelocity : -maxVelocity) : this.vy;
  350. this.y += this.vy * interval; // position
  351. }
  352. else {
  353. this.fy = 0;
  354. this.vy = 0;
  355. }
  356. };
  357. /**
  358. * Check if this node has a fixed x and y position
  359. * @return {boolean} true if fixed, false if not
  360. */
  361. Node.prototype.isFixed = function() {
  362. return (this.xFixed && this.yFixed);
  363. };
  364. /**
  365. * Check if this node is moving
  366. * @param {number} vmin the minimum velocity considered as "moving"
  367. * @return {boolean} true if moving, false if it has no velocity
  368. */
  369. Node.prototype.isMoving = function(vmin) {
  370. var velocity = Math.sqrt(Math.pow(this.vx,2) + Math.pow(this.vy,2));
  371. // this.velocity = Math.sqrt(Math.pow(this.vx,2) + Math.pow(this.vy,2))
  372. return (velocity > vmin);
  373. };
  374. /**
  375. * check if this node is selecte
  376. * @return {boolean} selected True if node is selected, else false
  377. */
  378. Node.prototype.isSelected = function() {
  379. return this.selected;
  380. };
  381. /**
  382. * Retrieve the value of the node. Can be undefined
  383. * @return {Number} value
  384. */
  385. Node.prototype.getValue = function() {
  386. return this.value;
  387. };
  388. /**
  389. * Calculate the distance from the nodes location to the given location (x,y)
  390. * @param {Number} x
  391. * @param {Number} y
  392. * @return {Number} value
  393. */
  394. Node.prototype.getDistance = function(x, y) {
  395. var dx = this.x - x,
  396. dy = this.y - y;
  397. return Math.sqrt(dx * dx + dy * dy);
  398. };
  399. /**
  400. * Adjust the value range of the node. The node will adjust it's radius
  401. * based on its value.
  402. * @param {Number} min
  403. * @param {Number} max
  404. */
  405. Node.prototype.setValueRange = function(min, max) {
  406. if (!this.radiusFixed && this.value !== undefined) {
  407. if (max == min) {
  408. this.options.radius= (this.options.radiusMin + this.options.radiusMax) / 2;
  409. }
  410. else {
  411. var scale = (this.options.radiusMax - this.options.radiusMin) / (max - min);
  412. this.options.radius= (this.value - min) * scale + this.options.radiusMin;
  413. }
  414. }
  415. this.baseRadiusValue = this.options.radius;
  416. };
  417. /**
  418. * Draw this node in the given canvas
  419. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  420. * @param {CanvasRenderingContext2D} ctx
  421. */
  422. Node.prototype.draw = function(ctx) {
  423. throw "Draw method not initialized for node";
  424. };
  425. /**
  426. * Recalculate the size of this node in the given canvas
  427. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  428. * @param {CanvasRenderingContext2D} ctx
  429. */
  430. Node.prototype.resize = function(ctx) {
  431. throw "Resize method not initialized for node";
  432. };
  433. /**
  434. * Check if this object is overlapping with the provided object
  435. * @param {Object} obj an object with parameters left, top, right, bottom
  436. * @return {boolean} True if location is located on node
  437. */
  438. Node.prototype.isOverlappingWith = function(obj) {
  439. return (this.left < obj.right &&
  440. this.left + this.width > obj.left &&
  441. this.top < obj.bottom &&
  442. this.top + this.height > obj.top);
  443. };
  444. Node.prototype._resizeImage = function (ctx) {
  445. // TODO: pre calculate the image size
  446. if (!this.width || !this.height) { // undefined or 0
  447. var width, height;
  448. if (this.value) {
  449. this.options.radius= this.baseRadiusValue;
  450. var scale = this.imageObj.height / this.imageObj.width;
  451. if (scale !== undefined) {
  452. width = this.options.radius|| this.imageObj.width;
  453. height = this.options.radius* scale || this.imageObj.height;
  454. }
  455. else {
  456. width = 0;
  457. height = 0;
  458. }
  459. }
  460. else {
  461. width = this.imageObj.width;
  462. height = this.imageObj.height;
  463. }
  464. this.width = width;
  465. this.height = height;
  466. this.growthIndicator = 0;
  467. if (this.width > 0 && this.height > 0) {
  468. this.width += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeWidthFactor;
  469. this.height += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeHeightFactor;
  470. this.options.radius+= Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeRadiusFactor;
  471. this.growthIndicator = this.width - width;
  472. }
  473. }
  474. };
  475. Node.prototype._drawImage = function (ctx) {
  476. this._resizeImage(ctx);
  477. this.left = this.x - this.width / 2;
  478. this.top = this.y - this.height / 2;
  479. var yLabel;
  480. if (this.imageObj.width != 0 ) {
  481. // draw the shade
  482. if (this.clusterSize > 1) {
  483. var lineWidth = ((this.clusterSize > 1) ? 10 : 0.0);
  484. lineWidth *= this.networkScaleInv;
  485. lineWidth = Math.min(0.2 * this.width,lineWidth);
  486. ctx.globalAlpha = 0.5;
  487. ctx.drawImage(this.imageObj, this.left - lineWidth, this.top - lineWidth, this.width + 2*lineWidth, this.height + 2*lineWidth);
  488. }
  489. // draw the image
  490. ctx.globalAlpha = 1.0;
  491. ctx.drawImage(this.imageObj, this.left, this.top, this.width, this.height);
  492. yLabel = this.y + this.height / 2;
  493. }
  494. else {
  495. // image still loading... just draw the label for now
  496. yLabel = this.y;
  497. }
  498. this.boundingBox.top = this.top;
  499. this.boundingBox.left = this.left;
  500. this.boundingBox.right = this.left + this.width;
  501. this.boundingBox.bottom = this.top + this.height;
  502. this._label(ctx, this.label, this.x, yLabel, undefined, "top");
  503. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelDimensions.left);
  504. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelDimensions.left + this.labelDimensions.width);
  505. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelDimensions.height);
  506. };
  507. Node.prototype._resizeBox = function (ctx) {
  508. if (!this.width) {
  509. var margin = 5;
  510. var textSize = this.getTextSize(ctx);
  511. this.width = textSize.width + 2 * margin;
  512. this.height = textSize.height + 2 * margin;
  513. this.width += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * 0.5 * this.clusterSizeWidthFactor;
  514. this.height += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * 0.5 * this.clusterSizeHeightFactor;
  515. this.growthIndicator = this.width - (textSize.width + 2 * margin);
  516. // this.options.radius+= Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * 0.5 * this.clusterSizeRadiusFactor;
  517. }
  518. };
  519. Node.prototype._drawBox = function (ctx) {
  520. this._resizeBox(ctx);
  521. this.left = this.x - this.width / 2;
  522. this.top = this.y - this.height / 2;
  523. var clusterLineWidth = 2.5;
  524. var borderWidth = this.options.borderWidth;
  525. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  526. ctx.strokeStyle = this.selected ? this.options.color.highlight.border : this.hover ? this.options.color.hover.border : this.options.color.border;
  527. // draw the outer border
  528. if (this.clusterSize > 1) {
  529. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  530. ctx.lineWidth *= this.networkScaleInv;
  531. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  532. ctx.roundRect(this.left-2*ctx.lineWidth, this.top-2*ctx.lineWidth, this.width+4*ctx.lineWidth, this.height+4*ctx.lineWidth, this.options.radius);
  533. ctx.stroke();
  534. }
  535. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  536. ctx.lineWidth *= this.networkScaleInv;
  537. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  538. ctx.fillStyle = this.selected ? this.options.color.highlight.background : this.options.color.background;
  539. ctx.roundRect(this.left, this.top, this.width, this.height, this.options.radius);
  540. ctx.fill();
  541. ctx.stroke();
  542. this.boundingBox.top = this.top;
  543. this.boundingBox.left = this.left;
  544. this.boundingBox.right = this.left + this.width;
  545. this.boundingBox.bottom = this.top + this.height;
  546. this._label(ctx, this.label, this.x, this.y);
  547. };
  548. Node.prototype._resizeDatabase = function (ctx) {
  549. if (!this.width) {
  550. var margin = 5;
  551. var textSize = this.getTextSize(ctx);
  552. var size = textSize.width + 2 * margin;
  553. this.width = size;
  554. this.height = size;
  555. // scaling used for clustering
  556. this.width += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeWidthFactor;
  557. this.height += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeHeightFactor;
  558. this.options.radius+= Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeRadiusFactor;
  559. this.growthIndicator = this.width - size;
  560. }
  561. };
  562. Node.prototype._drawDatabase = function (ctx) {
  563. this._resizeDatabase(ctx);
  564. this.left = this.x - this.width / 2;
  565. this.top = this.y - this.height / 2;
  566. var clusterLineWidth = 2.5;
  567. var borderWidth = this.options.borderWidth;
  568. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  569. ctx.strokeStyle = this.selected ? this.options.color.highlight.border : this.hover ? this.options.color.hover.border : this.options.color.border;
  570. // draw the outer border
  571. if (this.clusterSize > 1) {
  572. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  573. ctx.lineWidth *= this.networkScaleInv;
  574. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  575. ctx.database(this.x - this.width/2 - 2*ctx.lineWidth, this.y - this.height*0.5 - 2*ctx.lineWidth, this.width + 4*ctx.lineWidth, this.height + 4*ctx.lineWidth);
  576. ctx.stroke();
  577. }
  578. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  579. ctx.lineWidth *= this.networkScaleInv;
  580. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  581. ctx.fillStyle = this.selected ? this.options.color.highlight.background : this.hover ? this.options.color.hover.background : this.options.color.background;
  582. ctx.database(this.x - this.width/2, this.y - this.height*0.5, this.width, this.height);
  583. ctx.fill();
  584. ctx.stroke();
  585. this.boundingBox.top = this.top;
  586. this.boundingBox.left = this.left;
  587. this.boundingBox.right = this.left + this.width;
  588. this.boundingBox.bottom = this.top + this.height;
  589. this._label(ctx, this.label, this.x, this.y);
  590. };
  591. Node.prototype._resizeCircle = function (ctx) {
  592. if (!this.width) {
  593. var margin = 5;
  594. var textSize = this.getTextSize(ctx);
  595. var diameter = Math.max(textSize.width, textSize.height) + 2 * margin;
  596. this.options.radius = diameter / 2;
  597. this.width = diameter;
  598. this.height = diameter;
  599. // scaling used for clustering
  600. // this.width += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * 0.5 * this.clusterSizeWidthFactor;
  601. // this.height += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * 0.5 * this.clusterSizeHeightFactor;
  602. this.options.radius += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * 0.5 * this.clusterSizeRadiusFactor;
  603. this.growthIndicator = this.options.radius- 0.5*diameter;
  604. }
  605. };
  606. Node.prototype._drawCircle = function (ctx) {
  607. this._resizeCircle(ctx);
  608. this.left = this.x - this.width / 2;
  609. this.top = this.y - this.height / 2;
  610. var clusterLineWidth = 2.5;
  611. var borderWidth = this.options.borderWidth;
  612. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  613. ctx.strokeStyle = this.selected ? this.options.color.highlight.border : this.hover ? this.options.color.hover.border : this.options.color.border;
  614. // draw the outer border
  615. if (this.clusterSize > 1) {
  616. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  617. ctx.lineWidth *= this.networkScaleInv;
  618. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  619. ctx.circle(this.x, this.y, this.options.radius+2*ctx.lineWidth);
  620. ctx.stroke();
  621. }
  622. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  623. ctx.lineWidth *= this.networkScaleInv;
  624. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  625. ctx.fillStyle = this.selected ? this.options.color.highlight.background : this.hover ? this.options.color.hover.background : this.options.color.background;
  626. ctx.circle(this.x, this.y, this.options.radius);
  627. ctx.fill();
  628. ctx.stroke();
  629. this.boundingBox.top = this.y - this.options.radius;
  630. this.boundingBox.left = this.x - this.options.radius;
  631. this.boundingBox.right = this.x + this.options.radius;
  632. this.boundingBox.bottom = this.y + this.options.radius;
  633. this._label(ctx, this.label, this.x, this.y);
  634. };
  635. Node.prototype._resizeEllipse = function (ctx) {
  636. if (!this.width) {
  637. var textSize = this.getTextSize(ctx);
  638. this.width = textSize.width * 1.5;
  639. this.height = textSize.height * 2;
  640. if (this.width < this.height) {
  641. this.width = this.height;
  642. }
  643. var defaultSize = this.width;
  644. // scaling used for clustering
  645. this.width += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeWidthFactor;
  646. this.height += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeHeightFactor;
  647. this.options.radius += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeRadiusFactor;
  648. this.growthIndicator = this.width - defaultSize;
  649. }
  650. };
  651. Node.prototype._drawEllipse = function (ctx) {
  652. this._resizeEllipse(ctx);
  653. this.left = this.x - this.width / 2;
  654. this.top = this.y - this.height / 2;
  655. var clusterLineWidth = 2.5;
  656. var borderWidth = this.options.borderWidth;
  657. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  658. ctx.strokeStyle = this.selected ? this.options.color.highlight.border : this.hover ? this.options.color.hover.border : this.options.color.border;
  659. // draw the outer border
  660. if (this.clusterSize > 1) {
  661. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  662. ctx.lineWidth *= this.networkScaleInv;
  663. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  664. ctx.ellipse(this.left-2*ctx.lineWidth, this.top-2*ctx.lineWidth, this.width+4*ctx.lineWidth, this.height+4*ctx.lineWidth);
  665. ctx.stroke();
  666. }
  667. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  668. ctx.lineWidth *= this.networkScaleInv;
  669. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  670. ctx.fillStyle = this.selected ? this.options.color.highlight.background : this.hover ? this.options.color.hover.background : this.options.color.background;
  671. ctx.ellipse(this.left, this.top, this.width, this.height);
  672. ctx.fill();
  673. ctx.stroke();
  674. this.boundingBox.top = this.top;
  675. this.boundingBox.left = this.left;
  676. this.boundingBox.right = this.left + this.width;
  677. this.boundingBox.bottom = this.top + this.height;
  678. this._label(ctx, this.label, this.x, this.y);
  679. };
  680. Node.prototype._drawDot = function (ctx) {
  681. this._drawShape(ctx, 'circle');
  682. };
  683. Node.prototype._drawTriangle = function (ctx) {
  684. this._drawShape(ctx, 'triangle');
  685. };
  686. Node.prototype._drawTriangleDown = function (ctx) {
  687. this._drawShape(ctx, 'triangleDown');
  688. };
  689. Node.prototype._drawSquare = function (ctx) {
  690. this._drawShape(ctx, 'square');
  691. };
  692. Node.prototype._drawStar = function (ctx) {
  693. this._drawShape(ctx, 'star');
  694. };
  695. Node.prototype._resizeShape = function (ctx) {
  696. if (!this.width) {
  697. this.options.radius= this.baseRadiusValue;
  698. var size = 2 * this.options.radius;
  699. this.width = size;
  700. this.height = size;
  701. // scaling used for clustering
  702. this.width += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeWidthFactor;
  703. this.height += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeHeightFactor;
  704. this.options.radius+= Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * 0.5 * this.clusterSizeRadiusFactor;
  705. this.growthIndicator = this.width - size;
  706. }
  707. };
  708. Node.prototype._drawShape = function (ctx, shape) {
  709. this._resizeShape(ctx);
  710. this.left = this.x - this.width / 2;
  711. this.top = this.y - this.height / 2;
  712. var clusterLineWidth = 2.5;
  713. var borderWidth = this.options.borderWidth;
  714. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  715. var radiusMultiplier = 2;
  716. // choose draw method depending on the shape
  717. switch (shape) {
  718. case 'dot': radiusMultiplier = 2; break;
  719. case 'square': radiusMultiplier = 2; break;
  720. case 'triangle': radiusMultiplier = 3; break;
  721. case 'triangleDown': radiusMultiplier = 3; break;
  722. case 'star': radiusMultiplier = 4; break;
  723. }
  724. ctx.strokeStyle = this.selected ? this.options.color.highlight.border : this.hover ? this.options.color.hover.border : this.options.color.border;
  725. // draw the outer border
  726. if (this.clusterSize > 1) {
  727. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  728. ctx.lineWidth *= this.networkScaleInv;
  729. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  730. ctx[shape](this.x, this.y, this.options.radius+ radiusMultiplier * ctx.lineWidth);
  731. ctx.stroke();
  732. }
  733. ctx.lineWidth = (this.selected ? selectionLineWidth : borderWidth) + ((this.clusterSize > 1) ? clusterLineWidth : 0.0);
  734. ctx.lineWidth *= this.networkScaleInv;
  735. ctx.lineWidth = Math.min(this.width,ctx.lineWidth);
  736. ctx.fillStyle = this.selected ? this.options.color.highlight.background : this.hover ? this.options.color.hover.background : this.options.color.background;
  737. ctx[shape](this.x, this.y, this.options.radius);
  738. ctx.fill();
  739. ctx.stroke();
  740. this.boundingBox.top = this.y - this.options.radius;
  741. this.boundingBox.left = this.x - this.options.radius;
  742. this.boundingBox.right = this.x + this.options.radius;
  743. this.boundingBox.bottom = this.y + this.options.radius;
  744. if (this.label) {
  745. this._label(ctx, this.label, this.x, this.y + this.height / 2, undefined, 'top',true);
  746. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelDimensions.left);
  747. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelDimensions.left + this.labelDimensions.width);
  748. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelDimensions.height);
  749. }
  750. };
  751. Node.prototype._resizeText = function (ctx) {
  752. if (!this.width) {
  753. var margin = 5;
  754. var textSize = this.getTextSize(ctx);
  755. this.width = textSize.width + 2 * margin;
  756. this.height = textSize.height + 2 * margin;
  757. // scaling used for clustering
  758. this.width += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeWidthFactor;
  759. this.height += Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeHeightFactor;
  760. this.options.radius+= Math.min(this.clusterSize - 1, this.maxNodeSizeIncrements) * this.clusterSizeRadiusFactor;
  761. this.growthIndicator = this.width - (textSize.width + 2 * margin);
  762. }
  763. };
  764. Node.prototype._drawText = function (ctx) {
  765. this._resizeText(ctx);
  766. this.left = this.x - this.width / 2;
  767. this.top = this.y - this.height / 2;
  768. this._label(ctx, this.label, this.x, this.y);
  769. this.boundingBox.top = this.top;
  770. this.boundingBox.left = this.left;
  771. this.boundingBox.right = this.left + this.width;
  772. this.boundingBox.bottom = this.top + this.height;
  773. };
  774. Node.prototype._label = function (ctx, text, x, y, align, baseline, labelUnderNode) {
  775. if (text && Number(this.options.fontSize) * this.networkScale > this.fontDrawThreshold) {
  776. ctx.font = (this.selected ? "bold " : "") + this.options.fontSize + "px " + this.options.fontFace;
  777. var lines = text.split('\n');
  778. var lineCount = lines.length;
  779. var fontSize = (Number(this.options.fontSize) + 4); // TODO: why is this +4 ?
  780. var yLine = y + (1 - lineCount) / 2 * fontSize;
  781. if (labelUnderNode == true) {
  782. yLine = y + (1 - lineCount) / (2 * fontSize);
  783. }
  784. // font fill from edges now for nodes!
  785. var width = ctx.measureText(lines[0]).width;
  786. for (var i = 1; i < lineCount; i++) {
  787. var lineWidth = ctx.measureText(lines[i]).width;
  788. width = lineWidth > width ? lineWidth : width;
  789. }
  790. var height = this.options.fontSize * lineCount;
  791. var left = x - width / 2;
  792. var top = y - height / 2;
  793. if (baseline == "top") {
  794. top += 0.5 * fontSize;
  795. }
  796. this.labelDimensions = {top:top,left:left,width:width,height:height,yLine:yLine};
  797. // create the fontfill background
  798. if (this.options.fontFill !== undefined && this.options.fontFill !== null && this.options.fontFill !== "none") {
  799. ctx.fillStyle = this.options.fontFill;
  800. ctx.fillRect(left, top, width, height);
  801. }
  802. // draw text
  803. ctx.fillStyle = this.options.fontColor || "black";
  804. ctx.textAlign = align || "center";
  805. ctx.textBaseline = baseline || "middle";
  806. for (var i = 0; i < lineCount; i++) {
  807. ctx.fillText(lines[i], x, yLine);
  808. yLine += fontSize;
  809. }
  810. }
  811. };
  812. Node.prototype.getTextSize = function(ctx) {
  813. if (this.label !== undefined) {
  814. ctx.font = (this.selected ? "bold " : "") + this.options.fontSize + "px " + this.options.fontFace;
  815. var lines = this.label.split('\n'),
  816. height = (Number(this.options.fontSize) + 4) * lines.length,
  817. width = 0;
  818. for (var i = 0, iMax = lines.length; i < iMax; i++) {
  819. width = Math.max(width, ctx.measureText(lines[i]).width);
  820. }
  821. return {"width": width, "height": height};
  822. }
  823. else {
  824. return {"width": 0, "height": 0};
  825. }
  826. };
  827. /**
  828. * this is used to determine if a node is visible at all. this is used to determine when it needs to be drawn.
  829. * there is a safety margin of 0.3 * width;
  830. *
  831. * @returns {boolean}
  832. */
  833. Node.prototype.inArea = function() {
  834. if (this.width !== undefined) {
  835. return (this.x + this.width *this.networkScaleInv >= this.canvasTopLeft.x &&
  836. this.x - this.width *this.networkScaleInv < this.canvasBottomRight.x &&
  837. this.y + this.height*this.networkScaleInv >= this.canvasTopLeft.y &&
  838. this.y - this.height*this.networkScaleInv < this.canvasBottomRight.y);
  839. }
  840. else {
  841. return true;
  842. }
  843. };
  844. /**
  845. * checks if the core of the node is in the display area, this is used for opening clusters around zoom
  846. * @returns {boolean}
  847. */
  848. Node.prototype.inView = function() {
  849. return (this.x >= this.canvasTopLeft.x &&
  850. this.x < this.canvasBottomRight.x &&
  851. this.y >= this.canvasTopLeft.y &&
  852. this.y < this.canvasBottomRight.y);
  853. };
  854. /**
  855. * This allows the zoom level of the network to influence the rendering
  856. * We store the inverted scale and the coordinates of the top left, and bottom right points of the canvas
  857. *
  858. * @param scale
  859. * @param canvasTopLeft
  860. * @param canvasBottomRight
  861. */
  862. Node.prototype.setScaleAndPos = function(scale,canvasTopLeft,canvasBottomRight) {
  863. this.networkScaleInv = 1.0/scale;
  864. this.networkScale = scale;
  865. this.canvasTopLeft = canvasTopLeft;
  866. this.canvasBottomRight = canvasBottomRight;
  867. };
  868. /**
  869. * This allows the zoom level of the network to influence the rendering
  870. *
  871. * @param scale
  872. */
  873. Node.prototype.setScale = function(scale) {
  874. this.networkScaleInv = 1.0/scale;
  875. this.networkScale = scale;
  876. };
  877. /**
  878. * set the velocity at 0. Is called when this node is contained in another during clustering
  879. */
  880. Node.prototype.clearVelocity = function() {
  881. this.vx = 0;
  882. this.vy = 0;
  883. };
  884. /**
  885. * Basic preservation of (kinectic) energy
  886. *
  887. * @param massBeforeClustering
  888. */
  889. Node.prototype.updateVelocity = function(massBeforeClustering) {
  890. var energyBefore = this.vx * this.vx * massBeforeClustering;
  891. //this.vx = (this.vx < 0) ? -Math.sqrt(energyBefore/this.options.mass) : Math.sqrt(energyBefore/this.options.mass);
  892. this.vx = Math.sqrt(energyBefore/this.options.mass);
  893. energyBefore = this.vy * this.vy * massBeforeClustering;
  894. //this.vy = (this.vy < 0) ? -Math.sqrt(energyBefore/this.options.mass) : Math.sqrt(energyBefore/this.options.mass);
  895. this.vy = Math.sqrt(energyBefore/this.options.mass);
  896. };
  897. module.exports = Node;