Drilldown.js 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. /* *
  2. *
  3. * Highcharts Drilldown module
  4. *
  5. * Author: Torstein Honsi
  6. *
  7. * License: www.highcharts.com/license
  8. *
  9. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  10. *
  11. * */
  12. 'use strict';
  13. import A from '../Core/Animation/AnimationUtilities.js';
  14. var animObject = A.animObject;
  15. import Axis from '../Core/Axis/Axis.js';
  16. import Chart from '../Core/Chart/Chart.js';
  17. import Color from '../Core/Color/Color.js';
  18. import ColumnSeries from '../Series/Column/ColumnSeries.js';
  19. import H from '../Core/Globals.js';
  20. var noop = H.noop;
  21. import O from '../Core/Options.js';
  22. var defaultOptions = O.defaultOptions;
  23. import palette from '../Core/Color/Palette.js';
  24. import Point from '../Core/Series/Point.js';
  25. import Series from '../Core/Series/Series.js';
  26. import SeriesRegistry from '../Core/Series/SeriesRegistry.js';
  27. var seriesTypes = SeriesRegistry.seriesTypes;
  28. import SVGRenderer from '../Core/Renderer/SVG/SVGRenderer.js';
  29. import Tick from '../Core/Axis/Tick.js';
  30. import U from '../Core/Utilities.js';
  31. var addEvent = U.addEvent, removeEvent = U.removeEvent, extend = U.extend, fireEvent = U.fireEvent, format = U.format, merge = U.merge, objectEach = U.objectEach, pick = U.pick, syncTimeout = U.syncTimeout;
  32. /**
  33. * Gets fired when a drilldown point is clicked, before the new series is added.
  34. * Note that when clicking a category label to trigger multiple series
  35. * drilldown, one `drilldown` event is triggered per point in the category.
  36. *
  37. * @callback Highcharts.DrilldownCallbackFunction
  38. *
  39. * @param {Highcharts.Chart} this
  40. * The chart where the event occurs.
  41. *
  42. * @param {Highcharts.DrilldownEventObject} e
  43. * The drilldown event.
  44. */
  45. /**
  46. * The event arguments when a drilldown point is clicked.
  47. *
  48. * @interface Highcharts.DrilldownEventObject
  49. */ /**
  50. * If a category label was clicked, which index.
  51. * @name Highcharts.DrilldownEventObject#category
  52. * @type {number|undefined}
  53. */ /**
  54. * The original browser event (usually click) that triggered the drilldown.
  55. * @name Highcharts.DrilldownEventObject#originalEvent
  56. * @type {global.Event|undefined}
  57. */ /**
  58. * Prevents the default behaviour of the event.
  59. * @name Highcharts.DrilldownEventObject#preventDefault
  60. * @type {Function}
  61. */ /**
  62. * The originating point.
  63. * @name Highcharts.DrilldownEventObject#point
  64. * @type {Highcharts.Point}
  65. */ /**
  66. * If a category label was clicked, this array holds all points corresponding to
  67. * the category. Otherwise it is set to false.
  68. * @name Highcharts.DrilldownEventObject#points
  69. * @type {boolean|Array<Highcharts.Point>|undefined}
  70. */ /**
  71. * Options for the new series. If the event is utilized for async drilldown, the
  72. * seriesOptions are not added, but rather loaded async.
  73. * @name Highcharts.DrilldownEventObject#seriesOptions
  74. * @type {Highcharts.SeriesOptionsType|undefined}
  75. */ /**
  76. * The event target.
  77. * @name Highcharts.DrilldownEventObject#target
  78. * @type {Highcharts.Chart}
  79. */ /**
  80. * The event type.
  81. * @name Highcharts.DrilldownEventObject#type
  82. * @type {"drilldown"}
  83. */
  84. /**
  85. * This gets fired after all the series have been drilled up. This is especially
  86. * usefull in a chart with multiple drilldown series.
  87. *
  88. * @callback Highcharts.DrillupAllCallbackFunction
  89. *
  90. * @param {Highcharts.Chart} this
  91. * The chart where the event occurs.
  92. *
  93. * @param {Highcharts.DrillupAllEventObject} e
  94. * The final drillup event.
  95. */
  96. /**
  97. * The event arguments when all the series have been drilled up.
  98. *
  99. * @interface Highcharts.DrillupAllEventObject
  100. */ /**
  101. * Prevents the default behaviour of the event.
  102. * @name Highcharts.DrillupAllEventObject#preventDefault
  103. * @type {Function}
  104. */ /**
  105. * The event target.
  106. * @name Highcharts.DrillupAllEventObject#target
  107. * @type {Highcharts.Chart}
  108. */ /**
  109. * The event type.
  110. * @name Highcharts.DrillupAllEventObject#type
  111. * @type {"drillupall"}
  112. */
  113. /**
  114. * Gets fired when drilling up from a drilldown series.
  115. *
  116. * @callback Highcharts.DrillupCallbackFunction
  117. *
  118. * @param {Highcharts.Chart} this
  119. * The chart where the event occurs.
  120. *
  121. * @param {Highcharts.DrillupEventObject} e
  122. * The drillup event.
  123. */
  124. /**
  125. * The event arguments when drilling up from a drilldown series.
  126. *
  127. * @interface Highcharts.DrillupEventObject
  128. */ /**
  129. * Prevents the default behaviour of the event.
  130. * @name Highcharts.DrillupEventObject#preventDefault
  131. * @type {Function}
  132. */ /**
  133. * Options for the new series.
  134. * @name Highcharts.DrillupEventObject#seriesOptions
  135. * @type {Highcharts.SeriesOptionsType|undefined}
  136. */ /**
  137. * The event target.
  138. * @name Highcharts.DrillupEventObject#target
  139. * @type {Highcharts.Chart}
  140. */ /**
  141. * The event type.
  142. * @name Highcharts.DrillupEventObject#type
  143. * @type {"drillup"}
  144. */
  145. import '../Series/Column/ColumnSeries.js';
  146. var PieSeries = seriesTypes.pie, ddSeriesId = 1;
  147. // Add language
  148. extend(defaultOptions.lang,
  149. /**
  150. * @optionparent lang
  151. */
  152. {
  153. /**
  154. * The text for the button that appears when drilling down, linking back
  155. * to the parent series. The parent series' name is inserted for
  156. * `{series.name}`.
  157. *
  158. * @since 3.0.8
  159. * @product highcharts highmaps
  160. * @requires modules/drilldown
  161. *
  162. * @private
  163. */
  164. drillUpText: '◁ Back to {series.name}'
  165. });
  166. /**
  167. * Options for drill down, the concept of inspecting increasingly high
  168. * resolution data through clicking on chart items like columns or pie slices.
  169. *
  170. * The drilldown feature requires the drilldown.js file to be loaded,
  171. * found in the modules directory of the download package, or online at
  172. * [code.highcharts.com/modules/drilldown.js
  173. * ](https://code.highcharts.com/modules/drilldown.js).
  174. *
  175. * @product highcharts highmaps
  176. * @requires modules/drilldown
  177. * @optionparent drilldown
  178. * @sample {highcharts} highcharts/series-organization/drilldown
  179. * Organization chart drilldown
  180. */
  181. defaultOptions.drilldown = {
  182. /**
  183. * When this option is false, clicking a single point will drill down
  184. * all points in the same category, equivalent to clicking the X axis
  185. * label.
  186. *
  187. * @sample {highcharts} highcharts/drilldown/allowpointdrilldown-false/
  188. * Don't allow point drilldown
  189. *
  190. * @type {boolean}
  191. * @default true
  192. * @since 4.1.7
  193. * @product highcharts
  194. * @apioption drilldown.allowPointDrilldown
  195. */
  196. /**
  197. * An array of series configurations for the drill down. Each series
  198. * configuration uses the same syntax as the [series](#series) option set.
  199. * These drilldown series are hidden by default. The drilldown series is
  200. * linked to the parent series' point by its `id`.
  201. *
  202. * @type {Array<Highcharts.SeriesOptionsType>}
  203. * @since 3.0.8
  204. * @product highcharts highmaps
  205. * @apioption drilldown.series
  206. */
  207. /**
  208. * Additional styles to apply to the X axis label for a point that
  209. * has drilldown data. By default it is underlined and blue to invite
  210. * to interaction.
  211. *
  212. * In styled mode, active label styles can be set with the
  213. * `.highcharts-drilldown-axis-label` class.
  214. *
  215. * @sample {highcharts} highcharts/drilldown/labels/
  216. * Label styles
  217. *
  218. * @type {Highcharts.CSSObject}
  219. * @default { "cursor": "pointer", "color": "#003399", "fontWeight": "bold", "textDecoration": "underline" }
  220. * @since 3.0.8
  221. * @product highcharts highmaps
  222. */
  223. activeAxisLabelStyle: {
  224. /** @ignore-option */
  225. cursor: 'pointer',
  226. /** @ignore-option */
  227. color: palette.highlightColor100,
  228. /** @ignore-option */
  229. fontWeight: 'bold',
  230. /** @ignore-option */
  231. textDecoration: 'underline'
  232. },
  233. /**
  234. * Additional styles to apply to the data label of a point that has
  235. * drilldown data. By default it is underlined and blue to invite to
  236. * interaction.
  237. *
  238. * In styled mode, active data label styles can be applied with the
  239. * `.highcharts-drilldown-data-label` class.
  240. *
  241. * @sample {highcharts} highcharts/drilldown/labels/
  242. * Label styles
  243. *
  244. * @type {Highcharts.CSSObject}
  245. * @default { "cursor": "pointer", "color": "#003399", "fontWeight": "bold", "textDecoration": "underline" }
  246. * @since 3.0.8
  247. * @product highcharts highmaps
  248. */
  249. activeDataLabelStyle: {
  250. cursor: 'pointer',
  251. color: palette.highlightColor100,
  252. fontWeight: 'bold',
  253. textDecoration: 'underline'
  254. },
  255. /**
  256. * Set the animation for all drilldown animations. Animation of a drilldown
  257. * occurs when drilling between a column point and a column series,
  258. * or a pie slice and a full pie series. Drilldown can still be used
  259. * between series and points of different types, but animation will
  260. * not occur.
  261. *
  262. * The animation can either be set as a boolean or a configuration
  263. * object. If `true`, it will use the 'swing' jQuery easing and a duration
  264. * of 500 ms. If used as a configuration object, the following properties
  265. * are supported:
  266. *
  267. * - `duration`: The duration of the animation in milliseconds.
  268. *
  269. * - `easing`: A string reference to an easing function set on the `Math`
  270. * object. See
  271. * [the easing demo](https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/plotoptions/series-animation-easing/).
  272. *
  273. * @type {boolean|Partial<Highcharts.AnimationOptionsObject>}
  274. * @since 3.0.8
  275. * @product highcharts highmaps
  276. */
  277. animation: {
  278. /** @internal */
  279. duration: 500
  280. },
  281. /**
  282. * Options for the drill up button that appears when drilling down on a
  283. * series. The text for the button is defined in
  284. * [lang.drillUpText](#lang.drillUpText).
  285. *
  286. * @sample {highcharts} highcharts/drilldown/drillupbutton/
  287. * Drill up button
  288. * @sample {highmaps} highcharts/drilldown/drillupbutton/
  289. * Drill up button
  290. *
  291. * @since 3.0.8
  292. * @product highcharts highmaps
  293. */
  294. drillUpButton: {
  295. /**
  296. * What box to align the button to. Can be either `plotBox` or
  297. * `spacingBox`.
  298. *
  299. * @type {Highcharts.ButtonRelativeToValue}
  300. * @default plotBox
  301. * @since 3.0.8
  302. * @product highcharts highmaps
  303. * @apioption drilldown.drillUpButton.relativeTo
  304. */
  305. /**
  306. * A collection of attributes for the button. The object takes SVG
  307. * attributes like `fill`, `stroke`, `stroke-width` or `r`, the border
  308. * radius. The theme also supports `style`, a collection of CSS
  309. * properties for the text. Equivalent attributes for the hover state
  310. * are given in `theme.states.hover`.
  311. *
  312. * In styled mode, drill-up button styles can be applied with the
  313. * `.highcharts-drillup-button` class.
  314. *
  315. * @sample {highcharts} highcharts/drilldown/drillupbutton/
  316. * Button theming
  317. * @sample {highmaps} highcharts/drilldown/drillupbutton/
  318. * Button theming
  319. *
  320. * @type {object}
  321. * @since 3.0.8
  322. * @product highcharts highmaps
  323. * @apioption drilldown.drillUpButton.theme
  324. */
  325. /**
  326. * Positioning options for the button within the `relativeTo` box.
  327. * Available properties are `x`, `y`, `align` and `verticalAlign`.
  328. *
  329. * @type {Highcharts.AlignObject}
  330. * @since 3.0.8
  331. * @product highcharts highmaps
  332. */
  333. position: {
  334. /**
  335. * Vertical alignment of the button.
  336. *
  337. * @type {Highcharts.VerticalAlignValue}
  338. * @default top
  339. * @product highcharts highmaps
  340. * @apioption drilldown.drillUpButton.position.verticalAlign
  341. */
  342. /**
  343. * Horizontal alignment.
  344. *
  345. * @type {Highcharts.AlignValue}
  346. */
  347. align: 'right',
  348. /**
  349. * The X offset of the button.
  350. */
  351. x: -10,
  352. /**
  353. * The Y offset of the button.
  354. */
  355. y: 10
  356. }
  357. }
  358. };
  359. /**
  360. * Fires when a drilldown point is clicked, before the new series is added. This
  361. * event is also utilized for async drilldown, where the seriesOptions are not
  362. * added by option, but rather loaded async. Note that when clicking a category
  363. * label to trigger multiple series drilldown, one `drilldown` event is
  364. * triggered per point in the category.
  365. *
  366. * Event arguments:
  367. *
  368. * - `category`: If a category label was clicked, which index.
  369. *
  370. * - `originalEvent`: The original browser event (usually click) that triggered
  371. * the drilldown.
  372. *
  373. * - `point`: The originating point.
  374. *
  375. * - `points`: If a category label was clicked, this array holds all points
  376. * corresponding to the category.
  377. *
  378. * - `seriesOptions`: Options for the new series.
  379. *
  380. * @sample {highcharts} highcharts/drilldown/async/
  381. * Async drilldown
  382. *
  383. * @type {Highcharts.DrilldownCallbackFunction}
  384. * @since 3.0.8
  385. * @product highcharts highmaps
  386. * @context Highcharts.Chart
  387. * @requires modules/drilldown
  388. * @apioption chart.events.drilldown
  389. */
  390. /**
  391. * Fires when drilling up from a drilldown series.
  392. *
  393. * @type {Highcharts.DrillupCallbackFunction}
  394. * @since 3.0.8
  395. * @product highcharts highmaps
  396. * @context Highcharts.Chart
  397. * @requires modules/drilldown
  398. * @apioption chart.events.drillup
  399. */
  400. /**
  401. * In a chart with multiple drilldown series, this event fires after all the
  402. * series have been drilled up.
  403. *
  404. * @type {Highcharts.DrillupAllCallbackFunction}
  405. * @since 4.2.4
  406. * @product highcharts highmaps
  407. * @context Highcharts.Chart
  408. * @requires modules/drilldown
  409. * @apioption chart.events.drillupall
  410. */
  411. /**
  412. * The `id` of a series in the [drilldown.series](#drilldown.series) array to
  413. * use for a drilldown for this point.
  414. *
  415. * @sample {highcharts} highcharts/drilldown/basic/
  416. * Basic drilldown
  417. *
  418. * @type {string}
  419. * @since 3.0.8
  420. * @product highcharts
  421. * @requires modules/drilldown
  422. * @apioption series.line.data.drilldown
  423. */
  424. /**
  425. * A general fadeIn method.
  426. *
  427. * @requires module:modules/drilldown
  428. *
  429. * @function Highcharts.SVGElement#fadeIn
  430. *
  431. * @param {boolean|Partial<Highcharts.AnimationOptionsObject>} [animation]
  432. * The animation options for the element fade.
  433. */
  434. SVGRenderer.prototype.Element.prototype.fadeIn = function (animation) {
  435. this
  436. .attr({
  437. opacity: 0.1,
  438. visibility: 'inherit'
  439. })
  440. .animate({
  441. opacity: pick(this.newOpacity, 1) // newOpacity used in maps
  442. }, animation || {
  443. duration: 250
  444. });
  445. };
  446. /**
  447. * Add a series to the chart as drilldown from a specific point in the parent
  448. * series. This method is used for async drilldown, when clicking a point in a
  449. * series should result in loading and displaying a more high-resolution series.
  450. * When not async, the setup is simpler using the
  451. * [drilldown.series](https://api.highcharts.com/highcharts/drilldown.series)
  452. * options structure.
  453. *
  454. * @sample highcharts/drilldown/async/
  455. * Async drilldown
  456. *
  457. * @function Highcharts.Chart#addSeriesAsDrilldown
  458. *
  459. * @param {Highcharts.Point} point
  460. * The point from which the drilldown will start.
  461. *
  462. * @param {Highcharts.SeriesOptionsType} options
  463. * The series options for the new, detailed series.
  464. */
  465. Chart.prototype.addSeriesAsDrilldown = function (point, options) {
  466. this.addSingleSeriesAsDrilldown(point, options);
  467. this.applyDrilldown();
  468. };
  469. Chart.prototype.addSingleSeriesAsDrilldown = function (point, ddOptions) {
  470. var oldSeries = point.series, xAxis = oldSeries.xAxis, yAxis = oldSeries.yAxis, newSeries, pointIndex, levelSeries = [], levelSeriesOptions = [], level, levelNumber, last, colorProp;
  471. colorProp = this.styledMode ?
  472. { colorIndex: pick(point.colorIndex, oldSeries.colorIndex) } :
  473. { color: point.color || oldSeries.color };
  474. if (!this.drilldownLevels) {
  475. this.drilldownLevels = [];
  476. }
  477. levelNumber = oldSeries.options._levelNumber || 0;
  478. // See if we can reuse the registered series from last run
  479. last = this.drilldownLevels[this.drilldownLevels.length - 1];
  480. if (last && last.levelNumber !== levelNumber) {
  481. last = void 0;
  482. }
  483. ddOptions = extend(extend({
  484. _ddSeriesId: ddSeriesId++
  485. }, colorProp), ddOptions);
  486. pointIndex = oldSeries.points.indexOf(point);
  487. // Record options for all current series
  488. oldSeries.chart.series.forEach(function (series) {
  489. if (series.xAxis === xAxis && !series.isDrilling) {
  490. series.options._ddSeriesId =
  491. series.options._ddSeriesId || ddSeriesId++;
  492. series.options._colorIndex = series.userOptions._colorIndex;
  493. series.options._levelNumber =
  494. series.options._levelNumber || levelNumber; // #3182
  495. if (last) {
  496. levelSeries = last.levelSeries;
  497. levelSeriesOptions = last.levelSeriesOptions;
  498. }
  499. else {
  500. levelSeries.push(series);
  501. // (#10597)
  502. series.purgedOptions = merge({
  503. _ddSeriesId: series.options._ddSeriesId,
  504. _levelNumber: series.options._levelNumber,
  505. selected: series.options.selected
  506. }, series.userOptions);
  507. levelSeriesOptions.push(series.purgedOptions);
  508. }
  509. }
  510. });
  511. // Add a record of properties for each drilldown level
  512. level = extend({
  513. levelNumber: levelNumber,
  514. seriesOptions: oldSeries.options,
  515. seriesPurgedOptions: oldSeries.purgedOptions,
  516. levelSeriesOptions: levelSeriesOptions,
  517. levelSeries: levelSeries,
  518. shapeArgs: point.shapeArgs,
  519. // no graphic in line series with markers disabled
  520. bBox: point.graphic ? point.graphic.getBBox() : {},
  521. color: point.isNull ?
  522. new Color(colorProp.color).setOpacity(0).get() :
  523. colorProp.color,
  524. lowerSeriesOptions: ddOptions,
  525. pointOptions: oldSeries.options.data[pointIndex],
  526. pointIndex: pointIndex,
  527. oldExtremes: {
  528. xMin: xAxis && xAxis.userMin,
  529. xMax: xAxis && xAxis.userMax,
  530. yMin: yAxis && yAxis.userMin,
  531. yMax: yAxis && yAxis.userMax
  532. },
  533. resetZoomButton: this.resetZoomButton
  534. }, colorProp);
  535. // Push it to the lookup array
  536. this.drilldownLevels.push(level);
  537. // Reset names to prevent extending (#6704)
  538. if (xAxis && xAxis.names) {
  539. xAxis.names.length = 0;
  540. }
  541. newSeries = level.lowerSeries = this.addSeries(ddOptions, false);
  542. newSeries.options._levelNumber = levelNumber + 1;
  543. if (xAxis) {
  544. xAxis.oldPos = xAxis.pos;
  545. xAxis.userMin = xAxis.userMax = null;
  546. yAxis.userMin = yAxis.userMax = null;
  547. }
  548. // Run fancy cross-animation on supported and equal types
  549. if (oldSeries.type === newSeries.type) {
  550. newSeries.animate = newSeries.animateDrilldown || noop;
  551. newSeries.options.animation = true;
  552. }
  553. };
  554. Chart.prototype.applyDrilldown = function () {
  555. var drilldownLevels = this.drilldownLevels, levelToRemove;
  556. if (drilldownLevels && drilldownLevels.length > 0) { // #3352, async loading
  557. levelToRemove = drilldownLevels[drilldownLevels.length - 1].levelNumber;
  558. this.drilldownLevels.forEach(function (level) {
  559. if (level.levelNumber === levelToRemove) {
  560. level.levelSeries.forEach(function (series) {
  561. // Not removed, not added as part of a multi-series
  562. // drilldown
  563. if (series.options &&
  564. series.options._levelNumber === levelToRemove) {
  565. series.remove(false);
  566. }
  567. });
  568. }
  569. });
  570. }
  571. // We have a reset zoom button. Hide it and detatch it from the chart. It
  572. // is preserved to the layer config above.
  573. if (this.resetZoomButton) {
  574. this.resetZoomButton.hide();
  575. delete this.resetZoomButton;
  576. }
  577. this.pointer.reset();
  578. this.redraw();
  579. this.showDrillUpButton();
  580. fireEvent(this, 'afterDrilldown');
  581. };
  582. Chart.prototype.getDrilldownBackText = function () {
  583. var drilldownLevels = this.drilldownLevels, lastLevel;
  584. if (drilldownLevels && drilldownLevels.length > 0) { // #3352, async loading
  585. lastLevel = drilldownLevels[drilldownLevels.length - 1];
  586. lastLevel.series = lastLevel.seriesOptions;
  587. return format(this.options.lang.drillUpText, lastLevel);
  588. }
  589. };
  590. Chart.prototype.showDrillUpButton = function () {
  591. var chart = this, backText = this.getDrilldownBackText(), buttonOptions = chart.options.drilldown.drillUpButton, attr, states;
  592. if (!this.drillUpButton) {
  593. attr = buttonOptions.theme;
  594. states = attr && attr.states;
  595. this.drillUpButton = this.renderer.button(backText, null, null, function () {
  596. chart.drillUp();
  597. }, attr, states && states.hover, states && states.select)
  598. .addClass('highcharts-drillup-button')
  599. .attr({
  600. align: buttonOptions.position.align,
  601. zIndex: 7
  602. })
  603. .add()
  604. .align(buttonOptions.position, false, buttonOptions.relativeTo || 'plotBox');
  605. }
  606. else {
  607. this.drillUpButton.attr({
  608. text: backText
  609. })
  610. .align();
  611. }
  612. };
  613. /**
  614. * When the chart is drilled down to a child series, calling `chart.drillUp()`
  615. * will drill up to the parent series.
  616. *
  617. * @requires modules/drilldown
  618. *
  619. * @function Highcharts.Chart#drillUp
  620. */
  621. Chart.prototype.drillUp = function () {
  622. if (!this.drilldownLevels || this.drilldownLevels.length === 0) {
  623. return;
  624. }
  625. var chart = this, drilldownLevels = chart.drilldownLevels, levelNumber = drilldownLevels[drilldownLevels.length - 1].levelNumber, i = drilldownLevels.length, chartSeries = chart.series, seriesI, level, oldSeries, newSeries, oldExtremes, addSeries = function (seriesOptions) {
  626. var addedSeries;
  627. chartSeries.forEach(function (series) {
  628. if (series.options._ddSeriesId === seriesOptions._ddSeriesId) {
  629. addedSeries = series;
  630. }
  631. });
  632. addedSeries = addedSeries || chart.addSeries(seriesOptions, false);
  633. if (addedSeries.type === oldSeries.type &&
  634. addedSeries.animateDrillupTo) {
  635. addedSeries.animate = addedSeries.animateDrillupTo;
  636. }
  637. if (seriesOptions === level.seriesPurgedOptions) {
  638. newSeries = addedSeries;
  639. }
  640. };
  641. while (i--) {
  642. level = drilldownLevels[i];
  643. if (level.levelNumber === levelNumber) {
  644. drilldownLevels.pop();
  645. // Get the lower series by reference or id
  646. oldSeries = level.lowerSeries;
  647. if (!oldSeries.chart) { // #2786
  648. seriesI = chartSeries.length; // #2919
  649. while (seriesI--) {
  650. if (chartSeries[seriesI].options.id ===
  651. level.lowerSeriesOptions.id &&
  652. chartSeries[seriesI].options._levelNumber ===
  653. levelNumber + 1) { // #3867
  654. oldSeries = chartSeries[seriesI];
  655. break;
  656. }
  657. }
  658. }
  659. oldSeries.xData = []; // Overcome problems with minRange (#2898)
  660. level.levelSeriesOptions.forEach(addSeries);
  661. fireEvent(chart, 'drillup', {
  662. seriesOptions: level.seriesPurgedOptions ||
  663. level.seriesOptions
  664. });
  665. this.resetZoomButton && this.resetZoomButton.destroy(); // #8095
  666. if (newSeries.type === oldSeries.type) {
  667. newSeries.drilldownLevel = level;
  668. newSeries.options.animation =
  669. chart.options.drilldown.animation;
  670. if (oldSeries.animateDrillupFrom && oldSeries.chart) { // #2919
  671. oldSeries.animateDrillupFrom(level);
  672. }
  673. }
  674. newSeries.options._levelNumber = levelNumber;
  675. oldSeries.remove(false);
  676. // Reset the zoom level of the upper series
  677. if (newSeries.xAxis) {
  678. oldExtremes = level.oldExtremes;
  679. newSeries.xAxis.setExtremes(oldExtremes.xMin, oldExtremes.xMax, false);
  680. newSeries.yAxis.setExtremes(oldExtremes.yMin, oldExtremes.yMax, false);
  681. }
  682. // We have a resetZoomButton tucked away for this level. Attatch
  683. // it to the chart and show it.
  684. if (level.resetZoomButton) {
  685. chart.resetZoomButton = level.resetZoomButton;
  686. chart.resetZoomButton.show();
  687. }
  688. }
  689. }
  690. this.redraw();
  691. if (this.drilldownLevels.length === 0) {
  692. this.drillUpButton = this.drillUpButton.destroy();
  693. }
  694. else {
  695. this.drillUpButton.attr({
  696. text: this.getDrilldownBackText()
  697. })
  698. .align();
  699. }
  700. this.ddDupes.length = []; // #3315
  701. // Fire a once-off event after all series have been drilled up (#5158)
  702. fireEvent(chart, 'drillupall');
  703. };
  704. /* eslint-disable no-invalid-this */
  705. // Add update function to be called internally from Chart.update
  706. // (#7600, #12855)
  707. addEvent(Chart, 'afterInit', function () {
  708. var chart = this;
  709. chart.drilldown = {
  710. update: function (options, redraw) {
  711. merge(true, chart.options.drilldown, options);
  712. if (pick(redraw, true)) {
  713. chart.redraw();
  714. }
  715. }
  716. };
  717. });
  718. // Shift the drillUpButton to make the space for resetZoomButton, #8095.
  719. addEvent(Chart, 'afterShowResetZoom', function () {
  720. var chart = this, bbox = chart.resetZoomButton && chart.resetZoomButton.getBBox(), buttonOptions = chart.options.drilldown && chart.options.drilldown.drillUpButton;
  721. if (this.drillUpButton && bbox && buttonOptions && buttonOptions.position && buttonOptions.position.x) {
  722. this.drillUpButton.align({
  723. x: buttonOptions.position.x - bbox.width - 10,
  724. y: buttonOptions.position.y,
  725. align: buttonOptions.position.align
  726. }, false, buttonOptions.relativeTo || 'plotBox');
  727. }
  728. });
  729. addEvent(Chart, 'render', function () {
  730. (this.xAxis || []).forEach(function (axis) {
  731. axis.ddPoints = {};
  732. axis.series.forEach(function (series) {
  733. var i, xData = series.xData || [], points = series.points, p;
  734. for (i = 0; i < xData.length; i++) {
  735. p = series.options.data[i];
  736. // The `drilldown` property can only be set on an array or an
  737. // object
  738. if (typeof p !== 'number') {
  739. // Convert array to object (#8008)
  740. p = series.pointClass.prototype.optionsToObject
  741. .call({ series: series }, p);
  742. if (p.drilldown) {
  743. if (!axis.ddPoints[xData[i]]) {
  744. axis.ddPoints[xData[i]] = [];
  745. }
  746. axis.ddPoints[xData[i]].push(points ? points[i] : true);
  747. }
  748. }
  749. }
  750. });
  751. // Add drillability to ticks, and always keep it drillability updated
  752. // (#3951)
  753. objectEach(axis.ticks, Tick.prototype.drillable);
  754. });
  755. });
  756. /**
  757. * When drilling up, keep the upper series invisible until the lower series has
  758. * moved into place.
  759. *
  760. * @private
  761. * @function Highcharts.ColumnSeries#animateDrillupTo
  762. * @param {boolean} [init=false]
  763. * Whether to initialize animation
  764. */
  765. ColumnSeries.prototype.animateDrillupTo = function (init) {
  766. if (!init) {
  767. var newSeries = this, level = newSeries.drilldownLevel;
  768. // First hide all items before animating in again
  769. this.points.forEach(function (point) {
  770. var dataLabel = point.dataLabel;
  771. if (point.graphic) { // #3407
  772. point.graphic.hide();
  773. }
  774. if (dataLabel) {
  775. // The data label is initially hidden, make sure it is not faded
  776. // in (#6127)
  777. dataLabel.hidden = dataLabel.attr('visibility') === 'hidden';
  778. if (!dataLabel.hidden) {
  779. dataLabel.hide();
  780. if (point.connector) {
  781. point.connector.hide();
  782. }
  783. }
  784. }
  785. });
  786. // Do dummy animation on first point to get to complete
  787. syncTimeout(function () {
  788. if (newSeries.points) { // May be destroyed in the meantime, #3389
  789. // Unable to drillup with nodes, #13711
  790. var pointsWithNodes = [];
  791. newSeries.data.forEach(function (el) {
  792. pointsWithNodes.push(el);
  793. });
  794. if (newSeries.nodes) {
  795. pointsWithNodes = pointsWithNodes.concat(newSeries.nodes);
  796. }
  797. pointsWithNodes.forEach(function (point, i) {
  798. // Fade in other points
  799. var verb = i === (level && level.pointIndex) ? 'show' : 'fadeIn', inherit = verb === 'show' ? true : void 0, dataLabel = point.dataLabel;
  800. if (point.graphic) { // #3407
  801. point.graphic[verb](inherit);
  802. }
  803. if (dataLabel && !dataLabel.hidden) { // #6127
  804. dataLabel.fadeIn(); // #7384
  805. if (point.connector) {
  806. point.connector.fadeIn();
  807. }
  808. }
  809. });
  810. }
  811. }, Math.max(this.chart.options.drilldown.animation.duration - 50, 0));
  812. // Reset to prototype
  813. delete this.animate;
  814. }
  815. };
  816. ColumnSeries.prototype.animateDrilldown = function (init) {
  817. var series = this, chart = this.chart, drilldownLevels = chart.drilldownLevels, animateFrom, animationOptions = animObject(chart.options.drilldown.animation), xAxis = this.xAxis, styledMode = chart.styledMode;
  818. if (!init) {
  819. drilldownLevels.forEach(function (level) {
  820. if (series.options._ddSeriesId ===
  821. level.lowerSeriesOptions._ddSeriesId) {
  822. animateFrom = level.shapeArgs;
  823. if (!styledMode) {
  824. // Add the point colors to animate from
  825. animateFrom.fill = level.color;
  826. }
  827. }
  828. });
  829. animateFrom.x += pick(xAxis.oldPos, xAxis.pos) - xAxis.pos;
  830. this.points.forEach(function (point) {
  831. var animateTo = point.shapeArgs;
  832. if (!styledMode) {
  833. // Add the point colors to animate to
  834. animateTo.fill = point.color;
  835. }
  836. if (point.graphic) {
  837. point.graphic
  838. .attr(animateFrom)
  839. .animate(extend(point.shapeArgs, { fill: point.color || series.color }), animationOptions);
  840. }
  841. if (point.dataLabel) {
  842. point.dataLabel.fadeIn(animationOptions);
  843. }
  844. });
  845. // Reset to prototype
  846. delete this.animate;
  847. }
  848. };
  849. /**
  850. * When drilling up, pull out the individual point graphics from the lower
  851. * series and animate them into the origin point in the upper series.
  852. *
  853. * @private
  854. * @function Highcharts.ColumnSeries#animateDrillupFrom
  855. * @param {Highcharts.DrilldownLevelObject} level
  856. * Level container
  857. * @return {void}
  858. */
  859. ColumnSeries.prototype.animateDrillupFrom = function (level) {
  860. var animationOptions = animObject(this.chart.options.drilldown.animation), group = this.group,
  861. // For 3d column series all columns are added to one group
  862. // so we should not delete the whole group. #5297
  863. removeGroup = group !== this.chart.columnGroup, series = this;
  864. // Cancel mouse events on the series group (#2787)
  865. series.trackerGroups.forEach(function (key) {
  866. if (series[key]) { // we don't always have dataLabelsGroup
  867. series[key].on('mouseover');
  868. }
  869. });
  870. if (removeGroup) {
  871. delete this.group;
  872. }
  873. this.points.forEach(function (point) {
  874. var graphic = point.graphic, animateTo = level.shapeArgs, complete = function () {
  875. graphic.destroy();
  876. if (group && removeGroup) {
  877. group = group.destroy();
  878. }
  879. };
  880. if (graphic && animateTo) {
  881. delete point.graphic;
  882. if (!series.chart.styledMode) {
  883. animateTo.fill = level.color;
  884. }
  885. if (animationOptions.duration) {
  886. graphic.animate(animateTo, merge(animationOptions, { complete: complete }));
  887. }
  888. else {
  889. graphic.attr(animateTo);
  890. complete();
  891. }
  892. }
  893. });
  894. };
  895. if (PieSeries) {
  896. extend(PieSeries.prototype, {
  897. animateDrillupTo: ColumnSeries.prototype.animateDrillupTo,
  898. animateDrillupFrom: ColumnSeries.prototype.animateDrillupFrom,
  899. animateDrilldown: function (init) {
  900. var level = this.chart.drilldownLevels[this.chart.drilldownLevels.length - 1], animationOptions = this.chart.options.drilldown.animation;
  901. if (this.is('item')) {
  902. animationOptions.duration = 0;
  903. }
  904. // Unable to drill down in the horizontal item series #13372
  905. if (this.center) {
  906. var animateFrom = level.shapeArgs, start = animateFrom.start, angle = animateFrom.end - start, startAngle = angle / this.points.length, styledMode = this.chart.styledMode;
  907. if (!init) {
  908. this.points.forEach(function (point, i) {
  909. var animateTo = point.shapeArgs;
  910. if (!styledMode) {
  911. animateFrom.fill = level.color;
  912. animateTo.fill = point.color;
  913. }
  914. if (point.graphic) {
  915. point.graphic
  916. .attr(merge(animateFrom, {
  917. start: start + i * startAngle,
  918. end: start + (i + 1) * startAngle
  919. }))[animationOptions ? 'animate' : 'attr'](animateTo, animationOptions);
  920. }
  921. });
  922. // Reset to prototype
  923. delete this.animate;
  924. }
  925. }
  926. }
  927. });
  928. }
  929. Point.prototype.doDrilldown = function (_holdRedraw, category, originalEvent) {
  930. var series = this.series, chart = series.chart, drilldown = chart.options.drilldown, i = (drilldown.series || []).length, seriesOptions;
  931. if (!chart.ddDupes) {
  932. chart.ddDupes = [];
  933. }
  934. while (i-- && !seriesOptions) {
  935. if (drilldown.series[i].id === this.drilldown &&
  936. chart.ddDupes.indexOf(this.drilldown) === -1) {
  937. seriesOptions = drilldown.series[i];
  938. chart.ddDupes.push(this.drilldown);
  939. }
  940. }
  941. // Fire the event. If seriesOptions is undefined, the implementer can check
  942. // for seriesOptions, and call addSeriesAsDrilldown async if necessary.
  943. fireEvent(chart, 'drilldown', {
  944. point: this,
  945. seriesOptions: seriesOptions,
  946. category: category,
  947. originalEvent: originalEvent,
  948. points: (typeof category !== 'undefined' &&
  949. this.series.xAxis.getDDPoints(category).slice(0))
  950. }, function (e) {
  951. var chart = e.point.series && e.point.series.chart, seriesOptions = e.seriesOptions;
  952. if (chart && seriesOptions) {
  953. if (_holdRedraw) {
  954. chart.addSingleSeriesAsDrilldown(e.point, seriesOptions);
  955. }
  956. else {
  957. chart.addSeriesAsDrilldown(e.point, seriesOptions);
  958. }
  959. }
  960. });
  961. };
  962. /**
  963. * Drill down to a given category. This is the same as clicking on an axis
  964. * label.
  965. *
  966. * @private
  967. * @function Highcharts.Axis#drilldownCategory
  968. * @param {number} x
  969. * Tick position
  970. * @param {global.MouseEvent} e
  971. * Click event
  972. */
  973. Axis.prototype.drilldownCategory = function (x, e) {
  974. this.getDDPoints(x).forEach(function (point) {
  975. if (point &&
  976. point.series &&
  977. point.series.visible &&
  978. point.doDrilldown) { // #3197
  979. point.doDrilldown(true, x, e);
  980. }
  981. });
  982. this.chart.applyDrilldown();
  983. };
  984. /**
  985. * Return drillable points for this specific X value.
  986. *
  987. * @private
  988. * @function Highcharts.Axis#getDDPoints
  989. * @param {number} x
  990. * Tick position
  991. * @return {Array<(false|Highcharts.Point)>}
  992. * Drillable points
  993. */
  994. Axis.prototype.getDDPoints = function (x) {
  995. return (this.ddPoints && this.ddPoints[x] || []);
  996. };
  997. /**
  998. * Make a tick label drillable, or remove drilling on update.
  999. *
  1000. * @private
  1001. * @function Highcharts.Axis#drillable
  1002. */
  1003. Tick.prototype.drillable = function () {
  1004. var pos = this.pos, label = this.label, axis = this.axis, isDrillable = axis.coll === 'xAxis' && axis.getDDPoints, ddPointsX = isDrillable && axis.getDDPoints(pos), styledMode = axis.chart.styledMode;
  1005. if (isDrillable) {
  1006. if (label && ddPointsX && ddPointsX.length) {
  1007. label.drillable = true;
  1008. if (!label.basicStyles && !styledMode) {
  1009. label.basicStyles = merge(label.styles);
  1010. }
  1011. label.addClass('highcharts-drilldown-axis-label');
  1012. // #12656 - avoid duplicate of attach event
  1013. if (label.removeOnDrillableClick) {
  1014. removeEvent(label.element, 'click');
  1015. }
  1016. label.removeOnDrillableClick = addEvent(label.element, 'click', function (e) {
  1017. e.preventDefault();
  1018. axis.drilldownCategory(pos, e);
  1019. });
  1020. if (!styledMode) {
  1021. label.css(axis.chart.options.drilldown.activeAxisLabelStyle);
  1022. }
  1023. }
  1024. else if (label && label.drillable && label.removeOnDrillableClick) {
  1025. if (!styledMode) {
  1026. label.styles = {}; // reset for full overwrite of styles
  1027. label.css(label.basicStyles);
  1028. }
  1029. label.removeOnDrillableClick(); // #3806
  1030. label.removeClass('highcharts-drilldown-axis-label');
  1031. }
  1032. }
  1033. };
  1034. // On initialization of each point, identify its label and make it clickable.
  1035. // Also, provide a list of points associated to that label.
  1036. addEvent(Point, 'afterInit', function () {
  1037. var point = this;
  1038. if (point.drilldown && !point.unbindDrilldownClick) {
  1039. // Add the click event to the point
  1040. point.unbindDrilldownClick = addEvent(point, 'click', handlePointClick);
  1041. }
  1042. return point;
  1043. });
  1044. addEvent(Point, 'update', function (e) {
  1045. var point = this, options = e.options || {};
  1046. if (options.drilldown && !point.unbindDrilldownClick) {
  1047. // Add the click event to the point
  1048. point.unbindDrilldownClick = addEvent(point, 'click', handlePointClick);
  1049. }
  1050. else if (!options.drilldown &&
  1051. options.drilldown !== void 0 &&
  1052. point.unbindDrilldownClick) {
  1053. point.unbindDrilldownClick = point.unbindDrilldownClick();
  1054. }
  1055. });
  1056. var handlePointClick = function (e) {
  1057. var point = this, series = point.series;
  1058. if (series.xAxis &&
  1059. series.chart.options.drilldown.allowPointDrilldown ===
  1060. false) {
  1061. // #5822, x changed
  1062. series.xAxis.drilldownCategory(point.x, e);
  1063. }
  1064. else {
  1065. point.doDrilldown(void 0, void 0, e);
  1066. }
  1067. };
  1068. addEvent(Series, 'afterDrawDataLabels', function () {
  1069. var css = this.chart.options.drilldown.activeDataLabelStyle, renderer = this.chart.renderer, styledMode = this.chart.styledMode;
  1070. this.points.forEach(function (point) {
  1071. var dataLabelsOptions = point.options.dataLabels, pointCSS = pick(point.dlOptions, dataLabelsOptions && dataLabelsOptions.style, {});
  1072. if (point.drilldown && point.dataLabel) {
  1073. if (css.color === 'contrast' && !styledMode) {
  1074. pointCSS.color = renderer.getContrast(point.color || this.color);
  1075. }
  1076. if (dataLabelsOptions && dataLabelsOptions.color) {
  1077. pointCSS.color = dataLabelsOptions.color;
  1078. }
  1079. point.dataLabel
  1080. .addClass('highcharts-drilldown-data-label');
  1081. if (!styledMode) {
  1082. point.dataLabel
  1083. .css(css)
  1084. .css(pointCSS);
  1085. }
  1086. }
  1087. }, this);
  1088. });
  1089. var applyCursorCSS = function (element, cursor, addClass, styledMode) {
  1090. element[addClass ? 'addClass' : 'removeClass']('highcharts-drilldown-point');
  1091. if (!styledMode) {
  1092. element.css({ cursor: cursor });
  1093. }
  1094. };
  1095. // Mark the trackers with a pointer
  1096. addEvent(Series, 'afterDrawTracker', function () {
  1097. var styledMode = this.chart.styledMode;
  1098. this.points.forEach(function (point) {
  1099. if (point.drilldown && point.graphic) {
  1100. applyCursorCSS(point.graphic, 'pointer', true, styledMode);
  1101. }
  1102. });
  1103. });
  1104. addEvent(Point, 'afterSetState', function () {
  1105. var styledMode = this.series.chart.styledMode;
  1106. if (this.drilldown && this.series.halo && this.state === 'hover') {
  1107. applyCursorCSS(this.series.halo, 'pointer', true, styledMode);
  1108. }
  1109. else if (this.series.halo) {
  1110. applyCursorCSS(this.series.halo, 'auto', false, styledMode);
  1111. }
  1112. });
  1113. // After zooming out, shift the drillUpButton to the previous position, #8095.
  1114. addEvent(H.Chart, 'selection', function (event) {
  1115. if (event.resetSelection === true && this.drillUpButton) {
  1116. var buttonOptions = this.options.drilldown && this.options.drilldown.drillUpButton;
  1117. if (buttonOptions && buttonOptions.position) {
  1118. this.drillUpButton.align({
  1119. x: buttonOptions.position.x,
  1120. y: buttonOptions.position.y,
  1121. align: buttonOptions.position.align
  1122. }, false, buttonOptions.relativeTo || 'plotBox');
  1123. }
  1124. }
  1125. });
  1126. addEvent(H.Chart, 'drillup', function () {
  1127. if (this.resetZoomButton) {
  1128. this.resetZoomButton = this.resetZoomButton.destroy();
  1129. }
  1130. });