Pane.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /* *
  2. * (c) 2010-2019 Torstein Honsi
  3. *
  4. * License: www.highcharts.com/license
  5. */
  6. 'use strict';
  7. import H from '../parts/Globals.js';
  8. import '../mixins/centered-series.js';
  9. import '../parts/Utilities.js';
  10. var CenteredSeriesMixin = H.CenteredSeriesMixin,
  11. extend = H.extend,
  12. merge = H.merge,
  13. splat = H.splat;
  14. /**
  15. * The Pane object allows options that are common to a set of X and Y axes.
  16. *
  17. * In the future, this can be extended to basic Highcharts and Highstock.
  18. *
  19. * @private
  20. * @class
  21. * @name Highcharts.Pane
  22. *
  23. * @param {Highcharts.PaneOptions} options
  24. *
  25. * @param {Highcharts.Chart} chart
  26. */
  27. function Pane(options, chart) {
  28. this.init(options, chart);
  29. }
  30. // Extend the Pane prototype
  31. extend(Pane.prototype, {
  32. coll: 'pane', // Member of chart.pane
  33. /**
  34. * Initiate the Pane object
  35. *
  36. * @private
  37. * @function Highcharts.Pane#init
  38. *
  39. * @param {Highcharts.PaneOptions} options
  40. *
  41. * @param {Highcharts.Chart} chart
  42. */
  43. init: function (options, chart) {
  44. this.chart = chart;
  45. this.background = [];
  46. chart.pane.push(this);
  47. this.setOptions(options);
  48. },
  49. /**
  50. * @private
  51. * @function Highcharts.Pane#setOptions
  52. *
  53. * @param {Highcharts.PaneOptions} options
  54. */
  55. setOptions: function (options) {
  56. // Set options. Angular charts have a default background (#3318)
  57. this.options = options = merge(
  58. this.defaultOptions,
  59. this.chart.angular ? { background: {} } : undefined,
  60. options
  61. );
  62. },
  63. /**
  64. * Render the pane with its backgrounds.
  65. *
  66. * @private
  67. * @function Highcharts.Pane#render
  68. */
  69. render: function () {
  70. var options = this.options,
  71. backgroundOption = this.options.background,
  72. renderer = this.chart.renderer,
  73. len,
  74. i;
  75. if (!this.group) {
  76. this.group = renderer.g('pane-group')
  77. .attr({ zIndex: options.zIndex || 0 })
  78. .add();
  79. }
  80. this.updateCenter();
  81. // Render the backgrounds
  82. if (backgroundOption) {
  83. backgroundOption = splat(backgroundOption);
  84. len = Math.max(
  85. backgroundOption.length,
  86. this.background.length || 0
  87. );
  88. for (i = 0; i < len; i++) {
  89. // #6641 - if axis exists, chart is circular and apply
  90. // background
  91. if (backgroundOption[i] && this.axis) {
  92. this.renderBackground(
  93. merge(
  94. this.defaultBackgroundOptions,
  95. backgroundOption[i]
  96. ),
  97. i
  98. );
  99. } else if (this.background[i]) {
  100. this.background[i] = this.background[i].destroy();
  101. this.background.splice(i, 1);
  102. }
  103. }
  104. }
  105. },
  106. /**
  107. * Render an individual pane background.
  108. *
  109. * @private
  110. * @function Highcharts.Pane#renderBackground
  111. *
  112. * @param {Highcharts.PaneBackgroundOptions} backgroundOptions
  113. * Background options
  114. *
  115. * @param {number} i
  116. * The index of the background in this.backgrounds
  117. */
  118. renderBackground: function (backgroundOptions, i) {
  119. var method = 'animate',
  120. attribs = {
  121. 'class':
  122. 'highcharts-pane ' + (backgroundOptions.className || '')
  123. };
  124. if (!this.chart.styledMode) {
  125. extend(attribs, {
  126. 'fill': backgroundOptions.backgroundColor,
  127. 'stroke': backgroundOptions.borderColor,
  128. 'stroke-width': backgroundOptions.borderWidth
  129. });
  130. }
  131. if (!this.background[i]) {
  132. this.background[i] = this.chart.renderer.path()
  133. .add(this.group);
  134. method = 'attr';
  135. }
  136. this.background[i][method]({
  137. 'd': this.axis.getPlotBandPath(
  138. backgroundOptions.from,
  139. backgroundOptions.to,
  140. backgroundOptions
  141. )
  142. }).attr(attribs);
  143. },
  144. /**
  145. * The pane serves as a container for axes and backgrounds for circular
  146. * gauges and polar charts.
  147. *
  148. * @since 2.3.0
  149. * @product highcharts
  150. * @optionparent pane
  151. */
  152. defaultOptions: {
  153. /**
  154. * The end angle of the polar X axis or gauge value axis, given in
  155. * degrees where 0 is north. Defaults to [startAngle](#pane.startAngle)
  156. * + 360.
  157. *
  158. * @sample {highcharts} highcharts/demo/gauge-vu-meter/
  159. * VU-meter with custom start and end angle
  160. *
  161. * @type {number}
  162. * @since 2.3.0
  163. * @product highcharts
  164. * @apioption pane.endAngle
  165. */
  166. /**
  167. * The center of a polar chart or angular gauge, given as an array
  168. * of [x, y] positions. Positions can be given as integers that
  169. * transform to pixels, or as percentages of the plot area size.
  170. *
  171. * @sample {highcharts} highcharts/demo/gauge-vu-meter/
  172. * Two gauges with different center
  173. *
  174. * @type {Array<string|number>}
  175. * @default ["50%", "50%"]
  176. * @since 2.3.0
  177. * @product highcharts
  178. */
  179. center: ['50%', '50%'],
  180. /**
  181. * The size of the pane, either as a number defining pixels, or a
  182. * percentage defining a percentage of the plot are.
  183. *
  184. * @sample {highcharts} highcharts/demo/gauge-vu-meter/
  185. * Smaller size
  186. *
  187. * @type {number|string}
  188. * @product highcharts
  189. */
  190. size: '85%',
  191. /**
  192. * The start angle of the polar X axis or gauge axis, given in degrees
  193. * where 0 is north. Defaults to 0.
  194. *
  195. * @sample {highcharts} highcharts/demo/gauge-vu-meter/
  196. * VU-meter with custom start and end angle
  197. *
  198. * @since 2.3.0
  199. * @product highcharts
  200. */
  201. startAngle: 0
  202. },
  203. /**
  204. * An array of background items for the pane.
  205. *
  206. * @sample {highcharts} highcharts/demo/gauge-speedometer/
  207. * Speedometer gauge with multiple backgrounds
  208. *
  209. * @type {Array<*>}
  210. * @optionparent pane.background
  211. */
  212. defaultBackgroundOptions: {
  213. /**
  214. * The class name for this background.
  215. *
  216. * @sample {highcharts} highcharts/css/pane/
  217. * Panes styled by CSS
  218. * @sample {highstock} highcharts/css/pane/
  219. * Panes styled by CSS
  220. * @sample {highmaps} highcharts/css/pane/
  221. * Panes styled by CSS
  222. *
  223. * @type {string}
  224. * @default highcharts-pane
  225. * @since 5.0.0
  226. * @apioption pane.background.className
  227. */
  228. /**
  229. * The shape of the pane background. When `solid`, the background
  230. * is circular. When `arc`, the background extends only from the min
  231. * to the max of the value axis.
  232. *
  233. * @type {string}
  234. * @since 2.3.0
  235. * @validvalue ["arc", "circle", "solid"]
  236. * @product highcharts
  237. */
  238. shape: 'circle',
  239. /**
  240. * The pixel border width of the pane background.
  241. *
  242. * @since 2.3.0
  243. * @product highcharts
  244. */
  245. borderWidth: 1,
  246. /**
  247. * The pane background border color.
  248. *
  249. * @type {Highcharts.ColorString}
  250. * @since 2.3.0
  251. * @product highcharts
  252. */
  253. borderColor: '#cccccc',
  254. /**
  255. * The background color or gradient for the pane.
  256. *
  257. * @type {Highcharts.GradientColorObject}
  258. * @default { linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 }, stops: [[0, #ffffff], [1, #e6e6e6]] }
  259. * @since 2.3.0
  260. * @product highcharts
  261. */
  262. backgroundColor: {
  263. /**
  264. * @ignore
  265. */
  266. linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },
  267. /**
  268. * @ignore
  269. */
  270. stops: [
  271. [0, '#ffffff'],
  272. [1, '#e6e6e6']
  273. ]
  274. },
  275. /** @ignore-option */
  276. from: -Number.MAX_VALUE, // corrected to axis min
  277. /**
  278. * The inner radius of the pane background. Can be either numeric
  279. * (pixels) or a percentage string.
  280. *
  281. * @type {number|string}
  282. * @since 2.3.0
  283. * @product highcharts
  284. */
  285. innerRadius: 0,
  286. /**
  287. * @ignore-option
  288. */
  289. to: Number.MAX_VALUE, // corrected to axis max
  290. /**
  291. * The outer radius of the circular pane background. Can be either
  292. * numeric (pixels) or a percentage string.
  293. *
  294. * @type {number|string}
  295. * @since 2.3.0
  296. * @product highcharts
  297. */
  298. outerRadius: '105%'
  299. },
  300. /**
  301. * Gets the center for the pane and its axis.
  302. *
  303. * @private
  304. * @function Highcharts.Pane#updateCenter
  305. *
  306. * @param {Highcharts.RadialAxis} axis
  307. */
  308. updateCenter: function (axis) {
  309. this.center = (axis || this.axis || {}).center =
  310. CenteredSeriesMixin.getCenter.call(this);
  311. },
  312. /**
  313. * Destroy the pane item
  314. *
  315. * @ignore
  316. * @private
  317. * @function Highcharts.Pane#destroy
  318. * /
  319. destroy: function () {
  320. H.erase(this.chart.pane, this);
  321. this.background.forEach(function (background) {
  322. background.destroy();
  323. });
  324. this.background.length = 0;
  325. this.group = this.group.destroy();
  326. },
  327. */
  328. /**
  329. * Update the pane item with new options
  330. *
  331. * @private
  332. * @function Highcharts.Pane#update
  333. *
  334. * @param {Highcharts.PaneOptions} options
  335. * New pane options
  336. *
  337. * @param {boolean} redraw
  338. */
  339. update: function (options, redraw) {
  340. merge(true, this.options, options);
  341. this.setOptions(this.options);
  342. this.render();
  343. this.chart.axes.forEach(function (axis) {
  344. if (axis.pane === this) {
  345. axis.pane = null;
  346. axis.update({}, redraw);
  347. }
  348. }, this);
  349. }
  350. });
  351. H.Pane = Pane;