all files / common/annotation/ annotation.js

100% Statements 95/95
98.7% Branches 76/77
100% Functions 10/10
100% Lines 95/95
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   96×   128× 128× 128×       128×   61× 61×         61×     61×   61×   67× 67× 40× 40× 40× 40× 40× 40× 40× 40× 40× 40× 154× 154× 39× 39× 19×   19×     14×     20×       18×     115× 34× 34×     35× 32×     32× 32×       32×     27×     128× 128× 128× 128× 128× 113×     27× 27× 27× 89× 89× 13×         76× 19× 19×       27× 20× 20× 20×         113× 113×       113× 113× 109× 109× 109× 109×     218×     12× 12×     12× 12×     194× 194×   218×        
define(["require", "exports", "../utils/helper", "../utils/helper", "../utils/helper", "@syncfusion/ej2-base", "../model/constants", "@syncfusion/ej2-data"], function (require, exports, helper_1, helper_2, helper_3, ej2_base_1, constants_1, ej2_data_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var AnnotationBase = (function () {
        function AnnotationBase(control) {
            this.control = control;
        }
        AnnotationBase.prototype.render = function (annotation, index) {
            this.isChart = this.control.getModuleName() === 'chart';
            this.annotation = annotation;
            var childElement = helper_1.createTemplate(ej2_base_1.createElement('div', {
                id: this.control.element.id + '_Annotation_' + index,
                styles: 'position: absolute;'
            }), index, annotation.content, this.control);
            return childElement;
        };
        AnnotationBase.prototype.setAnnotationPixelValue = function (location) {
            var rect;
            rect = this.annotation.region === 'Chart' ?
                new helper_2.Rect(0, 0, this.control.availableSize.width, this.control.availableSize.height) :
                this.isChart ?
                    this.control.chartAxisLayoutPanel.seriesClipRect :
                    this.control.series[0].accumulationBound;
            location.x = ((typeof this.annotation.x !== 'string') ?
                ((typeof this.annotation.x === 'number') ? this.annotation.x : 0) :
                helper_2.stringToNumber(this.annotation.x, rect.width)) + rect.x;
            location.y = ((typeof this.annotation.y === 'number') ? this.annotation.y :
                helper_2.stringToNumber(this.annotation.y, rect.height)) + rect.y;
            return true;
        };
        AnnotationBase.prototype.setAnnotationPointValue = function (location) {
            var symbolLocation = new helper_2.ChartLocation(0, 0);
            if (this.isChart) {
                var xAxis = void 0;
                var yAxis = void 0;
                var chart = this.control;
                var annotation = this.annotation;
                var xValue = void 0;
                var isLog = false;
                var xAxisName = annotation.xAxisName;
                var yAxisName = annotation.yAxisName;
                var isInverted = chart.requireInvertedAxis;
                for (var _i = 0, _a = chart.axisCollections; _i < _a.length; _i++) {
                    var axis = _a[_i];
                    if (xAxisName === axis.name || (xAxisName == null && axis.name === 'primaryXAxis')) {
                        xAxis = axis;
                        if (xAxis.valueType.indexOf('Category') > -1) {
                            var xAnnotation = xAxis.valueType === 'DateTimeCategory' ? (annotation.x.getTime()).toString() :
                                annotation.x;
                            if (xAxis.labels.indexOf(xAnnotation) < 0) {
                                return false;
                            }
                            else {
                                xValue = xAxis.labels.indexOf(xAnnotation);
                            }
                        }
                        else if (xAxis.valueType === 'DateTime') {
                            var option = { skeleton: 'full', type: 'dateTime' };
                            xValue = (typeof this.annotation.x === 'object') ?
                                Date.parse(chart.intl.getDateParser(option)(chart.intl.getDateFormat(option)(new Date(ej2_data_1.DataUtil.parse.parseJson({ val: annotation.x }).val)))) : 0;
                        }
                        else {
                            xValue = +annotation.x;
                        }
                    }
                    else if (yAxisName === axis.name || (yAxisName == null && axis.name === 'primaryYAxis')) {
                        yAxis = axis;
                        isLog = yAxis.valueType === 'Logarithmic';
                    }
                }
                if (xAxis && yAxis && helper_2.withIn(xAxis.valueType === 'Logarithmic' ? helper_1.logBase(xValue, xAxis.logBase) : xValue, xAxis.visibleRange)) {
                    symbolLocation = helper_3.getPoint(xValue, helper_2.withIn((isLog ? helper_1.logBase(+this.annotation.y, yAxis.logBase) : +this.annotation.y), yAxis.visibleRange) ? +annotation.y :
                        isLog ? Math.pow(yAxis.logBase, yAxis.visibleRange.max) :
                            +annotation.y > yAxis.visibleRange.max ? yAxis.visibleRange.max : yAxis.visibleRange.min, xAxis, yAxis, isInverted);
                    location.x = symbolLocation.x + (isInverted ? yAxis.rect.x : xAxis.rect.x);
                    location.y = symbolLocation.y + (isInverted ? xAxis.rect.y : yAxis.rect.y);
                }
                else {
                    return false;
                }
                return true;
            }
            else {
                return this.setAccumulationPointValue(location);
            }
        };
        AnnotationBase.prototype.processAnnotation = function (annotation, index, parentElement) {
            var annotationElement;
            var location;
            location = new helper_2.ChartLocation(0, 0);
            annotationElement = this.render(annotation, index);
            if (this['setAnnotation' + annotation.coordinateUnits + 'Value'](location)) {
                this.setElementStyle(location, annotationElement, parentElement);
            }
        };
        AnnotationBase.prototype.setAccumulationPointValue = function (location) {
            var accumulation = this.control;
            var point;
            for (var _i = 0, _a = accumulation.visibleSeries[0].points; _i < _a.length; _i++) {
                var accPoint = _a[_i];
                if (typeof accPoint.x === 'object') {
                    if (Date.parse(accPoint.x) === Date.parse(this.annotation.x) &&
                        accPoint.y === this.annotation.y) {
                        point = accPoint;
                        break;
                    }
                }
                else {
                    if (accPoint.x === this.annotation.x && accPoint.y === this.annotation.y) {
                        point = accPoint;
                        break;
                    }
                }
            }
            if (point && point.visible) {
                location.x = point.symbolLocation.x;
                location.y = point.symbolLocation.y;
                return true;
            }
            else {
                return false;
            }
        };
        AnnotationBase.prototype.setElementStyle = function (location, element, parentElement) {
            var elementRect = helper_1.measureElementRect(element);
            var argsData = {
                cancel: false, name: constants_1.annotationRender, content: element,
                location: location
            };
            this.control.trigger(constants_1.annotationRender, argsData);
            if (!argsData.cancel) {
                argsData.content.style.left = this.setAlignmentValue(this.annotation.horizontalAlignment, elementRect.width, argsData.location.x) + 'px';
                argsData.content.style.top = this.setAlignmentValue(this.annotation.verticalAlignment, elementRect.height, argsData.location.y) + 'px';
                argsData.content.setAttribute('aria-label', this.annotation.description || 'Annotation');
                helper_2.appendElement(argsData.content, parentElement);
            }
        };
        AnnotationBase.prototype.setAlignmentValue = function (alignment, size, value) {
            switch (alignment) {
                case 'Top':
                case 'Near':
                    value -= size;
                    break;
                case 'Bottom':
                case 'Far':
                    value += 0;
                    break;
                case 'Middle':
                case 'Center':
                    value -= (size / 2);
                    break;
            }
            return value;
        };
        return AnnotationBase;
    }());
    exports.AnnotationBase = AnnotationBase;
});