MockPoint.js 11 KB

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