ControllablePath.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* *
  2. *
  3. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  4. *
  5. * */
  6. 'use strict';
  7. import ControllableMixin from '../Mixins/ControllableMixin.js';
  8. import H from '../../../Core/Globals.js';
  9. import MarkerMixin from '../Mixins/MarkerMixin.js';
  10. import U from '../../../Core/Utilities.js';
  11. var extend = U.extend;
  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 = /** @class */ (function () {
  34. /* *
  35. *
  36. * Constructors
  37. *
  38. * */
  39. function ControllablePath(annotation, options, index) {
  40. /* *
  41. *
  42. * Properties
  43. *
  44. * */
  45. this.addControlPoints = ControllableMixin.addControlPoints;
  46. this.anchor = ControllableMixin.anchor;
  47. this.attr = ControllableMixin.attr;
  48. this.attrsFromOptions = ControllableMixin.attrsFromOptions;
  49. this.destroy = ControllableMixin.destroy;
  50. this.getPointsOptions = ControllableMixin.getPointsOptions;
  51. this.init = ControllableMixin.init;
  52. this.linkPoints = ControllableMixin.linkPoints;
  53. this.point = ControllableMixin.point;
  54. this.rotate = ControllableMixin.rotate;
  55. this.scale = ControllableMixin.scale;
  56. this.setControlPointsVisibility = ControllableMixin.setControlPointsVisibility;
  57. this.setMarkers = MarkerMixin.setItemMarkers;
  58. this.transform = ControllableMixin.transform;
  59. this.transformPoint = ControllableMixin.transformPoint;
  60. this.translate = ControllableMixin.translate;
  61. this.translatePoint = ControllableMixin.translatePoint;
  62. this.translateShape = ControllableMixin.translateShape;
  63. this.update = ControllableMixin.update;
  64. /**
  65. * @type 'path'
  66. */
  67. this.type = 'path';
  68. this.init(annotation, options, index);
  69. this.collection = 'shapes';
  70. }
  71. /* *
  72. *
  73. * Functions
  74. *
  75. * */
  76. /**
  77. * Map the controllable path to 'd' path attribute.
  78. *
  79. * @return {Highcharts.SVGPathArray|null}
  80. * A path's d attribute.
  81. */
  82. ControllablePath.prototype.toD = function () {
  83. var dOption = this.options.d;
  84. if (dOption) {
  85. return typeof dOption === 'function' ?
  86. dOption.call(this) :
  87. dOption;
  88. }
  89. var points = this.points, len = points.length, showPath = len, point = points[0], position = showPath && this.anchor(point).absolutePosition, pointIndex = 0, command, d = [];
  90. if (position) {
  91. d.push(['M', position.x, position.y]);
  92. while (++pointIndex < len && showPath) {
  93. point = points[pointIndex];
  94. command = point.command || 'L';
  95. position = this.anchor(point).absolutePosition;
  96. if (command === 'M') {
  97. d.push([command, position.x, position.y]);
  98. }
  99. else if (command === 'L') {
  100. d.push([command, position.x, position.y]);
  101. }
  102. else if (command === 'Z') {
  103. d.push([command]);
  104. }
  105. showPath = point.series.visible;
  106. }
  107. }
  108. return showPath ?
  109. this.chart.renderer.crispLine(d, this.graphic.strokeWidth()) :
  110. null;
  111. };
  112. ControllablePath.prototype.shouldBeDrawn = function () {
  113. return (ControllableMixin.shouldBeDrawn.call(this) || Boolean(this.options.d));
  114. };
  115. ControllablePath.prototype.render = function (parent) {
  116. var options = this.options, attrs = this.attrsFromOptions(options);
  117. this.graphic = this.annotation.chart.renderer
  118. .path([['M', 0, 0]])
  119. .attr(attrs)
  120. .add(parent);
  121. if (options.className) {
  122. this.graphic.addClass(options.className);
  123. }
  124. this.tracker = this.annotation.chart.renderer
  125. .path([['M', 0, 0]])
  126. .addClass('highcharts-tracker-line')
  127. .attr({
  128. zIndex: 2
  129. })
  130. .add(parent);
  131. if (!this.annotation.chart.styledMode) {
  132. this.tracker.attr({
  133. 'stroke-linejoin': 'round',
  134. stroke: TRACKER_FILL,
  135. fill: TRACKER_FILL,
  136. 'stroke-width': this.graphic.strokeWidth() +
  137. options.snap * 2
  138. });
  139. }
  140. ControllableMixin.render.call(this);
  141. extend(this.graphic, {
  142. markerStartSetter: MarkerMixin.markerStartSetter,
  143. markerEndSetter: MarkerMixin.markerEndSetter
  144. });
  145. this.setMarkers(this);
  146. };
  147. ControllablePath.prototype.redraw = function (animation) {
  148. var d = this.toD(), action = animation ? 'animate' : 'attr';
  149. if (d) {
  150. this.graphic[action]({ d: d });
  151. this.tracker[action]({ d: d });
  152. }
  153. else {
  154. this.graphic.attr({ d: 'M 0 ' + -9e9 });
  155. this.tracker.attr({ d: 'M 0 ' + -9e9 });
  156. }
  157. this.graphic.placed = this.tracker.placed = Boolean(d);
  158. ControllableMixin.redraw.call(this, animation);
  159. };
  160. /* *
  161. *
  162. * Static Properties
  163. *
  164. * */
  165. /**
  166. * A map object which allows to map options attributes to element attributes
  167. *
  168. * @name Highcharts.AnnotationControllablePath.attrsMap
  169. * @type {Highcharts.Dictionary<string>}
  170. */
  171. ControllablePath.attrsMap = {
  172. dashStyle: 'dashstyle',
  173. strokeWidth: 'stroke-width',
  174. stroke: 'stroke',
  175. fill: 'fill',
  176. zIndex: 'zIndex'
  177. };
  178. return ControllablePath;
  179. }());
  180. export default ControllablePath;