all files / sparkline/rendering/ sparkline-tooltip.js

98.43% Statements 125/127
92.59% Branches 50/54
100% Functions 19/19
98.41% Lines 124/126
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                 21× 21×   13×   14× 14× 14× 14× 14× 14× 10× 96× 10× 10× 10× 96× 96× 38× 38× 38×           14×   13×     13× 13× 13×   13× 13× 13× 13× 13×               13× 13× 13×                                                                         23× 23×   22× 22× 22×   20×          
define(["require", "exports", "../utils/helper", "@syncfusion/ej2-base", "@syncfusion/ej2-svg-base"], function (require, exports, helper_1, ej2_base_1, ej2_svg_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var SparklineTooltip = (function () {
        function SparklineTooltip(sparkline) {
            this.sparkline = sparkline;
            this.addEventListener();
        }
        SparklineTooltip.prototype.addEventListener = function () {
            if (this.sparkline.isDestroyed) {
                return;
            }
            this.sparkline.on(ej2_base_1.Browser.isPointer ? 'pointerleave' : 'mouseleave', this.mouseLeaveHandler, this);
            this.sparkline.on(ej2_base_1.Browser.touchMoveEvent, this.mouseMoveHandler, this);
            this.sparkline.on(ej2_base_1.Browser.touchEndEvent, this.mouseUpHandler, this);
        };
        SparklineTooltip.prototype.mouseLeaveHandler = function (e) {
            this.removeTooltipElements();
        };
        SparklineTooltip.prototype.mouseUpHandler = function (e) {
            if (!this.sparkline.isTouch) {
                return;
            }
            this.processTooltip(e);
            this.fadeOut();
        };
        SparklineTooltip.prototype.fadeOut = function () {
            clearTimeout(this.clearTooltip);
            this.clearTooltip = setTimeout(this.removeTooltipElements.bind(this), 5000);
        };
        SparklineTooltip.prototype.removeTooltipElements = function () {
            this.removeTooltip();
            this.removeTracker();
        };
        SparklineTooltip.prototype.mouseMoveHandler = function (e) {
            this.processTooltip(e);
        };
        SparklineTooltip.prototype.processTooltip = function (e) {
            var pointIndex;
            var spark = this.sparkline;
            var visiblePoints = spark.sparklineRenderer.visiblePoints;
            var mouseX = spark.mouseX;
            var mouseY = spark.mouseY;
            if (spark.type !== 'Pie') {
                var locations = ej2_base_1.extend([], [], visiblePoints);
                var trackerPositions = locations.map(function (point) { return point.location.x; });
                var temp = Infinity;
                var mousePosition = void 0;
                for (var i = 0, diff = void 0, len = trackerPositions.length; i < len; i++) {
                    diff = Math.abs(mouseX - trackerPositions[i]);
                    if (temp > diff) {
                        temp = diff;
                        mousePosition = trackerPositions[i];
                        pointIndex = i;
                    }
                }
            }
            else {
                var target = e.target.id;
                pointIndex = parseInt(target.split('_pie_')[1], 10);
            }
            if (isNaN(pointIndex) || !helper_1.withInBounds(mouseX, mouseY, new helper_1.Rect(0, 0, spark.availableSize.width, spark.availableSize.height))) {
                this.removeTracker();
                this.removeTooltip();
                return;
            }
            Iif (this.pointIndex === pointIndex) {
                return;
            }
            this.pointIndex = pointIndex;
            this.renderTrackerLine(visiblePoints[pointIndex]);
            this.renderTooltip(visiblePoints[pointIndex]);
        };
        SparklineTooltip.prototype.renderTrackerLine = function (points) {
            var spark = this.sparkline;
            var tracker = spark.tooltipSettings.trackLineSettings;
            var color = (spark.theme === 'Highcontrast') ? '#FFFFFF' : '#000000';
            color = (tracker.color) ? tracker.color : color;
            if (!tracker.visible || spark.type === 'Pie') {
                return;
            }
            var group = helper_1.getIdElement(spark.element.id + '_sparkline_tracker_g');
            if (ej2_base_1.isNullOrUndefined(group)) {
                group = spark.renderer.createGroup({ id: spark.element.id + '_sparkline_tracker_g' });
                spark.svgObject.appendChild(group);
            }
            var pathEle = helper_1.getIdElement(spark.element.id + '_sparkline_tracker');
            var d = 'M ' + points.location.x + ' ' + spark.padding.top + ' L ' + points.location.x + ' ' +
                (spark.availableSize.height - spark.padding.bottom);
            if (ej2_base_1.isNullOrUndefined(pathEle)) {
                var pathOption = new helper_1.PathOption(spark.element.id + '_sparkline_tracker', 'transparent', tracker.width, color, 1);
                pathOption.d = d;
                helper_1.drawPath(spark, pathOption, group);
            }
            else {
                pathEle.setAttribute('d', d);
                pathEle.setAttribute('stroke-width', tracker.width.toString());
                pathEle.setAttribute('stroke', color);
            }
        };
        SparklineTooltip.prototype.renderTooltip = function (points) {
            var spark = this.sparkline;
            var tooltip = spark.tooltipSettings;
            if (!tooltip.visible) {
                return;
            }
            var div = helper_1.getIdElement(spark.element.id + '_sparkline_tooltip_div');
            if (ej2_base_1.isNullOrUndefined(div)) {
                div = ej2_base_1.createElement('div', { id: spark.element.id + '_sparkline_tooltip_div',
                    styles: 'pointer-events: none; position: absolute;z-index:1;' });
                helper_1.getIdElement(spark.element.id + '_Secondary_Element').appendChild(div);
            }
            var size = (spark.markerSettings.visible.length) ? spark.markerSettings.size : 0;
            var x = points.xVal.toString();
            if (spark.valueType === 'Category') {
                x = spark.dataSource[points.xVal][spark.xName];
            }
            else if (spark.valueType === 'DateTime') {
                x = new Date(points.xVal).toDateString();
            }
            var y = points.yVal.toString();
            var text = this.getFormat(spark.tooltipSettings.format, spark, x, this.formatValue(points.yVal, spark).toString());
            var location = { x: points.location.x, y: points.location.y };
            location = spark.type === 'Pie' ? { x: points.location.x, y: points.location.y } : location;
            var tooltipEvent = {
                name: 'tooltipInitialize', cancel: false, text: text,
                textStyle: {
                    size: tooltip.textStyle.size,
                    opacity: tooltip.textStyle.opacity,
                    fontWeight: tooltip.textStyle.fontWeight,
                    fontStyle: tooltip.textStyle.fontStyle,
                    fontFamily: tooltip.textStyle.fontFamily,
                    color: tooltip.textStyle.color,
                }
            };
            spark.trigger(tooltipEvent.name, tooltipEvent);
            Iif (tooltipEvent.cancel) {
                return;
            }
            var element = new ej2_svg_base_1.Tooltip({
                content: tooltipEvent.text,
                border: tooltip.border,
                template: tooltip.template,
                data: spark.dataSource[this.pointIndex],
                fill: tooltip.fill,
                textStyle: tooltipEvent.textStyle,
                enableAnimation: false,
                location: { x: location.x, y: location.y },
                shared: false,
                areaBounds: new helper_1.Rect(0, 0, spark.availableSize.width, spark.availableSize.height),
                theme: spark.theme
            });
            element.appendTo(div);
        };
        SparklineTooltip.prototype.getFormat = function (format, spark, x, y) {
            if (ej2_base_1.isNullOrUndefined(format) || format === '') {
                return [y];
            }
            var text = format;
            text = text.split('${' + spark.xName + '}').join(x).split('${' + spark.yName + '}').join(y);
            return [text];
        };
        SparklineTooltip.prototype.formatValue = function (value, sparkline) {
            var formatValue;
            var formatFunction;
            if (sparkline.format && !isNaN(Number(value))) {
                formatFunction = sparkline.intl.getNumberFormat({ format: sparkline.format, useGrouping: sparkline.useGroupingSeparator });
                formatValue = formatFunction(value);
            }
            else {
                formatValue = value;
            }
            return formatValue;
        };
        SparklineTooltip.prototype.removeTracker = function () {
            var tracker = this.sparkline.element.querySelector('#' + this.sparkline.element.id + '_sparkline_tracker_g');
            return tracker ? ej2_base_1.remove(tracker) : null;
        };
        SparklineTooltip.prototype.removeTooltip = function () {
            this.pointIndex = null;
            var tooltip = this.sparkline.element.querySelector('#' + this.sparkline.element.id + '_sparkline_tooltip_div');
            return tooltip ? ej2_base_1.remove(tooltip) : null;
        };
        SparklineTooltip.prototype.getModuleName = function () {
            return 'SparklineTooltip';
        };
        SparklineTooltip.prototype.destroy = function (sparkline) {
        };
        return SparklineTooltip;
    }());
    exports.SparklineTooltip = SparklineTooltip;
});