MockPoint.js 10 KB

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