ControllableRect.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import H from '../../parts/Globals.js';
  2. import '../../parts/Utilities.js';
  3. import controllableMixin from './controllableMixin.js';
  4. import ControllablePath from './ControllablePath.js';
  5. /**
  6. * A controllable rect class.
  7. *
  8. * @class
  9. * @mixes Annotation.controllableMixin
  10. * @memberOf Annotation
  11. *
  12. * @param {Highcharts.Annotation} annotation an annotation instance
  13. * @param {Object} options a rect's options
  14. * @param {number} index of the rectangle
  15. **/
  16. function ControllableRect(annotation, options, index) {
  17. this.init(annotation, options, index);
  18. this.collection = 'shapes';
  19. }
  20. /**
  21. * @typedef {Annotation.ControllablePath.AttrsMap}
  22. * Annotation.ControllableRect.AttrsMap
  23. * @property {string} width=width
  24. * @property {string} height=height
  25. */
  26. /**
  27. * A map object which allows to map options attributes to element attributes
  28. *
  29. * @type {Annotation.ControllableRect.AttrsMap}
  30. */
  31. ControllableRect.attrsMap = H.merge(ControllablePath.attrsMap, {
  32. width: 'width',
  33. height: 'height'
  34. });
  35. H.merge(
  36. true,
  37. ControllableRect.prototype,
  38. controllableMixin, /** @lends Annotation.ControllableRect# */ {
  39. /**
  40. * @type 'rect'
  41. */
  42. type: 'rect',
  43. render: function (parent) {
  44. var attrs = this.attrsFromOptions(this.options);
  45. this.graphic = this.annotation.chart.renderer
  46. .rect(0, -9e9, 0, 0)
  47. .attr(attrs)
  48. .add(parent);
  49. controllableMixin.render.call(this);
  50. },
  51. redraw: function (animation) {
  52. var position = this.anchor(this.points[0]).absolutePosition;
  53. if (position) {
  54. this.graphic[animation ? 'animate' : 'attr']({
  55. x: position.x,
  56. y: position.y,
  57. width: this.options.width,
  58. height: this.options.height
  59. });
  60. } else {
  61. this.attr({
  62. x: 0,
  63. y: -9e9
  64. });
  65. }
  66. this.graphic.placed = Boolean(position);
  67. controllableMixin.redraw.call(this, animation);
  68. },
  69. translate: function (dx, dy) {
  70. var annotationOptions = this.annotation.userOptions,
  71. shapeOptions = annotationOptions[this.collection][this.index];
  72. this.translatePoint(dx, dy, 0);
  73. // Options stored in chart:
  74. shapeOptions.point = this.options.point;
  75. }
  76. }
  77. );
  78. export default ControllableRect;