MockPoint.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /* *
  2. *
  3. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  4. *
  5. * */
  6. import H from '../../Core/Globals.js';
  7. /**
  8. * @private
  9. * @interface Highcharts.AnnotationMockLabelOptionsObject
  10. */ /**
  11. * Point instance of the point.
  12. * @name Highcharts.AnnotationMockLabelOptionsObject#point
  13. * @type {Highcharts.AnnotationMockPoint}
  14. */ /**
  15. * X value translated to x axis scale.
  16. * @name Highcharts.AnnotationMockLabelOptionsObject#x
  17. * @type {number|null}
  18. */ /**
  19. * Y value translated to y axis scale.
  20. * @name Highcharts.AnnotationMockLabelOptionsObject#y
  21. * @type {number|null}
  22. */
  23. /**
  24. * A mock series instance imitating a real series from a real point.
  25. * @private
  26. * @interface Highcharts.AnnotationMockSeries
  27. */ /**
  28. * Whether a series is visible.
  29. * @name Highcharts.AnnotationMockSeries#visible
  30. * @type {boolean}
  31. */ /**
  32. * A chart instance.
  33. * @name Highcharts.AnnotationMockSeries#chart
  34. * @type {Highcharts.Chart}
  35. */ /**
  36. * @name Highcharts.AnnotationMockSeries#getPlotBox
  37. * @type {Function}
  38. */
  39. /**
  40. * Indicates if this is a mock point for an annotation.
  41. * @name Highcharts.Point#mock
  42. * @type {boolean|undefined}
  43. */
  44. import U from '../../Core/Utilities.js';
  45. var defined = U.defined, extend = U.extend, fireEvent = U.fireEvent;
  46. import '../../Core/Axis/Axis.js';
  47. import '../../Core/Series/Series.js';
  48. /* eslint-disable no-invalid-this, valid-jsdoc */
  49. /**
  50. * A trimmed point object which imitates {@link Highchart.Point} class. It is
  51. * created when there is a need of pointing to some chart's position using axis
  52. * values or pixel values
  53. *
  54. * @requires modules/annotations
  55. *
  56. * @private
  57. * @class
  58. * @name Highcharts.AnnotationMockPoint
  59. *
  60. * @hideconstructor
  61. *
  62. * @param {Highcharts.Chart} chart
  63. * The chart instance.
  64. *
  65. * @param {Highcharts.AnnotationControllable|null} target
  66. * The related controllable.
  67. *
  68. * @param {Highcharts.AnnotationMockPointOptionsObject|Function} options
  69. * The options object.
  70. */
  71. var MockPoint = /** @class */ (function () {
  72. function MockPoint(chart, target, options) {
  73. this.isInside = void 0;
  74. this.plotX = void 0;
  75. this.plotY = void 0;
  76. this.x = void 0;
  77. this.y = void 0;
  78. /* *
  79. *
  80. * Functions
  81. *
  82. * */
  83. /**
  84. * A flag indicating that a point is not the real one.
  85. *
  86. * @type {boolean}
  87. * @default true
  88. */
  89. this.mock = true;
  90. /**
  91. * A mock series instance imitating a real series from a real point.
  92. *
  93. * @name Annotation.AnnotationMockPoint#series
  94. * @type {Highcharts.AnnotationMockSeries}
  95. */
  96. this.series = {
  97. visible: true,
  98. chart: chart,
  99. getPlotBox: H.Series.prototype.getPlotBox
  100. };
  101. /**
  102. * @name Annotation.AnnotationMockPoint#target
  103. * @type {Highcharts.AnnotationControllable|null}
  104. */
  105. this.target = target || null;
  106. /**
  107. * Options for the mock point.
  108. *
  109. * @name Annotation.AnnotationMockPoint#options
  110. * @type {Highcharts.AnnotationsMockPointOptionsObject}
  111. */
  112. this.options = options;
  113. /**
  114. * If an xAxis is set it represents the point's value in terms of the
  115. * xAxis.
  116. *
  117. * @name Annotation.AnnotationMockPoint#x
  118. * @type {number|undefined}
  119. */
  120. /**
  121. * If an yAxis is set it represents the point's value in terms of the
  122. * yAxis.
  123. *
  124. * @name Annotation.AnnotationMockPoint#y
  125. * @type {number|undefined}
  126. */
  127. /**
  128. * It represents the point's pixel x coordinate relative to its plot
  129. * box.
  130. *
  131. * @name Annotation.AnnotationMockPoint#plotX
  132. * @type {number|undefined}
  133. */
  134. /**
  135. * It represents the point's pixel y position relative to its plot box.
  136. *
  137. * @name Annotation.AnnotationMockPoint#plotY
  138. * @type {number|undefined}
  139. */
  140. /**
  141. * Whether the point is inside the plot box.
  142. *
  143. * @name Annotation.AnnotationMockPoint#isInside
  144. * @type {boolean|undefined}
  145. */
  146. this.applyOptions(this.getOptions());
  147. }
  148. /**
  149. * Create a mock point from a real Highcharts point.
  150. *
  151. * @private
  152. * @static
  153. *
  154. * @param {Highcharts.Point} point
  155. *
  156. * @return {Highcharts.AnnotationMockPoint}
  157. * A mock point instance.
  158. */
  159. MockPoint.fromPoint = function (point) {
  160. return new MockPoint(point.series.chart, null, {
  161. x: point.x,
  162. y: point.y,
  163. xAxis: point.series.xAxis,
  164. yAxis: point.series.yAxis
  165. });
  166. };
  167. /**
  168. * Get the pixel position from the point like object.
  169. *
  170. * @private
  171. * @static
  172. *
  173. * @param {Highcharts.AnnotationPointType} point
  174. *
  175. * @param {boolean} [paneCoordinates]
  176. * whether the pixel position should be relative
  177. *
  178. * @return {Highcharts.PositionObject} pixel position
  179. */
  180. MockPoint.pointToPixels = function (point, paneCoordinates) {
  181. var series = point.series, chart = series.chart, x = point.plotX, y = point.plotY, plotBox;
  182. if (chart.inverted) {
  183. if (point.mock) {
  184. x = point.plotY;
  185. y = point.plotX;
  186. }
  187. else {
  188. x = chart.plotWidth - point.plotY;
  189. y = chart.plotHeight - point.plotX;
  190. }
  191. }
  192. if (series && !paneCoordinates) {
  193. plotBox = series.getPlotBox();
  194. x += plotBox.translateX;
  195. y += plotBox.translateY;
  196. }
  197. return {
  198. x: x,
  199. y: y
  200. };
  201. };
  202. /**
  203. * Get fresh mock point options from the point like object.
  204. *
  205. * @private
  206. * @static
  207. *
  208. * @param {Highcharts.AnnotationPointType} point
  209. *
  210. * @return {Highcharts.AnnotationMockPointOptionsObject}
  211. * A mock point's options.
  212. */
  213. MockPoint.pointToOptions = function (point) {
  214. return {
  215. x: point.x,
  216. y: point.y,
  217. xAxis: point.series.xAxis,
  218. yAxis: point.series.yAxis
  219. };
  220. };
  221. /**
  222. * Check if the point has dynamic options.
  223. * @private
  224. * @return {boolean}
  225. * A positive flag if the point has dynamic options.
  226. */
  227. MockPoint.prototype.hasDynamicOptions = function () {
  228. return typeof this.options === 'function';
  229. };
  230. /**
  231. * Get the point's options.
  232. * @private
  233. * @return {Highcharts.AnnotationMockPointOptionsObject}
  234. * The mock point's options.
  235. */
  236. MockPoint.prototype.getOptions = function () {
  237. return this.hasDynamicOptions() ?
  238. this.options(this.target) :
  239. this.options;
  240. };
  241. /**
  242. * Apply options for the point.
  243. * @private
  244. * @param {Highcharts.AnnotationMockPointOptionsObject} options
  245. */
  246. MockPoint.prototype.applyOptions = function (options) {
  247. this.command = options.command;
  248. this.setAxis(options, 'x');
  249. this.setAxis(options, 'y');
  250. this.refresh();
  251. };
  252. /**
  253. * Set x or y axis.
  254. * @private
  255. * @param {Highcharts.AnnotationMockPointOptionsObject} options
  256. * @param {string} xOrY
  257. * 'x' or 'y' string literal
  258. */
  259. MockPoint.prototype.setAxis = function (options, xOrY) {
  260. var axisName = (xOrY + 'Axis'), axisOptions = options[axisName], chart = this.series.chart;
  261. this.series[axisName] =
  262. axisOptions instanceof H.Axis ?
  263. axisOptions :
  264. defined(axisOptions) ?
  265. (chart[axisName][axisOptions] ||
  266. chart.get(axisOptions)) :
  267. null;
  268. };
  269. /**
  270. * Transform the mock point to an anchor (relative position on the chart).
  271. * @private
  272. * @return {Array<number>}
  273. * A quadruple of numbers which denotes x, y, width and height of the box
  274. **/
  275. MockPoint.prototype.toAnchor = function () {
  276. var anchor = [this.plotX, this.plotY, 0, 0];
  277. if (this.series.chart.inverted) {
  278. anchor[0] = this.plotY;
  279. anchor[1] = this.plotX;
  280. }
  281. return anchor;
  282. };
  283. /**
  284. * Returns a label config object - the same as
  285. * Highcharts.Point.prototype.getLabelConfig
  286. * @private
  287. * @return {Highcharts.AnnotationMockLabelOptionsObject} the point's label config
  288. */
  289. MockPoint.prototype.getLabelConfig = function () {
  290. return {
  291. x: this.x,
  292. y: this.y,
  293. point: this
  294. };
  295. };
  296. /**
  297. * Check if the point is inside its pane.
  298. * @private
  299. * @return {boolean} A flag indicating whether the point is inside the pane.
  300. */
  301. MockPoint.prototype.isInsidePlot = function () {
  302. var plotX = this.plotX, plotY = this.plotY, xAxis = this.series.xAxis, yAxis = this.series.yAxis, e = {
  303. x: plotX,
  304. y: plotY,
  305. isInsidePlot: true
  306. };
  307. if (xAxis) {
  308. e.isInsidePlot = defined(plotX) && plotX >= 0 && plotX <= xAxis.len;
  309. }
  310. if (yAxis) {
  311. e.isInsidePlot =
  312. e.isInsidePlot &&
  313. defined(plotY) &&
  314. plotY >= 0 && plotY <= yAxis.len;
  315. }
  316. fireEvent(this.series.chart, 'afterIsInsidePlot', e);
  317. return e.isInsidePlot;
  318. };
  319. /**
  320. * Refresh point values and coordinates based on its options.
  321. * @private
  322. */
  323. MockPoint.prototype.refresh = function () {
  324. var series = this.series, xAxis = series.xAxis, yAxis = series.yAxis, options = this.getOptions();
  325. if (xAxis) {
  326. this.x = options.x;
  327. this.plotX = xAxis.toPixels(options.x, true);
  328. }
  329. else {
  330. this.x = null;
  331. this.plotX = options.x;
  332. }
  333. if (yAxis) {
  334. this.y = options.y;
  335. this.plotY = yAxis.toPixels(options.y, true);
  336. }
  337. else {
  338. this.y = null;
  339. this.plotY = options.y;
  340. }
  341. this.isInside = this.isInsidePlot();
  342. };
  343. /**
  344. * Translate the point.
  345. *
  346. * @private
  347. *
  348. * @param {number|undefined} cx
  349. * Origin x transformation.
  350. *
  351. * @param {number|undefined} cy
  352. * Origin y transformation.
  353. *
  354. * @param {number} dx
  355. * Translation for x coordinate.
  356. *
  357. * @param {number} dy
  358. * Translation for y coordinate.
  359. **/
  360. MockPoint.prototype.translate = function (_cx, _cy, dx, dy) {
  361. if (!this.hasDynamicOptions()) {
  362. this.plotX += dx;
  363. this.plotY += dy;
  364. this.refreshOptions();
  365. }
  366. };
  367. /**
  368. * Scale the point.
  369. *
  370. * @private
  371. *
  372. * @param {number} cx
  373. * Origin x transformation.
  374. *
  375. * @param {number} cy
  376. * Origin y transformation.
  377. *
  378. * @param {number} sx
  379. * Scale factor x.
  380. *
  381. * @param {number} sy
  382. * Scale factor y.
  383. */
  384. MockPoint.prototype.scale = function (cx, cy, sx, sy) {
  385. if (!this.hasDynamicOptions()) {
  386. var x = this.plotX * sx, y = this.plotY * sy, tx = (1 - sx) * cx, ty = (1 - sy) * cy;
  387. this.plotX = tx + x;
  388. this.plotY = ty + y;
  389. this.refreshOptions();
  390. }
  391. };
  392. /**
  393. * Rotate the point.
  394. * @private
  395. * @param {number} cx origin x rotation
  396. * @param {number} cy origin y rotation
  397. * @param {number} radians
  398. */
  399. MockPoint.prototype.rotate = function (cx, cy, radians) {
  400. if (!this.hasDynamicOptions()) {
  401. var cos = Math.cos(radians), sin = Math.sin(radians), x = this.plotX, y = this.plotY, tx, ty;
  402. x -= cx;
  403. y -= cy;
  404. tx = x * cos - y * sin;
  405. ty = x * sin + y * cos;
  406. this.plotX = tx + cx;
  407. this.plotY = ty + cy;
  408. this.refreshOptions();
  409. }
  410. };
  411. /**
  412. * Refresh point options based on its plot coordinates.
  413. * @private
  414. */
  415. MockPoint.prototype.refreshOptions = function () {
  416. var series = this.series, xAxis = series.xAxis, yAxis = series.yAxis;
  417. this.x = this.options.x = xAxis ?
  418. this.options.x = xAxis.toValue(this.plotX, true) :
  419. this.plotX;
  420. this.y = this.options.y = yAxis ?
  421. yAxis.toValue(this.plotY, true) :
  422. this.plotY;
  423. };
  424. return MockPoint;
  425. }());
  426. export default MockPoint;