all files / progressbar/types/ segment-progress.js

100% Statements 175/175
93.86% Branches 107/114
100% Functions 11/11
100% Lines 175/175
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     12×       12× 12× 12× 12× 12×   12× 12× 12× 12× 12× 12× 12× 12× 12× 12× 12× 37× 37× 28× 28×       37× 37× 37× 14×   37× 37× 37× 37×   12×   37×   10× 10× 10×   10×   10×       10× 10×   10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 10× 30×   30× 30× 23× 23×       30× 30× 30× 12×   30× 30× 30×   10×   111× 111×   28×   28× 28× 28× 28× 28× 28× 28× 28× 28× 28× 28×             28×   28× 28× 28× 28× 28× 28× 28× 28× 28×                            
define(["require", "exports", "../model/constant", "../utils/helper", "@syncfusion/ej2-svg-base"], function (require, exports, constant_1, helper_1, ej2_svg_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Segment = (function () {
        function Segment() {
        }
        Segment.prototype.createLinearSegment = function (progress, id, width, opacity, thickness, progressWidth) {
            var locX = (progress.enableRtl) ? ((progress.cornerRadius === 'Round') ?
                (progress.progressRect.x + progress.progressRect.width) - ((constant_1.lineCapRadius / 2) * thickness) :
                (progress.progressRect.x + progress.progressRect.width)) :
                ((progress.cornerRadius === 'Round') ? (progress.progressRect.x + (constant_1.lineCapRadius / 2) * thickness) : progress.progressRect.x);
            var locY = (progress.progressRect.y + (progress.progressRect.height / 2));
            var gapWidth = (progress.gapWidth || progress.themeStyle.linearGapWidth);
            var avlWidth = progressWidth / progress.segmentCount;
            var avlSegWidth = (progressWidth - ((progress.segmentCount - 1) * gapWidth));
            avlSegWidth = (avlSegWidth -
                ((progress.cornerRadius === 'Round') ? progress.segmentCount * (constant_1.lineCapRadius * thickness) : 0)) / progress.segmentCount;
            var gap = (progress.cornerRadius === 'Round') ? (gapWidth + (constant_1.lineCapRadius * thickness)) : gapWidth;
            var segmentGroup = progress.renderer.createGroup({ 'id': progress.element.id + id });
            var count = Math.ceil(width / avlWidth);
            var segWidth;
            var color;
            var j = 0;
            var option;
            var segmentPath;
            var tolWidth = (progress.cornerRadius === 'Round') ? (width - (constant_1.lineCapRadius * thickness)) : width;
            var linearThickness = progress.progressThickness || progress.themeStyle.linearProgressThickness;
            for (var i = 0; i < count; i++) {
                segWidth = (tolWidth < avlSegWidth) ? tolWidth : avlSegWidth;
                if (j < progress.segmentColor.length) {
                    color = progress.segmentColor[j];
                    j++;
                }
                else {
                    j = 0;
                    color = progress.segmentColor[j];
                    j++;
                }
                option = new ej2_svg_base_1.PathOption(progress.element.id + id + i, 'none', linearThickness, color, opacity, '0', this.getLinearSegmentPath(locX, locY, segWidth, progress.enableRtl));
                segmentPath = progress.renderer.drawPath(option);
                if (progress.cornerRadius === 'Round') {
                    segmentPath.setAttribute('stroke-linecap', 'round');
                }
                segmentGroup.appendChild(segmentPath);
                locX += (progress.enableRtl) ? -avlSegWidth - gap : avlSegWidth + gap;
                tolWidth -= avlSegWidth + gap;
                tolWidth = (tolWidth < 0) ? 0 : tolWidth;
            }
            return segmentGroup;
        };
        Segment.prototype.getLinearSegmentPath = function (x, y, width, enableRtl) {
            return 'M' + ' ' + x + ' ' + y + ' ' + 'L' + (x + ((enableRtl) ? -width : width)) + ' ' + y;
        };
        Segment.prototype.createCircularSegment = function (progress, id, x, y, r, value, opacity, thickness, totalAngle, progressWidth) {
            var start = progress.startAngle;
            var end = this.widthToAngle(progress.minimum, progress.maximum, value, progress.totalAngle);
            end -= (progress.cornerRadius === 'Round' && progress.totalAngle === constant_1.completeAngle) ?
                this.widthToAngle(0, progressWidth, ((constant_1.lineCapRadius / 2) * thickness), totalAngle) : 0;
            var size = (progressWidth - ((progress.totalAngle === constant_1.completeAngle) ? progress.segmentCount :
                progress.segmentCount - 1) * (progress.gapWidth || progress.themeStyle.circularGapWidth));
            size = (size -
                ((progress.cornerRadius === 'Round') ?
                    (((progress.totalAngle === constant_1.completeAngle) ?
                        progress.segmentCount : progress.segmentCount - 1) * constant_1.lineCapRadius * thickness) : 0)) / progress.segmentCount;
            var avlTolEnd = this.widthToAngle(0, progressWidth, (progressWidth / progress.segmentCount), totalAngle);
            avlTolEnd -= (progress.cornerRadius === 'Round' && progress.totalAngle === constant_1.completeAngle) ?
                this.widthToAngle(0, progressWidth, ((constant_1.lineCapRadius / 2) * thickness), totalAngle) : 0;
            var avlEnd = this.widthToAngle(0, progressWidth, size, totalAngle);
            var gap = this.widthToAngle(0, progressWidth, (progress.gapWidth || progress.themeStyle.circularGapWidth), totalAngle);
            gap += (progress.cornerRadius === 'Round') ? this.widthToAngle(0, progressWidth, (constant_1.lineCapRadius * thickness), totalAngle) : 0;
            var segmentGroup = progress.renderer.createGroup({ 'id': progress.element.id + id });
            var gapCount = Math.floor(end / avlTolEnd);
            var count = Math.ceil((end - gap * gapCount) / avlEnd);
            var segmentPath;
            var circularSegment;
            var segmentEnd;
            var avlSegEnd = (start + ((progress.enableRtl) ? -avlEnd : avlEnd)) % 360;
            var color;
            var j = 0;
            var option;
            var circularThickness = progress.progressThickness || progress.themeStyle.circularProgressThickness;
            for (var i = 0; i < count; i++) {
                segmentEnd = (progress.enableRtl) ? ((progress.startAngle - end > avlSegEnd) ? progress.startAngle - end : avlSegEnd) :
                    ((progress.startAngle + end < avlSegEnd) ? progress.startAngle + end : avlSegEnd);
                segmentPath = helper_1.getPathArc(x, y, r, start, segmentEnd, progress.enableRtl);
                if (j < progress.segmentColor.length) {
                    color = progress.segmentColor[j];
                    j++;
                }
                else {
                    j = 0;
                    color = progress.segmentColor[j];
                    j++;
                }
                option = new ej2_svg_base_1.PathOption(progress.element.id + id + i, 'none', circularThickness, color, opacity, '0', segmentPath);
                circularSegment = progress.renderer.drawPath(option);
                if (progress.cornerRadius === 'Round') {
                    circularSegment.setAttribute('stroke-linecap', 'round');
                }
                segmentGroup.appendChild(circularSegment);
                start = segmentEnd + ((progress.enableRtl) ? -gap : gap);
                avlSegEnd += (progress.enableRtl) ? -avlEnd - gap : avlEnd + gap;
            }
            return segmentGroup;
        };
        Segment.prototype.widthToAngle = function (min, max, value, totalAngle) {
            var angle = ((value - min) / (max - min)) * totalAngle;
            return angle;
        };
        Segment.prototype.createLinearRange = function (totalWidth, progress) {
            var posX = progress.progressRect.x + ((progress.enableRtl) ? progress.progressRect.width : 0);
            var startY = (progress.progressRect.y + (progress.progressRect.height / 2));
            var rangeGroup = progress.renderer.createGroup({ 'id': progress.element.id + '_LinearRangeGroup' });
            var range = progress.rangeColors;
            var thickness = progress.progressThickness || progress.themeStyle.linearProgressThickness;
            var opacity = progress.themeStyle.progressOpacity;
            var rangeMin = progress.minimum;
            var rangeMax = progress.value;
            var gradX = (progress.enableRtl) ? 0.1 : -0.1;
            var gradient;
            var validRange;
            var rangePath;
            var option;
            var startPos;
            var endPos;
            var startX;
            var endX;
            var color;
            var endColor;
            for (var i = 0; i < range.length; i++) {
                validRange = (range[i].start >= rangeMin && range[i].start <= rangeMax &&
                    range[i].end >= rangeMin && range[i].end <= rangeMax);
                startPos = totalWidth * progress.calculateProgressRange(range[i].start, rangeMin, rangeMax);
                endPos = totalWidth * progress.calculateProgressRange(range[i].end, rangeMin, rangeMax);
                startX = posX + ((progress.enableRtl) ? -startPos : startPos);
                endX = posX + ((progress.enableRtl) ? -endPos : endPos);
                startX = (validRange) ? ((progress.isGradient && i > 0) ? startX + gradX : startX) : posX;
                endX = (validRange) ? endX : posX;
                color = (progress.isGradient) ? 'url(#lineRangeGrad_' + i + ')' : range[i].color;
                option = new ej2_svg_base_1.PathOption(progress.element.id + '_LinearRange_' + i, 'none', thickness, color, opacity, '0', 'M' + ' ' + startX + ' ' + startY + ' ' + 'L' + endX + ' ' + startY);
                rangePath = progress.renderer.drawPath(option);
                rangeGroup.appendChild(rangePath);
                if (progress.isGradient) {
                    if (range.length - 1 === i) {
                        endColor = range[i].color;
                    }
                    else {
                        endColor = range[i + 1].color;
                    }
                    gradient = this.setLinearGradientColor(i, range[i].color, endColor, startX, endX, progress);
                    rangeGroup.appendChild(gradient);
                }
            }
            return rangeGroup;
        };
        Segment.prototype.createCircularRange = function (centerX, centerY, radius, progress) {
            var rangeGroup = progress.renderer.createGroup({ 'id': progress.element.id + '_CircularRangeGroup' });
            var range = progress.rangeColors;
            var thickness = progress.progressThickness || progress.themeStyle.linearProgressThickness;
            var opacity = progress.themeStyle.progressOpacity;
            var rangeMin = progress.minimum;
            var rangeMax = progress.value;
            var start = progress.startAngle;
            var tolAngle = this.widthToAngle(progress.minimum, progress.maximum, progress.value, progress.totalAngle);
            var gradient;
            var startAngle;
            var endAngle;
            var rangePath;
            var isValidRange;
            var option;
            var color;
            var endColor;
            for (var i = 0; i < range.length; i++) {
                isValidRange = (range[i].start >= rangeMin && range[i].start <= rangeMax &&
                    range[i].end >= rangeMin && range[i].end <= rangeMax);
                startAngle = this.widthToAngle(rangeMin, rangeMax, range[i].start, tolAngle);
                endAngle = this.widthToAngle(rangeMin, rangeMax, range[i].end, tolAngle);
                startAngle = (isValidRange) ? (start + ((progress.enableRtl) ? -startAngle : startAngle)) % 360 : start;
                endAngle = (isValidRange) ? (start + ((progress.enableRtl) ? -endAngle : endAngle)) % 360 : start;
                color = (progress.isGradient) ? 'url(#circleRangeGrad_' + i + ')' : range[i].color;
                option = new ej2_svg_base_1.PathOption(progress.element.id + '_CircularRange_' + i, 'none', thickness, color, opacity, '0', helper_1.getPathArc(centerX, centerY, radius, startAngle, endAngle, progress.enableRtl));
                rangePath = progress.renderer.drawPath(option);
                rangeGroup.appendChild(rangePath);
                if (progress.isGradient) {
                    if (range.length - 1 === i) {
                        endColor = range[i].color;
                    }
                    else {
                        endColor = range[i + 1].color;
                    }
                    gradient = this.setCircularGradientColor(i, range[i].color, endColor, startAngle, endAngle, radius, centerX, centerY, progress);
                    rangeGroup.appendChild(gradient);
                }
            }
            return rangeGroup;
        };
        Segment.prototype.setLinearGradientColor = function (id, startColor, endColor, start, end, progress) {
            var stopColor = [];
            var option = { id: 'lineRangeGrad_' + id + '', x1: start.toString(), x2: end.toString() };
            stopColor[0] = { color: startColor, colorStop: '50%' };
            stopColor[1] = { color: endColor, colorStop: '100%' };
            var linearGradient = progress.renderer.drawGradient('linearGradient', option, stopColor);
            linearGradient.firstElementChild.setAttribute('gradientUnits', 'userSpaceOnUse');
            return linearGradient;
        };
        Segment.prototype.setCircularGradientColor = function (id, startColor, endColor, start, end, rad, x, y, progress) {
            var stopColor = [];
            var pos1 = helper_1.degreeToLocation(x, y, rad, start);
            var pos2 = helper_1.degreeToLocation(x, y, rad, end);
            var option = {
                id: 'circleRangeGrad_' + id + '', x1: pos1.x.toString(), x2: pos2.x.toString(),
                y1: pos1.y.toString(), y2: pos2.y.toString()
            };
            stopColor[0] = { color: startColor, colorStop: '50%' };
            stopColor[1] = { color: endColor, colorStop: '100%' };
            var linearGradient = progress.renderer.drawGradient('linearGradient', option, stopColor);
            linearGradient.firstElementChild.setAttribute('gradientUnits', 'userSpaceOnUse');
            return linearGradient;
        };
        return Segment;
    }());
    exports.Segment = Segment;
});