ControllableRect.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. translate: controllableMixin.translateShape,
  44. render: function (parent) {
  45. var attrs = this.attrsFromOptions(this.options);
  46. this.graphic = this.annotation.chart.renderer
  47. .rect(0, -9e9, 0, 0)
  48. .attr(attrs)
  49. .add(parent);
  50. controllableMixin.render.call(this);
  51. },
  52. redraw: function (animation) {
  53. var position = this.anchor(this.points[0]).absolutePosition;
  54. if (position) {
  55. this.graphic[animation ? 'animate' : 'attr']({
  56. x: position.x,
  57. y: position.y,
  58. width: this.options.width,
  59. height: this.options.height
  60. });
  61. } else {
  62. this.attr({
  63. x: 0,
  64. y: -9e9
  65. });
  66. }
  67. this.graphic.placed = Boolean(position);
  68. controllableMixin.redraw.call(this, animation);
  69. }
  70. }
  71. );
  72. export default ControllableRect;