FunnelSeries.js 18 KB

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