all files / chart/user-interaction/ crosshair.js

98.96% Statements 191/193
92.91% Branches 118/127
100% Functions 20/20
98.96% Lines 191/193
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   260× 260× 260× 260× 260× 260×   260×     260× 260× 260× 260× 260×   10×     12×   125× 125×     125× 97× 96× 96×                 98× 98× 98× 98× 98× 98× 98× 98× 98×   97× 97× 97× 97× 71×     97× 94×     97× 69× 69× 69× 69× 69× 69× 69× 69×     28× 28× 28×     138× 138×   97× 97× 97× 97× 97× 97× 97× 97× 97× 97× 257× 257× 257× 192×   188× 188× 188× 188×   187× 187× 136×       136× 136× 136×   187× 187× 187× 187× 187×             188× 188× 188× 188× 188× 188×   188× 99× 99× 99×     89× 89× 89×   188×   183× 61×   122×     121× 121×     187× 187× 187× 187× 187× 187× 187× 187× 187× 187× 187× 98×   98× 98× 98× 98× 42×     98×   98×   98×   98×       89× 89× 89×   89× 89×         81× 10× 10×     89×   89×   89×   89×     187×   112×   14× 14× 14× 14× 14× 14×     144× 144×     14× 14× 14× 14× 14×             20665×          
define(["require", "exports", "@syncfusion/ej2-base", "../../common/utils/helper"], function (require, exports, ej2_base_1, helper_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Crosshair = (function () {
        function Crosshair(chart) {
            this.arrowLocation = new helper_1.ChartLocation(0, 0);
            this.rx = 2;
            this.ry = 2;
            this.chart = chart;
            this.elementID = this.chart.element.id;
            this.addEventListener();
        }
        Crosshair.prototype.addEventListener = function () {
            Iif (this.chart.isDestroyed) {
                return;
            }
            var cancelEvent = ej2_base_1.Browser.isPointer ? 'pointerleave' : 'mouseleave';
            this.chart.on(ej2_base_1.Browser.touchMoveEvent, this.mouseMoveHandler, this);
            this.chart.on(ej2_base_1.Browser.touchEndEvent, this.mouseUpHandler, this);
            this.chart.on(cancelEvent, this.mouseLeaveHandler, this);
            this.chart.on('tapHold', this.longPress, this);
        };
        Crosshair.prototype.mouseUpHandler = function () {
            if (this.chart.startMove) {
                this.removeCrosshair(2000);
            }
        };
        Crosshair.prototype.mouseLeaveHandler = function () {
            this.removeCrosshair(1000);
        };
        Crosshair.prototype.mouseMoveHandler = function (event) {
            var chart = this.chart;
            Iif (event.type === 'touchmove' && (ej2_base_1.Browser.isIos || ej2_base_1.Browser.isIos7) && chart.startMove && event.preventDefault) {
                event.preventDefault();
            }
            if (!chart.disableTrackTooltip) {
                if (helper_1.withInBounds(chart.mouseX, chart.mouseY, chart.chartAxisLayoutPanel.seriesClipRect)) {
                    Eif (chart.startMove || !chart.isTouch) {
                        this.crosshair();
                    }
                }
                else {
                    this.removeCrosshair(1000);
                }
            }
        };
        Crosshair.prototype.longPress = function () {
            var chart = this.chart;
            if (helper_1.withInBounds(chart.mouseX, chart.mouseY, chart.chartAxisLayoutPanel.seriesClipRect)) {
                this.crosshair();
            }
            return false;
        };
        Crosshair.prototype.crosshair = function () {
            var chart = this.chart;
            var horizontalCross = '';
            var verticalCross = '';
            var options;
            var crosshair = chart.crosshair;
            var chartRect = chart.chartAxisLayoutPanel.seriesClipRect;
            var crossGroup = document.getElementById(this.elementID + '_UserInteraction');
            this.stopAnimation();
            if (chart.tooltip.enable && !helper_1.withInBounds(chart.tooltipModule.valueX, chart.tooltipModule.valueY, chartRect)) {
                return null;
            }
            this.valueX = chart.tooltip.enable ? chart.tooltipModule.valueX : chart.mouseX;
            this.valueY = chart.tooltip.enable ? chart.tooltipModule.valueY : chart.mouseY;
            crossGroup.setAttribute('opacity', '1');
            if (crosshair.lineType === 'Both' || crosshair.lineType === 'Horizontal') {
                horizontalCross += 'M ' + chartRect.x + ' ' + this.valueY +
                    ' L ' + (chartRect.x + chartRect.width) + ' ' + this.valueY;
            }
            if (crosshair.lineType === 'Both' || crosshair.lineType === 'Vertical') {
                verticalCross += 'M ' + this.valueX + ' ' + chartRect.y +
                    ' L ' + this.valueX + ' ' + (chartRect.y + chartRect.height);
            }
            if (crossGroup.childNodes.length === 0) {
                var axisTooltipGroup = chart.renderer.createGroup({ 'id': this.elementID + '_crosshair_axis' });
                options = new helper_1.PathOption(this.elementID + '_HorizontalLine', 'none', crosshair.line.width, crosshair.line.color || chart.themeStyle.crosshairLine, 1, null, horizontalCross);
                this.renderCrosshairLine(options, crossGroup);
                options.d = verticalCross;
                options.id = this.elementID + '_VerticalLine';
                this.renderCrosshairLine(options, crossGroup);
                crossGroup.appendChild(axisTooltipGroup);
                this.renderAxisTooltip(chart, chartRect, crossGroup.lastChild);
            }
            else {
                document.getElementById(this.elementID + '_HorizontalLine').setAttribute('d', horizontalCross);
                document.getElementById(this.elementID + '_VerticalLine').setAttribute('d', verticalCross);
                this.renderAxisTooltip(chart, chartRect, crossGroup.lastChild);
            }
        };
        Crosshair.prototype.renderCrosshairLine = function (options, crossGroup) {
            var htmlObject = this.chart.renderer.drawPath(options);
            crossGroup.appendChild(htmlObject);
        };
        Crosshair.prototype.renderAxisTooltip = function (chart, chartRect, axisGroup) {
            var axis;
            var text;
            var rect;
            var pathElement;
            var textElem;
            var options;
            var padding = 5;
            var direction;
            var axisRect;
            for (var k = 0, length_1 = chart.axisCollections.length; k < length_1; k++) {
                axis = chart.axisCollections[k];
                axisRect = !axis.placeNextToAxisLine ? axis.rect : axis.updatedRect;
                if (axis.crosshairTooltip.enable) {
                    if ((this.valueX <= (axisRect.x + axisRect.width) && axisRect.x <= this.valueX) ||
                        (this.valueY <= (axisRect.y + axisRect.height) && axisRect.y <= this.valueY)) {
                        pathElement = document.getElementById(this.elementID + '_axis_tooltip_' + k);
                        textElem = document.getElementById(this.elementID + '_axis_tooltip_text_' + k);
                        text = this.getAxisText(axis);
                        if (!text) {
                            continue;
                        }
                        rect = this.tooltipLocation(text, axis, chartRect, axisRect);
                        if (pathElement === null) {
                            pathElement = chart.renderer.drawPath({
                                'id': this.elementID + '_axis_tooltip_' + k,
                                'fill': axis.crosshairTooltip.fill || chart.themeStyle.crosshairFill
                            });
                            axisGroup.appendChild(pathElement);
                            options = new helper_1.TextOption(this.elementID + '_axis_tooltip_text_' + k, 0, 0, 'start', text);
                            textElem = helper_1.textElement(options, axis.crosshairTooltip.textStyle, axis.crosshairTooltip.textStyle.color || chart.themeStyle.crosshairLabel, axisGroup);
                        }
                        direction = helper_1.findDirection(this.rx, this.ry, rect, this.arrowLocation, 10, this.isTop, this.isBottom, this.isLeft, this.valueX, this.valueY);
                        pathElement.setAttribute('d', direction);
                        textElem.textContent = text;
                        textElem.setAttribute('x', (rect.x + padding).toString());
                        textElem.setAttribute('y', (rect.y + padding + 3 * this.elementSize.height / 4).toString());
                    }
                    else {
                        helper_1.removeElement(this.elementID + '_axis_tooltip_' + k);
                        helper_1.removeElement(this.elementID + '_axis_tooltip_text_' + k);
                    }
                }
            }
        };
        Crosshair.prototype.getAxisText = function (axis) {
            var value;
            this.isBottom = false;
            this.isTop = false;
            this.isLeft = false;
            this.isRight = false;
            var labelValue = (axis.valueType === 'Category' && axis.labelPlacement === 'BetweenTicks')
                ? 0.5 : 0;
            if (axis.orientation === 'Horizontal') {
                value = helper_1.getValueXByPoint(Math.abs(this.valueX - axis.rect.x), axis.rect.width, axis) + labelValue;
                this.isBottom = !axis.opposedPosition;
                this.isTop = axis.opposedPosition;
            }
            else {
                value = helper_1.getValueYByPoint(Math.abs(this.valueY - axis.rect.y), axis.rect.height, axis) + labelValue;
                this.isRight = axis.opposedPosition;
                this.isLeft = !axis.opposedPosition;
            }
            if (axis.valueType === 'DateTime') {
                return axis.format(new Date(value));
            }
            else if (axis.valueType === 'Category') {
                return axis.labels[Math.floor(value)];
            }
            else if (axis.valueType === 'Logarithmic') {
                return value = axis.format(Math.pow(axis.logBase, value));
            }
            else {
                var customLabelFormat = axis.labelFormat && axis.labelFormat.match('{value}') !== null;
                return customLabelFormat ? axis.labelFormat.replace('{value}', axis.format(value)) : axis.format(value);
            }
        };
        Crosshair.prototype.tooltipLocation = function (text, axis, bounds, axisRect) {
            var isBottom = false;
            var isLeft = false;
            var padding = 5;
            var arrowPadding = 10;
            var tooltipRect;
            var boundsX = bounds.x;
            var boundsY = bounds.y;
            var islabelInside = axis.labelPosition === 'Inside';
            var scrollBarHeight = axis.zoomingScrollBar && axis.zoomingScrollBar.svgObject ? axis.scrollBarHeight : 0;
            this.elementSize = helper_1.measureText(text, axis.crosshairTooltip.textStyle);
            if (axis.orientation === 'Horizontal') {
                var yLocation = islabelInside ? axisRect.y - this.elementSize.height - (padding * 2 + arrowPadding) :
                    axisRect.y + scrollBarHeight;
                var height = islabelInside ? axisRect.y - this.elementSize.height - arrowPadding : axisRect.y + arrowPadding;
                this.arrowLocation = new helper_1.ChartLocation(this.valueX, yLocation);
                tooltipRect = new helper_1.Rect((this.valueX - (this.elementSize.width / 2) - padding), height + (!islabelInside ? scrollBarHeight : 0), this.elementSize.width + padding * 2, this.elementSize.height + padding * 2);
                if (axis.opposedPosition) {
                    tooltipRect.y = islabelInside ? axisRect.y : axisRect.y -
                        (this.elementSize.height + padding * 2 + arrowPadding) - scrollBarHeight;
                }
                if (tooltipRect.x < boundsX) {
                    tooltipRect.x = boundsX;
                }
                if (tooltipRect.x + tooltipRect.width > boundsX + bounds.width) {
                    tooltipRect.x -= ((tooltipRect.x + tooltipRect.width) - (boundsX + bounds.width));
                }
                if (this.arrowLocation.x + arrowPadding / 2 > tooltipRect.x + tooltipRect.width - this.rx) {
                    this.arrowLocation.x = tooltipRect.x + tooltipRect.width - this.rx - arrowPadding / 2;
                }
                if (this.arrowLocation.x - arrowPadding / 2 < tooltipRect.x + this.rx) {
                    this.arrowLocation.x = tooltipRect.x + this.rx + arrowPadding / 2;
                }
            }
            else {
                scrollBarHeight = scrollBarHeight * (axis.opposedPosition ? 1 : -1);
                this.arrowLocation = new helper_1.ChartLocation(axisRect.x, this.valueY);
                var width = islabelInside ? axisRect.x - scrollBarHeight :
                    axisRect.x - (this.elementSize.width) - (padding * 2 + arrowPadding);
                tooltipRect = new helper_1.Rect(width + scrollBarHeight, this.valueY - (this.elementSize.height / 2) - padding, this.elementSize.width + (padding * 2), this.elementSize.height + padding * 2);
                if (axis.opposedPosition) {
                    tooltipRect.x = islabelInside ? axisRect.x - this.elementSize.width - arrowPadding :
                        axisRect.x + arrowPadding + scrollBarHeight;
                    if ((tooltipRect.x + tooltipRect.width) > this.chart.availableSize.width) {
                        this.arrowLocation.x -= ((tooltipRect.x + tooltipRect.width) - this.chart.availableSize.width);
                        tooltipRect.x -= ((tooltipRect.x + tooltipRect.width) - this.chart.availableSize.width);
                    }
                }
                else {
                    if (tooltipRect.x < 0) {
                        this.arrowLocation.x -= tooltipRect.x;
                        tooltipRect.x = 0;
                    }
                }
                if (tooltipRect.y < boundsY) {
                    tooltipRect.y = boundsY;
                }
                if (tooltipRect.y + tooltipRect.height >= boundsY + bounds.height) {
                    tooltipRect.y -= ((tooltipRect.y + tooltipRect.height) - (boundsY + bounds.height));
                }
                if (this.arrowLocation.y + arrowPadding / 2 > tooltipRect.y + tooltipRect.height - this.ry) {
                    this.arrowLocation.y = tooltipRect.y + tooltipRect.height - this.ry - arrowPadding / 2;
                }
                if (this.arrowLocation.y - arrowPadding / 2 < tooltipRect.y + this.ry) {
                    this.arrowLocation.y = tooltipRect.y + this.ry + arrowPadding / 2;
                }
            }
            return tooltipRect;
        };
        Crosshair.prototype.stopAnimation = function () {
            helper_1.stopTimer(this.crosshairInterval);
        };
        Crosshair.prototype.removeCrosshair = function (duration) {
            var chart = this.chart;
            var crosshair = document.getElementById(this.elementID + '_UserInteraction');
            this.stopAnimation();
            Eif (crosshair && crosshair.getAttribute('opacity') !== '0') {
                this.crosshairInterval = setTimeout(function () {
                    new ej2_base_1.Animation({}).animate(crosshair, {
                        duration: 200,
                        progress: function (args) {
                            crosshair.style.animation = '';
                            crosshair.setAttribute('opacity', (1 - (args.timeStamp / args.duration)).toString());
                        },
                        end: function (model) {
                            crosshair.setAttribute('opacity', '0');
                            chart.startMove = false;
                            Eif (chart.tooltipModule) {
                                chart.tooltipModule.valueX = null;
                                chart.tooltipModule.valueY = null;
                            }
                        }
                    });
                }, duration);
            }
        };
        Crosshair.prototype.getModuleName = function () {
            return 'Crosshair';
        };
        Crosshair.prototype.destroy = function (chart) {
        };
        return Crosshair;
    }());
    exports.Crosshair = Crosshair;
});