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

94.59% Statements 140/148
88.31% Branches 68/77
100% Functions 22/22
95.17% Lines 138/145
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 23× 92× 69× 23× 23×   23×     33× 33×   33×     33× 33× 33×           93× 93×   22×   23× 23× 23× 23× 23× 23× 19× 186× 19× 19× 186× 186× 47× 47×           23×   22×     22× 22× 22×   22× 22× 22× 22×   18× 18× 14× 14×   18× 18×   18× 14× 14× 14×         27× 27× 27× 27×   23× 23× 18×       18×   23× 23×   22×   23× 23× 23× 23× 23× 23×                     23× 23×     23× 23× 23× 23× 23× 23× 23×             23×     23×                           23× 23×   23× 19×     23× 23× 23×     22×   23×   95× 95×   94× 94× 94×   87×          
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    Eif (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
            t[p[i]] = s[p[i]];
    return t;
};
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 () {
            Iif (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 () {
            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;
                for (var i = 0, diff = void 0, len = trackerPositions.length; i < len; i++) {
                    diff = Math.abs(mouseX - trackerPositions[i]);
                    if (temp > diff) {
                        temp = diff;
                        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.sparkTheme.trackerLineColor ? spark.sparkTheme.trackerLineColor : tracker.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', color, 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 _this = this;
            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 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 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 textColor = tooltip.textStyle.color || spark.sparkTheme.tooltipFontColor;
            var backgroundColor = tooltip.fill === '' ? spark.sparkTheme.tooltipFill : tooltip.fill;
            var tooltipEvent = {
                name: 'tooltipInitialize', cancel: false, text: text,
                textStyle: {
                    size: tooltip.textStyle.size,
                    opacity: spark.sparkTheme.tooltipTextOpacity || tooltip.textStyle.opacity,
                    fontWeight: tooltip.textStyle.fontWeight || spark.sparkTheme.tooltipFontWeight,
                    fontStyle: tooltip.textStyle.fontStyle,
                    fontFamily: tooltip.textStyle.fontFamily || spark.sparkTheme.tooltipFontFamily,
                    color: textColor
                }
            };
            spark.trigger('tooltipInitialize', tooltipEvent, function () {
                _this.addTooltip(tooltipEvent, spark, backgroundColor, tooltip, location, div);
            });
        };
        SparklineTooltip.prototype.addTooltip = function (tooltipEvent, spark, backgroundColor, tooltip, location, div, eventArgs) {
            var cancel;
            var arg;
            var tootipArgs;
            Eif (!ej2_base_1.isNullOrUndefined(tooltipEvent)) {
                var c = tooltipEvent.cancel, otherArgs = __rest(tooltipEvent, ["cancel"]);
                cancel = c;
                tootipArgs = tooltipEvent;
            }
            else {
                cancel = eventArgs.cancel;
                arg = eventArgs;
                tootipArgs = eventArgs;
            }
            Iif (tooltipEvent.cancel) {
                return;
            }
            var element = new ej2_svg_base_1.Tooltip({
                content: tootipArgs.text,
                border: tooltip.border,
                template: tooltip.template,
                data: spark.dataSource[this.pointIndex],
                fill: backgroundColor,
                textStyle: tootipArgs.textStyle,
                enableAnimation: false,
                location: { x: location.x, y: location.y },
                shared: false,
                availableSize: this.sparkline.availableSize,
                areaBounds: new helper_1.Rect(0, 0, spark.availableSize.width, spark.availableSize.height),
                theme: spark.theme
            });
            element.opacity = spark.sparkTheme.tooltipFillOpacity || element.opacity;
            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 () {
        };
        return SparklineTooltip;
    }());
    exports.SparklineTooltip = SparklineTooltip;
});