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.

499 lines
15 KiB

9 years ago
9 years ago
9 years ago
10 years ago
  1. var util = require('../../../util');
  2. import Label from './shared/Label'
  3. import BezierEdgeDynamic from './edges/BezierEdgeDynamic'
  4. import BezierEdgeStatic from './edges/BezierEdgeStatic'
  5. import StraightEdge from './edges/StraightEdge'
  6. /**
  7. * @class Edge
  8. *
  9. * A edge connects two nodes
  10. * @param {Object} properties Object with options. Must contain
  11. * At least options from and to.
  12. * Available options: from (number),
  13. * to (number), label (string, color (string),
  14. * width (number), style (string),
  15. * length (number), title (string)
  16. * @param {Network} network A Network object, used to find and edge to
  17. * nodes.
  18. * @param {Object} constants An object with default values for
  19. * example for the color
  20. */
  21. class Edge {
  22. constructor(options, body, globalOptions) {
  23. if (body === undefined) {
  24. throw "No body provided";
  25. }
  26. this.options = util.bridgeObject(globalOptions);
  27. this.body = body;
  28. // initialize variables
  29. this.id = undefined;
  30. this.fromId = undefined;
  31. this.toId = undefined;
  32. this.selected = false;
  33. this.hover = false;
  34. this.labelDirty = true;
  35. this.colorDirty = true;
  36. this.baseWidth = this.options.width;
  37. this.baseFontSize = this.options.font.size;
  38. this.from = undefined; // a node
  39. this.to = undefined; // a node
  40. this.edgeType = undefined;
  41. this.connected = false;
  42. this.labelModule = new Label(this.body, this.options);
  43. this.setOptions(options);
  44. }
  45. /**
  46. * Set or overwrite options for the edge
  47. * @param {Object} options an object with options
  48. * @param doNotEmit
  49. */
  50. setOptions(options) {
  51. if (!options) {
  52. return;
  53. }
  54. this.colorDirty = true;
  55. Edge.parseOptions(this.options, options, true);
  56. if (options.id !== undefined) {this.id = options.id;}
  57. if (options.from !== undefined) {this.fromId = options.from;}
  58. if (options.to !== undefined) {this.toId = options.to;}
  59. if (options.title !== undefined) {this.title = options.title;}
  60. if (options.value !== undefined) {options.value = parseFloat(options.value);}
  61. // update label Module
  62. this.updateLabelModule();
  63. let dataChanged = this.updateEdgeType();
  64. // if anything has been updates, reset the selection width and the hover width
  65. this._setInteractionWidths();
  66. // A node is connected when it has a from and to node that both exist in the network.body.nodes.
  67. this.connect();
  68. if (options.hidden !== undefined || options.physics !== undefined) {
  69. dataChanged = true;
  70. }
  71. return dataChanged;
  72. }
  73. static parseOptions(parentOptions, newOptions, allowDeletion = false) {
  74. var fields = [
  75. 'id',
  76. 'from',
  77. 'hidden',
  78. 'hoverWidth',
  79. 'label',
  80. 'labelHighlightBold',
  81. 'length',
  82. 'line',
  83. 'opacity',
  84. 'physics',
  85. 'selectionWidth',
  86. 'selfReferenceSize',
  87. 'to',
  88. 'title',
  89. 'value',
  90. 'width'
  91. ];
  92. // only deep extend the items in the field array. These do not have shorthand.
  93. util.selectiveDeepExtend(fields, parentOptions, newOptions, allowDeletion);
  94. util.mergeOptions(parentOptions, newOptions, 'smooth');
  95. util.mergeOptions(parentOptions, newOptions, 'shadow');
  96. if (newOptions.dashes !== undefined && newOptions.dashes !== null) {
  97. parentOptions.dashes = newOptions.dashes;
  98. }
  99. else if (allowDeletion === true && newOptions.dashes === null) {
  100. parentOptions.dashes = undefined;
  101. delete parentOptions.dashes;
  102. }
  103. // set the scaling newOptions
  104. if (newOptions.scaling !== undefined && newOptions.scaling !== null) {
  105. if (newOptions.scaling.min !== undefined) {parentOptions.scaling.min = newOptions.scaling.min;}
  106. if (newOptions.scaling.max !== undefined) {parentOptions.scaling.max = newOptions.scaling.max;}
  107. util.mergeOptions(parentOptions.scaling, newOptions.scaling, 'label');
  108. }
  109. else if (allowDeletion === true && newOptions.scaling === null) {
  110. parentOptions.scaling = undefined;
  111. delete parentOptions.scaling;
  112. }
  113. // hanlde multiple input cases for arrows
  114. if (newOptions.arrows !== undefined && newOptions.arrows !== null) {
  115. if (typeof newOptions.arrows === 'string') {
  116. let arrows = newOptions.arrows.toLowerCase();
  117. if (arrows.indexOf("to") != -1) {parentOptions.arrows.to.enabled = true;}
  118. if (arrows.indexOf("middle") != -1) {parentOptions.arrows.middle.enabled = true;}
  119. if (arrows.indexOf("from") != -1) {parentOptions.arrows.from.enabled = true;}
  120. }
  121. else if (typeof newOptions.arrows === 'object') {
  122. util.mergeOptions(parentOptions.arrows, newOptions.arrows, 'to');
  123. util.mergeOptions(parentOptions.arrows, newOptions.arrows, 'middle');
  124. util.mergeOptions(parentOptions.arrows, newOptions.arrows, 'from');
  125. }
  126. else {
  127. throw new Error("The arrow newOptions can only be an object or a string. Refer to the documentation. You used:" + JSON.stringify(newOptions.arrows));
  128. }
  129. }
  130. else if (allowDeletion === true && newOptions.arrows === null) {
  131. parentOptions.arrows = undefined;
  132. delete parentOptions.arrows;
  133. }
  134. // hanlde multiple input cases for color
  135. if (newOptions.color !== undefined && newOptions.color !== null) {
  136. if (util.isString(newOptions.color)) {
  137. parentOptions.color.color = newOptions.color;
  138. parentOptions.color.highlight = newOptions.color;
  139. parentOptions.color.hover = newOptions.color;
  140. parentOptions.color.inherit = false;
  141. }
  142. else {
  143. let colorsDefined = false;
  144. if (newOptions.color.color !== undefined) {parentOptions.color.color = newOptions.color.color; colorsDefined = true;}
  145. if (newOptions.color.highlight !== undefined) {parentOptions.color.highlight = newOptions.color.highlight; colorsDefined = true;}
  146. if (newOptions.color.hover !== undefined) {parentOptions.color.hover = newOptions.color.hover; colorsDefined = true;}
  147. if (newOptions.color.inherit !== undefined) {parentOptions.color.inherit = newOptions.color.inherit;}
  148. if (newOptions.color.opacity !== undefined) {parentOptions.color.opacity = Math.min(1,Math.max(0,newOptions.color.opacity));}
  149. if (newOptions.color.inherit === undefined && colorsDefined === true) {
  150. parentOptions.color.inherit = false;
  151. }
  152. }
  153. }
  154. else if (allowDeletion === true && newOptions.color === null) {
  155. parentOptions.color = undefined;
  156. delete parentOptions.color;
  157. }
  158. // handle the font settings
  159. if (newOptions.font !== undefined) {
  160. Label.parseOptions(parentOptions.font, newOptions);
  161. }
  162. }
  163. /**
  164. * update the options in the label module
  165. */
  166. updateLabelModule() {
  167. this.labelModule.setOptions(this.options, true);
  168. if (this.labelModule.baseSize !== undefined) {
  169. this.baseFontSize = this.labelModule.baseSize;
  170. }
  171. }
  172. /**
  173. * update the edge type, set the options
  174. * @returns {boolean}
  175. */
  176. updateEdgeType() {
  177. let dataChanged = false;
  178. let changeInType = true;
  179. if (this.edgeType !== undefined) {
  180. if (this.edgeType instanceof BezierEdgeDynamic && this.options.smooth.enabled === true && this.options.smooth.type === 'dynamic') {changeInType = false;}
  181. if (this.edgeType instanceof BezierEdgeStatic && this.options.smooth.enabled === true && this.options.smooth.type !== 'dynamic') {changeInType = false;}
  182. if (this.edgeType instanceof StraightEdge && this.options.smooth.enabled === false) {changeInType = false;}
  183. if (changeInType === true) {
  184. dataChanged = this.edgeType.cleanup();
  185. }
  186. }
  187. if (changeInType === true) {
  188. if (this.options.smooth.enabled === true) {
  189. if (this.options.smooth.type === 'dynamic') {
  190. dataChanged = true;
  191. this.edgeType = new BezierEdgeDynamic(this.options, this.body, this.labelModule);
  192. }
  193. else {
  194. this.edgeType = new BezierEdgeStatic(this.options, this.body, this.labelModule);
  195. }
  196. }
  197. else {
  198. this.edgeType = new StraightEdge(this.options, this.body, this.labelModule);
  199. }
  200. }
  201. else {
  202. // if nothing changes, we just set the options.
  203. this.edgeType.setOptions(this.options);
  204. }
  205. return dataChanged;
  206. }
  207. /**
  208. * Enable or disable the physics.
  209. * @param status
  210. */
  211. togglePhysics(status) {
  212. this.options.physics = status;
  213. this.edgeType.togglePhysics(status);
  214. }
  215. /**
  216. * Connect an edge to its nodes
  217. */
  218. connect() {
  219. this.disconnect();
  220. this.from = this.body.nodes[this.fromId] || undefined;
  221. this.to = this.body.nodes[this.toId] || undefined;
  222. this.connected = (this.from !== undefined && this.to !== undefined);
  223. if (this.connected === true) {
  224. this.from.attachEdge(this);
  225. this.to.attachEdge(this);
  226. }
  227. else {
  228. if (this.from) {
  229. this.from.detachEdge(this);
  230. }
  231. if (this.to) {
  232. this.to.detachEdge(this);
  233. }
  234. }
  235. this.edgeType.connect();
  236. }
  237. /**
  238. * Disconnect an edge from its nodes
  239. */
  240. disconnect() {
  241. if (this.from) {
  242. this.from.detachEdge(this);
  243. this.from = undefined;
  244. }
  245. if (this.to) {
  246. this.to.detachEdge(this);
  247. this.to = undefined;
  248. }
  249. this.connected = false;
  250. }
  251. /**
  252. * get the title of this edge.
  253. * @return {string} title The title of the edge, or undefined when no title
  254. * has been set.
  255. */
  256. getTitle() {
  257. return this.title;
  258. }
  259. /**
  260. * check if this node is selecte
  261. * @return {boolean} selected True if node is selected, else false
  262. */
  263. isSelected() {
  264. return this.selected;
  265. }
  266. /**
  267. * Retrieve the value of the edge. Can be undefined
  268. * @return {Number} value
  269. */
  270. getValue() {
  271. return this.options.value;
  272. }
  273. /**
  274. * Adjust the value range of the edge. The edge will adjust it's width
  275. * based on its value.
  276. * @param {Number} min
  277. * @param {Number} max
  278. * @param total
  279. */
  280. setValueRange(min, max, total) {
  281. if (this.options.value !== undefined) {
  282. var scale = this.options.scaling.customScalingFunction(min, max, total, this.options.value);
  283. var widthDiff = this.options.scaling.max - this.options.scaling.min;
  284. if (this.options.scaling.label.enabled === true) {
  285. var fontDiff = this.options.scaling.label.max - this.options.scaling.label.min;
  286. this.options.font.size = this.options.scaling.label.min + scale * fontDiff;
  287. }
  288. this.options.width = this.options.scaling.min + scale * widthDiff;
  289. }
  290. else {
  291. this.options.width = this.baseWidth;
  292. this.options.font.size = this.baseFontSize;
  293. }
  294. this._setInteractionWidths();
  295. }
  296. _setInteractionWidths() {
  297. if (typeof this.options.hoverWidth === 'function') {
  298. this.edgeType.hoverWidth = this.options.hoverWidth(this.options.width);
  299. }
  300. else {
  301. this.edgeType.hoverWidth = this.options.hoverWidth + this.options.width;
  302. }
  303. if (typeof this.options.selectionWidth === 'function') {
  304. this.edgeType.selectionWidth = this.options.selectionWidth(this.options.width);
  305. }
  306. else {
  307. this.edgeType.selectionWidth = this.options.selectionWidth + this.options.width;
  308. }
  309. }
  310. /**
  311. * Redraw a edge
  312. * Draw this edge in the given canvas
  313. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  314. * @param {CanvasRenderingContext2D} ctx
  315. */
  316. draw(ctx) {
  317. let via = this.edgeType.drawLine(ctx, this.selected, this.hover);
  318. this.drawArrows(ctx, via);
  319. this.drawLabel (ctx, via);
  320. }
  321. drawArrows(ctx, viaNode) {
  322. if (this.options.arrows.from.enabled === true) {this.edgeType.drawArrowHead(ctx,'from', viaNode, this.selected, this.hover);}
  323. if (this.options.arrows.middle.enabled === true) {this.edgeType.drawArrowHead(ctx,'middle', viaNode, this.selected, this.hover);}
  324. if (this.options.arrows.to.enabled === true) {this.edgeType.drawArrowHead(ctx,'to', viaNode, this.selected, this.hover);}
  325. }
  326. drawLabel(ctx, viaNode) {
  327. if (this.options.label !== undefined) {
  328. // set style
  329. var node1 = this.from;
  330. var node2 = this.to;
  331. var selected = (this.from.selected || this.to.selected || this.selected);
  332. if (node1.id != node2.id) {
  333. this.labelModule.pointToSelf = false;
  334. var point = this.edgeType.getPoint(0.5, viaNode);
  335. ctx.save();
  336. // if the label has to be rotated:
  337. if (this.options.font.align !== "horizontal") {
  338. this.labelModule.calculateLabelSize(ctx,selected,point.x,point.y);
  339. ctx.translate(point.x, this.labelModule.size.yLine);
  340. this._rotateForLabelAlignment(ctx);
  341. }
  342. // draw the label
  343. this.labelModule.draw(ctx, point.x, point.y, selected);
  344. ctx.restore();
  345. }
  346. else {
  347. // Ignore the orientations.
  348. this.labelModule.pointToSelf = true;
  349. var x, y;
  350. var radius = this.options.selfReferenceSize;
  351. if (node1.shape.width > node1.shape.height) {
  352. x = node1.x + node1.shape.width * 0.5;
  353. y = node1.y - radius;
  354. }
  355. else {
  356. x = node1.x + radius;
  357. y = node1.y - node1.shape.height * 0.5;
  358. }
  359. point = this._pointOnCircle(x, y, radius, 0.125);
  360. this.labelModule.draw(ctx, point.x, point.y, selected);
  361. }
  362. }
  363. }
  364. /**
  365. * Check if this object is overlapping with the provided object
  366. * @param {Object} obj an object with parameters left, top
  367. * @return {boolean} True if location is located on the edge
  368. */
  369. isOverlappingWith(obj) {
  370. if (this.connected) {
  371. var distMax = 10;
  372. var xFrom = this.from.x;
  373. var yFrom = this.from.y;
  374. var xTo = this.to.x;
  375. var yTo = this.to.y;
  376. var xObj = obj.left;
  377. var yObj = obj.top;
  378. var dist = this.edgeType.getDistanceToEdge(xFrom, yFrom, xTo, yTo, xObj, yObj);
  379. return (dist < distMax);
  380. }
  381. else {
  382. return false
  383. }
  384. }
  385. /**
  386. * Rotates the canvas so the text is most readable
  387. * @param {CanvasRenderingContext2D} ctx
  388. * @private
  389. */
  390. _rotateForLabelAlignment(ctx) {
  391. var dy = this.from.y - this.to.y;
  392. var dx = this.from.x - this.to.x;
  393. var angleInDegrees = Math.atan2(dy, dx);
  394. // rotate so label it is readable
  395. if ((angleInDegrees < -1 && dx < 0) || (angleInDegrees > 0 && dx < 0)) {
  396. angleInDegrees = angleInDegrees + Math.PI;
  397. }
  398. ctx.rotate(angleInDegrees);
  399. }
  400. /**
  401. * Get a point on a circle
  402. * @param {Number} x
  403. * @param {Number} y
  404. * @param {Number} radius
  405. * @param {Number} percentage. Value between 0 (line start) and 1 (line end)
  406. * @return {Object} point
  407. * @private
  408. */
  409. _pointOnCircle(x, y, radius, percentage) {
  410. var angle = percentage * 2 * Math.PI;
  411. return {
  412. x: x + radius * Math.cos(angle),
  413. y: y - radius * Math.sin(angle)
  414. }
  415. }
  416. select() {
  417. this.selected = true;
  418. }
  419. unselect() {
  420. this.selected = false;
  421. }
  422. }
  423. export default Edge;