Pane.js 14 KB

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