drilldown.src.js 39 KB

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