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.

440 lines
13 KiB

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);
  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. // A node is connected when it has a from and to node that both exist in the network.body.nodes.
  61. this.connect();
  62. // update label Module
  63. this.updateLabelModule();
  64. let dataChanged = this.updateEdgeType();
  65. return dataChanged;
  66. }
  67. static parseOptions(parentOptions, newOptions) {
  68. var fields = [
  69. 'id',
  70. 'font',
  71. 'from',
  72. 'hidden',
  73. 'hoverWidth',
  74. 'label',
  75. 'length',
  76. 'line',
  77. 'opacity',
  78. 'physics',
  79. 'selfReferenceSize',
  80. 'to',
  81. 'title',
  82. 'value',
  83. 'width',
  84. 'widthMin',
  85. 'widthMax',
  86. 'widthSelectionMultiplier'
  87. ];
  88. // only deep extend the items in the field array. These do not have shorthand.
  89. util.selectiveDeepExtend(fields, parentOptions, newOptions);
  90. util.mergeOptions(parentOptions, newOptions, 'smooth');
  91. util.mergeOptions(parentOptions, newOptions, 'dashes');
  92. // set the scaling newOptions
  93. if (newOptions.scaling !== undefined) {
  94. if (newOptions.scaling.min !== undefined) {parentOptions.scaling.min = newOptions.scaling.min;}
  95. if (newOptions.scaling.max !== undefined) {parentOptions.scaling.max = newOptions.scaling.max;}
  96. util.mergeOptions(parentOptions.scaling, newOptions.scaling, 'label');
  97. }
  98. // hanlde multiple input cases for arrows
  99. if (newOptions.arrows !== undefined) {
  100. if (typeof newOptions.arrows === 'string') {
  101. let arrows = newOptions.arrows.toLowerCase();
  102. if (arrows.indexOf("to") != -1) {parentOptions.arrows.to.enabled = true;}
  103. if (arrows.indexOf("middle") != -1) {parentOptions.arrows.middle.enabled = true;}
  104. if (arrows.indexOf("from") != -1) {parentOptions.arrows.from.enabled = true;}
  105. }
  106. else if (typeof newOptions.arrows === 'object') {
  107. util.mergeOptions(parentOptions.arrows, newOptions.arrows, 'to');
  108. util.mergeOptions(parentOptions.arrows, newOptions.arrows, 'middle');
  109. util.mergeOptions(parentOptions.arrows, newOptions.arrows, 'from');
  110. }
  111. else {
  112. throw new Error("The arrow newOptions can only be an object or a string. Refer to the documentation. You used:" + JSON.stringify(newOptions.arrows));
  113. }
  114. }
  115. // hanlde multiple input cases for color
  116. if (newOptions.color !== undefined) {
  117. if (util.isString(newOptions.color)) {
  118. parentOptions.color.color = newOptions.color;
  119. parentOptions.color.highlight = newOptions.color;
  120. parentOptions.color.hover = newOptions.color;
  121. parentOptions.color.inherit.enabled = false;
  122. }
  123. else {
  124. let colorsDefined = false;
  125. if (newOptions.color.color !== undefined) {parentOptions.color.color = newOptions.color.color; colorsDefined = true;}
  126. if (newOptions.color.highlight !== undefined) {parentOptions.color.highlight = newOptions.color.highlight; colorsDefined = true;}
  127. if (newOptions.color.hover !== undefined) {parentOptions.color.hover = newOptions.color.hover; colorsDefined = true;}
  128. if (newOptions.color.opacity !== undefined) {parentOptions.color.opacity = newOptions.color.opacity;}
  129. if (newOptions.color.inherit === undefined && colorsDefined === true) {
  130. parentOptions.color.inherit.enabled = false;
  131. }
  132. }
  133. util.mergeOptions(parentOptions.color, newOptions.color, 'inherit');
  134. }
  135. }
  136. updateLabelModule() {
  137. this.labelModule.setOptions(this.options);
  138. if (this.labelModule.baseSize !== undefined) {
  139. this.baseFontSize = this.labelModule.baseSize;
  140. }
  141. }
  142. updateEdgeType() {
  143. let dataChanged = false;
  144. let changeInType = true;
  145. if (this.edgeType !== undefined) {
  146. if (this.edgeType instanceof BezierEdgeDynamic && this.options.smooth.enabled === true && this.options.smooth.dynamic === true) {changeInType = false;}
  147. if (this.edgeType instanceof BezierEdgeStatic && this.options.smooth.enabled === true && this.options.smooth.dynamic === false){changeInType = false;}
  148. if (this.edgeType instanceof StraightEdge && this.options.smooth.enabled === false) {changeInType = false;}
  149. if (changeInType === true) {
  150. dataChanged = this.edgeType.cleanup();
  151. }
  152. }
  153. if (changeInType === true) {
  154. if (this.options.smooth.enabled === true) {
  155. if (this.options.smooth.dynamic === true) {
  156. dataChanged = true;
  157. this.edgeType = new BezierEdgeDynamic(this.options, this.body, this.labelModule);
  158. }
  159. else {
  160. this.edgeType = new BezierEdgeStatic(this.options, this.body, this.labelModule);
  161. }
  162. }
  163. else {
  164. this.edgeType = new StraightEdge(this.options, this.body, this.labelModule);
  165. }
  166. }
  167. else {
  168. // if nothing changes, we just set the options.
  169. this.edgeType.setOptions(this.options);
  170. }
  171. return dataChanged;
  172. }
  173. /**
  174. * Enable or disable the physics.
  175. * @param status
  176. */
  177. togglePhysics(status) {
  178. if (this.options.smooth.enabled === true && this.options.smooth.dynamic === true) {
  179. if (this.via === undefined) {
  180. this.via.pptions.physics = status;
  181. }
  182. }
  183. this.options.physics = status;
  184. }
  185. /**
  186. * Connect an edge to its nodes
  187. */
  188. connect() {
  189. this.disconnect();
  190. this.from = this.body.nodes[this.fromId] || undefined;
  191. this.to = this.body.nodes[this.toId] || undefined;
  192. this.connected = (this.from !== undefined && this.to !== undefined);
  193. if (this.connected === true) {
  194. this.from.attachEdge(this);
  195. this.to.attachEdge(this);
  196. }
  197. else {
  198. if (this.from) {
  199. this.from.detachEdge(this);
  200. }
  201. if (this.to) {
  202. this.to.detachEdge(this);
  203. }
  204. }
  205. }
  206. /**
  207. * Disconnect an edge from its nodes
  208. */
  209. disconnect() {
  210. if (this.from) {
  211. this.from.detachEdge(this);
  212. this.from = undefined;
  213. }
  214. if (this.to) {
  215. this.to.detachEdge(this);
  216. this.to = undefined;
  217. }
  218. this.connected = false;
  219. }
  220. /**
  221. * get the title of this edge.
  222. * @return {string} title The title of the edge, or undefined when no title
  223. * has been set.
  224. */
  225. getTitle() {
  226. return typeof this.title === "function" ? this.title() : this.title;
  227. }
  228. /**
  229. * check if this node is selecte
  230. * @return {boolean} selected True if node is selected, else false
  231. */
  232. isSelected() {
  233. return this.selected;
  234. }
  235. /**
  236. * Retrieve the value of the edge. Can be undefined
  237. * @return {Number} value
  238. */
  239. getValue() {
  240. return this.options.value;
  241. }
  242. /**
  243. * Adjust the value range of the edge. The edge will adjust it's width
  244. * based on its value.
  245. * @param {Number} min
  246. * @param {Number} max
  247. * @param total
  248. */
  249. setValueRange(min, max, total) {
  250. if (this.options.value !== undefined) {
  251. var scale = this.options.scaling.customScalingFunction(min, max, total, this.options.value);
  252. var widthDiff = this.options.scaling.max - this.options.scaling.min;
  253. if (this.options.scaling.label.enabled === true) {
  254. var fontDiff = this.options.scaling.label.max - this.options.scaling.label.min;
  255. this.options.font.size = this.options.scaling.label.min + scale * fontDiff;
  256. }
  257. this.options.width = this.options.scaling.min + scale * widthDiff;
  258. }
  259. else {
  260. this.options.width = this.baseWidth;
  261. this.options.font.size = this.baseFontSize;
  262. }
  263. }
  264. /**
  265. * Redraw a edge
  266. * Draw this edge in the given canvas
  267. * The 2d context of a HTML canvas can be retrieved by canvas.getContext("2d");
  268. * @param {CanvasRenderingContext2D} ctx
  269. */
  270. draw(ctx) {
  271. let via = this.edgeType.drawLine(ctx, this.selected, this.hover);
  272. this.drawArrows(ctx, via);
  273. this.drawLabel (ctx, via);
  274. }
  275. drawArrows(ctx, viaNode) {
  276. if (this.options.arrows.from.enabled === true) {this.edgeType.drawArrowHead(ctx,'from', viaNode, this.selected, this.hover);}
  277. if (this.options.arrows.middle.enabled === true) {this.edgeType.drawArrowHead(ctx,'middle', viaNode, this.selected, this.hover);}
  278. if (this.options.arrows.to.enabled === true) {this.edgeType.drawArrowHead(ctx,'to', viaNode, this.selected, this.hover);}
  279. }
  280. drawLabel(ctx, viaNode) {
  281. if (this.options.label !== undefined) {
  282. // set style
  283. var node1 = this.from;
  284. var node2 = this.to;
  285. var selected = (this.from.selected || this.to.selected || this.selected);
  286. if (node1.id != node2.id) {
  287. var point = this.edgeType.getPoint(0.5, viaNode);
  288. ctx.save();
  289. // if the label has to be rotated:
  290. if (this.options.font.align !== "horizontal") {
  291. this.labelModule.calculateLabelSize(ctx,selected,point.x,point.y);
  292. ctx.translate(point.x, this.labelModule.size.yLine);
  293. this._rotateForLabelAlignment(ctx);
  294. }
  295. // draw the label
  296. this.labelModule.draw(ctx, point.x, point.y, selected);
  297. ctx.restore();
  298. }
  299. else {
  300. var x, y;
  301. var radius = this.options.selfReferenceSize;
  302. if (node1.width > node1.height) {
  303. x = node1.x + node1.width * 0.5;
  304. y = node1.y - radius;
  305. }
  306. else {
  307. x = node1.x + radius;
  308. y = node1.y - node1.height * 0.5;
  309. }
  310. point = this._pointOnCircle(x, y, radius, 0.125);
  311. this.labelModule.draw(ctx, point.x, point.y, selected);
  312. }
  313. }
  314. }
  315. /**
  316. * Check if this object is overlapping with the provided object
  317. * @param {Object} obj an object with parameters left, top
  318. * @return {boolean} True if location is located on the edge
  319. */
  320. isOverlappingWith(obj) {
  321. if (this.connected) {
  322. var distMax = 10;
  323. var xFrom = this.from.x;
  324. var yFrom = this.from.y;
  325. var xTo = this.to.x;
  326. var yTo = this.to.y;
  327. var xObj = obj.left;
  328. var yObj = obj.top;
  329. var dist = this.edgeType.getDistanceToEdge(xFrom, yFrom, xTo, yTo, xObj, yObj);
  330. return (dist < distMax);
  331. }
  332. else {
  333. return false
  334. }
  335. }
  336. /**
  337. * Rotates the canvas so the text is most readable
  338. * @param {CanvasRenderingContext2D} ctx
  339. * @private
  340. */
  341. _rotateForLabelAlignment(ctx) {
  342. var dy = this.from.y - this.to.y;
  343. var dx = this.from.x - this.to.x;
  344. var angleInDegrees = Math.atan2(dy, dx);
  345. // rotate so label it is readable
  346. if ((angleInDegrees < -1 && dx < 0) || (angleInDegrees > 0 && dx < 0)) {
  347. angleInDegrees = angleInDegrees + Math.PI;
  348. }
  349. ctx.rotate(angleInDegrees);
  350. }
  351. /**
  352. * Get a point on a circle
  353. * @param {Number} x
  354. * @param {Number} y
  355. * @param {Number} radius
  356. * @param {Number} percentage. Value between 0 (line start) and 1 (line end)
  357. * @return {Object} point
  358. * @private
  359. */
  360. _pointOnCircle(x, y, radius, percentage) {
  361. var angle = percentage * 2 * Math.PI;
  362. return {
  363. x: x + radius * Math.cos(angle),
  364. y: y - radius * Math.sin(angle)
  365. }
  366. }
  367. select() {
  368. this.selected = true;
  369. }
  370. unselect() {
  371. this.selected = false;
  372. }
  373. }
  374. export default Edge;