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.

1064 lines
37 KiB

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