FunnelSeries.js 18 KB

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