variwide.src.js 15.2 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
/**
 * @license Highcharts JS v7.2.0 (2019-09-03)
 *
 * Highcharts variwide module
 *
 * (c) 2010-2019 Torstein Honsi
 *
 * License: www.highcharts.com/license
 */
'use strict';
(function (factory) {
    if (typeof module === 'object' && module.exports) {
        factory['default'] = factory;
        module.exports = factory;
    } else if (typeof define === 'function' && define.amd) {
        define('highcharts/modules/variwide', ['highcharts'], function (Highcharts) {
            factory(Highcharts);
            factory.Highcharts = Highcharts;
            return factory;
        });
    } else {
        factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
    }
}(function (Highcharts) {
    var _modules = Highcharts ? Highcharts._modules : {};
    function _registerModule(obj, path, args, fn) {
        if (!obj.hasOwnProperty(path)) {
            obj[path] = fn.apply(null, args);
        }
    }
    _registerModule(_modules, 'modules/variwide.src.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js']], function (H, U) {
        /* *
         * Highcharts variwide module
         *
         * (c) 2010-2019 Torstein Honsi
         *
         * License: www.highcharts.com/license
         */



        var isNumber = U.isNumber;


        var addEvent = H.addEvent,
            seriesType = H.seriesType,
            seriesTypes = H.seriesTypes,
            pick = H.pick;

        /**
         * @private
         * @class
         * @name Highcharts.seriesTypes.variwide
         *
         * @augments Highcharts.Series
         */
        seriesType('variwide', 'column'

            /**
             * A variwide chart (related to marimekko chart) is a column chart with a
             * variable width expressing a third dimension.
             *
             * @sample {highcharts} highcharts/demo/variwide/
             *         Variwide chart
             * @sample {highcharts} highcharts/series-variwide/inverted/
             *         Inverted variwide chart
             * @sample {highcharts} highcharts/series-variwide/datetime/
             *         Variwide columns on a datetime axis
             *
             * @extends      plotOptions.column
             * @since        6.0.0
             * @product      highcharts
             * @excluding    boostThreshold, crisp, depth, edgeColor, edgeWidth,
             *               groupZPadding
             * @optionparent plotOptions.variwide
             */
            , {
                /**
                 * In a variwide chart, the point padding is 0 in order to express the
                 * horizontal stacking of items.
                 */
                pointPadding: 0,
                /**
                 * In a variwide chart, the group padding is 0 in order to express the
                 * horizontal stacking of items.
                 */
                groupPadding: 0
            }, {
                irregularWidths: true,
                pointArrayMap: ['y', 'z'],
                parallelArrays: ['x', 'y', 'z'],
                processData: function (force) {
                    this.totalZ = 0;
                    this.relZ = [];
                    seriesTypes.column.prototype.processData.call(this, force);

                    (this.xAxis.reversed ?
                        this.zData.slice().reverse() :
                        this.zData).forEach(
                        function (z, i) {
                            this.relZ[i] = this.totalZ;
                            this.totalZ += z;
                        },
                        this
                    );

                    if (this.xAxis.categories) {
                        this.xAxis.variwide = true;
                        this.xAxis.zData = this.zData; // Used for label rank
                    }
                },

                /**
                 * Translate an x value inside a given category index into the distorted
                 * axis translation.
                 *
                 * @private
                 * @function Highcharts.Series#postTranslate
                 *
                 * @param {number} index
                 *        The category index
                 *
                 * @param {number} x
                 *        The X pixel position in undistorted axis pixels
                 *
                 * @return {number}
                 *         Distorted X position
                 */
                postTranslate: function (index, x, point) {

                    var axis = this.xAxis,
                        relZ = this.relZ,
                        i = axis.reversed ? relZ.length - index : index,
                        goRight = axis.reversed ? -1 : 1,
                        len = axis.len,
                        totalZ = this.totalZ,
                        linearSlotLeft = i / relZ.length * len,
                        linearSlotRight = (i + goRight) / relZ.length * len,
                        slotLeft = (pick(relZ[i], totalZ) / totalZ) * len,
                        slotRight = (pick(relZ[i + goRight], totalZ) / totalZ) * len,
                        xInsideLinearSlot = x - linearSlotLeft,
                        ret;

                    // Set crosshairWidth for every point (#8173)
                    if (point) {
                        point.crosshairWidth = slotRight - slotLeft;
                    }

                    ret = slotLeft +
                    xInsideLinearSlot * (slotRight - slotLeft) /
                    (linearSlotRight - linearSlotLeft);

                    return ret;
                },

                // Extend translation by distoring X position based on Z.
                translate: function () {

                    // Temporarily disable crisping when computing original shapeArgs
                    var crispOption = this.options.crisp,
                        xAxis = this.xAxis;

                    this.options.crisp = false;

                    seriesTypes.column.prototype.translate.call(this);

                    // Reset option
                    this.options.crisp = crispOption;

                    var inverted = this.chart.inverted,
                        crisp = this.borderWidth % 2 / 2;

                    // Distort the points to reflect z dimension
                    this.points.forEach(function (point, i) {
                        var left, right;

                        if (xAxis.variwide) {
                            left = this.postTranslate(
                                i,
                                point.shapeArgs.x,
                                point
                            );

                            right = this.postTranslate(
                                i,
                                point.shapeArgs.x + point.shapeArgs.width
                            );

                            // For linear or datetime axes, the variwide column should
                            // start with X and extend Z units, without modifying the
                            // axis.
                        } else {
                            left = point.plotX;
                            right = xAxis.translate(
                                point.x + point.z,
                                0,
                                0,
                                0,
                                1
                            );
                        }

                        if (this.options.crisp) {
                            left = Math.round(left) - crisp;
                            right = Math.round(right) - crisp;
                        }

                        point.shapeArgs.x = left;
                        point.shapeArgs.width = right - left;

                        // Crosshair position (#8083)
                        point.plotX = (left + right) / 2;

                        // Adjust the tooltip position
                        if (!inverted) {
                            point.tooltipPos[0] =
                            point.shapeArgs.x + point.shapeArgs.width / 2;
                        } else {
                            point.tooltipPos[1] =
                            xAxis.len - point.shapeArgs.x - point.shapeArgs.width / 2;
                        }
                    }, this);

                    if (this.options.stacking) {
                        this.correctStackLabels();
                    }
                },

                // Function that corrects stack labels positions
                correctStackLabels: function () {
                    var series = this,
                        options = series.options,
                        yAxis = series.yAxis,
                        pointStack,
                        pointWidth,
                        stack,
                        xValue;

                    series.points.forEach(function (point) {
                        xValue = point.x;
                        pointWidth = point.shapeArgs.width;
                        stack = yAxis.stacks[(series.negStacks &&
                            point.y <
                                (options.startFromThreshold ? 0 : options.threshold) ?
                            '-' :
                            '') + series.stackKey];
                        pointStack = stack[xValue];

                        if (stack && pointStack && !point.isNull) {
                            pointStack.setOffset(
                                -(pointWidth / 2) || 0,
                                pointWidth || 0,
                                undefined,
                                undefined,
                                point.plotX
                            );
                        }
                    });
                }

                // Point functions
            }, {
                isValid: function () {
                    return isNumber(this.y) && isNumber(this.z);
                }
            });

        H.Tick.prototype.postTranslate = function (xy, xOrY, index) {
            var axis = this.axis,
                pos = xy[xOrY] - axis.pos;

            if (!axis.horiz) {
                pos = axis.len - pos;
            }
            pos = axis.series[0].postTranslate(index, pos);

            if (!axis.horiz) {
                pos = axis.len - pos;
            }
            xy[xOrY] = axis.pos + pos;
        };

        // Same width as the category (#8083)
        addEvent(H.Axis, 'afterDrawCrosshair', function (e) {
            if (this.variwide && this.cross) {
                this.cross.attr('stroke-width', e.point && e.point.crosshairWidth);
            }
        });

        // On a vertical axis, apply anti-collision logic to the labels.
        addEvent(H.Axis, 'afterRender', function () {
            var axis = this;

            if (!this.horiz && this.variwide) {
                this.chart.labelCollectors.push(function () {
                    return axis.tickPositions.map(function (pos, i) {
                        var label = axis.ticks[pos].label;

                        label.labelrank = axis.zData[i];
                        return label;
                    });
                });
            }
        });

        addEvent(H.Tick, 'afterGetPosition', function (e) {
            var axis = this.axis,
                xOrY = axis.horiz ? 'x' : 'y';

            if (axis.variwide) {
                this[xOrY + 'Orig'] = e.pos[xOrY];
                this.postTranslate(e.pos, xOrY, this.pos);
            }
        });

        H.wrap(H.Tick.prototype, 'getLabelPosition', function (
            proceed,
            x,
            y,
            label,
            horiz,
            labelOptions,
            tickmarkOffset,
            index
        ) {
            var args = Array.prototype.slice.call(arguments, 1),
                xy,
                xOrY = horiz ? 'x' : 'y';

            // Replace the x with the original x
            if (this.axis.variwide && typeof this[xOrY + 'Orig'] === 'number') {
                args[horiz ? 0 : 1] = this[xOrY + 'Orig'];
            }

            xy = proceed.apply(this, args);

            // Post-translate
            if (this.axis.variwide && this.axis.categories) {
                this.postTranslate(xy, xOrY, index);
            }
            return xy;
        });


        /**
         * A `variwide` series. If the [type](#series.variwide.type) option is not
         * specified, it is inherited from [chart.type](#chart.type).
         *
         * @extends   series,plotOptions.variwide
         * @product   highcharts
         * @apioption series.variwide
         */

        /**
         * An array of data points for the series. For the `variwide` series type,
         * points can be given in the following ways:
         *
         * 1. An array of arrays with 3 or 2 values. In this case, the values correspond
         *    to `x,y,z`. If the first value is a string, it is applied as the name of
         *    the point, and the `x` value is inferred. The `x` value can also be
         *    omitted, in which case the inner arrays should be of length 2. Then the
         *    `x` value is automatically calculated, either starting at 0 and
         *    incremented by 1, or from `pointStart` and `pointInterval` given in the
         *    series options.
         *    ```js
         *       data: [
         *           [0, 1, 2],
         *           [1, 5, 5],
         *           [2, 0, 2]
         *       ]
         *    ```
         *
         * 2. 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.variwide.turboThreshold), this option is not
         *    available.
         *    ```js
         *       data: [{
         *           x: 1,
         *           y: 1,
         *           z: 1,
         *           name: "Point2",
         *           color: "#00FF00"
         *       }, {
         *           x: 1,
         *           y: 5,
         *           z: 4,
         *           name: "Point1",
         *           color: "#FF00FF"
         *       }]
         *    ```
         *
         * @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
         *
         * @type      {Array<Array<(number|string),number>|Array<(number|string),number,number>|*>}
         * @extends   series.line.data
         * @excluding marker
         * @product   highcharts
         * @apioption series.variwide.data
         */

        /**
         * The relative width for each column. On a category axis, the widths are
         * distributed so they sum up to the X axis length. On linear and datetime axes,
         * the columns will be laid out from the X value and Z units along the axis.
         *
         * @type      {number}
         * @product   highcharts
         * @apioption series.variwide.data.z
         */

    });
    _registerModule(_modules, 'masters/modules/variwide.src.js', [], function () {


    });
}));