Drilldown.js 40 KB

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