Column.js 13.1 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
/**
 * (c) 2010-2018 Torstein Honsi
 *
 * License: www.highcharts.com/license
 */
'use strict';
import H from '../parts/Globals.js';
import '../parts/Utilities.js';
import '../parts/Series.js';
var addEvent = H.addEvent,
    each = H.each,
    perspective = H.perspective,
    pick = H.pick,
    Series = H.Series,
    seriesTypes = H.seriesTypes,
    inArray = H.inArray,
    svg = H.svg,
    wrap = H.wrap;



/**
 * Depth of the columns in a 3D column chart. Requires `highcharts-3d.js`.
 *
 * @type {Number}
 * @default 25
 * @since 4.0
 * @product highcharts
 * @apioption plotOptions.column.depth
 */

/**
 * 3D columns only. The color of the edges. Similar to `borderColor`,
 *  except it defaults to the same color as the column.
 *
 * @type {Color}
 * @product highcharts
 * @apioption plotOptions.column.edgeColor
 */

/**
 * 3D columns only. The width of the colored edges.
 *
 * @type {Number}
 * @default 1
 * @product highcharts
 * @apioption plotOptions.column.edgeWidth
 */

/**
 * The spacing between columns on the Z Axis in a 3D chart. Requires
 * `highcharts-3d.js`.
 *
 * @type {Number}
 * @default 1
 * @since 4.0
 * @product highcharts
 * @apioption plotOptions.column.groupZPadding
 */

wrap(seriesTypes.column.prototype, 'translate', function (proceed) {
    proceed.apply(this, [].slice.call(arguments, 1));

    // Do not do this if the chart is not 3D
    if (this.chart.is3d()) {
        this.translate3dShapes();
    }
});

// In 3D we need to pass point.outsidePlot option to the justifyDataLabel
// method for disabling justifying dataLabels in columns outside plot
wrap(H.Series.prototype, 'alignDataLabel', function (proceed) {
    arguments[3].outside3dPlot = arguments[1].outside3dPlot;
    proceed.apply(this, [].slice.call(arguments, 1));
});

// Don't use justifyDataLabel when point is outsidePlot
wrap(H.Series.prototype, 'justifyDataLabel', function (proceed) {
    return !(arguments[2].outside3dPlot) ?
        proceed.apply(this, [].slice.call(arguments, 1)) :
        false;
});

seriesTypes.column.prototype.translate3dPoints = function () {};
seriesTypes.column.prototype.translate3dShapes = function () {

    var series = this,
        chart = series.chart,
        seriesOptions = series.options,
        depth = seriesOptions.depth || 25,
        stack = seriesOptions.stacking ?
            (seriesOptions.stack || 0) :
            series.index, // #4743
        z = stack * (depth + (seriesOptions.groupZPadding || 1)),
        borderCrisp = series.borderWidth % 2 ? 0.5 : 0;

    if (chart.inverted && !series.yAxis.reversed) {
        borderCrisp *= -1;
    }

    if (seriesOptions.grouping !== false) {
        z = 0;
    }

    z += (seriesOptions.groupZPadding || 1);
    each(series.data, function (point) {
        // #7103 Reset outside3dPlot flag
        point.outside3dPlot = null;
        if (point.y !== null) {
            var shapeArgs = point.shapeArgs,
                tooltipPos = point.tooltipPos,
                // Array for final shapeArgs calculation.
                // We are checking two dimensions (x and y).
                dimensions = [['x', 'width'], ['y', 'height']],
                borderlessBase; // Crisped rects can have +/- 0.5 pixels offset.

            // #3131 We need to check if column is inside plotArea.
            each(dimensions, function (d) {
                borderlessBase = shapeArgs[d[0]] - borderCrisp;
                if (borderlessBase < 0) {
                    // If borderLessBase is smaller than 0, it is needed to set
                    // its value to 0 or 0.5 depending on borderWidth
                    // borderWidth may be even or odd.
                    shapeArgs[d[1]] += shapeArgs[d[0]] + borderCrisp;
                    shapeArgs[d[0]] = -borderCrisp;
                    borderlessBase = 0;
                }
                if (
                    (
                        borderlessBase + shapeArgs[d[1]] >
                        series[d[0] + 'Axis'].len
                    ) &&
                    // Do not change height/width of column if 0 (#6708)
                    shapeArgs[d[1]] !== 0
                ) {
                    shapeArgs[d[1]] =
                        series[d[0] + 'Axis'].len - shapeArgs[d[0]];
                }
                if (
                    // Do not remove columns with zero height/width.
                    (shapeArgs[d[1]] !== 0) &&
                    (
                        shapeArgs[d[0]] >= series[d[0] + 'Axis'].len ||
                        shapeArgs[d[0]] + shapeArgs[d[1]] <= borderCrisp
                    )
                ) {
                    // Set args to 0 if column is outside the chart.
                    for (var key in shapeArgs) {
                        shapeArgs[key] = 0;
                    }
                    // #7103 outside3dPlot flag is set on Points which are
                    // currently outside of plot.
                    point.outside3dPlot = true;
                }
            });

            point.shapeType = 'cuboid';
            shapeArgs.z = z;
            shapeArgs.depth = depth;
            shapeArgs.insidePlotArea = true;

            // Translate the tooltip position in 3d space
            tooltipPos = perspective(
                [{ x: tooltipPos[0], y: tooltipPos[1], z: z }],
                chart,
                true
            )[0];
            point.tooltipPos = [tooltipPos.x, tooltipPos.y];
        }
    });
    // store for later use #4067
    series.z = z;
};

