funnel.src.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /**
  2. * @license Highcharts JS v8.2.0 (2020-08-20)
  3. *
  4. * Highcharts funnel module
  5. *
  6. * (c) 2010-2019 Torstein Honsi
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. 'use strict';
  11. (function (factory) {
  12. if (typeof module === 'object' && module.exports) {
  13. factory['default'] = factory;
  14. module.exports = factory;
  15. } else if (typeof define === 'function' && define.amd) {
  16. define('highcharts/modules/funnel', ['highcharts'], function (Highcharts) {
  17. factory(Highcharts);
  18. factory.Highcharts = Highcharts;
  19. return factory;
  20. });
  21. } else {
  22. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  23. }
  24. }(function (Highcharts) {
  25. var _modules = Highcharts ? Highcharts._modules : {};
  26. function _registerModule(obj, path, args, fn) {
  27. if (!obj.hasOwnProperty(path)) {
  28. obj[path] = fn.apply(null, args);
  29. }
  30. }
  31. _registerModule(_modules, 'Series/FunnelSeries.js', [_modules['Core/Chart/Chart.js'], _modules['Core/Globals.js'], _modules['Core/Utilities.js']], function (Chart, H, U) {
  32. /* *
  33. *
  34. * Highcharts funnel module
  35. *
  36. * (c) 2010-2020 Torstein Honsi
  37. *
  38. * License: www.highcharts.com/license
  39. *
  40. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  41. *
  42. * */
  43. /* eslint indent: 0 */
  44. var noop = H.noop,
  45. seriesType = H.seriesType,
  46. seriesTypes = H.seriesTypes;
  47. var addEvent = U.addEvent,
  48. fireEvent = U.fireEvent,
  49. isArray = U.isArray,
  50. pick = U.pick;
  51. /**
  52. * @private
  53. * @class
  54. * @name Highcharts.seriesTypes.funnel
  55. *
  56. * @augments Highcharts.Series
  57. */
  58. seriesType('funnel', 'pie',
  59. /**
  60. * Funnel charts are a type of chart often used to visualize stages in a
  61. * sales project, where the top are the initial stages with the most
  62. * clients. It requires that the modules/funnel.js file is loaded.
  63. *
  64. * @sample highcharts/demo/funnel/
  65. * Funnel demo
  66. *
  67. * @extends plotOptions.pie
  68. * @excluding innerSize,size,dataSorting
  69. * @product highcharts
  70. * @requires modules/funnel
  71. * @optionparent plotOptions.funnel
  72. */
  73. {
  74. /**
  75. * Initial animation is by default disabled for the funnel chart.
  76. */
  77. animation: false,
  78. /**
  79. * The center of the series. By default, it is centered in the middle
  80. * of the plot area, so it fills the plot area height.
  81. *
  82. * @type {Array<number|string>}
  83. * @default ["50%", "50%"]
  84. * @since 3.0
  85. */
  86. center: ['50%', '50%'],
  87. /**
  88. * The width of the funnel compared to the width of the plot area,
  89. * or the pixel width if it is a number.
  90. *
  91. * @type {number|string}
  92. * @since 3.0
  93. */
  94. width: '90%',
  95. /**
  96. * The width of the neck, the lower part of the funnel. A number defines
  97. * pixel width, a percentage string defines a percentage of the plot
  98. * area width.
  99. *
  100. * @sample {highcharts} highcharts/demo/funnel/
  101. * Funnel demo
  102. *
  103. * @type {number|string}
  104. * @since 3.0
  105. */
  106. neckWidth: '30%',
  107. /**
  108. * The height of the funnel or pyramid. If it is a number it defines
  109. * the pixel height, if it is a percentage string it is the percentage
  110. * of the plot area height.
  111. *
  112. * @sample {highcharts} highcharts/demo/funnel/
  113. * Funnel demo
  114. *
  115. * @type {number|string}
  116. * @since 3.0
  117. */
  118. height: '100%',
  119. /**
  120. * The height of the neck, the lower part of the funnel. A number
  121. * defines pixel width, a percentage string defines a percentage of the
  122. * plot area height.
  123. *
  124. * @type {number|string}
  125. */
  126. neckHeight: '25%',
  127. /**
  128. * A reversed funnel has the widest area down. A reversed funnel with
  129. * no neck width and neck height is a pyramid.
  130. *
  131. * @since 3.0.10
  132. */
  133. reversed: false,
  134. /**
  135. * To avoid adapting the data label size in Pie.drawDataLabels.
  136. * @ignore-option
  137. */
  138. size: true,
  139. dataLabels: {
  140. connectorWidth: 1,
  141. verticalAlign: 'middle'
  142. },
  143. /**
  144. * Options for the series states.
  145. */
  146. states: {
  147. /**
  148. * @excluding halo, marker, lineWidth, lineWidthPlus
  149. * @apioption plotOptions.funnel.states.hover
  150. */
  151. /**
  152. * Options for a selected funnel item.
  153. *
  154. * @excluding halo, marker, lineWidth, lineWidthPlus
  155. */
  156. select: {
  157. /**
  158. * A specific color for the selected point.
  159. *
  160. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  161. */
  162. color: '#cccccc',
  163. /**
  164. * A specific border color for the selected point.
  165. *
  166. * @type {Highcharts.ColorString}
  167. */
  168. borderColor: '#000000'
  169. }
  170. }
  171. },
  172. // Properties
  173. {
  174. animate: noop,
  175. // Overrides the pie translate method
  176. translate: function () {
  177. var sum = 0,
  178. series = this,
  179. chart = series.chart,
  180. options = series.options,
  181. reversed = options.reversed,
  182. ignoreHiddenPoint = options.ignoreHiddenPoint,
  183. plotWidth = chart.plotWidth,
  184. plotHeight = chart.plotHeight,
  185. cumulative = 0, // start at top
  186. center = options.center,
  187. centerX = getLength(center[0],
  188. plotWidth),
  189. centerY = getLength(center[1],
  190. plotHeight),
  191. width = getLength(options.width,
  192. plotWidth),
  193. tempWidth,
  194. height = getLength(options.height,
  195. plotHeight),
  196. neckWidth = getLength(options.neckWidth,
  197. plotWidth),
  198. neckHeight = getLength(options.neckHeight,
  199. plotHeight),
  200. neckY = (centerY - height / 2) + height - neckHeight,
  201. data = series.data,
  202. path,
  203. fraction,
  204. half = (options.dataLabels.position === 'left' ?
  205. 1 :
  206. 0),
  207. x1,
  208. y1,
  209. x2,
  210. x3,
  211. y3,
  212. x4,
  213. y5;
  214. /**
  215. * Get positions - either an integer or a percentage string must be
  216. * given.
  217. * @private
  218. * @param {number|string|undefined} length
  219. * Length
  220. * @param {number} relativeTo
  221. * Relative factor
  222. * @return {number}
  223. * Relative position
  224. */
  225. function getLength(length, relativeTo) {
  226. return (/%$/).test(length) ?
  227. relativeTo * parseInt(length, 10) / 100 :
  228. parseInt(length, 10);
  229. }
  230. series.getWidthAt = function (y) {
  231. var top = (centerY - height / 2);
  232. return (y > neckY || height === neckHeight) ?
  233. neckWidth :
  234. neckWidth + (width - neckWidth) *
  235. (1 - (y - top) / (height - neckHeight));
  236. };
  237. series.getX = function (y, half, point) {
  238. return centerX + (half ? -1 : 1) *
  239. ((series.getWidthAt(reversed ? 2 * centerY - y : y) / 2) +
  240. point.labelDistance);
  241. };
  242. // Expose
  243. series.center = [centerX, centerY, height];
  244. series.centerX = centerX;
  245. /*
  246. Individual point coordinate naming:
  247. x1,y1 _________________ x2,y1
  248. \ /
  249. \ /
  250. \ /
  251. \ /
  252. \ /
  253. x3,y3 _________ x4,y3
  254. Additional for the base of the neck:
  255. | |
  256. | |
  257. | |
  258. x3,y5 _________ x4,y5
  259. */
  260. // get the total sum
  261. data.forEach(function (point) {
  262. if (!ignoreHiddenPoint || point.visible !== false) {
  263. sum += point.y;
  264. }
  265. });
  266. data.forEach(function (point) {
  267. // set start and end positions
  268. y5 = null;
  269. fraction = sum ? point.y / sum : 0;
  270. y1 = centerY - height / 2 + cumulative * height;
  271. y3 = y1 + fraction * height;
  272. tempWidth = series.getWidthAt(y1);
  273. x1 = centerX - tempWidth / 2;
  274. x2 = x1 + tempWidth;
  275. tempWidth = series.getWidthAt(y3);
  276. x3 = centerX - tempWidth / 2;
  277. x4 = x3 + tempWidth;
  278. // the entire point is within the neck
  279. if (y1 > neckY) {
  280. x1 = x3 = centerX - neckWidth / 2;
  281. x2 = x4 = centerX + neckWidth / 2;
  282. // the base of the neck
  283. }
  284. else if (y3 > neckY) {
  285. y5 = y3;
  286. tempWidth = series.getWidthAt(neckY);
  287. x3 = centerX - tempWidth / 2;
  288. x4 = x3 + tempWidth;
  289. y3 = neckY;
  290. }
  291. if (reversed) {
  292. y1 = 2 * centerY - y1;
  293. y3 = 2 * centerY - y3;
  294. if (y5 !== null) {
  295. y5 = 2 * centerY - y5;
  296. }
  297. }
  298. // save the path
  299. path = [
  300. ['M', x1, y1],
  301. ['L', x2, y1],
  302. ['L', x4, y3]
  303. ];
  304. if (y5 !== null) {
  305. path.push(['L', x4, y5], ['L', x3, y5]);
  306. }
  307. path.push(['L', x3, y3], ['Z']);
  308. // prepare for using shared dr
  309. point.shapeType = 'path';
  310. point.shapeArgs = { d: path };
  311. // for tooltips and data labels
  312. point.percentage = fraction * 100;
  313. point.plotX = centerX;
  314. point.plotY = (y1 + (y5 || y3)) / 2;
  315. // Placement of tooltips and data labels
  316. point.tooltipPos = [
  317. centerX,
  318. point.plotY
  319. ];
  320. point.dlBox = {
  321. x: x3,
  322. y: y1,
  323. topWidth: x2 - x1,
  324. bottomWidth: x4 - x3,
  325. height: Math.abs(pick(y5, y3) - y1),
  326. width: NaN
  327. };
  328. // Slice is a noop on funnel points
  329. point.slice = noop;
  330. // Mimicking pie data label placement logic
  331. point.half = half;
  332. if (!ignoreHiddenPoint || point.visible !== false) {
  333. cumulative += fraction;
  334. }
  335. });
  336. fireEvent(series, 'afterTranslate');
  337. },
  338. // Funnel items don't have angles (#2289)
  339. sortByAngle: function (points) {
  340. points.sort(function (a, b) {
  341. return a.plotY - b.plotY;
  342. });
  343. },
  344. // Extend the pie data label method
  345. drawDataLabels: function () {
  346. var series = this,
  347. data = series.data,
  348. labelDistance = series.options.dataLabels.distance,
  349. leftSide,
  350. sign,
  351. point,
  352. i = data.length,
  353. x,
  354. y;
  355. // In the original pie label anticollision logic, the slots are
  356. // distributed from one labelDistance above to one labelDistance
  357. // below the pie. In funnels we don't want this.
  358. series.center[2] -= 2 * labelDistance;
  359. // Set the label position array for each point.
  360. while (i--) {
  361. point = data[i];
  362. leftSide = point.half;
  363. sign = leftSide ? 1 : -1;
  364. y = point.plotY;
  365. point.labelDistance = pick(point.options.dataLabels &&
  366. point.options.dataLabels.distance, labelDistance);
  367. series.maxLabelDistance = Math.max(point.labelDistance, series.maxLabelDistance || 0);
  368. x = series.getX(y, leftSide, point);
  369. // set the anchor point for data labels
  370. point.labelPosition = {
  371. // initial position of the data label - it's utilized for
  372. // finding the final position for the label
  373. natural: {
  374. x: 0,
  375. y: y
  376. },
  377. 'final': {
  378. // used for generating connector path -
  379. // initialized later in drawDataLabels function
  380. // x: undefined,
  381. // y: undefined
  382. },
  383. // left - funnel on the left side of the data label
  384. // right - funnel on the right side of the data label
  385. alignment: leftSide ? 'right' : 'left',
  386. connectorPosition: {
  387. breakAt: {
  388. x: x + (point.labelDistance - 5) * sign,
  389. y: y
  390. },
  391. touchingSliceAt: {
  392. x: x + point.labelDistance * sign,
  393. y: y
  394. }
  395. }
  396. };
  397. }
  398. seriesTypes[series.options.dataLabels.inside ? 'column' : 'pie'].prototype.drawDataLabels.call(this);
  399. },
  400. alignDataLabel: function (point, dataLabel, options, alignTo, isNew) {
  401. var series = point.series,
  402. reversed = series.options.reversed,
  403. dlBox = point.dlBox || point.shapeArgs,
  404. align = options.align,
  405. verticalAlign = options.verticalAlign,
  406. inside = ((series.options || {}).dataLabels || {}).inside,
  407. centerY = series.center[1],
  408. pointPlotY = (reversed ?
  409. 2 * centerY - point.plotY :
  410. point.plotY),
  411. widthAtLabel = series.getWidthAt(pointPlotY - dlBox.height / 2 +
  412. dataLabel.height),
  413. offset = verticalAlign === 'middle' ?
  414. (dlBox.topWidth - dlBox.bottomWidth) / 4 :
  415. (widthAtLabel - dlBox.bottomWidth) / 2,
  416. y = dlBox.y,
  417. x = dlBox.x;
  418. if (verticalAlign === 'middle') {
  419. y = dlBox.y - dlBox.height / 2 + dataLabel.height / 2;
  420. }
  421. else if (verticalAlign === 'top') {
  422. y = dlBox.y - dlBox.height + dataLabel.height +
  423. options.padding;
  424. }
  425. if (verticalAlign === 'top' && !reversed ||
  426. verticalAlign === 'bottom' && reversed ||
  427. verticalAlign === 'middle') {
  428. if (align === 'right') {
  429. x = dlBox.x - options.padding + offset;
  430. }
  431. else if (align === 'left') {
  432. x = dlBox.x + options.padding - offset;
  433. }
  434. }
  435. alignTo = {
  436. x: x,
  437. y: reversed ? y - dlBox.height : y,
  438. width: dlBox.bottomWidth,
  439. height: dlBox.height
  440. };
  441. options.verticalAlign = 'bottom';
  442. // Call the parent method
  443. if (!inside || point.visible) {
  444. Highcharts.Series.prototype.alignDataLabel.call(this, point, dataLabel, options, alignTo, isNew);
  445. }
  446. if (inside) {
  447. if (!point.visible && point.dataLabel) {
  448. // Avoid animation from top
  449. point.dataLabel.placed = false;
  450. }
  451. // If label is inside and we have contrast, set it:
  452. if (point.contrastColor) {
  453. dataLabel.css({
  454. color: point.contrastColor
  455. });
  456. }
  457. }
  458. }
  459. });
  460. /* eslint-disable no-invalid-this */
  461. addEvent(Chart, 'afterHideAllOverlappingLabels', function () {
  462. this.series.forEach(function (series) {
  463. var dataLabelsOptions = series.options && series.options.dataLabels;
  464. if (isArray(dataLabelsOptions)) {
  465. dataLabelsOptions = dataLabelsOptions[0];
  466. }
  467. if (series.is('pie') &&
  468. series.placeDataLabels &&
  469. dataLabelsOptions &&
  470. !dataLabelsOptions.inside) {
  471. series.placeDataLabels();
  472. }
  473. });
  474. });
  475. /**
  476. * A `funnel` series. If the [type](#series.funnel.type) option is
  477. * not specified, it is inherited from [chart.type](#chart.type).
  478. *
  479. * @extends series,plotOptions.funnel
  480. * @excluding dataParser, dataURL, stack, xAxis, yAxis, dataSorting,
  481. * boostBlending, boostThreshold
  482. * @product highcharts
  483. * @requires modules/funnel
  484. * @apioption series.funnel
  485. */
  486. /**
  487. * An array of data points for the series. For the `funnel` series type,
  488. * points can be given in the following ways:
  489. *
  490. * 1. An array of numerical values. In this case, the numerical values
  491. * will be interpreted as `y` options. Example:
  492. *
  493. * ```js
  494. * data: [0, 5, 3, 5]
  495. * ```
  496. *
  497. * 2. An array of objects with named values. The following snippet shows only a
  498. * few settings, see the complete options set below. If the total number of data
  499. * points exceeds the series' [turboThreshold](#series.funnel.turboThreshold),
  500. * this option is not available.
  501. *
  502. * ```js
  503. * data: [{
  504. * y: 3,
  505. * name: "Point2",
  506. * color: "#00FF00"
  507. * }, {
  508. * y: 1,
  509. * name: "Point1",
  510. * color: "#FF00FF"
  511. * }]
  512. * ```
  513. *
  514. * @sample {highcharts} highcharts/chart/reflow-true/
  515. * Numerical values
  516. * @sample {highcharts} highcharts/series/data-array-of-arrays/
  517. * Arrays of numeric x and y
  518. * @sample {highcharts} highcharts/series/data-array-of-arrays-datetime/
  519. * Arrays of datetime x and y
  520. * @sample {highcharts} highcharts/series/data-array-of-name-value/
  521. * Arrays of point.name and y
  522. * @sample {highcharts} highcharts/series/data-array-of-objects/
  523. * Config objects
  524. *
  525. * @type {Array<number|null|*>}
  526. * @extends series.pie.data
  527. * @excluding sliced
  528. * @product highcharts
  529. * @apioption series.funnel.data
  530. */
  531. /**
  532. * Pyramid series type.
  533. *
  534. * @private
  535. * @class
  536. * @name Highcharts.seriesTypes.pyramid
  537. *
  538. * @augments Highcharts.Series
  539. */
  540. seriesType('pyramid', 'funnel',
  541. /**
  542. * A pyramid series is a special type of funnel, without neck and reversed
  543. * by default.
  544. *
  545. * @sample highcharts/demo/pyramid/
  546. * Pyramid chart
  547. *
  548. * @extends plotOptions.funnel
  549. * @product highcharts
  550. * @requires modules/funnel
  551. * @optionparent plotOptions.pyramid
  552. */
  553. {
  554. /**
  555. * The pyramid neck width is zero by default, as opposed to the funnel,
  556. * which shares the same layout logic.
  557. *
  558. * @since 3.0.10
  559. */
  560. neckWidth: '0%',
  561. /**
  562. * The pyramid neck width is zero by default, as opposed to the funnel,
  563. * which shares the same layout logic.
  564. *
  565. * @since 3.0.10
  566. */
  567. neckHeight: '0%',
  568. /**
  569. * The pyramid is reversed by default, as opposed to the funnel, which
  570. * shares the layout engine, and is not reversed.
  571. *
  572. * @since 3.0.10
  573. */
  574. reversed: true
  575. });
  576. /**
  577. * A `pyramid` series. If the [type](#series.pyramid.type) option is
  578. * not specified, it is inherited from [chart.type](#chart.type).
  579. *
  580. * @extends series,plotOptions.pyramid
  581. * @excluding dataParser, dataURL, stack, xAxis, yAxis, dataSorting,
  582. * boostThreshold, boostBlending
  583. * @product highcharts
  584. * @requires modules/funnel
  585. * @apioption series.pyramid
  586. */
  587. /**
  588. * An array of data points for the series. For the `pyramid` series
  589. * type, points can be given in the following ways:
  590. *
  591. * 1. An array of numerical values. In this case, the numerical values will be
  592. * interpreted as `y` options. Example:
  593. * ```js
  594. * data: [0, 5, 3, 5]
  595. * ```
  596. *
  597. * 2. An array of objects with named values. The following snippet shows only a
  598. * few settings, see the complete options set below. If the total number of
  599. * data points exceeds the series'
  600. * [turboThreshold](#series.pyramid.turboThreshold), this option is not
  601. * available.
  602. * ```js
  603. * data: [{
  604. * y: 9,
  605. * name: "Point2",
  606. * color: "#00FF00"
  607. * }, {
  608. * y: 6,
  609. * name: "Point1",
  610. * color: "#FF00FF"
  611. * }]
  612. * ```
  613. *
  614. * @sample {highcharts} highcharts/chart/reflow-true/
  615. * Numerical values
  616. * @sample {highcharts} highcharts/series/data-array-of-objects/
  617. * Config objects
  618. *
  619. * @type {Array<number|null|*>}
  620. * @extends series.pie.data
  621. * @excluding sliced
  622. * @product highcharts
  623. * @apioption series.pyramid.data
  624. */
  625. ''; // adds doclets above into transpiled file
  626. });
  627. _registerModule(_modules, 'masters/modules/funnel.src.js', [], function () {
  628. });
  629. }));