controllableMixin.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. 'use strict';
  2. import H from './../../parts/Globals.js';
  3. import U from './../../parts/Utilities.js';
  4. var isObject = U.isObject,
  5. isString = U.isString,
  6. splat = U.splat;
  7. import './../../parts/Tooltip.js';
  8. import ControlPoint from './../ControlPoint.js';
  9. import MockPoint from './../MockPoint.js';
  10. /**
  11. * It provides methods for handling points, control points
  12. * and points transformations.
  13. *
  14. * @private
  15. * @mixin
  16. * @memberOf Annotation
  17. */
  18. var controllableMixin = {
  19. /**
  20. * Init the controllable
  21. *
  22. * @param {Annotation} annotation - an annotation instance
  23. * @param {Object} options - options specific for controllable
  24. * @param {number} index - index of the controllable element
  25. **/
  26. init: function (annotation, options, index) {
  27. this.annotation = annotation;
  28. this.chart = annotation.chart;
  29. this.options = options;
  30. this.points = [];
  31. this.controlPoints = [];
  32. this.index = index;
  33. this.linkPoints();
  34. this.addControlPoints();
  35. },
  36. /**
  37. * Redirect attr usage on the controllable graphic element.
  38. **/
  39. attr: function () {
  40. this.graphic.attr.apply(this.graphic, arguments);
  41. },
  42. /**
  43. * Get the controllable's points options.
  44. *
  45. * @return {Array<PointLikeOptions>} - an array of points' options.
  46. *
  47. */
  48. getPointsOptions: function () {
  49. var options = this.options;
  50. return options.points || (options.point && splat(options.point));
  51. },
  52. /**
  53. * Utility function for mapping item's options
  54. * to element's attribute
  55. *
  56. * @param {Object} options
  57. * @return {Object} mapped options
  58. **/
  59. attrsFromOptions: function (options) {
  60. var map = this.constructor.attrsMap,
  61. attrs = {},
  62. key,
  63. mappedKey,
  64. styledMode = this.chart.styledMode;
  65. for (key in options) {
  66. mappedKey = map[key];
  67. if (
  68. mappedKey &&
  69. (
  70. !styledMode ||
  71. ['fill', 'stroke', 'stroke-width']
  72. .indexOf(mappedKey) === -1
  73. )
  74. ) {
  75. attrs[mappedKey] = options[key];
  76. }
  77. }
  78. return attrs;
  79. },
  80. /**
  81. * @typedef {Object} Annotation.controllableMixin.Position
  82. * @property {number} x
  83. * @property {number} y
  84. */
  85. /**
  86. * An object which denotes an anchor position
  87. *
  88. * @typedef Annotation.controllableMixin.AnchorPosition
  89. * Annotation.controllableMixin.Position
  90. * @property {number} height
  91. * @property {number} width
  92. */
  93. /**
  94. * An object which denots a controllable's anchor positions
  95. * - relative and absolute.
  96. *
  97. * @typedef {Object} Annotation.controllableMixin.Anchor
  98. * @property {Annotation.controllableMixin.AnchorPosition} relativePosition
  99. * @property {Annotation.controllableMixin.AnchorPosition} absolutePosition
  100. */
  101. /**
  102. * Returns object which denotes anchor position - relative and absolute.
  103. *
  104. * @param {Annotation.PointLike} point a point like object
  105. * @return {Annotation.controllableMixin.Anchor} a controllable anchor
  106. */
  107. anchor: function (point) {
  108. var plotBox = point.series.getPlotBox(),
  109. box = point.mock ?
  110. point.toAnchor() :
  111. H.Tooltip.prototype.getAnchor.call({
  112. chart: point.series.chart
  113. }, point),
  114. anchor = {
  115. x: box[0] + (this.options.x || 0),
  116. y: box[1] + (this.options.y || 0),
  117. height: box[2] || 0,
  118. width: box[3] || 0
  119. };
  120. return {
  121. relativePosition: anchor,
  122. absolutePosition: H.merge(anchor, {
  123. x: anchor.x + plotBox.translateX,
  124. y: anchor.y + plotBox.translateY
  125. })
  126. };
  127. },
  128. /**
  129. * Map point's options to a point-like object.
  130. *
  131. * @param {Highcharts.MockPointOptionsObject} pointOptions
  132. * point's options
  133. * @param {Highcharts.PointLike} point
  134. * a point like instance
  135. *
  136. * @return {Highcharts.PointLike|null}
  137. * if the point is found/set returns this point, otherwise null
  138. */
  139. point: function (pointOptions, point) {
  140. if (pointOptions && pointOptions.series) {
  141. return pointOptions;
  142. }
  143. if (!point || point.series === null) {
  144. if (isObject(pointOptions)) {
  145. point = new MockPoint(
  146. this.chart,
  147. this,
  148. pointOptions
  149. );
  150. } else if (isString(pointOptions)) {
  151. point = this.chart.get(pointOptions) || null;
  152. } else if (typeof pointOptions === 'function') {
  153. var pointConfig = pointOptions.call(point, this);
  154. point = pointConfig.series ?
  155. pointConfig :
  156. new MockPoint(
  157. this.chart,
  158. this,
  159. pointOptions
  160. );
  161. }
  162. }
  163. return point;
  164. },
  165. /**
  166. * Find point-like objects based on points options.
  167. *
  168. * @return {Array<Annotation.PointLike>} an array of point-like objects
  169. */
  170. linkPoints: function () {
  171. var pointsOptions = this.getPointsOptions(),
  172. points = this.points,
  173. len = (pointsOptions && pointsOptions.length) || 0,
  174. i,
  175. point;
  176. for (i = 0; i < len; i++) {
  177. point = this.point(pointsOptions[i], points[i]);
  178. if (!point) {
  179. points.length = 0;
  180. return;
  181. }
  182. if (point.mock) {
  183. point.refresh();
  184. }
  185. points[i] = point;
  186. }
  187. return points;
  188. },
  189. /**
  190. * Add control points to a controllable.
  191. */
  192. addControlPoints: function () {
  193. var controlPointsOptions = this.options.controlPoints;
  194. (controlPointsOptions || []).forEach(
  195. function (controlPointOptions, i) {
  196. var options = H.merge(
  197. this.options.controlPointOptions,
  198. controlPointOptions
  199. );
  200. if (!options.index) {
  201. options.index = i;
  202. }
  203. controlPointsOptions[i] = options;
  204. this.controlPoints.push(
  205. new ControlPoint(this.chart, this, options)
  206. );
  207. },
  208. this
  209. );
  210. },
  211. /**
  212. * Check if a controllable should be rendered/redrawn.
  213. *
  214. * @return {boolean} whether a controllable should be drawn.
  215. */
  216. shouldBeDrawn: function () {
  217. return Boolean(this.points.length);
  218. },
  219. /**
  220. * Render a controllable.
  221. **/
  222. render: function () {
  223. this.controlPoints.forEach(function (controlPoint) {
  224. controlPoint.render();
  225. });
  226. },
  227. /**
  228. * Redraw a controllable.
  229. *
  230. * @param {boolean} animation
  231. **/
  232. redraw: function (animation) {
  233. this.controlPoints.forEach(function (controlPoint) {
  234. controlPoint.redraw(animation);
  235. });
  236. },
  237. /**
  238. * Transform a controllable with a specific transformation.
  239. *
  240. * @param {string} transformation a transformation name
  241. * @param {number} cx origin x transformation
  242. * @param {number} cy origin y transformation
  243. * @param {number} p1 param for the transformation
  244. * @param {number} p2 param for the transformation
  245. **/
  246. transform: function (transformation, cx, cy, p1, p2) {
  247. if (this.chart.inverted) {
  248. var temp = cx;
  249. cx = cy;
  250. cy = temp;
  251. }
  252. this.points.forEach(function (point, i) {
  253. this.transformPoint(transformation, cx, cy, p1, p2, i);
  254. }, this);
  255. },
  256. /**
  257. * Transform a point with a specific transformation
  258. * If a transformed point is a real point it is replaced with
  259. * the mock point.
  260. *
  261. * @param {string} transformation a transformation name
  262. * @param {number} cx origin x transformation
  263. * @param {number} cy origin y transformation
  264. * @param {number} p1 param for the transformation
  265. * @param {number} p2 param for the transformation
  266. * @param {number} i index of the point
  267. *
  268. **/
  269. transformPoint: function (transformation, cx, cy, p1, p2, i) {
  270. var point = this.points[i];
  271. if (!point.mock) {
  272. point = this.points[i] = MockPoint.fromPoint(point);
  273. }
  274. point[transformation](cx, cy, p1, p2);
  275. },
  276. /**
  277. * Translate a controllable.
  278. *
  279. * @param {number} dx translation for x coordinate
  280. * @param {number} dy translation for y coordinate
  281. **/
  282. translate: function (dx, dy) {
  283. this.transform('translate', null, null, dx, dy);
  284. },
  285. /**
  286. * Translate a specific point within a controllable.
  287. *
  288. * @param {number} dx translation for x coordinate
  289. * @param {number} dy translation for y coordinate
  290. * @param {number} i index of the point
  291. **/
  292. translatePoint: function (dx, dy, i) {
  293. this.transformPoint('translate', null, null, dx, dy, i);
  294. },
  295. /**
  296. * Translate shape within controllable item.
  297. * Replaces `controllable.translate` method.
  298. *
  299. * @param {number} dx translation for x coordinate
  300. * @param {number} dy translation for y coordinate
  301. */
  302. translateShape: function (dx, dy) {
  303. var chart = this.annotation.chart,
  304. // Annotation.options
  305. shapeOptions = this.annotation.userOptions,
  306. // Chart.options.annotations
  307. annotationIndex = chart.annotations.indexOf(this.annotation),
  308. chartOptions = chart.options.annotations[annotationIndex];
  309. this.translatePoint(dx, dy, 0);
  310. // Options stored in:
  311. // - chart (for exporting)
  312. // - current config (for redraws)
  313. chartOptions[this.collection][this.index].point = this.options.point;
  314. shapeOptions[this.collection][this.index].point = this.options.point;
  315. },
  316. /**
  317. * Rotate a controllable.
  318. *
  319. * @param {number} cx origin x rotation
  320. * @param {number} cy origin y rotation
  321. * @param {number} radians
  322. **/
  323. rotate: function (cx, cy, radians) {
  324. this.transform('rotate', cx, cy, radians);
  325. },
  326. /**
  327. * Scale a controllable.
  328. *
  329. * @param {number} cx origin x rotation
  330. * @param {number} cy origin y rotation
  331. * @param {number} sx scale factor x
  332. * @param {number} sy scale factor y
  333. */
  334. scale: function (cx, cy, sx, sy) {
  335. this.transform('scale', cx, cy, sx, sy);
  336. },
  337. /**
  338. * Set control points' visibility.
  339. *
  340. * @param {boolean} [visible]
  341. */
  342. setControlPointsVisibility: function (visible) {
  343. this.controlPoints.forEach(function (controlPoint) {
  344. controlPoint.setVisibility(visible);
  345. });
  346. },
  347. /**
  348. * Destroy a controllable.
  349. */
  350. destroy: function () {
  351. if (this.graphic) {
  352. this.graphic = this.graphic.destroy();
  353. }
  354. if (this.tracker) {
  355. this.tracker = this.tracker.destroy();
  356. }
  357. this.controlPoints.forEach(function (controlPoint) {
  358. controlPoint.destroy();
  359. });
  360. this.chart = null;
  361. this.points = null;
  362. this.controlPoints = null;
  363. this.options = null;
  364. if (this.annotation) {
  365. this.annotation = null;
  366. }
  367. },
  368. /**
  369. * Update a controllable.
  370. *
  371. * @param {Object} newOptions
  372. */
  373. update: function (newOptions) {
  374. var annotation = this.annotation,
  375. options = H.merge(true, this.options, newOptions),
  376. parentGroup = this.graphic.parentGroup;
  377. this.destroy();
  378. this.constructor(annotation, options);
  379. this.render(parentGroup);
  380. this.redraw();
  381. }
  382. };
  383. export default controllableMixin;