Pane.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /* *
  2. *
  3. * (c) 2010-2021 Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. 'use strict';
  11. import Chart from '../Core/Chart/Chart.js';
  12. import H from '../Core/Globals.js';
  13. import palette from '../Core/Color/Palette.js';
  14. import Pointer from '../Core/Pointer.js';
  15. import U from '../Core/Utilities.js';
  16. var addEvent = U.addEvent, extend = U.extend, merge = U.merge, pick = U.pick, splat = U.splat;
  17. /**
  18. * @typedef {"arc"|"circle"|"solid"} Highcharts.PaneBackgroundShapeValue
  19. */
  20. import centeredSeriesMixin from '../Mixins/CenteredSeries.js';
  21. /* eslint-disable no-invalid-this, valid-jsdoc */
  22. Chart.prototype.collectionsWithUpdate.push('pane');
  23. /**
  24. * The Pane object allows options that are common to a set of X and Y axes.
  25. *
  26. * In the future, this can be extended to basic Highcharts and Highcharts Stock.
  27. *
  28. * @private
  29. * @class
  30. * @name Highcharts.Pane
  31. * @param {Highcharts.PaneOptions} options
  32. * @param {Highcharts.Chart} chart
  33. */
  34. var Pane = /** @class */ (function () {
  35. function Pane(options, chart) {
  36. this.background = void 0;
  37. this.center = void 0;
  38. this.chart = void 0;
  39. this.options = void 0;
  40. this.coll = 'pane'; // Member of chart.pane
  41. /**
  42. * The pane serves as a container for axes and backgrounds for circular
  43. * gauges and polar charts.
  44. *
  45. * @since 2.3.0
  46. * @product highcharts
  47. * @requires highcharts-more
  48. * @optionparent pane
  49. */
  50. this.defaultOptions = {
  51. /**
  52. * The end angle of the polar X axis or gauge value axis, given in
  53. * degrees where 0 is north. Defaults to [startAngle](#pane.startAngle)
  54. * + 360.
  55. *
  56. * @sample {highcharts} highcharts/demo/gauge-vu-meter/
  57. * VU-meter with custom start and end angle
  58. *
  59. * @type {number}
  60. * @since 2.3.0
  61. * @product highcharts
  62. * @apioption pane.endAngle
  63. */
  64. /**
  65. * The center of a polar chart or angular gauge, given as an array
  66. * of [x, y] positions. Positions can be given as integers that
  67. * transform to pixels, or as percentages of the plot area size.
  68. *
  69. * @sample {highcharts} highcharts/demo/gauge-vu-meter/
  70. * Two gauges with different center
  71. *
  72. * @type {Array<string|number>}
  73. * @default ["50%", "50%"]
  74. * @since 2.3.0
  75. * @product highcharts
  76. */
  77. center: ['50%', '50%'],
  78. /**
  79. * The size of the pane, either as a number defining pixels, or a
  80. * percentage defining a percentage of the available plot area (the
  81. * smallest of the plot height or plot width).
  82. *
  83. * @sample {highcharts} highcharts/demo/gauge-vu-meter/
  84. * Smaller size
  85. *
  86. * @type {number|string}
  87. * @product highcharts
  88. */
  89. size: '85%',
  90. /**
  91. * The inner size of the pane, either as a number defining pixels, or a
  92. * percentage defining a percentage of the pane's size.
  93. *
  94. * @sample {highcharts} highcharts/series-polar/column-inverted-inner
  95. * The inner size set to 20%
  96. *
  97. * @type {number|string}
  98. * @product highcharts
  99. */
  100. innerSize: '0%',
  101. /**
  102. * The start angle of the polar X axis or gauge axis, given in degrees
  103. * where 0 is north. Defaults to 0.
  104. *
  105. * @sample {highcharts} highcharts/demo/gauge-vu-meter/
  106. * VU-meter with custom start and end angle
  107. *
  108. * @since 2.3.0
  109. * @product highcharts
  110. */
  111. startAngle: 0
  112. };
  113. /**
  114. * An array of background items for the pane.
  115. *
  116. * @sample {highcharts} highcharts/demo/gauge-speedometer/
  117. * Speedometer gauge with multiple backgrounds
  118. *
  119. * @type {Array<*>}
  120. * @optionparent pane.background
  121. */
  122. this.defaultBackgroundOptions = {
  123. /**
  124. * The class name for this background.
  125. *
  126. * @sample {highcharts} highcharts/css/pane/
  127. * Panes styled by CSS
  128. * @sample {highstock} highcharts/css/pane/
  129. * Panes styled by CSS
  130. * @sample {highmaps} highcharts/css/pane/
  131. * Panes styled by CSS
  132. *
  133. * @type {string}
  134. * @default highcharts-pane
  135. * @since 5.0.0
  136. * @apioption pane.background.className
  137. */
  138. /**
  139. * The shape of the pane background. When `solid`, the background
  140. * is circular. When `arc`, the background extends only from the min
  141. * to the max of the value axis.
  142. *
  143. * @type {Highcharts.PaneBackgroundShapeValue}
  144. * @since 2.3.0
  145. * @product highcharts
  146. */
  147. shape: 'circle',
  148. /**
  149. * The pixel border width of the pane background.
  150. *
  151. * @since 2.3.0
  152. * @product highcharts
  153. */
  154. borderWidth: 1,
  155. /**
  156. * The pane background border color.
  157. *
  158. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  159. * @since 2.3.0
  160. * @product highcharts
  161. */
  162. borderColor: palette.neutralColor20,
  163. /**
  164. * The background color or gradient for the pane.
  165. *
  166. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  167. * @default { linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 }, stops: [[0, #ffffff], [1, #e6e6e6]] }
  168. * @since 2.3.0
  169. * @product highcharts
  170. */
  171. backgroundColor: {
  172. /** @ignore-option */
  173. linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },
  174. /** @ignore-option */
  175. stops: [
  176. [0, palette.backgroundColor],
  177. [1, palette.neutralColor10]
  178. ]
  179. },
  180. /** @ignore-option */
  181. from: -Number.MAX_VALUE,
  182. /**
  183. * The inner radius of the pane background. Can be either numeric
  184. * (pixels) or a percentage string.
  185. *
  186. * @type {number|string}
  187. * @since 2.3.0
  188. * @product highcharts
  189. */
  190. innerRadius: 0,
  191. /** @ignore-option */
  192. to: Number.MAX_VALUE,
  193. /**
  194. * The outer radius of the circular pane background. Can be either
  195. * numeric (pixels) or a percentage string.
  196. *
  197. * @type {number|string}
  198. * @since 2.3.0
  199. * @product highcharts
  200. */
  201. outerRadius: '105%'
  202. };
  203. this.init(options, chart);
  204. }
  205. /**
  206. * Initialize the Pane object
  207. *
  208. * @private
  209. * @function Highcharts.Pane#init
  210. *
  211. * @param {Highcharts.PaneOptions} options
  212. *
  213. * @param {Highcharts.Chart} chart
  214. */
  215. Pane.prototype.init = function (options, chart) {
  216. this.chart = chart;
  217. this.background = [];
  218. chart.pane.push(this);
  219. this.setOptions(options);
  220. };
  221. /**
  222. * @private
  223. * @function Highcharts.Pane#setOptions
  224. *
  225. * @param {Highcharts.PaneOptions} options
  226. */
  227. Pane.prototype.setOptions = function (options) {
  228. // Set options. Angular charts have a default background (#3318)
  229. this.options = options = merge(this.defaultOptions, this.chart.angular ? { background: {} } : void 0, options);
  230. };
  231. /**
  232. * Render the pane with its backgrounds.
  233. *
  234. * @private
  235. * @function Highcharts.Pane#render
  236. */
  237. Pane.prototype.render = function () {
  238. var options = this.options, backgroundOption = this.options.background, renderer = this.chart.renderer, len, i;
  239. if (!this.group) {
  240. this.group = renderer.g('pane-group')
  241. .attr({ zIndex: options.zIndex || 0 })
  242. .add();
  243. }
  244. this.updateCenter();
  245. // Render the backgrounds
  246. if (backgroundOption) {
  247. backgroundOption = splat(backgroundOption);
  248. len = Math.max(backgroundOption.length, this.background.length || 0);
  249. for (i = 0; i < len; i++) {
  250. // #6641 - if axis exists, chart is circular and apply
  251. // background
  252. if (backgroundOption[i] && this.axis) {
  253. this.renderBackground(merge(this.defaultBackgroundOptions, backgroundOption[i]), i);
  254. }
  255. else if (this.background[i]) {
  256. this.background[i] = this.background[i].destroy();
  257. this.background.splice(i, 1);
  258. }
  259. }
  260. }
  261. };
  262. /**
  263. * Render an individual pane background.
  264. *
  265. * @private
  266. * @function Highcharts.Pane#renderBackground
  267. *
  268. * @param {Highcharts.PaneBackgroundOptions} backgroundOptions
  269. * Background options
  270. *
  271. * @param {number} i
  272. * The index of the background in this.backgrounds
  273. */
  274. Pane.prototype.renderBackground = function (backgroundOptions, i) {
  275. var method = 'animate', attribs = {
  276. 'class': 'highcharts-pane ' + (backgroundOptions.className || '')
  277. };
  278. if (!this.chart.styledMode) {
  279. extend(attribs, {
  280. 'fill': backgroundOptions.backgroundColor,
  281. 'stroke': backgroundOptions.borderColor,
  282. 'stroke-width': backgroundOptions.borderWidth
  283. });
  284. }
  285. if (!this.background[i]) {
  286. this.background[i] = this.chart.renderer
  287. .path()
  288. .add(this.group);
  289. method = 'attr';
  290. }
  291. this.background[i][method]({
  292. 'd': this.axis.getPlotBandPath(backgroundOptions.from, backgroundOptions.to, backgroundOptions)
  293. }).attr(attribs);
  294. };
  295. /**
  296. * Gets the center for the pane and its axis.
  297. *
  298. * @private
  299. * @function Highcharts.Pane#updateCenter
  300. * @param {Highcharts.Axis} [axis]
  301. * @return {void}
  302. */
  303. Pane.prototype.updateCenter = function (axis) {
  304. this.center = (axis ||
  305. this.axis ||
  306. {}).center = centeredSeriesMixin.getCenter.call(this);
  307. };
  308. /**
  309. * Destroy the pane item
  310. *
  311. * @ignore
  312. * @private
  313. * @function Highcharts.Pane#destroy
  314. * /
  315. destroy: function () {
  316. erase(this.chart.pane, this);
  317. this.background.forEach(function (background) {
  318. background.destroy();
  319. });
  320. this.background.length = 0;
  321. this.group = this.group.destroy();
  322. },
  323. */
  324. /**
  325. * Update the pane item with new options
  326. *
  327. * @private
  328. * @function Highcharts.Pane#update
  329. * @param {Highcharts.PaneOptions} options
  330. * New pane options
  331. * @param {boolean} [redraw]
  332. * @return {void}
  333. */
  334. Pane.prototype.update = function (options, redraw) {
  335. merge(true, this.options, options);
  336. merge(true, this.chart.options.pane, options); // #9917
  337. this.setOptions(this.options);
  338. this.render();
  339. this.chart.axes.forEach(function (axis) {
  340. if (axis.pane === this) {
  341. axis.pane = null;
  342. axis.update({}, redraw);
  343. }
  344. }, this);
  345. };
  346. return Pane;
  347. }());
  348. /**
  349. * Check whether element is inside or outside pane.
  350. * @private
  351. * @param {number} x Element's x coordinate
  352. * @param {number} y Element's y coordinate
  353. * @param {Array<number>} center Pane's center (x, y) and diameter
  354. * @return {boolean}
  355. */
  356. function isInsidePane(x, y, center) {
  357. return Math.sqrt(Math.pow(x - center[0], 2) + Math.pow(y - center[1], 2)) <= center[2] / 2;
  358. }
  359. Chart.prototype.getHoverPane = function (eventArgs) {
  360. var chart = this;
  361. var hoverPane;
  362. if (eventArgs) {
  363. chart.pane.forEach(function (pane) {
  364. var plotX = eventArgs.chartX - chart.plotLeft, plotY = eventArgs.chartY - chart.plotTop, x = chart.inverted ? plotY : plotX, y = chart.inverted ? plotX : plotY;
  365. if (isInsidePane(x, y, pane.center)) {
  366. hoverPane = pane;
  367. }
  368. });
  369. }
  370. return hoverPane;
  371. };
  372. addEvent(Chart, 'afterIsInsidePlot', function (e) {
  373. var chart = this;
  374. if (chart.polar) {
  375. e.isInsidePlot = chart.pane.some(function (pane) { return isInsidePane(e.x, e.y, pane.center); });
  376. }
  377. });
  378. addEvent(Pointer, 'beforeGetHoverData', function (eventArgs) {
  379. var chart = this.chart;
  380. if (chart.polar) {
  381. // Find pane we are currently hovering over.
  382. chart.hoverPane = chart.getHoverPane(eventArgs);
  383. // Edit filter method to handle polar
  384. eventArgs.filter = function (s) {
  385. return (s.visible &&
  386. !(!eventArgs.shared && s.directTouch) && // #3821
  387. pick(s.options.enableMouseTracking, true) &&
  388. (!chart.hoverPane || s.xAxis.pane === chart.hoverPane));
  389. };
  390. }
  391. else {
  392. chart.hoverPane = void 0;
  393. }
  394. });
  395. addEvent(Pointer, 'afterGetHoverData', function (eventArgs) {
  396. var chart = this.chart;
  397. if (eventArgs.hoverPoint &&
  398. eventArgs.hoverPoint.plotX &&
  399. eventArgs.hoverPoint.plotY &&
  400. chart.hoverPane &&
  401. !isInsidePane(eventArgs.hoverPoint.plotX, eventArgs.hoverPoint.plotY, chart.hoverPane.center)) {
  402. eventArgs.hoverPoint = void 0;
  403. }
  404. });
  405. H.Pane = Pane;
  406. export default H.Pane;