all files / accumulation-chart/renderer/ pie-base.js

95.12% Statements 234/246
86.16% Branches 137/159
100% Functions 26/26
95.04% Lines 230/242
11 statements, 6 functions, 9 branches Ignored     
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          984× 984× 984×   25×     25×     25× 25× 25× 25× 25× 25×     24×     25× 25× 25× 25× 25× 25×     24×                 25× 22×   25×     25×   25× 50× 50× 50× 50× 50× 34×     16×                 50×     534× 534× 534× 534× 534× 50× 50× 50× 50×         484× 484× 475× 475×   475× 475×       90× 90×               534× 534× 534×     584× 534× 534× 534× 501×   534×   190×       534× 534×       534× 534× 534× 534× 534×   1569× 1569× 1569× 1569× 1569×   501× 501× 501× 501× 109×     1035× 1035× 1035× 1035× 1035× 1035× 1035× 1035× 1035× 1035× 1035× 1035× 1035× 1035× 1035×   1035× 1024×   1035× 1035× 3045× 3045× 3030×     1035× 1035× 1035×   8194× 8194×   13799× 13799× 13799× 13799× 12125×     1674×     12125× 12125× 12125× 12125× 12125× 12125× 12125× 12125× 12125× 12125× 12125× 12125× 12125× 12125×   1674× 1674× 1674× 1674× 1674× 1674× 1674× 1674× 1674× 1674× 1674× 1674× 1674× 1674× 1674× 1674× 1674× 1674× 1674× 1674× 1674× 1674× 1674× 98×     1576×     13799× 13799× 1911×   13799× 13799× 13799×   13799× 13799× 132027× 132027× 129452×     13799×   129× 129× 129× 129× 129× 129× 129× 129× 129× 129×       6795× 6795×     129× 129× 104×   129× 129× 129× 129×   129× 129× 129×     129× 129× 129× 124×                      
/* istanbul ignore next */ 
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
define(["require", "exports", "@syncfusion/ej2-base", "../../common/utils/helper", "@syncfusion/ej2-svg-base", "../../common/model/constants", "./accumulation-base"], function (require, exports, ej2_base_1, helper_1, ej2_svg_base_1, constants_1, accumulation_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var PieBase = (function (_super) {
        __extends(PieBase, _super);
        function PieBase() {
            var _this = _super !== null && _super.apply(this, arguments) || this;
            _this.activeAnimationCount = 0;
            return _this;
        }
        PieBase.prototype.normalizeSeriesRadius = function (series) {
            Iif (series[0].type !== 'Pie' || (!series || series.length === 0)) {
                return;
            }
            Iif (series.length === 1) {
                return;
            }
            var chartArea = this.accumulation.initialClipRect;
            var size = Math.min(chartArea.width, chartArea.height);
            var baseOuterRadius;
            var firstSeriesRadius = series[0].radius;
            var firstSeriesRadiusNum = parseInt(firstSeriesRadius, 10);
            if (typeof firstSeriesRadius === 'string' &&
                firstSeriesRadius.indexOf('%') !== -1 &&
                !isNaN(firstSeriesRadiusNum)) {
                baseOuterRadius = (firstSeriesRadiusNum / 100) * (size / 2);
            }
            else Eif (typeof firstSeriesRadius === 'string' && !isNaN(firstSeriesRadiusNum)) {
                baseOuterRadius = firstSeriesRadiusNum;
            }
            series[0]._calculatedRadius = baseOuterRadius;
            for (var i = 1; i < series.length; i++) {
                var seriesRadius = void 0;
                var seriesRadiusValue = series[i].radius;
                var seriesRadiusNum = parseInt(seriesRadiusValue, 10);
                if (typeof seriesRadiusValue === 'string' &&
                    seriesRadiusValue.indexOf('%') !== -1 &&
                    !isNaN(seriesRadiusNum)) {
                    seriesRadius = (seriesRadiusNum / 100) * (size / 2);
                }
                else Eif (typeof seriesRadiusValue === 'string' && !isNaN(seriesRadiusNum)) {
                    seriesRadius = seriesRadiusNum;
                }
                else if (typeof seriesRadiusValue === 'number' && !isNaN(seriesRadiusValue)) {
                    seriesRadius = seriesRadiusValue;
                }
                else {
                    seriesRadius = baseOuterRadius * (1 - (i * 0.25));
                }
                if (seriesRadius >= series[i - 1]._calculatedRadius) {
                    seriesRadius = series[i - 1]._calculatedRadius * 0.75;
                }
                Iif (seriesRadius > size / 2) {
                    seriesRadius = size / 2;
                }
                series[i]._calculatedRadius = seriesRadius;
            }
            for (var i = 0; i < series.length; i++) {
                var innerRadiusValue = series[i].innerRadius;
                var innerRadius = void 0;
                Eif (typeof innerRadiusValue === 'string') {
                    var innerRadiusNum = parseInt(innerRadiusValue, 10);
                    if (innerRadiusValue.indexOf('%') !== -1 && !isNaN(innerRadiusNum)) {
                        innerRadius = (innerRadiusNum / 100) * series[i]._calculatedRadius;
                    }
                    else {
                        innerRadius = innerRadiusNum;
                    }
                }
                else if (typeof innerRadiusValue === 'number' && !isNaN(innerRadiusValue)) {
                    innerRadius = innerRadiusValue;
                }
                else {
                    innerRadius = 0;
                }
                series[i]._calculatedInnerRadius = innerRadius;
            }
        };
        PieBase.prototype.initProperties = function (chart, series) {
            this.accumulation = chart;
            this.size = Math.min(chart.initialClipRect.width, chart.initialClipRect.height);
            this.initAngles(series);
            var pieCoefficient = 0.8;
            if (series._calculatedRadius) {
                this.isRadiusMapped = false;
                this.pieBaseRadius = series._calculatedRadius;
                this.innerRadius = series._calculatedInnerRadius || 0;
                this.pieBaseLabelRadius = series.dataLabel.position === 'Inside'
                    ? (((this.pieBaseRadius - this.innerRadius) / 2) + this.innerRadius)
                    : (this.pieBaseRadius + helper_1.stringToNumber(series.dataLabel.connectorStyle.length || '4%', this.size / 2));
            }
            else {
                var r = parseInt(series.radius, 10);
                if ((series.radius.indexOf('%') !== -1 || typeof r === 'number') && !isNaN(r)) {
                    this.isRadiusMapped = false;
                    this.pieBaseRadius = helper_1.stringToNumber(series.radius, series.border.width > 20
                        ? (0.5 * pieCoefficient * this.size) : this.size / 2);
                    this.innerRadius = helper_1.stringToNumber(series.innerRadius, this.pieBaseRadius);
                    this.pieBaseLabelRadius = series.dataLabel.position === 'Inside' ? (((this.pieBaseRadius - this.innerRadius) / 2) + this.innerRadius) :
                        (this.pieBaseRadius + helper_1.stringToNumber(series.dataLabel.connectorStyle.length || '4%', this.size / 2));
                }
                else {
                    var radiusCollection = [];
                    this.isRadiusMapped = true;
                    for (var i = 0; i < Object.keys(series.points).length; i++) {
                        Eif (series.points[i].sliceRadius.indexOf('%') !== -1) {
                            radiusCollection[i] = helper_1.stringToNumber(series.points[i].sliceRadius, this.size / 2);
                        }
                        else {
                            radiusCollection[i] = parseInt(series.points[i].sliceRadius, 10);
                        }
                    }
                    var minRadius = Math.min.apply(null, radiusCollection);
                    var maxRadius = Math.max.apply(null, radiusCollection);
                    this.pieBaseRadius = this.seriesRadius = maxRadius;
                    this.innerRadius = helper_1.stringToNumber(series.innerRadius, this.seriesRadius);
                    this.innerRadius = this.innerRadius > minRadius ? (this.innerRadius / 2) : this.innerRadius;
                }
            }
            this.radius = this.pieBaseRadius;
            this.labelRadius = this.pieBaseLabelRadius;
            series._explodeDistance = series.explode
                ? helper_1.stringToNumber(series.explodeOffset, this.pieBaseRadius)
                : 0;
            chart.explodeDistance = Math.max.apply(Math, chart.visibleSeries.map(function (s) { return s._explodeDistance || 0; }));
            this.findCenter(chart, series);
            this.center = this.pieBaseCenter;
            if (!chart.redraw) {
                this.defaultLabelBound(series, series.dataLabel.visible, series.dataLabel.position);
            }
            this.totalAngle -= 0.001;
        };
        PieBase.prototype.getLabelRadius = function (series, point) {
            return series.dataLabel.position === 'Inside' ?
                ((((helper_1.stringToNumber(point.sliceRadius, this.pieBaseRadius) - this.innerRadius)) / 2) + this.innerRadius) :
                (helper_1.stringToNumber(point.sliceRadius, this.size / 2) + helper_1.stringToNumber(series.dataLabel.connectorStyle.length || '4%', this.size / 2));
        };
        PieBase.prototype.findCenter = function (accumulation, series) {
            this.accumulation = accumulation;
            this.pieBaseCenter = {
                x: helper_1.stringToNumber(accumulation.center.x, accumulation.initialClipRect.width) + (accumulation.initialClipRect.x),
                y: helper_1.stringToNumber(accumulation.center.y, accumulation.initialClipRect.height) + (accumulation.initialClipRect.y)
            };
            var accumulationRect = this.getSeriesBound(series);
            var accumulationRectCenter = new helper_1.ChartLocation(accumulationRect.x + accumulationRect.width / 2, accumulationRect.y + accumulationRect.height / 2);
            this.pieBaseCenter.x += (this.pieBaseCenter.x - accumulationRectCenter.x);
            this.pieBaseCenter.y += (this.pieBaseCenter.y - accumulationRectCenter.y);
            this.accumulation.origin = this.pieBaseCenter;
        };
        PieBase.prototype.initAngles = function (series) {
            var endAngle = ej2_base_1.isNullOrUndefined(series.endAngle) ? series.startAngle : series.endAngle;
            this.totalAngle = (endAngle - series.startAngle) % 360;
            this.startAngle = series.startAngle - 90;
            this.totalAngle = this.totalAngle <= 0 ? (360 + this.totalAngle) : this.totalAngle;
            this.startAngle = (this.startAngle < 0 ? (this.startAngle + 360) : this.startAngle) % 360;
        };
        PieBase.prototype.defaultLabelBound = function (series, visible, position) {
            var accumulationBound = this.getSeriesBound(series);
            series.accumulationBound = accumulationBound;
            series.labelBound = new ej2_svg_base_1.Rect(accumulationBound.x, accumulationBound.y, accumulationBound.width + accumulationBound.x, accumulationBound.height + accumulationBound.y);
            if (visible && position === 'Outside') {
                series.labelBound = new ej2_svg_base_1.Rect(Infinity, Infinity, -Infinity, -Infinity);
            }
        };
        PieBase.prototype.getSeriesBound = function (series) {
            var rect = new ej2_svg_base_1.Rect(Infinity, Infinity, -Infinity, -Infinity);
            this.initAngles(series);
            var start = this.startAngle;
            var total = this.totalAngle;
            var end = (this.startAngle + total) % 360;
            end = (end === 0) ? 360 : end;
            series.findMaxBounds(rect, this.getRectFromAngle(start));
            series.findMaxBounds(rect, this.getRectFromAngle(end));
            series.findMaxBounds(rect, new ej2_svg_base_1.Rect(this.pieBaseCenter.x, this.pieBaseCenter.y, 0, 0));
            var nextQuandrant = (Math.floor(start / 90) * 90 + 90) % 360;
            var lastQuadrant = (Math.floor(end / 90) * 90) % 360;
            lastQuadrant = (lastQuadrant === 0) ? 360 : lastQuadrant;
            Eif (total >= 90 || lastQuadrant === nextQuandrant) {
                series.findMaxBounds(rect, this.getRectFromAngle(nextQuandrant));
                series.findMaxBounds(rect, this.getRectFromAngle(lastQuadrant));
            }
            if (start === 0 || (start + total >= 360)) {
                series.findMaxBounds(rect, this.getRectFromAngle(0));
            }
            var length = nextQuandrant === lastQuadrant ? 0 : Math.floor(total / 90);
            for (var i = 1; i < length; i++) {
                nextQuandrant = nextQuandrant + 90;
                if ((nextQuandrant < lastQuadrant || end < start) || total === 360) {
                    series.findMaxBounds(rect, this.getRectFromAngle(nextQuandrant));
                }
            }
            rect.width -= rect.x;
            rect.height -= rect.y;
            return rect;
        };
        PieBase.prototype.getRectFromAngle = function (angle) {
            var location = helper_1.degreeToLocation(angle, this.pieBaseRadius, this.pieBaseCenter);
            return new ej2_svg_base_1.Rect(location.x, location.y, 0, 0);
        };
        PieBase.prototype.getPathArc = function (center, start, end, radius, innerRadius, borderRadius, isBorder, seriesPoints) {
            var degree = end - start;
            degree = degree < 0 ? (degree + 360) : degree;
            var flag = (degree < 180) ? 0 : 1;
            if (!innerRadius && innerRadius === 0) {
                return this.getPiePath(center, helper_1.degreeToLocation(start, radius, center), helper_1.degreeToLocation(end, radius, center), radius, flag, borderRadius, seriesPoints);
            }
            else {
                return this.getDoughnutPath(center, helper_1.degreeToLocation(start, radius, center), helper_1.degreeToLocation(end, radius, center), radius, helper_1.degreeToLocation(start, innerRadius, center), helper_1.degreeToLocation(end, innerRadius, center), innerRadius, flag, borderRadius, isBorder, seriesPoints);
            }
        };
        PieBase.prototype.getPiePath = function (center, start, end, radius, clockWise, cornerRadius, seriesPoints) {
            var sliceCount = this.sliceCheck(seriesPoints);
            cornerRadius = sliceCount === 1 ? 0 : cornerRadius;
            var startAngle = Math.atan2(start.y - center.y, start.x - center.x);
            var endAngle = Math.atan2(end.y - center.y, end.x - center.x);
            cornerRadius = this.adjustCornerRadius(startAngle, endAngle, radius, cornerRadius);
            var x1 = start.x - cornerRadius * Math.cos(startAngle);
            var y1 = start.y - cornerRadius * Math.sin(startAngle);
            var x2 = end.x - cornerRadius * Math.cos(Math.PI / 2 + endAngle);
            var y2 = end.y - cornerRadius * Math.sin(Math.PI / 2 + endAngle);
            var cx2 = end.x - cornerRadius * Math.cos(endAngle);
            var cy2 = end.y - cornerRadius * Math.sin(endAngle);
            var cx1 = start.x + cornerRadius * Math.cos(Math.PI / 2 + startAngle);
            var cy1 = start.y + cornerRadius * Math.sin(Math.PI / 2 + startAngle);
            return "M " + center.x + " " + center.y + " L " + x1 + " " + y1 + " A " + cornerRadius + " " + cornerRadius + " 0 0 1 " + cx1 + " " + cy1 + " A " + radius + " " + radius + " 0 " + clockWise + " 1 " + x2 + " " + y2 + " A " + cornerRadius + " " + cornerRadius + " 0 0 1 " + cx2 + " " + cy2 + " Z";
        };
        PieBase.prototype.getDoughnutPath = function (center, start, end, radius, innerStart, innerEnd, innerRadius, clockWise, cornerRadius, isBorder, seriesPoints) {
            var sliceCount = this.sliceCheck(seriesPoints);
            cornerRadius = sliceCount === 1 ? 0 : cornerRadius;
            var startAngle = Math.atan2(start.y - innerStart.y, start.x - innerStart.x);
            var endAngle = Math.atan2(end.y - innerEnd.y, end.x - innerEnd.x);
            cornerRadius = this.adjustCornerRadius(startAngle, endAngle, innerRadius, cornerRadius);
            cornerRadius = (isBorder && (this.innerRadius === 0)) ? cornerRadius * -1 : cornerRadius;
            var x1 = start.x - cornerRadius * Math.cos(startAngle);
            var y1 = start.y - cornerRadius * Math.sin(startAngle);
            var x2 = end.x - cornerRadius * Math.cos(Math.PI / 2 + endAngle);
            var y2 = end.y - cornerRadius * Math.sin(Math.PI / 2 + endAngle);
            var x3 = innerEnd.x + cornerRadius * Math.cos(endAngle);
            var y3 = innerEnd.y + cornerRadius * Math.sin(endAngle);
            var x4 = innerStart.x + cornerRadius * Math.cos(Math.PI / 2 + startAngle);
            var y4 = innerStart.y + cornerRadius * Math.sin(Math.PI / 2 + startAngle);
            var cx1 = start.x + cornerRadius * Math.cos(Math.PI / 2 + startAngle);
            var cy1 = start.y + cornerRadius * Math.sin(Math.PI / 2 + startAngle);
            var cx2 = end.x - cornerRadius * Math.cos(endAngle);
            var cy2 = end.y - cornerRadius * Math.sin(endAngle);
            var cx3 = innerEnd.x - cornerRadius * Math.cos(Math.PI / 2 + endAngle);
            var cy3 = innerEnd.y - cornerRadius * Math.sin(Math.PI / 2 + endAngle);
            var cx4 = innerStart.x + cornerRadius * Math.cos(startAngle);
            var cy4 = innerStart.y + cornerRadius * Math.sin(startAngle);
            if (isBorder) {
                return "M " + cx1 + " " + cy1 + " A " + radius + " " + radius + " 0 " + clockWise + " 1 " + x2 + " " + y2 + " L " + cx3 + " " + cy3 + " A " + innerRadius + " " + innerRadius + " 0 " + clockWise + " 0 " + x4 + " " + y4 + " Z";
            }
            else {
                return "M " + x1 + " " + y1 + " A " + cornerRadius + " " + cornerRadius + " 0 0 1 " + cx1 + " " + cy1 + " A " + radius + " " + radius + " 0 " + clockWise + " 1 " + x2 + " " + y2 + " A " + cornerRadius + " " + cornerRadius + " 0 0 1 " + cx2 + " " + cy2 + " L " + x3 + " " + y3 + " A " + cornerRadius + " " + cornerRadius + " 0 0 1 " + cx3 + " " + cy3 + " A " + innerRadius + " " + innerRadius + " 0 " + clockWise + " 0 " + x4 + " " + y4 + " A " + cornerRadius + " " + cornerRadius + " 0 0 1 " + cx4 + " " + cy4 + " Z";
            }
        };
        PieBase.prototype.adjustCornerRadius = function (startAngle, endAngle, radius, cornerRadius) {
            var anglePerSlice = Math.abs(endAngle - startAngle);
            if (anglePerSlice > Math.PI) {
                anglePerSlice = 2 * Math.PI - anglePerSlice;
            }
            var angleFactor = anglePerSlice / (2 * Math.PI);
            var adjustedCornerRadius = radius * angleFactor;
            return Math.min(cornerRadius, adjustedCornerRadius);
        };
        PieBase.prototype.sliceCheck = function (seriesPoints) {
            var isOneSlice = 0;
            for (var index = 0; index < seriesPoints.length; index++) {
                var point = seriesPoints[index];
                if (point.visible) {
                    isOneSlice++;
                }
            }
            return isOneSlice;
        };
        PieBase.prototype.doAnimation = function (slice, series, groupElement, borderRadius, seriesPoints) {
            var _this = this;
            var startAngle = series.startAngle - 90;
            var duration = this.accumulation.duration ? this.accumulation.duration : series.animation.duration;
            var value;
            this.activeAnimationCount++;
            this.pieBaseCenter.x += 1;
            var radius = Math.max(this.accumulation.availableSize.height, this.accumulation.availableSize.width) * 0.75;
            radius += radius * (0.414);
            var effect = helper_1.getAnimationFunction('Linear');
            new ej2_base_1.Animation({}).animate(slice, {
                duration: (duration === 0 && ej2_base_1.animationMode === 'Enable') ? 1000 : duration,
                delay: series.animation.delay,
                progress: function (args) {
                    value = effect(args.timeStamp, startAngle, _this.totalAngle, args.duration);
                    slice.setAttribute('d', _this.getPathArc(_this.pieBaseCenter, startAngle, value, radius, 0, borderRadius, false, seriesPoints));
                },
                end: function () {
                    _this.activeAnimationCount--;
                    if (_this.activeAnimationCount === 0) {
                        _this.pieBaseCenter.x -= 1;
                    }
                    slice.setAttribute('d', _this.getPathArc(_this.pieBaseCenter, 0, 359.99999, radius, 0, borderRadius, false, seriesPoints));
                    _this.accumulation.trigger(constants_1.animationComplete, { series: series, accumulation: _this.accumulation, chart: _this.accumulation });
                    var datalabelGroup = helper_1.getElement(_this.accumulation.element.id + '_datalabel_Series_' + series.index);
                    if (datalabelGroup) {
                        helper_1.markerAnimate(datalabelGroup, series.animation.delay, series.animation.duration, series, null, null, false);
                    }
                    groupElement.style.cssText = '';
                    var annotationElement = helper_1.getElement(_this.accumulation.element.id + '_Annotation_Collections');
                    Iif (annotationElement) {
                        annotationElement.style.visibility = 'visible';
                    }
                    Eif (_this.accumulation.isMultiSeries && _this.accumulation.seriesAnimationCount > 0) {
                        _this.accumulation.completedSeriesAnimations++;
                        if (_this.accumulation.completedSeriesAnimations >= _this.accumulation.seriesAnimationCount) {
                            _this.accumulation.animateSeries = false;
                        }
                    }
                    else {
                        _this.accumulation.animateSeries = false;
                    }
                }
            });
        };
        return PieBase;
    }(accumulation_base_1.AccumulationBase));
    exports.PieBase = PieBase;
});