wrap(seriesTypes.column.prototype, 'animate', function (proceed) {
    if (!this.chart.is3d()) {
        proceed.apply(this, [].slice.call(arguments, 1));
    } else {
        var args = arguments,
            init = args[1],
            yAxis = this.yAxis,
            series = this,
            reversed = this.yAxis.reversed;

        if (svg) { // VML is too slow anyway
            if (init) {
                each(series.data, function (point) {
                    if (point.y !== null) {
                        point.height = point.shapeArgs.height;
                        point.shapey = point.shapeArgs.y;    // #2968
                        point.shapeArgs.height = 1;
                        if (!reversed) {
                            if (point.stackY) {
                                point.shapeArgs.y =
                                    point.plotY + yAxis.translate(point.stackY);
                            } else {
                                point.shapeArgs.y =
                                    point.plotY +
                                    (
                                        point.negative ?
                                        -point.height :
                                        point.height
                                    );
                            }
                        }
                    }
                });

            } else { // run the animation
                each(series.data, function (point) {
                    if (point.y !== null) {
                        point.shapeArgs.height = point.height;
                        point.shapeArgs.y = point.shapey;    // #2968
                        // null value do not have a graphic
                        if (point.graphic) {
                            point.graphic.animate(
                                point.shapeArgs,
                                series.options.animation
                            );
                        }
                    }
                });

                // redraw datalabels to the correct position
                this.drawDataLabels();

                // delete this function to allow it only once
                series.animate = null;
            }
        }
    }
});

/*
 * In case of 3d columns there is no sense to add this columns
 * to a specific series group - if series is added to a group
 * all columns will have the same zIndex in comparison with different series
 */

wrap(
    seriesTypes.column.prototype,
    'plotGroup',
    function (proceed, prop, name, visibility, zIndex, parent) {
        if (this.chart.is3d() && parent && !this[prop]) {
            if (!this.chart.columnGroup) {
                this.chart.columnGroup =
                    this.chart.renderer.g('columnGroup').add(parent);
            }
            this[prop] = this.chart.columnGroup;
            this.chart.columnGroup.attr(this.getPlotBox());
            this[prop].survive = true;
        }
        return proceed.apply(this, Array.prototype.slice.call(arguments, 1));
    }
);

/*
 * When series is not added to group it is needed to change
 * setVisible method to allow correct Legend funcionality
 * This wrap is basing on pie chart series
 */
wrap(
    seriesTypes.column.prototype,
    'setVisible',
    function (proceed, vis) {
        var series = this,
            pointVis;
        if (series.chart.is3d()) {
            each(series.data, function (point) {
                point.visible = point.options.visible = vis =
                    vis === undefined ? !point.visible : vis;
                pointVis = vis ? 'visible' : 'hidden';
                series.options.data[inArray(point, series.data)] =
                    point.options;
                if (point.graphic) {
                    point.graphic.attr({
                        visibility: pointVis
                    });
                }
            });
        }
        proceed.apply(this, Array.prototype.slice.call(arguments, 1));
    }
);

