WaterfallSeries.js 16.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
/**
 * (c) 2010-2018 Torstein Honsi
 *
 * License: www.highcharts.com/license
 */
'use strict';
import H from '../parts/Globals.js';
import '../parts/Utilities.js';
import '../parts/Options.js';
import '../parts/Series.js';
import '../parts/Point.js';
var correctFloat = H.correctFloat,
    isNumber = H.isNumber,
    pick = H.pick,
    Point = H.Point,
    Series = H.Series,
    seriesType = H.seriesType,
    seriesTypes = H.seriesTypes;

/**
 * A waterfall chart displays sequentially introduced positive or negative
 * values in cumulative columns.
 *
 * @sample       highcharts/demo/waterfall/
 *               Waterfall chart
 * @sample       highcharts/plotoptions/waterfall-inverted/
 *               Horizontal (inverted) waterfall
 * @sample       highcharts/plotoptions/waterfall-stacked/
 *               Stacked waterfall chart
 * @extends      {plotOptions.column}
 * @product      highcharts
 * @optionparent plotOptions.waterfall
 */
seriesType('waterfall', 'column', {

    /**
     * The color used specifically for positive point columns. When not
     * specified, the general series color is used.
     *
     * In styled mode, the waterfall colors can be set with the
     * `.highcharts-point-negative`, `.highcharts-sum` and
     * `.highcharts-intermediate-sum` classes.
     *
     * @type      {Color}
     * @sample    {highcharts} highcharts/demo/waterfall/ Waterfall
     * @product   highcharts
     * @apioption plotOptions.waterfall.upColor
     */

    dataLabels: {
        inside: true
    },
    

    /**
     * The width of the line connecting waterfall columns.
     *
     * @product highcharts
     */
    lineWidth: 1,

    /**
     * The color of the line that connects columns in a waterfall series.
     *
     * In styled mode, the stroke can be set with the `.highcharts-graph` class.
     *
     * @type    {Color}
     * @default #333333
     * @since   3.0
     * @product highcharts
     */
    lineColor: '#333333',

    /**
     * A name for the dash style to use for the line connecting the columns
     * of the waterfall series. Possible values:
     *
     * *   Solid
     * *   ShortDash
     * *   ShortDot
     * *   ShortDashDot
     * *   ShortDashDotDot
     * *   Dot
     * *   Dash
     * *   LongDash
     * *   DashDot
     * *   LongDashDot
     * *   LongDashDotDot
     *
     * In styled mode, the stroke dash-array can be set with the
     * `.highcharts-graph` class.
     *
     * @type    {String}
     * @default Dot
     * @since   3.0
     * @product highcharts
     */
    dashStyle: 'dot',

    /**
     * The color of the border of each waterfall column.
     *
     * In styled mode, the border stroke can be set with the
     * `.highcharts-point` class.
     *
     * @type    {Color}
     * @default #333333
     * @since   3.0
     * @product highcharts
     */
    borderColor: '#333333',

    states: {
        hover: {
            lineWidthPlus: 0 // #3126
        }
    }
    

// Prototype members
}, {
    pointValKey: 'y',

    /**
     * Property needed to prevent lines between the columns from disappearing
     * when negativeColor is used.
     */
    showLine: true,

    /**
     * After generating points, set y-values for all sums.
     */
    generatePoints: function () {
        var previousIntermediate = this.options.threshold,
            point,
            len,
            i,
            y;
        // Parent call:
        seriesTypes.column.prototype.generatePoints.apply(this);

        for (i = 0, len = this.points.length; i < len; i++) {
            point = this.points[i];
            y = this.processedYData[i];
            // override point value for sums
            // #3710 Update point does not propagate to sum
            if (point.isSum) {
                point.y = correctFloat(y);
            } else if (point.isIntermediateSum) {
                point.y = correctFloat(y - previousIntermediate); // #3840
                previousIntermediate = y;
            }
        }
    },

    /**
     * Translate data points from raw values
     */
    translate: function () {
        var series = this,
            options = series.options,
            yAxis = series.yAxis,
            len,
            i,
            points,
            point,
            shapeArgs,
            stack,
            y,
            yValue,
            previousY,
            previousIntermediate,
            range,
            minPointLength = pick(options.minPointLength, 5),
            halfMinPointLength = minPointLength / 2,
            threshold = options.threshold,
            stacking = options.stacking,
            stackIndicator,
            tooltipY;

        // run column series translate
        seriesTypes.column.prototype.translate.apply(series);

        previousY = previousIntermediate = threshold;
        points = series.points;

        for (i = 0, len = points.length; i < len; i++) {
            // cache current point object
            point = points[i];
            yValue = series.processedYData[i];
            shapeArgs = point.shapeArgs;

            // get current stack
            stack = stacking &&
                yAxis.stacks[
                    (series.negStacks && yValue < threshold ? '-' : '') +
                        series.stackKey
                ];
            stackIndicator = series.getStackIndicator(
                stackIndicator,
                point.x,
                series.index
            );
            range = pick(
                stack && stack[point.x].points[stackIndicator.key],
                [0, yValue]
            );

            // up points
            y = Math.max(previousY, previousY + point.y) + range[0];
            shapeArgs.y = yAxis.translate(y, 0, 1, 0, 1);

            // sum points
            if (point.isSum) {
                shapeArgs.y = yAxis.translate(range[1], 0, 1, 0, 1);
                shapeArgs.height = Math.min(
                        yAxis.translate(range[0], 0, 1, 0, 1),
                        yAxis.len
                    ) - shapeArgs.y; // #4256

            } else if (point.isIntermediateSum) {
                shapeArgs.y = yAxis.translate(range[1], 0, 1, 0, 1);
                shapeArgs.height = Math.min(
                        yAxis.translate(previousIntermediate, 0, 1, 0, 1),
                        yAxis.len
                    ) - shapeArgs.y;
                previousIntermediate = range[1];

            // If it's not the sum point, update previous stack end position
            // and get shape height (#3886)
            } else {
                shapeArgs.height = yValue > 0 ?
                    yAxis.translate(previousY, 0, 1, 0, 1) - shapeArgs.y :
                    yAxis.translate(previousY, 0, 1, 0, 1) -
                        yAxis.translate(previousY - yValue, 0, 1, 0, 1);

                previousY += stack && stack[point.x] ?
                    stack[point.x].total :
                    yValue;

                point.below = previousY < pick(threshold, 0);
            }

            // #3952 Negative sum or intermediate sum not rendered correctly
            if (shapeArgs.height < 0) {
                shapeArgs.y += shapeArgs.height;
                shapeArgs.height *= -1;
            }

            point.plotY = shapeArgs.y = Math.round(shapeArgs.y) -
                (series.borderWidth % 2) / 2;
            // #3151
            shapeArgs.height = Math.max(Math.round(shapeArgs.height), 0.001);
            point.yBottom = shapeArgs.y + shapeArgs.height;

            if (shapeArgs.height <= minPointLength && !point.isNull) {
                shapeArgs.height = minPointLength;
                shapeArgs.y -= halfMinPointLength;
                point.plotY = shapeArgs.y;
                if (point.y < 0) {
                    point.minPointLengthOffset = -halfMinPointLength;
                } else {
                    point.minPointLengthOffset = halfMinPointLength;
                }
            } else {
                if (point.isNull) {
                    shapeArgs.width = 0;
                }
                point.minPointLengthOffset = 0;
            }

            // Correct tooltip placement (#3014)
            tooltipY = point.plotY + (point.negative ? shapeArgs.height : 0);

            if (series.chart.inverted) {
                point.tooltipPos[0] = yAxis.len - tooltipY;
            } else {
                point.tooltipPos[1] = tooltipY;
            }
        }
    },

    /**
     * Call default processData then override yData to reflect
     * waterfall's extremes on yAxis
     */
    processData: function (force) {
        var series = this,
            options = series.options,
            yData = series.yData,
            // #3710 Update point does not propagate to sum
            points = series.options.data,
            point,
            dataLength = yData.length,
            threshold = options.threshold || 0,
            subSum,
            sum,
            dataMin,
            dataMax,
            y,
            i;

        sum = subSum = dataMin = dataMax = threshold;

        for (i = 0; i < dataLength; i++) {
            y = yData[i];
            point = points && points[i] ? points[i] : {};

            if (y === 'sum' || point.isSum) {
                yData[i] = correctFloat(sum);
            } else if (y === 'intermediateSum' || point.isIntermediateSum) {
                yData[i] = correctFloat(subSum);
            } else {
                sum += y;
                subSum += y;
            }
            dataMin = Math.min(sum, dataMin);
            dataMax = Math.max(sum, dataMax);
        }

        Series.prototype.processData.call(this, force);

        // Record extremes only if stacking was not set:
        if (!series.options.stacking) {
            series.dataMin = dataMin;
            series.dataMax = dataMax;
        }
    },

    /**
     * Return y value or string if point is sum
     */
    toYData: function (pt) {
        if (pt.isSum) {
            // #3245 Error when first element is Sum or Intermediate Sum
            return (pt.x === 0 ? null : 'sum');
        }
        if (pt.isIntermediateSum) {
            return (pt.x === 0 ? null : 'intermediateSum'); // #3245
        }
        return pt.y;
    },

    
    /**
     * Postprocess mapping between options and SVG attributes
     */
    pointAttribs: function (point, state) {

        var upColor = this.options.upColor,
            attr;

        // Set or reset up color (#3710, update to negative)
        if (upColor && !point.options.color) {
            point.color = point.y > 0 ? upColor : null;
        }

        attr = seriesTypes.column.prototype.pointAttribs.call(
                this,
                point,
                state
            );

        // The dashStyle option in waterfall applies to the graph, not
        // the points
        delete attr.dashstyle;

        return attr;
    },
    

    /**
     * Return an empty path initially, because we need to know the
     * stroke-width in order to set the final path.
     */
    getGraphPath: function () {
        return ['M', 0, 0];
    },

    /**
     * Draw columns' connector lines
     */
    getCrispPath: function () {

        var data = this.data,
            length = data.length,
            lineWidth = this.graph.strokeWidth() + this.borderWidth,
            normalizer = Math.round(lineWidth) % 2 / 2,
            reversedXAxis = this.xAxis.reversed,
            reversedYAxis = this.yAxis.reversed,
            path = [],
            prevArgs,
            pointArgs,
            i,
            d;

        for (i = 1; i < length; i++) {
            pointArgs = data[i].shapeArgs;
            prevArgs = data[i - 1].shapeArgs;

            d = [
                'M',
                prevArgs.x + (reversedXAxis ? 0 : prevArgs.width),
                prevArgs.y + data[i - 1].minPointLengthOffset + normalizer,
                'L',
                pointArgs.x + (reversedXAxis ? prevArgs.width : 0),
                prevArgs.y + data[i - 1].minPointLengthOffset + normalizer
            ];

            if (
                (data[i - 1].y < 0 && !reversedYAxis) ||
                (data[i - 1].y > 0 && reversedYAxis)
            ) {
                d[2] += prevArgs.height;
                d[5] += prevArgs.height;
            }

            path = path.concat(d);
        }

        return path;
    },

    /**
     * The graph is initially drawn with an empty definition, then updated with
     * crisp rendering.
     */
    drawGraph: function () {
        Series.prototype.drawGraph.call(this);
        this.graph.attr({
            d: this.getCrispPath()
        });
    },

    /**
     * Waterfall has stacking along the x-values too.
     */
    setStackedPoints: function () {
        var series = this,
            options = series.options,
            stackedYLength,
            i;

        Series.prototype.setStackedPoints.apply(series, arguments);

        stackedYLength = series.stackedYData ? series.stackedYData.length : 0;

        // Start from the second point:
        for (i = 1; i < stackedYLength; i++) {
            if (
                !options.data[i].isSum &&
                !options.data[i].isIntermediateSum
            ) {
                // Sum previous stacked data as waterfall can grow up/down:
                series.stackedYData[i] += series.stackedYData[i - 1];
            }
        }
    },

    /**
     * Extremes for a non-stacked series are recorded in processData.
     * In case of stacking, use Series.stackedYData to calculate extremes.
     */
    getExtremes: function () {
        if (this.options.stacking) {
            return Series.prototype.getExtremes.apply(this, arguments);
        }
    }


// Point members
}, {
    getClassName: function () {
        var className = Point.prototype.getClassName.call(this);

        if (this.isSum) {
            className += ' highcharts-sum';
        } else if (this.isIntermediateSum) {
            className += ' highcharts-intermediate-sum';
        }
        return className;
    },
    /**
     * Pass the null test in ColumnSeries.translate.
     */
    isValid: function () {
        return isNumber(this.y, true) || this.isSum || this.isIntermediateSum;
    }

});

