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

95.74% Statements 180/188
91.54% Branches 119/130
100% Functions 25/25
96.22% Lines 178/185
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 33× 132× 99× 33× 33×   33×     46× 46×   46×     46× 46× 46×           114× 114×   31×   32× 32× 32× 32× 32× 32× 28× 223× 28× 28× 223× 223× 66× 66×           32×   31×     31× 31× 31×   31× 31× 31× 31×   27× 27× 18× 18×   27× 27×   27× 18× 18× 18×         37× 37× 37× 37×   33× 33× 23×       23×   33× 33×   32×   33× 33× 33× 33× 33× 33×                     33× 33×     33× 33× 33× 33× 33× 33× 33×             33×     33×                             33× 33×   33× 22×   11× 11× 11× 11× 10× 10× 16× 16×     11×   16×   128× 128× 128× 128× 32×     96× 96×   128× 12×   116× 116× 116× 28×   28×   88× 64× 64×     24×   92×   125× 22×   103× 33×   70× 26× 12×   14×               44× 16×   28×         26×   26× 26×   116× 116×   115× 115× 115×   122×          
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(), points);
            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,
                enableRTL: spark.enableRtl,
                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, points) {
            if (ej2_base_1.isNullOrUndefined(format) || format === '') {
                return [y];
            }
            var text = format;
            var xName = spark.xName;
            var yName = spark.yName;
            if (text.indexOf('${') !== -1) {
                var previous = void 0;
                do {
                    previous = text;
                    text = this.resolveSparklineTooltipTokens(text, spark, x, y, points, xName, yName);
                } while (text !== previous && text.indexOf('${') !== -1);
            }
            return [text];
        };
        SparklineTooltip.prototype.resolveSparklineTooltipTokens = function (input, spark, x, y, points, xName, yName) {
            return input.replace(/\$\{([^{}]+)\}/g, this.replaceSparklineTooltipToken.bind(this, spark, x, y, points, xName, yName));
        };
        SparklineTooltip.prototype.replaceSparklineTooltipToken = function (spark, x, y, points, xName, yName, match, token) {
            var colonIndex = token.indexOf(':');
            var rawPath;
            var inlineFormat = '';
            if (colonIndex === -1) {
                rawPath = token;
            }
            else {
                rawPath = token.substring(0, colonIndex);
                inlineFormat = token.substring(colonIndex + 1).trim();
            }
            if (rawPath !== rawPath.trim() || /\s/.test(rawPath)) {
                return match;
            }
            var rawValue;
            var fallbackText;
            if (rawPath === xName) {
                rawValue = spark.valueType === 'DateTime' ? new Date(points.xVal) :
                    spark.valueType === 'Category' ? spark.dataSource[points.xVal][spark.xName] : points.xVal;
                fallbackText = x;
            }
            else if (rawPath === yName) {
                rawValue = points.yVal;
                fallbackText = y;
            }
            else {
                return match;
            }
            return this.formatValue(rawValue, spark, inlineFormat, fallbackText).toString();
        };
        SparklineTooltip.prototype.formatValue = function (value, sparkline, inlineFormat, fallbackText) {
            if (ej2_base_1.isNullOrUndefined(value)) {
                return String(value);
            }
            if (typeof value === 'number' && (isNaN(value) || !isFinite(value))) {
                return isNaN(value) ? 'NaN' : value < 0 ? '-Infinity' : 'Infinity';
            }
            if (!ej2_base_1.isNullOrUndefined(inlineFormat) && inlineFormat !== '') {
                if (value instanceof Date && sparkline.intl && !this.isNumericFormat(inlineFormat)) {
                    return sparkline.intl.getDateFormat({ format: inlineFormat })(value);
                }
                if (typeof value === 'number' &&
                    sparkline.intl &&
                    this.isNumericFormat(inlineFormat)) {
                    return sparkline.intl.getNumberFormat({
                        format: inlineFormat,
                        useGrouping: sparkline.useGroupingSeparator
                    })(value);
                }
                return !ej2_base_1.isNullOrUndefined(fallbackText) ? fallbackText : String(value);
            }
            if (!ej2_base_1.isNullOrUndefined(fallbackText)) {
                return fallbackText;
            }
            if (sparkline.format && typeof value === 'number' && !isNaN(Number(value))) {
                var formatFunction = sparkline.intl.getNumberFormat({
                    format: sparkline.format,
                    useGrouping: sparkline.useGroupingSeparator
                });
                return formatFunction(value);
            }
            return String(value);
        };
        SparklineTooltip.prototype.isNumericFormat = function (format) {
            format = format ? format.trim() : '';
            return !!format && (/^[nNpPcCeE]\d*$/.test(format) || /[0#%]/.test(format));
        };
        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;
});