Drilldown.js 39 KB

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