all files / chart/series/ error-bar.js

98.98% Statements 195/197
94.74% Branches 108/114
100% Functions 20/20
98.98% Lines 195/197
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   56×   67×   66× 66×   66× 66× 66× 66× 66× 66× 66× 66× 66× 66× 66× 66× 66× 66× 528× 528× 512× 512× 512×   184× 184×   152× 152×   176× 176× 176×   512× 512× 512× 512× 512× 512× 512× 512× 512×       512× 512× 512× 512× 512×     512×     512× 512× 312× 312×     200× 200×   512×     512× 512× 512×   512×   512×   248× 248×   88× 88× 88×   24× 24× 24× 24×   56× 56× 56× 56× 56×   96× 96×   344× 344× 344× 344×   344×   344×   344× 344× 344× 344×   344×   344×   176× 176× 176× 176× 176× 176× 176×   512× 512× 512× 512× 512× 512× 512× 512×   176× 176×   152× 152×   184× 184×   512×   168× 168×   168× 168×   176× 176×   512×   80× 80× 80× 80× 80× 80× 80× 80× 80× 80× 80× 80× 80×   32× 32× 32×   32× 32× 32×   16× 16× 16× 16×   80× 640× 640× 256×   384× 256×     128× 128×     80× 80× 80× 80× 80×   66× 66× 66× 66× 66× 66× 66× 66×       66×         66×         16× 128×     128× 128×   16×     20665×          
define(["require", "exports", "../../common/utils/helper", "../../common/utils/helper"], function (require, exports, helper_1, helper_2) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var ErrorBar = (function () {
        function ErrorBar(chart) {
            this.chart = chart;
        }
        ErrorBar.prototype.render = function (series) {
            if (this.chart.chartAreaType === 'PolarRadar') {
                return null;
            }
            this.createElement(series);
            this.renderErrorBar(series);
        };
        ErrorBar.prototype.renderErrorBar = function (series) {
            var seriesIndex = series.index;
            var point;
            var symbolId;
            var capId;
            var errorbar = series.errorBar;
            var errorBarCap = series.errorBar.errorBarCap;
            var border = series.border;
            var start;
            var mid;
            var end;
            var isInverted;
            var isVert;
            var errorDirection = ['', ''];
            for (var _i = 0, _a = series.points; _i < _a.length; _i++) {
                var point_1 = _a[_i];
                if (point_1.visible && point_1.symbolLocations[0]) {
                    var errorX = 0;
                    var errorY = 0;
                    switch (errorbar.mode) {
                        case 'Vertical':
                            errorY = errorbar.verticalError;
                            break;
                        case 'Horizontal':
                            errorX = errorbar.horizontalError;
                            break;
                        case 'Both':
                            errorX = errorbar.horizontalError;
                            errorY = errorbar.verticalError;
                            break;
                    }
                    errorDirection = this['calculate' + errorbar.type + 'Value'](point_1, series, this.chart.requireInvertedAxis, errorX, errorY);
                    symbolId = this.chart.element.id + '_Series_' + '_ErrorBarGroup_' + seriesIndex + '_Point_' + point_1.index;
                    capId = this.chart.element.id + '_Series_' + '_ErrorBarCap_' + seriesIndex + '_Point_' + point_1.index;
                    var shapeOption = new helper_2.PathOption(symbolId, '', errorbar.width, errorbar.color || this.chart.themeStyle.errorBar, null, '', errorDirection[0]);
                    var path = this.chart.renderer.drawPath(shapeOption);
                    series.errorBarElement.appendChild(path);
                    var capOption = new helper_2.PathOption(capId, '', errorBarCap.width, errorBarCap.color || this.chart.themeStyle.errorBar, null, '', errorDirection[1]);
                    var capPath = this.chart.renderer.drawPath(capOption);
                    series.errorBarElement.appendChild(capPath);
                }
            }
        };
        ErrorBar.prototype.findLocation = function (point, series, isInverted, x1, y1) {
            var errorbar = series.errorBar;
            var direction = errorbar.direction;
            var location = [];
            var stackedValue = series.stackedValues;
            var yValue = series.type.indexOf('Stacking') > -1 ? series.stackedValues.endValues[point.index] :
                (series.seriesType === 'HighLow' || series.seriesType === 'HighLowOpenClose') ? (series.points[point.index].high) :
                    point.yValue;
            var startPoint = helper_2.getPoint(point.xValue + ((direction === 'Plus' || direction === 'Both') ? (errorbar.type === 'Custom' &&
                (errorbar.mode === 'Horizontal' || errorbar.mode === 'Both')) ? x1 = errorbar.horizontalPositiveError : x1 : 0), yValue + ((direction === 'Plus' || direction === 'Both') ? (errorbar.type === 'Custom' &&
                (errorbar.mode === 'Vertical' || errorbar.mode === 'Both')) ? y1 = errorbar.verticalPositiveError : y1 : 0), series.xAxis, series.yAxis, isInverted);
            location.push(startPoint);
            if (series.isRectSeries) {
                var midPoint = point.symbolLocations[0];
                location.push(midPoint);
            }
            else {
                var midPoint = helper_2.getPoint(point.xValue, point.yValue, series.xAxis, series.yAxis, isInverted);
                location.push(midPoint);
            }
            var endPoint = helper_2.getPoint(point.xValue - ((direction === 'Minus' || direction === 'Both') ? (errorbar.type === 'Custom' &&
                (errorbar.mode === 'Horizontal' || errorbar.mode === 'Both')) ? x1 = errorbar.horizontalNegativeError : x1 : 0), yValue - ((direction === 'Minus' || direction === 'Both') ? (errorbar.type === 'Custom' &&
                (errorbar.mode === 'Vertical' || errorbar.mode === 'Both')) ? y1 = errorbar.verticalNegativeError : y1 : 0), series.xAxis, series.yAxis, isInverted);
            location.push(endPoint);
            point.error = (errorbar.mode === 'Vertical') ? errorbar.verticalError : errorbar.horizontalError;
            this.negativeHeight = (errorbar.mode === 'Vertical' || errorbar.mode === 'Both') ? (isInverted ? (location[1].x - location[2].x) :
                (location[2].y - location[1].y)) : 0;
            this.positiveHeight = (errorbar.mode === 'Vertical' || errorbar.mode === 'Both') ? (isInverted ? (location[0].x - location[1].x) :
                (location[1].y - location[0].y)) : 0;
            return this.getErrorDirection(location[0], location[1], location[2], series, isInverted);
        };
        ErrorBar.prototype.calculateFixedValue = function (point, series, isInverted, errorX, errorY, xAxis, yAxis) {
            var errorbar = series.errorBar;
            return this.findLocation(point, series, isInverted, errorX, errorY);
        };
        ErrorBar.prototype.calculatePercentageValue = function (point, series, isInverted, errorX, errorY, xAxis, yAxis) {
            errorX = (errorX / 100) * point.xValue;
            errorY = (errorY / 100) * point.yValue;
            return this.findLocation(point, series, isInverted, errorX, errorY);
        };
        ErrorBar.prototype.calculateStandardDeviationValue = function (point, series, isInverted, errorX, errorY, xAxis, yAxis) {
            var getMean = this.meanCalculation(series, series.errorBar.mode);
            errorX = errorX * (getMean.horizontalSquareRoot + getMean.horizontalMean);
            errorY = errorY * (getMean.verticalSquareRoot + getMean.verticalMean);
            return this.findLocation(point, series, isInverted, errorX, errorY);
        };
        ErrorBar.prototype.calculateStandardErrorValue = function (point, series, isInverted, errorX, errorY, xAxis, yAxis) {
            var length = series.points.length;
            var getMean = this.meanCalculation(series, series.errorBar.mode);
            errorX = ((errorX * getMean.horizontalSquareRoot) / Math.sqrt(length));
            errorY = ((errorY * getMean.verticalSquareRoot) / Math.sqrt(length));
            return this.findLocation(point, series, isInverted, errorX, errorY);
        };
        ErrorBar.prototype.calculateCustomValue = function (point, series, isInverted, errorX, errorY, xAxis, yAxis) {
            var errorbar = series.errorBar;
            return this.findLocation(point, series, isInverted, errorX, errorY);
        };
        ErrorBar.prototype.getHorizontalDirection = function (start, mid, end, direction, errorMode, capLength) {
            var path = '';
            var capDirection = '';
            path += ' M ' + start.x + ' ' + mid.y + ' L ' + end.x + ' ' + mid.y;
            capDirection += (direction === 'Plus' || direction === 'Both') ? ' M ' + (start.x) + ' ' + (mid.y - capLength) + ' L '
                + (start.x) + ' ' + (mid.y + capLength) : '';
            capDirection += (direction === 'Minus' || direction === 'Both') ? ' M ' + (end.x) + ' ' + (mid.y - capLength) + ' L '
                + (end.x) + ' ' + (mid.y + capLength) : ' ';
            return [path, capDirection];
        };
        ErrorBar.prototype.getVerticalDirection = function (start, mid, end, direction, errorMode, capLength) {
            var path = '';
            var capDirection = '';
            path += ' M ' + mid.x + ' ' + start.y + ' L ' + mid.x + ' ' + end.y;
            capDirection += (direction === 'Plus' || direction === 'Both') ? ' M ' + (mid.x - capLength) + ' ' + start.y + ' L '
                + (mid.x + capLength) + ' ' + start.y : '';
            capDirection += (direction === 'Minus' || direction === 'Both') ? ' M ' + (mid.x - capLength) + ' ' + end.y + ' L '
                + (mid.x + capLength) + ' ' + end.y : '';
            return [path, capDirection];
        };
        ErrorBar.prototype.getBothDirection = function (start, mid, end, direction, errorMode, capLength) {
            var capDirection = '';
            var path = '';
            var pathH = this.getHorizontalDirection(start, mid, end, direction, errorMode, capLength);
            var pathV = this.getVerticalDirection(start, mid, end, direction, errorMode, capLength);
            path = pathH[0].concat(pathV[0]);
            capDirection = pathH[1].concat(pathV[1]);
            return [path, capDirection];
        };
        ErrorBar.prototype.getErrorDirection = function (start, mid, end, series, isInverted) {
            var direction = series.errorBar.direction;
            var mode = series.errorBar.mode;
            var capLength = series.errorBar.errorBarCap.length;
            var path = '';
            var paths;
            var capDirection = '';
            var errorMode = mode;
            switch (mode) {
                case 'Both':
                    errorMode = mode;
                    break;
                case 'Horizontal':
                    errorMode = (isInverted) ? 'Vertical' : mode;
                    break;
                case 'Vertical':
                    errorMode = (isInverted) ? 'Horizontal' : mode;
                    break;
            }
            switch (errorMode) {
                case 'Horizontal':
                    paths = this.getHorizontalDirection(start, mid, end, direction, errorMode, capLength);
                    break;
                case 'Vertical':
                    paths = this.getVerticalDirection(start, mid, end, direction, errorMode, capLength);
                    break;
                case 'Both':
                    paths = this.getBothDirection(start, mid, end, direction, errorMode, capLength);
                    break;
            }
            return [paths[0], paths[1]];
        };
        ErrorBar.prototype.meanCalculation = function (series, mode) {
            var sumOfX = 0;
            var sumOfY = 0;
            var verticalMean = 0;
            var horizontalMean = 0;
            var horSquareDev;
            var verStandardMean;
            var horStandardMean;
            var verSquareTotal;
            var horSquareTotal;
            var verSquareRoot;
            var horSquareRoot;
            var length = series.points.length;
            switch (mode) {
                case 'Vertical':
                    sumOfY = helper_2.sum(series.yData);
                    verticalMean = sumOfY / length;
                    break;
                case 'Horizontal':
                    sumOfX = helper_2.sum(series.xData);
                    horizontalMean = sumOfX / length;
                    break;
                case 'Both':
                    sumOfY = helper_2.sum(series.yData);
                    verticalMean = sumOfY / length;
                    sumOfX = helper_2.sum(series.xData);
                    horizontalMean = sumOfX / length;
            }
            for (var _i = 0, _a = series.points; _i < _a.length; _i++) {
                var point = _a[_i];
                if (mode === 'Vertical') {
                    sumOfY = sumOfY + Math.pow((point.yValue - verticalMean), 2);
                }
                else if (mode === 'Horizontal') {
                    sumOfX = sumOfX + Math.pow((point.xValue - horizontalMean), 2);
                }
                else {
                    sumOfY = sumOfY + Math.pow((point.yValue - verticalMean), 2);
                    sumOfX = sumOfX + Math.pow((point.xValue - horizontalMean), 2);
                }
            }
            verStandardMean = sumOfY / (length - 1);
            verSquareRoot = Math.sqrt(sumOfY / (length - 1));
            horStandardMean = sumOfX / (length - 1);
            horSquareRoot = Math.sqrt(sumOfX / (length - 1));
            return new helper_1.Mean(verStandardMean, verSquareRoot, horStandardMean, horSquareRoot, verticalMean, horizontalMean);
        };
        ErrorBar.prototype.createElement = function (series) {
            var explodeValue = 5;
            var render = series.chart.renderer;
            var transform;
            transform = series.chart.chartAreaType === 'Cartesian' ? 'translate(' + series.clipRect.x + ',' + (series.clipRect.y) + ')' : '';
            var markerHeight = (series.marker.height + explodeValue) / 2;
            var markerWidth = (series.marker.width + explodeValue) / 2;
            Eif (series.chart.chartAreaType === 'Cartesian') {
                var errorBarClipRect = render.drawClipPath(new helper_1.RectOption(this.chart.element.id + '_ChartErrorBarClipRect_' + series.index, 'transparent', { width: 1, color: 'Gray' }, 1, {
                    x: -markerWidth, y: -markerHeight,
                    width: series.clipRect.width + markerWidth * 2, height: series.clipRect.height + markerHeight * 2
                }));
                series.errorBarElement = render.createGroup({
                    'id': this.chart.element.id + 'ErrorBarGroup' + series.index,
                    'transform': transform,
                    'clip-path': 'url(#' + this.chart.element.id + '_ChartErrorBarClipRect_' + series.index + ')'
                });
                series.errorBarElement.appendChild(errorBarClipRect);
            }
        };
        ErrorBar.prototype.doErrorBarAnimation = function (series) {
            var errorBarElements = series.errorBarElement.childNodes;
            Iif (!errorBarElements) {
                return null;
            }
            var delay = series.animation.delay + series.animation.duration;
            var j = 1;
            while (j < errorBarElements.length) {
                for (var i = 0; i < series.points.length; i++) {
                    Iif (!series.points[i].symbolLocations[0]) {
                        continue;
                    }
                    errorBarElements[j].style.visibility = 'hidden';
                    helper_2.templateAnimate(errorBarElements[j], delay, 350, series.chart.requireInvertedAxis ? 'SlideLeftIn' : 'SlideBottomIn', false);
                }
                j++;
            }
        };
        ErrorBar.prototype.getModuleName = function () {
            return 'ErrorBar';
        };
        ErrorBar.prototype.destroy = function (chart) {
        };
        return ErrorBar;
    }());
    exports.ErrorBar = ErrorBar;
});