/**
 * A `waterfall` series. If the [type](#series.waterfall.type) option
 * is not specified, it is inherited from [chart.type](#chart.type).
 *
 * @type      {Object}
 * @extends   series,plotOptions.waterfall
 * @excluding dataParser,dataURL
 * @product   highcharts
 * @apioption series.waterfall
 */

/**
 * An array of data points for the series. For the `waterfall` series
 * type, points can be given in the following ways:
 *
 * 1.  An array of numerical values. In this case, the numerical values
 * will be interpreted as `y` options. The `x` values will be automatically
 * calculated, either starting at 0 and incremented by 1, or from `pointStart`
 * and `pointInterval` given in the series options. If the axis has
 * categories, these will be used. Example:
 *
 *  ```js
 *  data: [0, 5, 3, 5]
 *  ```
 *
 * 2.  An array of arrays with 2 values. In this case, the values correspond
 * to `x,y`. If the first value is a string, it is applied as the name
 * of the point, and the `x` value is inferred.
 *
 *  ```js
 *     data: [
 *         [0, 7],
 *         [1, 8],
 *         [2, 3]
 *     ]
 *  ```
 *
 * 3.  An array of objects with named values. The following snippet shows only a
 * few settings, see the complete options set below. If the total number of data
 * points exceeds the series'
 * [turboThreshold](#series.waterfall.turboThreshold),
 * this option is not available.
 *
 *  ```js
 *     data: [{
 *         x: 1,
 *         y: 8,
 *         name: "Point2",
 *         color: "#00FF00"
 *     }, {
 *         x: 1,
 *         y: 8,
 *         name: "Point1",
 *         color: "#FF00FF"
 *     }]
 *  ```
 *
 * @type      {Array<Object|Array|Number>}
 * @extends   series.line.data
 * @excluding marker
 * @sample    {highcharts} highcharts/chart/reflow-true/
 *            Numerical values
 * @sample    {highcharts} highcharts/series/data-array-of-arrays/
 *            Arrays of numeric x and y
 * @sample    {highcharts} highcharts/series/data-array-of-arrays-datetime/
 *            Arrays of datetime x and y
 * @sample    {highcharts} highcharts/series/data-array-of-name-value/
 *            Arrays of point.name and y
 * @sample    {highcharts} highcharts/series/data-array-of-objects/
 *            Config objects
 * @product   highcharts
 * @apioption series.waterfall.data
 */


/**
 * When this property is true, the points acts as a summary column for
 * the values added or substracted since the last intermediate sum,
 * or since the start of the series. The `y` value is ignored.
 *
 * @type      {Boolean}
 * @sample    {highcharts} highcharts/demo/waterfall/ Waterfall
 * @default   false
 * @product   highcharts
 * @apioption series.waterfall.data.isIntermediateSum
 */

/**
 * When this property is true, the point display the total sum across
 * the entire series. The `y` value is ignored.
 *
 * @type      {Boolean}
 * @sample    {highcharts} highcharts/demo/waterfall/ Waterfall
 * @default   false
 * @product   highcharts
 * @apioption series.waterfall.data.isSum
 */