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.

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