all files / chart/axis/ strip-line.js

98.49% Statements 196/199
90.72% Branches 215/237
100% Functions 23/23
98.49% Lines 196/199
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     265× 265× 265× 265× 265× 139× 139×     126×         118×   118×       265× 265× 265× 265× 265×   265× 21×   21×   21× 21× 10×   10×     11× 11×     265× 259×     286× 286×     286× 286× 286× 286×     293× 293× 293× 44× 44× 11× 11×   44×   26×                   249×     572× 25×   547× 30×   572×   12× 12× 12×   138× 138× 138× 138× 138× 138× 138× 138×         138×       138× 138×   138× 336× 336× 336× 302× 302× 150×   20×   150× 18×   18×   18×           18× 18× 133× 133× 133× 133×   133× 133×       132× 132×         138×   48×   12×   264× 264× 264× 264×     264× 228×     228×   264×   264× 264× 264×           49× 49× 49× 49× 49× 49× 49× 49× 49× 29× 29×   29× 29×     20× 20× 20× 20×   49×   28×       28×   133×     126×     20× 20× 16×     10×         265× 265×     264× 264× 264×   265× 49×     49× 49×       49×   98× 98×   12× 12×   19× 19×   98×   41538×          
define(["require", "exports", "@syncfusion/ej2-base", "../../common/utils/helper", "@syncfusion/ej2-svg-base", "@syncfusion/ej2-data"], function (require, exports, ej2_base_1, helper_1, ej2_svg_base_1, ej2_data_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var StripLine = (function () {
        function StripLine() {
        }
        StripLine.prototype.measureStripLine = function (axis, stripline, seriesClipRect, startValue, segmentAxis, chart) {
            var actualStart;
            var actualEnd;
            var orientation = axis.orientation;
            var isDateTimeAxis = axis.valueType === 'DateTime';
            if (stripline.isRepeat && stripline.size !== null) {
                actualStart = startValue;
                actualEnd = null;
            }
            else {
                if (axis.valueType === 'DateTimeCategory') {
                    var start = stripline.start;
                    var end = stripline.end;
                    actualStart = (start != null && typeof start !== 'number') ?
                        axis.labels.indexOf(this.dateToMilliSeconds(start, chart).toString()) : start;
                    actualEnd = (end != null && typeof end !== 'number') ?
                        axis.labels.indexOf(this.dateToMilliSeconds(end, chart).toString()) : end;
                }
                else {
                    actualStart = stripline.start === null ? null : isDateTimeAxis && this.isCoreDate(stripline.start) ?
                        this.dateToMilliSeconds(stripline.start, chart) : +(axis.valueType === 'Logarithmic' ? helper_1.logBase(stripline.start, axis.logBase) : stripline.start);
                    actualEnd = stripline.end === null ? null : isDateTimeAxis && this.isCoreDate(stripline.start) ?
                        this.dateToMilliSeconds(stripline.end, chart) : +(axis.valueType === 'Logarithmic' ? helper_1.logBase(stripline.end, axis.logBase) : stripline.end);
                }
            }
            var rect = this.getFromTovalue(actualStart, actualEnd, stripline.size, stripline.startFromAxis, axis, stripline);
            var height = (orientation === 'Vertical') ? (rect.to - rect.from) * axis.rect.height : seriesClipRect.height;
            var width = (orientation === 'Horizontal') ? (rect.to - rect.from) * axis.rect.width : seriesClipRect.width;
            var x = (orientation === 'Vertical') ? seriesClipRect.x : ((rect.from * axis.rect.width) + axis.rect.x);
            var y = (orientation === 'Horizontal') ? seriesClipRect.y : (axis.rect.y + axis.rect.height -
                ((stripline.sizeType === 'Pixel' ? rect.from : rect.to) * axis.rect.height));
            if (stripline.isSegmented && stripline.segmentStart != null && stripline.segmentEnd != null && stripline.sizeType !== 'Pixel') {
                var start = isDateTimeAxis && this.isCoreDate(stripline.segmentStart) ?
                    this.dateToMilliSeconds(stripline.segmentStart, chart) : +stripline.segmentStart;
                var end = isDateTimeAxis && this.isCoreDate(stripline.segmentEnd) ?
                    this.dateToMilliSeconds(stripline.segmentEnd, chart) : +stripline.segmentEnd;
                var segRect = this.getFromTovalue(start, end, null, null, segmentAxis, stripline);
                if (segmentAxis.orientation === 'Vertical') {
                    y = (segmentAxis.rect.y + segmentAxis.rect.height -
                        (segRect.to * segmentAxis.rect.height));
                    height = (segRect.to - segRect.from) * segmentAxis.rect.height;
                }
                else {
                    x = ((segRect.from * segmentAxis.rect.width) + segmentAxis.rect.x);
                    width = (segRect.to - segRect.from) * segmentAxis.rect.width;
                }
            }
            if ((height !== 0 && width !== 0) || (stripline.sizeType === 'Pixel' && (stripline.start !== null || stripline.startFromAxis))) {
                return new ej2_svg_base_1.Rect(x, y, width, height);
            }
            return new ej2_svg_base_1.Rect(0, 0, 0, 0);
        };
        StripLine.prototype.getFromTovalue = function (start, end, size, startFromAxis, axis, stripline) {
            var from = (!stripline.isRepeat && startFromAxis) ? axis.visibleRange.min : start;
            Iif (axis.valueType === 'Double' && size !== null && !startFromAxis && stripline.start == null) {
                from += size;
            }
            var to = this.getToValue(Math.max(start, ej2_base_1.isNullOrUndefined(end) ? start : end), from, size, axis, end, stripline);
            from = this.findValue(from, axis);
            to = this.findValue(to, axis);
            return { from: helper_1.valueToCoefficient(axis.isAxisInverse ? to : from, axis),
                to: helper_1.valueToCoefficient(axis.isAxisInverse ? from : to, axis) };
        };
        StripLine.prototype.getToValue = function (to, from, size, axis, end, stripline) {
            var sizeType = stripline.sizeType;
            var isEnd = (end === null);
            if (axis.valueType === 'DateTime') {
                var fromValue = new Date(from);
                if (sizeType === 'Auto') {
                    sizeType = axis.actualIntervalType;
                    size *= axis.visibleRange.interval;
                }
                switch (sizeType) {
                    case 'Years':
                        return (isEnd ? new Date(fromValue.setFullYear(fromValue.getFullYear() + size)) : to);
                    case 'Months':
                        return (isEnd ? new Date(fromValue.setMonth(fromValue.getMonth() + size)) : to);
                    case 'Days':
                        return (isEnd ? new Date(fromValue.setDate(fromValue.getDate() + size)) : to);
                    case 'Hours':
                        return (isEnd ? new Date(fromValue.setHours(fromValue.getHours() + size)) : to);
                    case 'Minutes':
                        return (isEnd ? new Date(fromValue.setMinutes(fromValue.getMinutes() + size)) : to);
                    case 'Seconds':
                        return (isEnd ? new Date(fromValue.setSeconds(fromValue.getSeconds() + size)) : to);
                    default:
                        return from;
                }
            }
            else {
                return stripline.sizeType === 'Pixel' ? from : (isEnd ? (from + size) : to);
            }
        };
        StripLine.prototype.findValue = function (value, axis) {
            if (value < axis.visibleRange.min) {
                value = axis.visibleRange.min;
            }
            else if (value > axis.visibleRange.max) {
                value = axis.visibleRange.max;
            }
            return value;
        };
        StripLine.prototype.dateParse = function (value, chart) {
            var dateParser = chart.intl.getDateParser({ skeleton: 'full', type: 'dateTime' });
            var dateFormatter = chart.intl.getDateFormat({ skeleton: 'full', type: 'dateTime' });
            return new Date((Date.parse(dateParser(dateFormatter(new Date(ej2_data_1.DataUtil.parse.parseJson({ val: value }).val))))));
        };
        StripLine.prototype.renderStripLine = function (chart, position, axes) {
            var id = chart.element.id + '_stripline_' + position + '_';
            var seriesClipRect = chart.chartAxisLayoutPanel.seriesClipRect;
            var end = 0;
            var limit = 0;
            var startValue = 0;
            var segmentAxis = null;
            var range;
            var options = new helper_1.RectOption(id + 'ClipRect', 'transparent', { width: 1, color: 'Gray' }, 1, {
                x: seriesClipRect.x, y: seriesClipRect.y,
                width: seriesClipRect.width,
                height: seriesClipRect.height
            });
            var striplineGroup = chart.renderer.createGroup({
                id: id + 'collections',
                'clip-path': 'url(#' + id + 'ClipRect' + ')'
            });
            Eif (!chart.enableCanvas) {
                striplineGroup.appendChild(helper_1.appendClipElement(chart.redraw, options, chart.renderer));
            }
            for (var _i = 0, axes_1 = axes; _i < axes_1.length; _i++) {
                var axis = axes_1[_i];
                var count = 0;
                for (var _a = 0, _b = axis.stripLines; _a < _b.length; _a++) {
                    var stripline = _b[_a];
                    if (stripline.visible && stripline.zIndex === position) {
                        if (stripline.isSegmented && stripline.segmentStart != null && stripline.segmentEnd != null &&
                            stripline.sizeType !== 'Pixel') {
                            segmentAxis = this.getSegmentAxis(axes, axis, stripline);
                        }
                        if (stripline.isRepeat && stripline.repeatEvery != null && stripline.size !== null && stripline.sizeType !== 'Pixel') {
                            limit = (stripline.repeatUntil != null) ? ((axis.valueType === 'DateTime') ?
                                this.dateToMilliSeconds(stripline.repeatUntil, chart) : +stripline.repeatUntil) : axis.actualRange.max;
                            startValue = axis.valueType === 'DateTime' && this.isCoreDate(stripline.start) ?
                                this.dateToMilliSeconds(stripline.start, chart) : stripline.start;
                            if ((stripline.startFromAxis && axis.valueType === 'DateTime' && stripline.sizeType === 'Auto') ||
                                (stripline.start < axis.visibleRange.min)) {
                                startValue = axis.visibleLabels[0] &&
                                    axis.visibleLabels[0].value === axis.visibleRange.min ? axis.visibleRange.min :
                                    axis.visibleLabels[0] && axis.visibleLabels[0].value - (axis.valueType === 'DateTime' ? axis.dateTimeInterval :
                                        axis.visibleRange.interval);
                            }
                            startValue = stripline.startFromAxis && axis.valueType !== 'DateTime' ? axis.visibleRange.min : startValue;
                            while (startValue < limit) {
                                end = (startValue + (axis.valueType === 'DateTime' ? axis.dateTimeInterval * +stripline.size : stripline.size));
                                range = helper_1.withIn(end, axis.visibleRange);
                                Eif ((startValue >= axis.visibleRange.min && startValue < axis.visibleRange.max) || range) {
                                    this.renderStripLineElement(axis, stripline, seriesClipRect, id, striplineGroup, chart, startValue, segmentAxis, count);
                                }
                                count++;
                                startValue = this.getStartValue(axis, stripline, startValue);
                            }
                        }
                        else {
                            this.renderStripLineElement(axis, stripline, seriesClipRect, id, striplineGroup, chart, null, segmentAxis, count);
                            count++;
                        }
                    }
                }
            }
            helper_1.appendChildElement(chart.enableCanvas, chart.svgObject, striplineGroup, chart.redraw);
        };
        StripLine.prototype.isCoreDate = function (value) {
            return typeof value === 'string' ? true : false;
        };
        StripLine.prototype.dateToMilliSeconds = function (value, chart) {
            return this.dateParse(value, chart).getTime();
        };
        StripLine.prototype.renderPath = function (stripline, rect, id, parent, chart, axis) {
            var element = helper_1.getElement(id);
            var direction = element ? element.getAttribute('d') : '';
            var strokeWidth = stripline.size;
            var d = (axis.orientation === 'Vertical') ? ('M ' + rect.x + ' ' + rect.y + ' ' + 'L ' + (rect.x + rect.width)
                + ' ' + rect.y) :
                ('M ' + rect.x + ' ' + rect.y + ' ' + 'L ' + rect.x + ' ' + (rect.y + rect.height));
            if (stripline.sizeType !== 'Pixel') {
                d = (axis.orientation === 'Vertical') ? ('M ' + rect.x + ' ' + (rect.y + (rect.height / 2)) + ' ' + 'L ' + (rect.x + rect.width)
                    + ' ' + (rect.y + (rect.height / 2))) :
                    ('M ' + (rect.x + (rect.width / 2)) + ' ' + rect.y + ' ' + 'L ' + (rect.x + (rect.width / 2)) + ' ' + (rect.y + rect.height));
                strokeWidth = axis.orientation === 'Vertical' ? rect.height : rect.width;
            }
            helper_1.appendChildElement(chart.enableCanvas, parent, chart.renderer.drawPath(new ej2_svg_base_1.PathOption(id, 'none', strokeWidth, stripline.color, stripline.opacity, stripline.dashArray, d)), chart.redraw, true, 'x', 'y', null, direction, true, null, null, chart.duration);
        };
        StripLine.prototype.renderRectangle = function (stripline, rect, id, parent, chart) {
            var element = helper_1.getElement(id);
            var previousRect = element ? new ej2_svg_base_1.Rect(+element.getAttribute('x'), +element.getAttribute('y'), +element.getAttribute('width'), +element.getAttribute('height')) : null;
            helper_1.appendChildElement(chart.enableCanvas, parent, chart.renderer.drawRectangle(new helper_1.RectOption(id, 'none', stripline.border, stripline.opacity, rect, 0, 0, '', stripline.border.dashArray)), chart.redraw, true, 'x', 'y', null, null, true, true, previousRect, chart.duration);
        };
        StripLine.prototype.drawImage = function (stripline, rect, id, parent, chart) {
            Iif (stripline.sizeType === 'Pixel') {
                rect.width = rect.width ? rect.width : stripline.size;
                rect.height = rect.height ? rect.height : stripline.size;
            }
            var image = new helper_1.ImageOption(rect.height, rect.width, stripline.imageUrl, rect.x, rect.y, id, 'visible', 'none');
            var htmlObject = chart.renderer.drawImage(image);
            helper_1.appendChildElement(chart.enableCanvas, parent, htmlObject, chart.redraw, true, 'x', 'y', null, null, true, true);
        };
        StripLine.prototype.renderText = function (stripline, rect, id, parent, chart, axis) {
            var textSize = ej2_svg_base_1.measureText(stripline.text, stripline.textStyle, chart.themeStyle.stripLineLabelFont);
            var isRotationNull = (stripline.rotation === null);
            var textMid = isRotationNull ? 3 * (textSize.height / 8) : 0;
            var ty = rect.y + (rect.height / 2) + textMid;
            var rotation = isRotationNull ? ((axis.orientation === 'Vertical') ? 0 : -90) : stripline.rotation;
            var tx = rect.x + (rect.width / 2);
            var anchor;
            var padding = 5;
            if (axis.orientation === 'Horizontal') {
                tx = this.getTextStart(tx + (textMid * this.factor(stripline.horizontalAlignment)), rect.width, stripline.horizontalAlignment);
                ty = this.getTextStart(ty - textMid, rect.height, stripline.verticalAlignment) +
                    (stripline.verticalAlignment === 'Start' && !isRotationNull ? (textSize.height / 4) : 0);
                anchor = isRotationNull ? this.invertAlignment(stripline.verticalAlignment) : stripline.horizontalAlignment;
                anchor = tx - textSize.width / 2 < axis.rect.x ? 'Start' : tx + textSize.width / 2 > axis.rect.width ? 'End' : anchor;
            }
            else {
                tx = this.getTextStart(tx, rect.width, stripline.horizontalAlignment);
                ty = this.getTextStart(ty + (textMid * this.factor(stripline.verticalAlignment)) - padding, rect.height, stripline.verticalAlignment);
                anchor = stripline.horizontalAlignment;
                anchor = chart.enableRtl ? (anchor === 'End' ? 'Start' : anchor === 'Start' ? 'End' : anchor) : anchor;
            }
            helper_1.textElement(chart.renderer, new ej2_svg_base_1.TextOption(id, tx, ty, anchor, stripline.text, 'rotate(' + rotation + ' ' + tx + ',' + ty + ')', 'middle'), stripline.textStyle, stripline.textStyle.color || chart.themeStyle.stripLineLabelFont.color, parent, null, null, null, null, null, null, null, null, chart.enableCanvas, null, chart.themeStyle.stripLineLabelFont);
        };
        StripLine.prototype.invertAlignment = function (anchor) {
            switch (anchor) {
                case 'Start':
                    anchor = 'End';
                    break;
                case 'End':
                    anchor = 'Start';
                    break;
            }
            return anchor;
        };
        StripLine.prototype.getStartValue = function (axis, stripline, startValue) {
            if (axis.valueType === 'DateTime') {
                return (this.getToValue(null, startValue, +stripline.repeatEvery, axis, null, stripline));
            }
            else {
                return startValue + (+stripline.repeatEvery);
            }
        };
        StripLine.prototype.getSegmentAxis = function (axes, axis, stripline) {
            var segment;
            if (stripline.segmentAxisName == null) {
                return (axis.orientation === 'Horizontal') ? axes[1] : axes[0];
            }
            else {
                for (var i = 0; i < axes.length; i++) {
                    if (stripline.segmentAxisName === axes[i].name) {
                        segment = axes[i];
                    }
                }
                return segment;
            }
        };
        StripLine.prototype.renderStripLineElement = function (axis, stripline, seriesClipRect, id, striplineGroup, chart, startValue, segmentAxis, count) {
            var rect = this.measureStripLine(axis, stripline, seriesClipRect, startValue, segmentAxis, chart);
            if (stripline.imageUrl) {
                this.drawImage(stripline, rect, id + 'rect_' + axis.name + '_' + count, striplineGroup, chart);
            }
            else {
                this.renderPath(stripline, rect, id + (stripline.sizeType === 'Pixel' ? 'path_' : 'rect_') + axis.name + '_' + count, striplineGroup, chart, axis);
                var pixelRect = new ej2_svg_base_1.Rect(axis.orientation === 'Horizontal' ? (rect.x - stripline.size / 2) : rect.x, axis.orientation === 'Vertical' ? (rect.y - stripline.size / 2) : rect.y, rect.width ? rect.width : stripline.size, rect.height ? rect.height : stripline.size);
                this.renderRectangle(stripline, stripline.sizeType === 'Pixel' ? pixelRect : rect, id + 'border_' + axis.name + '_' + count, striplineGroup, chart);
            }
            if (stripline.text !== '') {
                this.renderText(stripline, rect, id + 'text_' + axis.name + '_' + count, striplineGroup, chart, axis);
            }
        };
        StripLine.prototype.factor = function (anchor) {
            var factor = 0;
            switch (anchor) {
                case 'Start':
                    factor = 1;
                    break;
                case 'End':
                    factor = -1;
                    break;
            }
            return factor;
        };
        StripLine.prototype.getTextStart = function (xy, size, textAlignment) {
            var padding = 5;
            switch (textAlignment) {
                case 'Start':
                    xy = xy - (size / 2) + padding;
                    break;
                case 'End':
                    xy = xy + (size / 2) - padding;
                    break;
            }
            return xy;
        };
        StripLine.prototype.getModuleName = function () {
            return 'StripLine';
        };
        StripLine.prototype.destroy = function () {
        };
        return StripLine;
    }());
    exports.StripLine = StripLine;
});