FlagsSymbols.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* *
  2. *
  3. * Imports
  4. *
  5. * */
  6. import H from '../../Core/Globals.js';
  7. var Renderer = H.Renderer, VMLRenderer = H.VMLRenderer;
  8. import SVGRenderer from '../../Core/Renderer/SVG/SVGRenderer.js';
  9. var symbols = SVGRenderer.prototype.symbols;
  10. /* *
  11. *
  12. * Symbols
  13. *
  14. * */
  15. // create the flag icon with anchor
  16. symbols.flag = function (x, y, w, h, options) {
  17. var anchorX = (options && options.anchorX) || x, anchorY = (options && options.anchorY) || y;
  18. // To do: unwanted any cast because symbols.circle has wrong type, it
  19. // actually returns an SVGPathArray
  20. var path = symbols.circle(anchorX - 1, anchorY - 1, 2, 2);
  21. path.push(['M', anchorX, anchorY], ['L', x, y + h], ['L', x, y], ['L', x + w, y], ['L', x + w, y + h], ['L', x, y + h], ['Z']);
  22. return path;
  23. };
  24. /**
  25. * Create the circlepin and squarepin icons with anchor.
  26. * @private
  27. * @param {string} shape - circle or square
  28. * @return {void}
  29. */
  30. function createPinSymbol(shape) {
  31. symbols[shape + 'pin'] = function (x, y, w, h, options) {
  32. var anchorX = options && options.anchorX, anchorY = options && options.anchorY, path;
  33. // For single-letter flags, make sure circular flags are not taller
  34. // than their width
  35. if (shape === 'circle' && h > w) {
  36. x -= Math.round((h - w) / 2);
  37. w = h;
  38. }
  39. path = (symbols[shape])(x, y, w, h);
  40. if (anchorX && anchorY) {
  41. /**
  42. * If the label is below the anchor, draw the connecting line from
  43. * the top edge of the label, otherwise start drawing from the
  44. * bottom edge
  45. */
  46. var labelX = anchorX;
  47. if (shape === 'circle') {
  48. labelX = x + w / 2;
  49. }
  50. else {
  51. var startSeg = path[0];
  52. var endSeg = path[1];
  53. if (startSeg[0] === 'M' && endSeg[0] === 'L') {
  54. labelX = (startSeg[1] + endSeg[1]) / 2;
  55. }
  56. }
  57. var labelY = (y > anchorY) ? y : y + h;
  58. path.push([
  59. 'M',
  60. labelX,
  61. labelY
  62. ], [
  63. 'L',
  64. anchorX,
  65. anchorY
  66. ]);
  67. path = path.concat(symbols.circle(anchorX - 1, anchorY - 1, 2, 2));
  68. }
  69. return path;
  70. };
  71. }
  72. createPinSymbol('circle');
  73. createPinSymbol('square');
  74. /**
  75. * The symbol callbacks are generated on the SVGRenderer object in all browsers.
  76. * Even VML browsers need this in order to generate shapes in export. Now share
  77. * them with the VMLRenderer.
  78. */
  79. if (Renderer === VMLRenderer) {
  80. ['circlepin', 'flag', 'squarepin'].forEach(function (shape) {
  81. VMLRenderer.prototype.symbols[shape] = symbols[shape];
  82. });
  83. }
  84. /* *
  85. *
  86. * Default Export
  87. *
  88. * */
  89. export default symbols;