WaterfallSeries.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /* *
  2. *
  3. * (c) 2010-2020 Torstein Honsi
  4. *
  5. * License: www.highcharts.com/license
  6. *
  7. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  8. *
  9. * */
  10. 'use strict';
  11. import Axis from '../Core/Axis/Axis.js';
  12. import BaseSeries from '../Core/Series/Series.js';
  13. var seriesTypes = BaseSeries.seriesTypes;
  14. import Chart from '../Core/Chart/Chart.js';
  15. import H from '../Core/Globals.js';
  16. import Point from '../Core/Series/Point.js';
  17. import StackItem from '../Extensions/Stacking.js';
  18. import U from '../Core/Utilities.js';
  19. var addEvent = U.addEvent, arrayMax = U.arrayMax, arrayMin = U.arrayMin, correctFloat = U.correctFloat, isNumber = U.isNumber, objectEach = U.objectEach, pick = U.pick;
  20. import '../Core/Options.js';
  21. import './ColumnSeries.js';
  22. var Series = H.Series;
  23. /**
  24. * Returns true if the key is a direct property of the object.
  25. * @private
  26. * @param {*} obj - Object with property to test
  27. * @param {string} key - Property key to test
  28. * @return {boolean} - Whether it is a direct property
  29. */
  30. function ownProp(obj, key) {
  31. return Object.hasOwnProperty.call(obj, key);
  32. }
  33. /**
  34. * @private
  35. */
  36. var WaterfallAxis;
  37. (function (WaterfallAxis) {
  38. /* *
  39. *
  40. * Interfaces
  41. *
  42. * */
  43. /* *
  44. *
  45. * Classes
  46. *
  47. * */
  48. /**
  49. * @private
  50. */
  51. var Composition = /** @class */ (function () {
  52. /* *
  53. *
  54. * Constructors
  55. *
  56. * */
  57. /**
  58. * @private
  59. */
  60. function Composition(axis) {
  61. this.axis = axis;
  62. this.stacks = {
  63. changed: false
  64. };
  65. }
  66. /* *
  67. *
  68. * Functions
  69. *
  70. * */
  71. /**
  72. * Calls StackItem.prototype.render function that creates and renders
  73. * stack total label for each waterfall stack item.
  74. *
  75. * @private
  76. * @function Highcharts.Axis#renderWaterfallStackTotals
  77. */
  78. Composition.prototype.renderStackTotals = function () {
  79. var yAxis = this.axis, waterfallStacks = yAxis.waterfall.stacks, stackTotalGroup = yAxis.stacking && yAxis.stacking.stackTotalGroup, dummyStackItem = new StackItem(yAxis, yAxis.options.stackLabels, false, 0, void 0);
  80. this.dummyStackItem = dummyStackItem;
  81. // Render each waterfall stack total
  82. objectEach(waterfallStacks, function (type) {
  83. objectEach(type, function (stackItem) {
  84. dummyStackItem.total = stackItem.stackTotal;
  85. if (stackItem.label) {
  86. dummyStackItem.label = stackItem.label;
  87. }
  88. StackItem.prototype.render.call(dummyStackItem, stackTotalGroup);
  89. stackItem.label = dummyStackItem.label;
  90. delete dummyStackItem.label;
  91. });
  92. });
  93. dummyStackItem.total = null;
  94. };
  95. return Composition;
  96. }());
  97. WaterfallAxis.Composition = Composition;
  98. /* *
  99. *
  100. * Functions
  101. *
  102. * */
  103. /**
  104. * @private
  105. */
  106. function compose(AxisClass, ChartClass) {
  107. addEvent(AxisClass, 'init', onInit);
  108. addEvent(AxisClass, 'afterBuildStacks', onAfterBuildStacks);
  109. addEvent(AxisClass, 'afterRender', onAfterRender);
  110. addEvent(ChartClass, 'beforeRedraw', onBeforeRedraw);
  111. }
  112. WaterfallAxis.compose = compose;
  113. /**
  114. * @private
  115. */
  116. function onAfterBuildStacks() {
  117. var axis = this;
  118. var stacks = axis.waterfall.stacks;
  119. if (stacks) {
  120. stacks.changed = false;
  121. delete stacks.alreadyChanged;
  122. }
  123. }
  124. /**
  125. * @private
  126. */
  127. function onAfterRender() {
  128. var axis = this;
  129. var stackLabelOptions = axis.options.stackLabels;
  130. if (stackLabelOptions && stackLabelOptions.enabled &&
  131. axis.waterfall.stacks) {
  132. axis.waterfall.renderStackTotals();
  133. }
  134. }
  135. /**
  136. * @private
  137. */
  138. function onBeforeRedraw() {
  139. var axes = this.axes, series = this.series, i = series.length;
  140. while (i--) {
  141. if (series[i].options.stacking) {
  142. axes.forEach(function (axis) {
  143. if (!axis.isXAxis) {
  144. axis.waterfall.stacks.changed = true;
  145. }
  146. });
  147. i = 0;
  148. }
  149. }
  150. }
  151. /**
  152. * @private
  153. */
  154. function onInit() {
  155. var axis = this;
  156. if (!axis.waterfall) {
  157. axis.waterfall = new Composition(axis);
  158. }
  159. }
  160. })(WaterfallAxis || (WaterfallAxis = {}));
  161. // eslint-disable-next-line valid-jsdoc
  162. /**
  163. * A waterfall chart displays sequentially introduced positive or negative
  164. * values in cumulative columns.
  165. *
  166. * @sample highcharts/demo/waterfall/
  167. * Waterfall chart
  168. * @sample highcharts/plotoptions/waterfall-inverted/
  169. * Horizontal (inverted) waterfall
  170. * @sample highcharts/plotoptions/waterfall-stacked/
  171. * Stacked waterfall chart
  172. *
  173. * @extends plotOptions.column
  174. * @excluding boostThreshold, boostBlending
  175. * @product highcharts
  176. * @requires highcharts-more
  177. * @optionparent plotOptions.waterfall
  178. */
  179. BaseSeries.seriesType('waterfall', 'column', {
  180. /**
  181. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  182. * @apioption plotOptions.waterfall.color
  183. */
  184. /**
  185. * The color used specifically for positive point columns. When not
  186. * specified, the general series color is used.
  187. *
  188. * In styled mode, the waterfall colors can be set with the
  189. * `.highcharts-point-negative`, `.highcharts-sum` and
  190. * `.highcharts-intermediate-sum` classes.
  191. *
  192. * @sample {highcharts} highcharts/demo/waterfall/
  193. * Waterfall
  194. *
  195. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  196. * @product highcharts
  197. * @apioption plotOptions.waterfall.upColor
  198. */
  199. dataLabels: {
  200. inside: true
  201. },
  202. /**
  203. * The width of the line connecting waterfall columns.
  204. *
  205. * @product highcharts
  206. */
  207. lineWidth: 1,
  208. /**
  209. * The color of the line that connects columns in a waterfall series.
  210. *
  211. * In styled mode, the stroke can be set with the `.highcharts-graph` class.
  212. *
  213. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  214. * @since 3.0
  215. * @product highcharts
  216. */
  217. lineColor: '#333333',
  218. /**
  219. * A name for the dash style to use for the line connecting the columns
  220. * of the waterfall series. Possible values: Dash, DashDot, Dot, LongDash,
  221. * LongDashDot, LongDashDotDot, ShortDash, ShortDashDot, ShortDashDotDot,
  222. * ShortDot, Solid
  223. *
  224. * In styled mode, the stroke dash-array can be set with the
  225. * `.highcharts-graph` class.
  226. *
  227. * @type {Highcharts.DashStyleValue}
  228. * @since 3.0
  229. * @product highcharts
  230. */
  231. dashStyle: 'Dot',
  232. /**
  233. * The color of the border of each waterfall column.
  234. *
  235. * In styled mode, the border stroke can be set with the
  236. * `.highcharts-point` class.
  237. *
  238. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  239. * @since 3.0
  240. * @product highcharts
  241. */
  242. borderColor: '#333333',
  243. states: {
  244. hover: {
  245. lineWidthPlus: 0 // #3126
  246. }
  247. }
  248. // Prototype members
  249. }, {
  250. pointValKey: 'y',
  251. // Property needed to prevent lines between the columns from disappearing
  252. // when negativeColor is used.
  253. showLine: true,
  254. // After generating points, set y-values for all sums.
  255. generatePoints: function () {
  256. var point, len, i, y;
  257. // Parent call:
  258. seriesTypes.column.prototype.generatePoints.apply(this);
  259. for (i = 0, len = this.points.length; i < len; i++) {
  260. point = this.points[i];
  261. y = this.processedYData[i];
  262. // override point value for sums
  263. // #3710 Update point does not propagate to sum
  264. if (point.isIntermediateSum || point.isSum) {
  265. point.y = correctFloat(y);
  266. }
  267. }
  268. },
  269. // Translate data points from raw values
  270. translate: function () {
  271. var series = this, options = series.options, yAxis = series.yAxis, len, i, points, point, shapeArgs, y, yValue, previousY, previousIntermediate, range, minPointLength = pick(options.minPointLength, 5), halfMinPointLength = minPointLength / 2, threshold = options.threshold, stacking = options.stacking, tooltipY, actualStack = yAxis.waterfall.stacks[series.stackKey], actualStackX, dummyStackItem, total, pointY, yPos, hPos;
  272. // run column series translate
  273. seriesTypes.column.prototype.translate.apply(series);
  274. previousY = previousIntermediate = threshold;
  275. points = series.points;
  276. for (i = 0, len = points.length; i < len; i++) {
  277. // cache current point object
  278. point = points[i];
  279. yValue = series.processedYData[i];
  280. shapeArgs = point.shapeArgs;
  281. range = [0, yValue];
  282. pointY = point.y;
  283. // code responsible for correct positions of stacked points
  284. // starts here
  285. if (stacking) {
  286. if (actualStack) {
  287. actualStackX = actualStack[i];
  288. if (stacking === 'overlap') {
  289. total =
  290. actualStackX.stackState[actualStackX.stateIndex--];
  291. y = pointY >= 0 ? total : total - pointY;
  292. if (ownProp(actualStackX, 'absolutePos')) {
  293. delete actualStackX.absolutePos;
  294. }
  295. if (ownProp(actualStackX, 'absoluteNeg')) {
  296. delete actualStackX.absoluteNeg;
  297. }
  298. }
  299. else {
  300. if (pointY >= 0) {
  301. total = actualStackX.threshold +
  302. actualStackX.posTotal;
  303. actualStackX.posTotal -= pointY;
  304. y = total;
  305. }
  306. else {
  307. total = actualStackX.threshold +
  308. actualStackX.negTotal;
  309. actualStackX.negTotal -= pointY;
  310. y = total - pointY;
  311. }
  312. if (!actualStackX.posTotal) {
  313. if (ownProp(actualStackX, 'absolutePos')) {
  314. actualStackX.posTotal =
  315. actualStackX.absolutePos;
  316. delete actualStackX.absolutePos;
  317. }
  318. }
  319. if (!actualStackX.negTotal) {
  320. if (ownProp(actualStackX, 'absoluteNeg')) {
  321. actualStackX.negTotal =
  322. actualStackX.absoluteNeg;
  323. delete actualStackX.absoluteNeg;
  324. }
  325. }
  326. }
  327. if (!point.isSum) {
  328. // the connectorThreshold property is later used in
  329. // getCrispPath function to draw a connector line in a
  330. // correct place
  331. actualStackX.connectorThreshold =
  332. actualStackX.threshold + actualStackX.stackTotal;
  333. }
  334. if (yAxis.reversed) {
  335. yPos = (pointY >= 0) ? (y - pointY) : (y + pointY);
  336. hPos = y;
  337. }
  338. else {
  339. yPos = y;
  340. hPos = y - pointY;
  341. }
  342. point.below = yPos <= pick(threshold, 0);
  343. shapeArgs.y = yAxis.translate(yPos, 0, 1, 0, 1);
  344. shapeArgs.height = Math.abs(shapeArgs.y -
  345. yAxis.translate(hPos, 0, 1, 0, 1));
  346. }
  347. dummyStackItem = yAxis.waterfall.dummyStackItem;
  348. if (dummyStackItem) {
  349. dummyStackItem.x = i;
  350. dummyStackItem.label = actualStack[i].label;
  351. dummyStackItem.setOffset(series.pointXOffset || 0, series.barW || 0, series.stackedYNeg[i], series.stackedYPos[i]);
  352. }
  353. }
  354. else {
  355. // up points
  356. y =
  357. Math.max(previousY, previousY + pointY) + range[0];
  358. shapeArgs.y =
  359. yAxis.translate(y, 0, 1, 0, 1);
  360. // sum points
  361. if (point.isSum) {
  362. shapeArgs.y = yAxis.translate(range[1], 0, 1, 0, 1);
  363. shapeArgs.height = Math.min(yAxis.translate(range[0], 0, 1, 0, 1), yAxis.len) - shapeArgs.y; // #4256
  364. }
  365. else if (point.isIntermediateSum) {
  366. if (pointY >= 0) {
  367. yPos = range[1] + previousIntermediate;
  368. hPos = previousIntermediate;
  369. }
  370. else {
  371. yPos = previousIntermediate;
  372. hPos = range[1] + previousIntermediate;
  373. }
  374. if (yAxis.reversed) {
  375. // swapping values
  376. yPos ^= hPos;
  377. hPos ^= yPos;
  378. yPos ^= hPos;
  379. }
  380. shapeArgs.y = yAxis.translate(yPos, 0, 1, 0, 1);
  381. shapeArgs.height = Math.abs(shapeArgs.y -
  382. Math.min(yAxis.translate(hPos, 0, 1, 0, 1), yAxis.len));
  383. previousIntermediate += range[1];
  384. // If it's not the sum point, update previous stack end position
  385. // and get shape height (#3886)
  386. }
  387. else {
  388. shapeArgs.height = yValue > 0 ?
  389. yAxis.translate(previousY, 0, 1, 0, 1) - shapeArgs.y :
  390. yAxis.translate(previousY, 0, 1, 0, 1) - yAxis.translate(previousY - yValue, 0, 1, 0, 1);
  391. previousY += yValue;
  392. point.below = previousY < pick(threshold, 0);
  393. }
  394. // #3952 Negative sum or intermediate sum not rendered correctly
  395. if (shapeArgs.height < 0) {
  396. shapeArgs.y += shapeArgs.height;
  397. shapeArgs.height *= -1;
  398. }
  399. }
  400. point.plotY = shapeArgs.y =
  401. Math.round(shapeArgs.y) - (series.borderWidth % 2) / 2;
  402. // #3151
  403. shapeArgs.height =
  404. Math.max(Math.round(shapeArgs.height), 0.001);
  405. point.yBottom = shapeArgs.y + shapeArgs.height;
  406. if (shapeArgs.height <= minPointLength && !point.isNull) {
  407. shapeArgs.height = minPointLength;
  408. shapeArgs.y -= halfMinPointLength;
  409. point.plotY = shapeArgs.y;
  410. if (point.y < 0) {
  411. point.minPointLengthOffset = -halfMinPointLength;
  412. }
  413. else {
  414. point.minPointLengthOffset = halfMinPointLength;
  415. }
  416. }
  417. else {
  418. if (point.isNull) {
  419. shapeArgs.width = 0;
  420. }
  421. point.minPointLengthOffset = 0;
  422. }
  423. // Correct tooltip placement (#3014)
  424. tooltipY =
  425. point.plotY + (point.negative ? shapeArgs.height : 0);
  426. if (series.chart.inverted) {
  427. point.tooltipPos[0] = yAxis.len - tooltipY;
  428. }
  429. else {
  430. point.tooltipPos[1] = tooltipY;
  431. }
  432. }
  433. },
  434. // Call default processData then override yData to reflect waterfall's
  435. // extremes on yAxis
  436. processData: function (force) {
  437. var series = this, options = series.options, yData = series.yData,
  438. // #3710 Update point does not propagate to sum
  439. points = options.data, point, dataLength = yData.length, threshold = options.threshold || 0, subSum, sum, dataMin, dataMax, y, i;
  440. sum = subSum = dataMin = dataMax = 0;
  441. for (i = 0; i < dataLength; i++) {
  442. y = yData[i];
  443. point = points && points[i] ? points[i] : {};
  444. if (y === 'sum' || point.isSum) {
  445. yData[i] = correctFloat(sum);
  446. }
  447. else if (y === 'intermediateSum' ||
  448. point.isIntermediateSum) {
  449. yData[i] = correctFloat(subSum);
  450. subSum = 0;
  451. }
  452. else {
  453. sum += y;
  454. subSum += y;
  455. }
  456. dataMin = Math.min(sum, dataMin);
  457. dataMax = Math.max(sum, dataMax);
  458. }
  459. Series.prototype.processData.call(this, force);
  460. // Record extremes only if stacking was not set:
  461. if (!options.stacking) {
  462. series.dataMin = dataMin + threshold;
  463. series.dataMax = dataMax;
  464. }
  465. return;
  466. },
  467. // Return y value or string if point is sum
  468. toYData: function (pt) {
  469. if (pt.isSum) {
  470. return 'sum';
  471. }
  472. if (pt.isIntermediateSum) {
  473. return 'intermediateSum';
  474. }
  475. return pt.y;
  476. },
  477. updateParallelArrays: function (point, i) {
  478. Series.prototype.updateParallelArrays.call(this, point, i);
  479. // Prevent initial sums from triggering an error (#3245, #7559)
  480. if (this.yData[0] === 'sum' || this.yData[0] === 'intermediateSum') {
  481. this.yData[0] = null;
  482. }
  483. },
  484. // Postprocess mapping between options and SVG attributes
  485. pointAttribs: function (point, state) {
  486. var upColor = this.options.upColor, attr;
  487. // Set or reset up color (#3710, update to negative)
  488. if (upColor && !point.options.color) {
  489. point.color = point.y > 0 ? upColor : null;
  490. }
  491. attr = seriesTypes.column.prototype.pointAttribs.call(this, point, state);
  492. // The dashStyle option in waterfall applies to the graph, not
  493. // the points
  494. delete attr.dashstyle;
  495. return attr;
  496. },
  497. // Return an empty path initially, because we need to know the stroke-width
  498. // in order to set the final path.
  499. getGraphPath: function () {
  500. return [['M', 0, 0]];
  501. },
  502. // Draw columns' connector lines
  503. getCrispPath: function () {
  504. var data = this.data, yAxis = this.yAxis, length = data.length, graphNormalizer = Math.round(this.graph.strokeWidth()) % 2 / 2, borderNormalizer = Math.round(this.borderWidth) % 2 / 2, reversedXAxis = this.xAxis.reversed, reversedYAxis = this.yAxis.reversed, stacking = this.options.stacking, path = [], connectorThreshold, prevStack, prevStackX, prevPoint, yPos, isPos, prevArgs, pointArgs, i;
  505. for (i = 1; i < length; i++) {
  506. pointArgs = data[i].shapeArgs;
  507. prevPoint = data[i - 1];
  508. prevArgs = data[i - 1].shapeArgs;
  509. prevStack = yAxis.waterfall.stacks[this.stackKey];
  510. isPos = prevPoint.y > 0 ? -prevArgs.height : 0;
  511. if (prevStack && prevArgs && pointArgs) {
  512. prevStackX = prevStack[i - 1];
  513. // y position of the connector is different when series are
  514. // stacked, yAxis is reversed and it also depends on point's
  515. // value
  516. if (stacking) {
  517. connectorThreshold = prevStackX.connectorThreshold;
  518. yPos = Math.round((yAxis.translate(connectorThreshold, 0, 1, 0, 1) +
  519. (reversedYAxis ? isPos : 0))) - graphNormalizer;
  520. }
  521. else {
  522. yPos =
  523. prevArgs.y + prevPoint.minPointLengthOffset +
  524. borderNormalizer - graphNormalizer;
  525. }
  526. path.push([
  527. 'M',
  528. (prevArgs.x || 0) + (reversedXAxis ?
  529. 0 :
  530. (prevArgs.width || 0)),
  531. yPos
  532. ], [
  533. 'L',
  534. (pointArgs.x || 0) + (reversedXAxis ?
  535. (pointArgs.width || 0) :
  536. 0),
  537. yPos
  538. ]);
  539. }
  540. if (!stacking &&
  541. path.length &&
  542. prevArgs &&
  543. ((prevPoint.y < 0 && !reversedYAxis) ||
  544. (prevPoint.y > 0 && reversedYAxis))) {
  545. path[path.length - 2][2] += prevArgs.height;
  546. path[path.length - 1][2] += prevArgs.height;
  547. }
  548. }
  549. return path;
  550. },
  551. // The graph is initially drawn with an empty definition, then updated with
  552. // crisp rendering.
  553. drawGraph: function () {
  554. Series.prototype.drawGraph.call(this);
  555. this.graph.attr({
  556. d: this.getCrispPath()
  557. });
  558. },
  559. // Waterfall has stacking along the x-values too.
  560. setStackedPoints: function () {
  561. var series = this, options = series.options, waterfallStacks = series.yAxis.waterfall.stacks, seriesThreshold = options.threshold, stackThreshold = seriesThreshold || 0, interSum = stackThreshold, stackKey = series.stackKey, xData = series.xData, xLength = xData.length, actualStack, actualStackX, totalYVal, actualSum, prevSum, statesLen, posTotal, negTotal, xPoint, yVal, x, alreadyChanged, changed;
  562. // function responsible for calculating correct values for stackState
  563. // array of each stack item. The arguments are: firstS - the value for
  564. // the first state, nextS - the difference between the previous and the
  565. // newest state, sInx - counter used in the for that updates each state
  566. // when necessary, sOff - offset that must be added to each state when
  567. // they need to be updated (if point isn't a total sum)
  568. // eslint-disable-next-line require-jsdoc
  569. function calculateStackState(firstS, nextS, sInx, sOff) {
  570. if (!statesLen) {
  571. actualStackX.stackState[0] = firstS;
  572. statesLen = actualStackX.stackState.length;
  573. }
  574. else {
  575. for (sInx; sInx < statesLen; sInx++) {
  576. actualStackX.stackState[sInx] += sOff;
  577. }
  578. }
  579. actualStackX.stackState.push(actualStackX.stackState[statesLen - 1] + nextS);
  580. }
  581. series.yAxis.stacking.usePercentage = false;
  582. totalYVal = actualSum = prevSum = stackThreshold;
  583. // code responsible for creating stacks for waterfall series
  584. if (series.visible ||
  585. !series.chart.options.chart.ignoreHiddenSeries) {
  586. changed = waterfallStacks.changed;
  587. alreadyChanged = waterfallStacks.alreadyChanged;
  588. // in case of a redraw, stack for each x value must be
  589. // emptied (only for the first series in a specific stack)
  590. // and recalculated once more
  591. if (alreadyChanged &&
  592. alreadyChanged.indexOf(stackKey) < 0) {
  593. changed = true;
  594. }
  595. if (!waterfallStacks[stackKey]) {
  596. waterfallStacks[stackKey] = {};
  597. }
  598. actualStack = waterfallStacks[stackKey];
  599. for (var i = 0; i < xLength; i++) {
  600. x = xData[i];
  601. if (!actualStack[x] || changed) {
  602. actualStack[x] = {
  603. negTotal: 0,
  604. posTotal: 0,
  605. stackTotal: 0,
  606. threshold: 0,
  607. stateIndex: 0,
  608. stackState: [],
  609. label: ((changed &&
  610. actualStack[x]) ?
  611. actualStack[x].label :
  612. void 0)
  613. };
  614. }
  615. actualStackX = actualStack[x];
  616. yVal = series.yData[i];
  617. if (yVal >= 0) {
  618. actualStackX.posTotal += yVal;
  619. }
  620. else {
  621. actualStackX.negTotal += yVal;
  622. }
  623. // points do not exist yet, so raw data is used
  624. xPoint = options.data[i];
  625. posTotal = actualStackX.absolutePos =
  626. actualStackX.posTotal;
  627. negTotal = actualStackX.absoluteNeg =
  628. actualStackX.negTotal;
  629. actualStackX.stackTotal = posTotal + negTotal;
  630. statesLen = actualStackX.stackState.length;
  631. if (xPoint && xPoint.isIntermediateSum) {
  632. calculateStackState(prevSum, actualSum, 0, prevSum);
  633. prevSum = actualSum;
  634. actualSum = seriesThreshold;
  635. // swapping values
  636. stackThreshold ^= interSum;
  637. interSum ^= stackThreshold;
  638. stackThreshold ^= interSum;
  639. }
  640. else if (xPoint && xPoint.isSum) {
  641. calculateStackState(seriesThreshold, totalYVal, statesLen);
  642. stackThreshold = seriesThreshold;
  643. }
  644. else {
  645. calculateStackState(stackThreshold, yVal, 0, totalYVal);
  646. if (xPoint) {
  647. totalYVal += yVal;
  648. actualSum += yVal;
  649. }
  650. }
  651. actualStackX.stateIndex++;
  652. actualStackX.threshold = stackThreshold;
  653. stackThreshold += actualStackX.stackTotal;
  654. }
  655. waterfallStacks.changed = false;
  656. if (!waterfallStacks.alreadyChanged) {
  657. waterfallStacks.alreadyChanged = [];
  658. }
  659. waterfallStacks.alreadyChanged.push(stackKey);
  660. }
  661. },
  662. // Extremes for a non-stacked series are recorded in processData.
  663. // In case of stacking, use Series.stackedYData to calculate extremes.
  664. getExtremes: function () {
  665. var stacking = this.options.stacking, yAxis, waterfallStacks, stackedYNeg, stackedYPos;
  666. if (stacking) {
  667. yAxis = this.yAxis;
  668. waterfallStacks = yAxis.waterfall.stacks;
  669. stackedYNeg = this.stackedYNeg = [];
  670. stackedYPos = this.stackedYPos = [];
  671. // the visible y range can be different when stacking is set to
  672. // overlap and different when it's set to normal
  673. if (stacking === 'overlap') {
  674. objectEach(waterfallStacks[this.stackKey], function (stackX) {
  675. stackedYNeg.push(arrayMin(stackX.stackState));
  676. stackedYPos.push(arrayMax(stackX.stackState));
  677. });
  678. }
  679. else {
  680. objectEach(waterfallStacks[this.stackKey], function (stackX) {
  681. stackedYNeg.push(stackX.negTotal + stackX.threshold);
  682. stackedYPos.push(stackX.posTotal + stackX.threshold);
  683. });
  684. }
  685. return {
  686. dataMin: arrayMin(stackedYNeg),
  687. dataMax: arrayMax(stackedYPos)
  688. };
  689. }
  690. // When not stacking, data extremes have already been computed in the
  691. // processData function.
  692. return {
  693. dataMin: this.dataMin,
  694. dataMax: this.dataMax
  695. };
  696. }
  697. // Point members
  698. }, {
  699. getClassName: function () {
  700. var className = Point.prototype.getClassName.call(this);
  701. if (this.isSum) {
  702. className += ' highcharts-sum';
  703. }
  704. else if (this.isIntermediateSum) {
  705. className += ' highcharts-intermediate-sum';
  706. }
  707. return className;
  708. },
  709. // Pass the null test in ColumnSeries.translate.
  710. isValid: function () {
  711. return (isNumber(this.y) ||
  712. this.isSum ||
  713. Boolean(this.isIntermediateSum));
  714. }
  715. });
  716. /**
  717. * A `waterfall` series. If the [type](#series.waterfall.type) option
  718. * is not specified, it is inherited from [chart.type](#chart.type).
  719. *
  720. * @extends series,plotOptions.waterfall
  721. * @excluding dataParser, dataURL, boostThreshold, boostBlending
  722. * @product highcharts
  723. * @requires highcharts-more
  724. * @apioption series.waterfall
  725. */
  726. /**
  727. * An array of data points for the series. For the `waterfall` series
  728. * type, points can be given in the following ways:
  729. *
  730. * 1. An array of numerical values. In this case, the numerical values will be
  731. * interpreted as `y` options. The `x` values will be automatically
  732. * calculated, either starting at 0 and incremented by 1, or from
  733. * `pointStart` and `pointInterval` given in the series options. If the axis
  734. * has categories, these will be used. Example:
  735. * ```js
  736. * data: [0, 5, 3, 5]
  737. * ```
  738. *
  739. * 2. An array of arrays with 2 values. In this case, the values correspond to
  740. * `x,y`. If the first value is a string, it is applied as the name of the
  741. * point, and the `x` value is inferred.
  742. * ```js
  743. * data: [
  744. * [0, 7],
  745. * [1, 8],
  746. * [2, 3]
  747. * ]
  748. * ```
  749. *
  750. * 3. An array of objects with named values. The following snippet shows only a
  751. * few settings, see the complete options set below. If the total number of
  752. * data points exceeds the series'
  753. * [turboThreshold](#series.waterfall.turboThreshold), this option is not
  754. * available.
  755. * ```js
  756. * data: [{
  757. * x: 1,
  758. * y: 8,
  759. * name: "Point2",
  760. * color: "#00FF00"
  761. * }, {
  762. * x: 1,
  763. * y: 8,
  764. * name: "Point1",
  765. * color: "#FF00FF"
  766. * }]
  767. * ```
  768. *
  769. * @sample {highcharts} highcharts/chart/reflow-true/
  770. * Numerical values
  771. * @sample {highcharts} highcharts/series/data-array-of-arrays/
  772. * Arrays of numeric x and y
  773. * @sample {highcharts} highcharts/series/data-array-of-arrays-datetime/
  774. * Arrays of datetime x and y
  775. * @sample {highcharts} highcharts/series/data-array-of-name-value/
  776. * Arrays of point.name and y
  777. * @sample {highcharts} highcharts/series/data-array-of-objects/
  778. * Config objects
  779. *
  780. * @type {Array<number|Array<(number|string),(number|null)>|null|*>}
  781. * @extends series.line.data
  782. * @excluding marker
  783. * @product highcharts
  784. * @apioption series.waterfall.data
  785. */
  786. /**
  787. * When this property is true, the points acts as a summary column for
  788. * the values added or substracted since the last intermediate sum,
  789. * or since the start of the series. The `y` value is ignored.
  790. *
  791. * @sample {highcharts} highcharts/demo/waterfall/
  792. * Waterfall
  793. *
  794. * @type {boolean}
  795. * @default false
  796. * @product highcharts
  797. * @apioption series.waterfall.data.isIntermediateSum
  798. */
  799. /**
  800. * When this property is true, the point display the total sum across
  801. * the entire series. The `y` value is ignored.
  802. *
  803. * @sample {highcharts} highcharts/demo/waterfall/
  804. * Waterfall
  805. *
  806. * @type {boolean}
  807. * @default false
  808. * @product highcharts
  809. * @apioption series.waterfall.data.isSum
  810. */
  811. ''; // adds doclets above to transpiled file
  812. WaterfallAxis.compose(Axis, Chart);
  813. export default WaterfallAxis;