seriesTypes.column.prototype.handle3dGrouping = true;
addEvent(Series, 'afterInit', function () {
    if (this.chart.is3d() && this.handle3dGrouping) {
        var seriesOptions = this.options,
            grouping = seriesOptions.grouping,
            stacking = seriesOptions.stacking,
            reversedStacks = pick(this.yAxis.options.reversedStacks, true),
            z = 0;

        if (!(grouping !== undefined && !grouping)) {
            var stacks = this.chart.retrieveStacks(stacking),
                stack = seriesOptions.stack || 0,
                i; // position within the stack
            for (i = 0; i < stacks[stack].series.length; i++) {
                if (stacks[stack].series[i] === this) {
                    break;
                }
            }
            z = (10 * (stacks.totalStacks - stacks[stack].position)) +
                (reversedStacks ? i : -i); // #4369

            // In case when axis is reversed, columns are also reversed inside
            // the group (#3737)
            if (!this.xAxis.reversed) {
                z = (stacks.totalStacks * 10) - z;
            }
        }

        seriesOptions.zIndex = z;
    }
});



wrap(Series.prototype, 'alignDataLabel', function (proceed) {

    // Only do this for 3D columns and columnranges
    if (
        this.chart.is3d() &&
        (this.type === 'column' || this.type === 'columnrange')
    ) {
        var series = this,
            chart = series.chart;

        var args = arguments,
            alignTo = args[4],
            point = args[1];

        var pos = ({ x: alignTo.x, y: alignTo.y, z: series.z });
        pos = perspective([pos], chart, true)[0];
        alignTo.x = pos.x;
        // #7103 If point is outside of plotArea, hide data label.
        alignTo.y = point.outside3dPlot ? -9e9 : pos.y;
    }

    proceed.apply(this, [].slice.call(arguments, 1));
});

// Added stackLabels position calculation for 3D charts.
wrap(H.StackItem.prototype, 'getStackBox', function (proceed, chart) { // #3946
    var stackBox = proceed.apply(this, [].slice.call(arguments, 1));

    // Only do this for 3D chart.
    if (chart.is3d()) {
        var pos = ({
            x: stackBox.x,
            y: stackBox.y,
            z: 0
        });
        pos = H.perspective([pos], chart, true)[0];
        stackBox.x = pos.x;
        stackBox.y = pos.y;
    }

    return stackBox;
});

/*
    EXTENSION FOR 3D CYLINDRICAL COLUMNS
    Not supported
*/
/*
var defaultOptions = H.getOptions();
defaultOptions.plotOptions.cylinder =
    H.merge(defaultOptions.plotOptions.column);
var CylinderSeries = H.extendClass(seriesTypes.column, {
    type: 'cylinder'
});
seriesTypes.cylinder = CylinderSeries;

wrap(seriesTypes.cylinder.prototype, 'translate', function (proceed) {
    proceed.apply(this, [].slice.call(arguments, 1));

    // Do not do this if the chart is not 3D
    if (!this.chart.is3d()) {
        return;
    }

    var series = this,
        chart = series.chart,
        options = chart.options,
        cylOptions = options.plotOptions.cylinder,
        options3d = options.chart.options3d,
        depth = cylOptions.depth || 0,
        alpha = chart.alpha3d;

    var z = cylOptions.stacking ?
        (this.options.stack || 0) * depth :
        series._i * depth;
    z += depth / 2;

    if (cylOptions.grouping !== false) { z = 0; }

    each(series.data, function (point) {
        var shapeArgs = point.shapeArgs,
            deg2rad = H.deg2rad;
        point.shapeType = 'arc3d';
        shapeArgs.x += depth / 2;
        shapeArgs.z = z;
        shapeArgs.start = 0;
        shapeArgs.end = 2 * PI;
        shapeArgs.r = depth * 0.95;
        shapeArgs.innerR = 0;
        shapeArgs.depth =
            shapeArgs.height * (1 / sin((90 - alpha) * deg2rad)) - z;
        shapeArgs.alpha = 90 - alpha;
        shapeArgs.beta = 0;
    });
});
*/