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.

457 lines
10 KiB

Add arrow types for EndPoints.js (#3839) * Add arrowhead support As described in TODO, dotparser.js does not support 'arrowhead' attribubte of edge. This update is for adding 'dot' and 'tee'(bar) styles. * Add example for arrow styles * Add box arrowhead To support box arrowhead of graphviz, add Box endpoint class in EndPoints.js and box attribute in dotparser.js. * Add diamond arrowhead To support diamond arrowhead of graphviz, add Diamond endpoint class in EndPoints.js and diamond attribute in dotparser.js. * Add crow arrowhead To support crow arrowhead of graphviz, add Crow endpoint class in EndPoints.js and crow attribute in dotparser.js. * Add normal arrowhead To support normal arrowhead of graphviz, add Triangle endpoint class in EndPoints.js and normal attribute in dotparser.js. * Add curve arrowhead To support curve arrowhead of graphviz, add Curve endpoint class in EndPoints.js and curve attribute in dotparser.js. * Add inverted curve arrowhead To support inverted curve arrowhead of graphviz, add InvertedCurve endpoint class in EndPoints.js and icurve attribute in dotparser.js. * Add vee arrowhead To support vee arrowhead of graphviz, add Vee endpoint class in EndPoints.js and vee attribute in dotparser.js. * Add arrowhead examples * Fix bug for accessing null attribute In createEdge(), accessing 'attr' causes an error if the edge has no attribute and the value is null. This update fixes bug for accessing null 'attr'. * Update description for arrows.to.type option Add followingn options for 'arrows.to.type'. * box * crow * curve * diamond * inv_curve * triangle * inv_triangle * vee * Update edgeStyle example for arrow types Add arrow types for the example. 'box', 'crow', 'curve', 'inv_curve', 'diamond', 'triangle', 'inv_triangle', 'vee'
6 years ago
Add arrow types for EndPoints.js (#3839) * Add arrowhead support As described in TODO, dotparser.js does not support 'arrowhead' attribubte of edge. This update is for adding 'dot' and 'tee'(bar) styles. * Add example for arrow styles * Add box arrowhead To support box arrowhead of graphviz, add Box endpoint class in EndPoints.js and box attribute in dotparser.js. * Add diamond arrowhead To support diamond arrowhead of graphviz, add Diamond endpoint class in EndPoints.js and diamond attribute in dotparser.js. * Add crow arrowhead To support crow arrowhead of graphviz, add Crow endpoint class in EndPoints.js and crow attribute in dotparser.js. * Add normal arrowhead To support normal arrowhead of graphviz, add Triangle endpoint class in EndPoints.js and normal attribute in dotparser.js. * Add curve arrowhead To support curve arrowhead of graphviz, add Curve endpoint class in EndPoints.js and curve attribute in dotparser.js. * Add inverted curve arrowhead To support inverted curve arrowhead of graphviz, add InvertedCurve endpoint class in EndPoints.js and icurve attribute in dotparser.js. * Add vee arrowhead To support vee arrowhead of graphviz, add Vee endpoint class in EndPoints.js and vee attribute in dotparser.js. * Add arrowhead examples * Fix bug for accessing null attribute In createEdge(), accessing 'attr' causes an error if the edge has no attribute and the value is null. This update fixes bug for accessing null 'attr'. * Update description for arrows.to.type option Add followingn options for 'arrows.to.type'. * box * crow * curve * diamond * inv_curve * triangle * inv_triangle * vee * Update edgeStyle example for arrow types Add arrow types for the example. 'box', 'crow', 'curve', 'inv_curve', 'diamond', 'triangle', 'inv_triangle', 'vee'
6 years ago
Add arrow types for EndPoints.js (#3839) * Add arrowhead support As described in TODO, dotparser.js does not support 'arrowhead' attribubte of edge. This update is for adding 'dot' and 'tee'(bar) styles. * Add example for arrow styles * Add box arrowhead To support box arrowhead of graphviz, add Box endpoint class in EndPoints.js and box attribute in dotparser.js. * Add diamond arrowhead To support diamond arrowhead of graphviz, add Diamond endpoint class in EndPoints.js and diamond attribute in dotparser.js. * Add crow arrowhead To support crow arrowhead of graphviz, add Crow endpoint class in EndPoints.js and crow attribute in dotparser.js. * Add normal arrowhead To support normal arrowhead of graphviz, add Triangle endpoint class in EndPoints.js and normal attribute in dotparser.js. * Add curve arrowhead To support curve arrowhead of graphviz, add Curve endpoint class in EndPoints.js and curve attribute in dotparser.js. * Add inverted curve arrowhead To support inverted curve arrowhead of graphviz, add InvertedCurve endpoint class in EndPoints.js and icurve attribute in dotparser.js. * Add vee arrowhead To support vee arrowhead of graphviz, add Vee endpoint class in EndPoints.js and vee attribute in dotparser.js. * Add arrowhead examples * Fix bug for accessing null attribute In createEdge(), accessing 'attr' causes an error if the edge has no attribute and the value is null. This update fixes bug for accessing null 'attr'. * Update description for arrows.to.type option Add followingn options for 'arrows.to.type'. * box * crow * curve * diamond * inv_curve * triangle * inv_triangle * vee * Update edgeStyle example for arrow types Add arrow types for the example. 'box', 'crow', 'curve', 'inv_curve', 'diamond', 'triangle', 'inv_triangle', 'vee'
6 years ago
Add arrow types for EndPoints.js (#3839) * Add arrowhead support As described in TODO, dotparser.js does not support 'arrowhead' attribubte of edge. This update is for adding 'dot' and 'tee'(bar) styles. * Add example for arrow styles * Add box arrowhead To support box arrowhead of graphviz, add Box endpoint class in EndPoints.js and box attribute in dotparser.js. * Add diamond arrowhead To support diamond arrowhead of graphviz, add Diamond endpoint class in EndPoints.js and diamond attribute in dotparser.js. * Add crow arrowhead To support crow arrowhead of graphviz, add Crow endpoint class in EndPoints.js and crow attribute in dotparser.js. * Add normal arrowhead To support normal arrowhead of graphviz, add Triangle endpoint class in EndPoints.js and normal attribute in dotparser.js. * Add curve arrowhead To support curve arrowhead of graphviz, add Curve endpoint class in EndPoints.js and curve attribute in dotparser.js. * Add inverted curve arrowhead To support inverted curve arrowhead of graphviz, add InvertedCurve endpoint class in EndPoints.js and icurve attribute in dotparser.js. * Add vee arrowhead To support vee arrowhead of graphviz, add Vee endpoint class in EndPoints.js and vee attribute in dotparser.js. * Add arrowhead examples * Fix bug for accessing null attribute In createEdge(), accessing 'attr' causes an error if the edge has no attribute and the value is null. This update fixes bug for accessing null 'attr'. * Update description for arrows.to.type option Add followingn options for 'arrows.to.type'. * box * crow * curve * diamond * inv_curve * triangle * inv_triangle * vee * Update edgeStyle example for arrow types Add arrow types for the example. 'box', 'crow', 'curve', 'inv_curve', 'diamond', 'triangle', 'inv_triangle', 'vee'
6 years ago
  1. /** ============================================================================
  2. * Location of all the endpoint drawing routines.
  3. *
  4. * Every endpoint has its own drawing routine, which contains an endpoint definition.
  5. *
  6. * The endpoint definitions must have the following properies:
  7. *
  8. * - (0,0) is the connection point to the node it attaches to
  9. * - The endpoints are orientated to the positive x-direction
  10. * - The length of the endpoint is at most 1
  11. *
  12. * As long as the endpoint classes remain simple and not too numerous, they will
  13. * be contained within this module.
  14. * All classes here except `EndPoints` should be considered as private to this module.
  15. *
  16. * -----------------------------------------------------------------------------
  17. * ### Further Actions
  18. *
  19. * After adding a new endpoint here, you also need to do the following things:
  20. *
  21. * - Add the new endpoint name to `network/options.js` in array `endPoints`.
  22. * - Add the new endpoint name to the documentation.
  23. * Scan for 'arrows.to.type` and add it to the description.
  24. * - Add the endpoint to the examples. At the very least, add it to example
  25. * `edgeStyles/arrowTypes`.
  26. * ============================================================================= */
  27. /**
  28. * Common methods for endpoints
  29. *
  30. * @class
  31. */
  32. class EndPoint {
  33. /**
  34. * Apply transformation on points for display.
  35. *
  36. * The following is done:
  37. * - rotate by the specified angle
  38. * - multiply the (normalized) coordinates by the passed length
  39. * - offset by the target coordinates
  40. *
  41. * @param {Array<Point>} points
  42. * @param {ArrowData} arrowData
  43. * @static
  44. */
  45. static transform(points, arrowData) {
  46. if (!(points instanceof Array)) {
  47. points = [points];
  48. }
  49. var x = arrowData.point.x;
  50. var y = arrowData.point.y;
  51. var angle = arrowData.angle
  52. var length = arrowData.length;
  53. for(var i = 0; i < points.length; ++i) {
  54. var p = points[i];
  55. var xt = p.x * Math.cos(angle) - p.y * Math.sin(angle);
  56. var yt = p.x * Math.sin(angle) + p.y * Math.cos(angle);
  57. p.x = x + length*xt;
  58. p.y = y + length*yt;
  59. }
  60. }
  61. /**
  62. * Draw a closed path using the given real coordinates.
  63. *
  64. * @param {CanvasRenderingContext2D} ctx
  65. * @param {Array.<Point>} points
  66. * @static
  67. */
  68. static drawPath(ctx, points) {
  69. ctx.beginPath();
  70. ctx.moveTo(points[0].x, points[0].y);
  71. for(var i = 1; i < points.length; ++i) {
  72. ctx.lineTo(points[i].x, points[i].y);
  73. }
  74. ctx.closePath();
  75. }
  76. }
  77. /**
  78. * Drawing methods for the arrow endpoint.
  79. * @extends EndPoint
  80. */
  81. class Arrow extends EndPoint {
  82. /**
  83. * Draw this shape at the end of a line.
  84. *
  85. * @param {CanvasRenderingContext2D} ctx
  86. * @param {ArrowData} arrowData
  87. * @static
  88. */
  89. static draw(ctx, arrowData) {
  90. // Normalized points of closed path, in the order that they should be drawn.
  91. // (0, 0) is the attachment point, and the point around which should be rotated
  92. var points = [
  93. { x: 0 , y: 0 },
  94. { x:-1 , y: 0.3},
  95. { x:-0.9, y: 0 },
  96. { x:-1 , y:-0.3},
  97. ];
  98. EndPoint.transform(points, arrowData);
  99. EndPoint.drawPath(ctx, points);
  100. }
  101. }
  102. /**
  103. * Drawing methods for the crow endpoint.
  104. * @extends EndPoint
  105. */
  106. class Crow {
  107. /**
  108. * Draw this shape at the end of a line.
  109. *
  110. * @param {CanvasRenderingContext2D} ctx
  111. * @param {ArrowData} arrowData
  112. * @static
  113. */
  114. static draw(ctx, arrowData) {
  115. // Normalized points of closed path, in the order that they should be drawn.
  116. // (0, 0) is the attachment point, and the point around which should be rotated
  117. var points = [
  118. { x:-1, y: 0},
  119. { x:0, y: 0.3},
  120. { x:-0.4, y: 0},
  121. { x:0, y:-0.3},
  122. ];
  123. EndPoint.transform(points, arrowData);
  124. EndPoint.drawPath(ctx, points);
  125. }
  126. }
  127. /**
  128. * Drawing methods for the curve endpoint.
  129. * @extends EndPoint
  130. */
  131. class Curve {
  132. /**
  133. * Draw this shape at the end of a line.
  134. *
  135. * @param {CanvasRenderingContext2D} ctx
  136. * @param {ArrowData} arrowData
  137. * @static
  138. */
  139. static draw(ctx, arrowData) {
  140. // Normalized points of closed path, in the order that they should be drawn.
  141. // (0, 0) is the attachment point, and the point around which should be rotated
  142. var point = {x:-0.4, y:0};
  143. EndPoint.transform(point, arrowData);
  144. // Update endpoint style for drawing transparent arc.
  145. ctx.strokeStyle = ctx.fillStyle;
  146. ctx.fillStyle = 'rgba(0, 0, 0, 0)';
  147. // Define curve endpoint as semicircle.
  148. var pi = Math.PI
  149. var start_angle = arrowData.angle - pi/2;
  150. var end_angle = arrowData.angle + pi/2;
  151. ctx.beginPath();
  152. ctx.arc(point.x, point.y, arrowData.length*0.4, start_angle, end_angle, false);
  153. ctx.stroke()
  154. }
  155. }
  156. /**
  157. * Drawing methods for the inverted curve endpoint.
  158. * @extends EndPoint
  159. */
  160. class InvertedCurve {
  161. /**
  162. * Draw this shape at the end of a line.
  163. *
  164. * @param {CanvasRenderingContext2D} ctx
  165. * @param {ArrowData} arrowData
  166. * @static
  167. */
  168. static draw(ctx, arrowData) {
  169. // Normalized points of closed path, in the order that they should be drawn.
  170. // (0, 0) is the attachment point, and the point around which should be rotated
  171. var point = {x:-0.3, y:0};
  172. EndPoint.transform(point, arrowData);
  173. // Update endpoint style for drawing transparent arc.
  174. ctx.strokeStyle = ctx.fillStyle;
  175. ctx.fillStyle = 'rgba(0, 0, 0, 0)';
  176. // Define inverted curve endpoint as semicircle.
  177. var pi = Math.PI
  178. var start_angle = arrowData.angle + pi/2;
  179. var end_angle = arrowData.angle + 3*pi/2;
  180. ctx.beginPath();
  181. ctx.arc(point.x, point.y, arrowData.length*0.4, start_angle, end_angle, false);
  182. ctx.stroke()
  183. }
  184. }
  185. /**
  186. * Drawing methods for the trinagle endpoint.
  187. * @extends EndPoint
  188. */
  189. class Triangle {
  190. /**
  191. * Draw this shape at the end of a line.
  192. *
  193. * @param {CanvasRenderingContext2D} ctx
  194. * @param {ArrowData} arrowData
  195. * @static
  196. */
  197. static draw(ctx, arrowData) {
  198. // Normalized points of closed path, in the order that they should be drawn.
  199. // (0, 0) is the attachment point, and the point around which should be rotated
  200. var points = [
  201. { x:0.02, y:0},
  202. { x:-1, y: 0.3},
  203. { x:-1, y:-0.3},
  204. ];
  205. EndPoint.transform(points, arrowData);
  206. EndPoint.drawPath(ctx, points);
  207. }
  208. }
  209. /**
  210. * Drawing methods for the inverted trinagle endpoint.
  211. * @extends EndPoint
  212. */
  213. class InvertedTriangle {
  214. /**
  215. * Draw this shape at the end of a line.
  216. *
  217. * @param {CanvasRenderingContext2D} ctx
  218. * @param {ArrowData} arrowData
  219. * @static
  220. */
  221. static draw(ctx, arrowData) {
  222. // Normalized points of closed path, in the order that they should be drawn.
  223. // (0, 0) is the attachment point, and the point around which should be rotated
  224. var points = [
  225. { x:0, y:0.3},
  226. { x:0, y: -0.3},
  227. { x:-1, y:0},
  228. ];
  229. EndPoint.transform(points, arrowData);
  230. EndPoint.drawPath(ctx, points);
  231. }
  232. }
  233. /**
  234. * Drawing methods for the circle endpoint.
  235. */
  236. class Circle {
  237. /**
  238. * Draw this shape at the end of a line.
  239. *
  240. * @param {CanvasRenderingContext2D} ctx
  241. * @param {ArrowData} arrowData
  242. * @static
  243. */
  244. static draw(ctx, arrowData) {
  245. var point = {x:-0.4, y:0};
  246. EndPoint.transform(point, arrowData);
  247. ctx.circle(point.x, point.y, arrowData.length*0.4);
  248. }
  249. }
  250. /**
  251. * Drawing methods for the bar endpoint.
  252. */
  253. class Bar {
  254. /**
  255. * Draw this shape at the end of a line.
  256. *
  257. * @param {CanvasRenderingContext2D} ctx
  258. * @param {ArrowData} arrowData
  259. * @static
  260. */
  261. static draw(ctx, arrowData) {
  262. /*
  263. var points = [
  264. {x:0, y:0.5},
  265. {x:0, y:-0.5}
  266. ];
  267. EndPoint.transform(points, arrowData);
  268. ctx.beginPath();
  269. ctx.moveTo(points[0].x, points[0].y);
  270. ctx.lineTo(points[1].x, points[1].y);
  271. ctx.stroke();
  272. */
  273. var points = [
  274. {x:0, y:0.5},
  275. {x:0, y:-0.5},
  276. {x:-0.15, y:-0.5},
  277. {x:-0.15, y:0.5},
  278. ];
  279. EndPoint.transform(points, arrowData);
  280. EndPoint.drawPath(ctx, points);
  281. }
  282. }
  283. /**
  284. * Drawing methods for the box endpoint.
  285. */
  286. class Box {
  287. /**
  288. * Draw this shape at the end of a line.
  289. *
  290. * @param {CanvasRenderingContext2D} ctx
  291. * @param {ArrowData} arrowData
  292. * @static
  293. */
  294. static draw(ctx, arrowData) {
  295. var points = [
  296. {x:0, y:0.3},
  297. {x:0, y:-0.3},
  298. {x:-0.6, y:-0.3},
  299. {x:-0.6, y:0.3},
  300. ];
  301. EndPoint.transform(points, arrowData);
  302. EndPoint.drawPath(ctx, points);
  303. }
  304. }
  305. /**
  306. * Drawing methods for the diamond endpoint.
  307. */
  308. class Diamond {
  309. /**
  310. * Draw this shape at the end of a line.
  311. *
  312. * @param {CanvasRenderingContext2D} ctx
  313. * @param {ArrowData} arrowData
  314. * @static
  315. */
  316. static draw(ctx, arrowData) {
  317. var points = [
  318. {x:0, y:0},
  319. {x:-0.5, y:-0.3},
  320. {x:-1, y:0},
  321. {x:-0.5, y:0.3},
  322. ];
  323. EndPoint.transform(points, arrowData);
  324. EndPoint.drawPath(ctx, points);
  325. }
  326. }
  327. /**
  328. * Drawing methods for the vee endpoint.
  329. * @extends EndPoint
  330. */
  331. class Vee {
  332. /**
  333. * Draw this shape at the end of a line.
  334. *
  335. * @param {CanvasRenderingContext2D} ctx
  336. * @param {ArrowData} arrowData
  337. * @static
  338. */
  339. static draw(ctx, arrowData) {
  340. // Normalized points of closed path, in the order that they should be drawn.
  341. // (0, 0) is the attachment point, and the point around which should be rotated
  342. var points = [
  343. { x:-1, y: 0.3},
  344. { x:-0.5, y: 0},
  345. { x:-1, y:-0.3},
  346. { x:0, y: 0},
  347. ];
  348. EndPoint.transform(points, arrowData);
  349. EndPoint.drawPath(ctx, points);
  350. }
  351. }
  352. /**
  353. * Drawing methods for the endpoints.
  354. */
  355. class EndPoints {
  356. /**
  357. * Draw an endpoint
  358. *
  359. * @param {CanvasRenderingContext2D} ctx
  360. * @param {ArrowData} arrowData
  361. * @static
  362. */
  363. static draw(ctx, arrowData) {
  364. var type;
  365. if (arrowData.type) {
  366. type = arrowData.type.toLowerCase();
  367. }
  368. switch (type) {
  369. case 'circle':
  370. Circle.draw(ctx, arrowData);
  371. break;
  372. case 'box':
  373. Box.draw(ctx, arrowData);
  374. break;
  375. case 'crow':
  376. Crow.draw(ctx, arrowData);
  377. break;
  378. case 'curve':
  379. Curve.draw(ctx, arrowData);
  380. break;
  381. case 'diamond':
  382. Diamond.draw(ctx, arrowData);
  383. break;
  384. case 'inv_curve':
  385. InvertedCurve.draw(ctx, arrowData);
  386. break;
  387. case 'triangle':
  388. Triangle.draw(ctx, arrowData);
  389. break;
  390. case 'inv_triangle':
  391. InvertedTriangle.draw(ctx, arrowData);
  392. break;
  393. case 'bar':
  394. Bar.draw(ctx, arrowData);
  395. break;
  396. case 'vee':
  397. Vee.draw(ctx, arrowData);
  398. break;
  399. case 'arrow': // fall-through
  400. default:
  401. Arrow.draw(ctx, arrowData);
  402. }
  403. }
  404. }
  405. export default EndPoints;