ControllablePath.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* *
  2. *
  3. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  4. *
  5. * */
  6. 'use strict';
  7. import controllableMixin from './controllableMixin.js';
  8. import H from './../../parts/Globals.js';
  9. import markerMixin from './markerMixin.js';
  10. import U from './../../parts/Utilities.js';
  11. var extend = U.extend, merge = U.merge;
  12. // See TRACKER_FILL in highcharts.src.js
  13. var TRACKER_FILL = 'rgba(192,192,192,' + (H.svg ? 0.0001 : 0.002) + ')';
  14. /* eslint-disable no-invalid-this, valid-jsdoc */
  15. /**
  16. * A controllable path class.
  17. *
  18. * @requires modules/annotations
  19. *
  20. * @private
  21. * @class
  22. * @name Highcharts.AnnotationControllablePath
  23. *
  24. * @param {Highcharts.Annotation}
  25. * Related annotation.
  26. *
  27. * @param {Highcharts.AnnotationsShapeOptions} options
  28. * A path's options object.
  29. *
  30. * @param {number} index
  31. * Index of the path.
  32. **/
  33. var ControllablePath = function (annotation, options, index) {
  34. this.init(annotation, options, index);
  35. this.collection = 'shapes';
  36. };
  37. /**
  38. * A map object which allows to map options attributes to element attributes
  39. *
  40. * @name Highcharts.AnnotationControllablePath.attrsMap
  41. * @type {Highcharts.Dictionary<string>}
  42. */
  43. ControllablePath.attrsMap = {
  44. dashStyle: 'dashstyle',
  45. strokeWidth: 'stroke-width',
  46. stroke: 'stroke',
  47. fill: 'fill',
  48. zIndex: 'zIndex'
  49. };
  50. merge(true, ControllablePath.prototype, controllableMixin, /** @lends Highcharts.AnnotationControllablePath# */ {
  51. /**
  52. * @type 'path'
  53. */
  54. type: 'path',
  55. setMarkers: markerMixin.setItemMarkers,
  56. /**
  57. * Map the controllable path to 'd' path attribute.
  58. *
  59. * @return {Highcharts.SVGPathArray|null}
  60. * A path's d attribute.
  61. */
  62. toD: function () {
  63. var dOption = this.options.d;
  64. if (dOption) {
  65. return typeof dOption === 'function' ?
  66. dOption.call(this) :
  67. dOption;
  68. }
  69. var points = this.points, len = points.length, showPath = len, point = points[0], position = showPath && this.anchor(point).absolutePosition, pointIndex = 0, command, d = [];
  70. if (position) {
  71. d.push(['M', position.x, position.y]);
  72. while (++pointIndex < len && showPath) {
  73. point = points[pointIndex];
  74. command = point.command || 'L';
  75. position = this.anchor(point).absolutePosition;
  76. if (command === 'M') {
  77. d.push([command, position.x, position.y]);
  78. }
  79. else if (command === 'L') {
  80. d.push([command, position.x, position.y]);
  81. }
  82. else if (command === 'Z') {
  83. d.push([command]);
  84. }
  85. showPath = point.series.visible;
  86. }
  87. }
  88. return showPath ?
  89. this.chart.renderer.crispLine(d, this.graphic.strokeWidth()) :
  90. null;
  91. },
  92. shouldBeDrawn: function () {
  93. return (controllableMixin.shouldBeDrawn.call(this) || Boolean(this.options.d));
  94. },
  95. render: function (parent) {
  96. var options = this.options, attrs = this.attrsFromOptions(options);
  97. this.graphic = this.annotation.chart.renderer
  98. .path([['M', 0, 0]])
  99. .attr(attrs)
  100. .add(parent);
  101. if (options.className) {
  102. this.graphic.addClass(options.className);
  103. }
  104. this.tracker = this.annotation.chart.renderer
  105. .path([['M', 0, 0]])
  106. .addClass('highcharts-tracker-line')
  107. .attr({
  108. zIndex: 2
  109. })
  110. .add(parent);
  111. if (!this.annotation.chart.styledMode) {
  112. this.tracker.attr({
  113. 'stroke-linejoin': 'round',
  114. stroke: TRACKER_FILL,
  115. fill: TRACKER_FILL,
  116. 'stroke-width': this.graphic.strokeWidth() +
  117. options.snap * 2
  118. });
  119. }
  120. controllableMixin.render.call(this);
  121. extend(this.graphic, {
  122. markerStartSetter: markerMixin.markerStartSetter,
  123. markerEndSetter: markerMixin.markerEndSetter
  124. });
  125. this.setMarkers(this);
  126. },
  127. redraw: function (animation) {
  128. var d = this.toD(), action = animation ? 'animate' : 'attr';
  129. if (d) {
  130. this.graphic[action]({ d: d });
  131. this.tracker[action]({ d: d });
  132. }
  133. else {
  134. this.graphic.attr({ d: 'M 0 ' + -9e9 });
  135. this.tracker.attr({ d: 'M 0 ' + -9e9 });
  136. }
  137. this.graphic.placed = this.tracker.placed = Boolean(d);
  138. controllableMixin.redraw.call(this, animation);
  139. }
  140. });
  141. export default ControllablePath;