Pointer.js 39.8 KB
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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
/**
 * (c) 2010-2018 Torstein Honsi
 *
 * License: www.highcharts.com/license
 */

/**
 * One position in relation to an axis.
 *
 * @typedef Highcharts.PointerAxisCoordinateObject
 *
 * @property {Highcharts.Axis} axis
 *           Related axis.
 *
 * @property {number} value
 *           Axis value.
 */

/**
 * Positions in terms of axis values.
 *
 * @typedef Highcharts.PointerAxisCoordinatesObject
 *
 * @property {Array<Highcharts.PointerAxisCoordinateObject>} xAxis
 *           Positions on the x-axis.
 *
 * @property {Array<Highcharts.PointerAxisCoordinateObject>} yAxis
 *           Positions on the y-axis.
 */

/**
 * Pointer coordinates.
 *
 * @typedef Highcharts.PointerCoordinatesObject
 *
 * @property {number} chartX
 *
 * @property {number} chartY
 */

/**
 * A native browser mouse or touch event, extended with position information
 * relative to the {@link Chart.container}.
 *
 * @typedef {global.PointerEvent} Highcharts.PointerEventObject
 *
 * @property {number} chartX
 *           The X coordinate of the pointer interaction relative to the
 *           chart.
 *
 * @property {number} chartY
 *           The Y coordinate of the pointer interaction relative to the
 *           chart.
 */

/**
 * Axis-specific data of a selection.
 *
 * @typedef Highcharts.SelectDataObject
 *
 * @property {Highcharts.Axis} axis
 *
 * @property {number} min
 *
 * @property {number} max
 */

/**
 * Object for select events.
 *
 * @typedef Highcharts.SelectEventObject
 *
 * @property {global.Event} originalEvent
 *
 * @property {Array<Highcharts.SelectDataObject>} xAxis
 *
 * @property {Array<Highcharts.SelectDataObject>} yAxis
 */

'use strict';

import Highcharts from './Globals.js';
import './Utilities.js';
import './Tooltip.js';
import './Color.js';

var H = Highcharts,
    addEvent = H.addEvent,
    attr = H.attr,
    charts = H.charts,
    color = H.color,
    css = H.css,
    defined = H.defined,
    each = H.each,
    extend = H.extend,
    find = H.find,
    fireEvent = H.fireEvent,
    isNumber = H.isNumber,
    isObject = H.isObject,
    offset = H.offset,
    pick = H.pick,
    splat = H.splat,
    Tooltip = H.Tooltip;

/**
 * The mouse and touch tracker object. Each {@link Chart} item has one
 * assosiated Pointer item that can be accessed from the  {@link Chart.pointer}
 * property.
 *
 * @class
 * @name Highcharts.Pointer
 *
 * @param {Highcharts.Chart} chart
 *        The Chart instance.
 *
 * @param {Highcharts.Options} options
 *        The root options object. The pointer uses options from the chart and
 *        tooltip structures.
 */
Highcharts.Pointer = function (chart, options) {
    this.init(chart, options);
};

Highcharts.Pointer.prototype = {
    /**
     * Initialize the Pointer.
     *
     * @private
     * @function Highcharts.Pointer#init
     *
     * @param {Highcharts.Chart} chart
     *        The Chart instance.
     *
     * @param {Highcharts.Options} options
     *        The root options object. The pointer uses options from the chart
     *        and tooltip structures.
     */
    init: function (chart, options) {

        // Store references
        this.options = options;
        this.chart = chart;

        // Do we need to handle click on a touch device?
        this.runChartClick =
            options.chart.events && !!options.chart.events.click;

        this.pinchDown = [];
        this.lastValidTouch = {};

        if (Tooltip) {
            /**
             * Tooltip object for points of series.
             *
             * @name Highcharts.Chart#tooltip
             * @type {Highcharts.Tooltip}
             */
            chart.tooltip = new Tooltip(chart, options.tooltip);
            this.followTouchMove = pick(options.tooltip.followTouchMove, true);
        }

        this.setDOMEvents();
    },

    /**
     * Resolve the zoomType option, this is reset on all touch start and mouse
     * down events.
     *
     * @private
     * @function Highcharts.Pointer#zoomOption
     *
     * @param {global.Event} e
     *        Event object.
     */
    zoomOption: function (e) {
        var chart = this.chart,
            options = chart.options.chart,
            zoomType = options.zoomType || '',
            inverted = chart.inverted,
            zoomX,
            zoomY;

        // Look for the pinchType option
        if (/touch/.test(e.type)) {
            zoomType = pick(options.pinchType, zoomType);
        }

        this.zoomX = zoomX = /x/.test(zoomType);
        this.zoomY = zoomY = /y/.test(zoomType);
        this.zoomHor = (zoomX && !inverted) || (zoomY && inverted);
        this.zoomVert = (zoomY && !inverted) || (zoomX && inverted);
        this.hasZoom = zoomX || zoomY;
    },

    /**
     * Takes a browser event object and extends it with custom Highcharts
     * properties `chartX` and `chartY` in order to work on the internal
     * coordinate system.
     *
     * @function Highcharts.Pointer#normalize
     *
     * @param {global.Event} e
     *        Event object in standard browsers.
     *
     * @return {Highcharts.PointerEventObject}
     *         A browser event with extended properties `chartX` and `chartY`.
     */
    normalize: function (e, chartPosition) {
        var ePos;

        // iOS (#2757)
        ePos = e.touches ?
            (e.touches.length ? e.touches.item(0) : e.changedTouches[0]) :
            e;

        // Get mouse position
        if (!chartPosition) {
            this.chartPosition = chartPosition = offset(this.chart.container);
        }

        return extend(e, {
            chartX: Math.round(ePos.pageX - chartPosition.left),
            chartY: Math.round(ePos.pageY - chartPosition.top)
        });
    },

    /**
     * Get the click position in terms of axis values.
     *
     * @function Highcharts.Pointer#getCoordinates
     *
     * @param {Highcharts.PointerEventObject} e
     *        Pointer event, extended with `chartX` and `chartY` properties.
     *
     * @return {Highcharts.PointerAxisCoordinatesObject}
     */
    getCoordinates: function (e) {

        var coordinates = {
            xAxis: [],
            yAxis: []
        };

        each(this.chart.axes, function (axis) {
            coordinates[axis.isXAxis ? 'xAxis' : 'yAxis'].push({
                axis: axis,
                value: axis.toValue(e[axis.horiz ? 'chartX' : 'chartY'])
            });
        });

        return coordinates;
    },

    /**
     * Finds the closest point to a set of coordinates, using the k-d-tree
     * algorithm.
     *
     * @function Highcharts.Pointer#findNearestKDPoints
     *
     * @param {Array<Highcharts.Series>} series
     *        All the series to search in.
     *
     * @param {boolean} shared
     *        Whether it is a shared tooltip or not.
     *
     * @param {Highcharts.PointerCoordinatesObject} coordinates
     *        Chart coordinates of the pointer.
     *
     * @return {Point|undefined}
     *         The point closest to given coordinates.
     */
    findNearestKDPoint: function (series, shared, coordinates) {
        var closest,
            sort = function (p1, p2) {
                var isCloserX = p1.distX - p2.distX,
                    isCloser = p1.dist - p2.dist,
                    isAbove =
                        (p2.series.group && p2.series.group.zIndex) -
                        (p1.series.group && p1.series.group.zIndex),
                    result;

                // We have two points which are not in the same place on xAxis
                // and shared tooltip:
                if (isCloserX !== 0 && shared) { // #5721
                    result = isCloserX;
                // Points are not exactly in the same place on x/yAxis:
                } else if (isCloser !== 0) {
                    result = isCloser;
                // The same xAxis and yAxis position, sort by z-index:
                } else if (isAbove !== 0) {
                    result = isAbove;
                // The same zIndex, sort by array index:
                } else {
                    result = p1.series.index > p2.series.index ? -1 : 1;
                }
                return result;
            };
        each(series, function (s) {
            var noSharedTooltip = s.noSharedTooltip && shared,
                compareX = (
                    !noSharedTooltip &&
                    s.options.findNearestPointBy.indexOf('y') < 0
                ),
                point = s.searchPoint(
                    coordinates,
                    compareX
                );
            if (
                // Check that we actually found a point on the series.
                isObject(point, true) &&
                // Use the new point if it is closer.
                (!isObject(closest, true) || (sort(closest, point) > 0))
            ) {
                closest = point;
            }
        });
        return closest;
    },

    /**
     * @private
     * @function Highcharts.Pointer#getPointFromEvent
     *
     * @param {global.Event} e
     *
     * @return {Highcharts.Point|undefined}
     */
    getPointFromEvent: function (e) {
        var target = e.target,
            point;

        while (target && !point) {
            point = target.point;
            target = target.parentNode;
        }
        return point;
    },

    /**
     * @private
     * @function Highcharts.Pointer#getChartCoordinatesFromPoint
     *
     * @param {Highcharts.Point} point
     *
     * @param {boolean} inverted
     *
     * @return {Highcharts.PointerCoordinatesObject}
     */
    getChartCoordinatesFromPoint: function (point, inverted) {
        var series = point.series,
            xAxis = series.xAxis,
            yAxis = series.yAxis,
            plotX = pick(point.clientX, point.plotX),
            shapeArgs = point.shapeArgs;

        if (xAxis && yAxis) {
            return inverted ? {
                chartX: xAxis.len + xAxis.pos - plotX,
                chartY: yAxis.len + yAxis.pos - point.plotY
            } : {
                chartX: plotX + xAxis.pos,
                chartY: point.plotY + yAxis.pos
            };
        } else if (shapeArgs && shapeArgs.x && shapeArgs.y) {
            // E.g. pies do not have axes
            return {
                chartX: shapeArgs.x,
                chartY: shapeArgs.y
            };
        }
    },

    /**
     * Calculates what is the current hovered point/points and series.
     *
     * @private
     * @function Highcharts.Pointer#getHoverData
     *
     * @param {Highcharts.Point|undefined} existingHoverPoint
     *        The point currrently beeing hovered.
     *
     * @param {Highcharts.Series|undefined} existingHoverSeries
     *        The series currently beeing hovered.
     *
     * @param {Array<Highcharts.Series>} series
     *        All the series in the chart.
     *
     * @param {boolean} isDirectTouch
     *        Is the pointer directly hovering the point.
     *
     * @param {boolean} shared
     *        Whether it is a shared tooltip or not.
     *
     * @param {Highcharts.PointerCoordinatesObject} coordinates
     *        Chart coordinates of the pointer.
     *
     * @return {*}
     *         Object containing resulting hover data: hoverPoint, hoverSeries,
     *         and hoverPoints.
     */
    getHoverData: function (
        existingHoverPoint,
        existingHoverSeries,
        series,
        isDirectTouch,
        shared,
        coordinates,
        params
    ) {
        var hoverPoint,
            hoverPoints = [],
            hoverSeries = existingHoverSeries,
            isBoosting = params && params.isBoosting,
            useExisting = !!(isDirectTouch && existingHoverPoint),
            notSticky = hoverSeries && !hoverSeries.stickyTracking,
            filter = function (s) {
                return (
                    s.visible &&
                    !(!shared && s.directTouch) && // #3821
                    pick(s.options.enableMouseTracking, true)
                );
            },
            // Which series to look in for the hover point
            searchSeries = notSticky ?
                // Only search on hovered series if it has stickyTracking false
                [hoverSeries] :
                // Filter what series to look in.
                H.grep(series, function (s) {
                    return filter(s) && s.stickyTracking;
                });

        // Use existing hovered point or find the one closest to coordinates.
        hoverPoint = useExisting ?
            existingHoverPoint :
            this.findNearestKDPoint(searchSeries, shared, coordinates);

        // Assign hover series
        hoverSeries = hoverPoint && hoverPoint.series;

        // If we have a hoverPoint, assign hoverPoints.
        if (hoverPoint) {
            // When tooltip is shared, it displays more than one point
            if (shared && !hoverSeries.noSharedTooltip) {
                searchSeries = H.grep(series, function (s) {
                    return filter(s) && !s.noSharedTooltip;
                });

                // Get all points with the same x value as the hoverPoint
                each(searchSeries, function (s) {
                    var point = find(s.points, function (p) {
                        return p.x === hoverPoint.x && !p.isNull;
                    });
                    if (isObject(point)) {
                        /*
                        * Boost returns a minimal point. Convert it to a usable
                        * point for tooltip and states.
                        */
                        if (isBoosting) {
                            point = s.getPoint(point);
                        }
                        hoverPoints.push(point);
                    }
                });
            } else {
                hoverPoints.push(hoverPoint);
            }
        }
        return {
            hoverPoint: hoverPoint,
            hoverSeries: hoverSeries,
            hoverPoints: hoverPoints
        };
    },

    /**
     * With line type charts with a single tracker, get the point closest to the
     * mouse. Run Point.onMouseOver and display tooltip for the point or points.
     *
     * @private
     * @function Highcharts.Pointer#runPointActions
     *
     * @param {global.Event} e
     *
     * @param {Highcharts.Point} p
     *
     * @fires Highcharts.Point#event:mouseOut
     * @fires Highcharts.Point#event:mouseOver
     */
    runPointActions: function (e, p) {
        var pointer = this,
            chart = pointer.chart,
            series = chart.series,
            tooltip = chart.tooltip && chart.tooltip.options.enabled ?
                chart.tooltip :
                undefined,
            shared = tooltip ? tooltip.shared : false,
            hoverPoint = p || chart.hoverPoint,
            hoverSeries = hoverPoint && hoverPoint.series || chart.hoverSeries,
            // onMouseOver or already hovering a series with directTouch
            isDirectTouch = e.type !== 'touchmove' && (
                !!p || (
                    (hoverSeries && hoverSeries.directTouch) &&
                    pointer.isDirectTouch
                )
            ),
            hoverData = this.getHoverData(
                hoverPoint,
                hoverSeries,
                series,
                isDirectTouch,
                shared,
                e,
                { isBoosting: chart.isBoosting }
            ),
            useSharedTooltip,
            followPointer,
            anchor,
            points;

        // Update variables from hoverData.
        hoverPoint = hoverData.hoverPoint;
        points = hoverData.hoverPoints;
        hoverSeries = hoverData.hoverSeries;
        followPointer = hoverSeries && hoverSeries.tooltipOptions.followPointer;
        useSharedTooltip = (
            shared &&
            hoverSeries &&
            !hoverSeries.noSharedTooltip
        );

        // Refresh tooltip for kdpoint if new hover point or tooltip was hidden
        // #3926, #4200
        if (
            hoverPoint &&
            // !(hoverSeries && hoverSeries.directTouch) &&
            (hoverPoint !== chart.hoverPoint || (tooltip && tooltip.isHidden))
        ) {
            each(chart.hoverPoints || [], function (p) {
                if (H.inArray(p, points) === -1) {
                    p.setState();
                }
            });
            // Do mouseover on all points (#3919, #3985, #4410, #5622)
            each(points || [], function (p) {
                p.setState('hover');
            });
            // set normal state to previous series
            if (chart.hoverSeries !== hoverSeries) {
                hoverSeries.onMouseOver();
            }

            // If tracking is on series in stead of on each point,
            // fire mouseOver on hover point. // #4448
            if (chart.hoverPoint) {
                chart.hoverPoint.firePointEvent('mouseOut');
            }

            // Hover point may have been destroyed in the event handlers (#7127)
            if (!hoverPoint.series) {
                return;
            }

            hoverPoint.firePointEvent('mouseOver');
            chart.hoverPoints = points;
            chart.hoverPoint = hoverPoint;
            // Draw tooltip if necessary
            if (tooltip) {
                tooltip.refresh(useSharedTooltip ? points : hoverPoint, e);
            }
        // Update positions (regardless of kdpoint or hoverPoint)
        } else if (followPointer && tooltip && !tooltip.isHidden) {
            anchor = tooltip.getAnchor([{}], e);
            tooltip.updatePosition({ plotX: anchor[0], plotY: anchor[1] });
        }

        // Start the event listener to pick up the tooltip and crosshairs
        if (!pointer.unDocMouseMove) {
            pointer.unDocMouseMove = addEvent(
                chart.container.ownerDocument,
                'mousemove',
                function (e) {
                    var chart = charts[H.hoverChartIndex];
                    if (chart) {
                        chart.pointer.onDocumentMouseMove(e);
                    }
                }
            );
        }

        // Issues related to crosshair #4927, #5269 #5066, #5658
        each(chart.axes, function drawAxisCrosshair(axis) {
            var snap = pick(axis.crosshair.snap, true),
                point = !snap ?
                    undefined :
                    H.find(points, function (p) {
                        return p.series[axis.coll] === axis;
                    });

            // Axis has snapping crosshairs, and one of the hover points belongs
            // to axis. Always call drawCrosshair when it is not snap.
            if (point || !snap) {
                axis.drawCrosshair(e, point);
            // Axis has snapping crosshairs, but no hover point belongs to axis
            } else {
                axis.hideCrosshair();
            }
        });
    },

    /**
     * Reset the tracking by hiding the tooltip, the hover series state and the
     * hover point
     *
     * @function Highcharts.Pointer#reset
     *
     * @param {boolean} allowMove
     *        Instead of destroying the tooltip altogether, allow moving it if
     *        possible.
     *
     * @param {number} delay
     */
    reset: function (allowMove, delay) {
        var pointer = this,
            chart = pointer.chart,
            hoverSeries = chart.hoverSeries,
            hoverPoint = chart.hoverPoint,
            hoverPoints = chart.hoverPoints,
            tooltip = chart.tooltip,
            tooltipPoints = tooltip && tooltip.shared ?
                hoverPoints :
                hoverPoint;

        // Check if the points have moved outside the plot area (#1003, #4736,
        // #5101)
        if (allowMove && tooltipPoints) {
            each(splat(tooltipPoints), function (point) {
                if (point.series.isCartesian && point.plotX === undefined) {
                    allowMove = false;
                }
            });
        }

        // Just move the tooltip, #349
        if (allowMove) {
            if (tooltip && tooltipPoints) {
                tooltip.refresh(tooltipPoints);
                if (tooltip.shared && hoverPoints) { // #8284
                    each(hoverPoints, function (point) {
                        point.setState(point.state, true);
                        if (point.series.isCartesian) {
                            if (point.series.xAxis.crosshair) {
                                point.series.xAxis.drawCrosshair(null, point);
                            }
                            if (point.series.yAxis.crosshair) {
                                point.series.yAxis.drawCrosshair(null, point);
                            }
                        }
                    });
                } else if (hoverPoint) { // #2500
                    hoverPoint.setState(hoverPoint.state, true);
                    each(chart.axes, function (axis) {
                        if (axis.crosshair) {
                            axis.drawCrosshair(null, hoverPoint);
                        }
                    });
                }
            }

        // Full reset
        } else {

            if (hoverPoint) {
                hoverPoint.onMouseOut();
            }

            if (hoverPoints) {
                each(hoverPoints, function (point) {
                    point.setState();
                });
            }

            if (hoverSeries) {
                hoverSeries.onMouseOut();
            }

            if (tooltip) {
                tooltip.hide(delay);
            }

            if (pointer.unDocMouseMove) {
                pointer.unDocMouseMove = pointer.unDocMouseMove();
            }

            // Remove crosshairs
            each(chart.axes, function (axis) {
                axis.hideCrosshair();
            });

            pointer.hoverX = chart.hoverPoints = chart.hoverPoint = null;
        }
    },

    /**
     * Scale series groups to a certain scale and translation.
     *
     * @private
     * @function Highcharts.Pointer#scaleGroups
     *
     * @param {Highcharts.SeriesPlotBoxObject} attribs
     *
     * @param {boolean} clip
     */
    scaleGroups: function (attribs, clip) {

        var chart = this.chart,
            seriesAttribs;

        // Scale each series
        each(chart.series, function (series) {
            seriesAttribs = attribs || series.getPlotBox(); // #1701
            if (series.xAxis && series.xAxis.zoomEnabled && series.group) {
                series.group.attr(seriesAttribs);
                if (series.markerGroup) {
                    series.markerGroup.attr(seriesAttribs);
                    series.markerGroup.clip(clip ? chart.clipRect : null);
                }
                if (series.dataLabelsGroup) {
                    series.dataLabelsGroup.attr(seriesAttribs);
                }
            }
        });

        // Clip
        chart.clipRect.attr(clip || chart.clipBox);
    },

    /**
     * Start a drag operation.
     *
     * @private
     * @function Highcharts.Pointer#dragStart
     *
     * @param {Highcharts.PointerEventObject} e
     */
    dragStart: function (e) {
        var chart = this.chart;

        // Record the start position
        chart.mouseIsDown = e.type;
        chart.cancelClick = false;
        chart.mouseDownX = this.mouseDownX = e.chartX;
        chart.mouseDownY = this.mouseDownY = e.chartY;
    },

    /**
     * Perform a drag operation in response to a mousemove event while the mouse
     * is down.
     *
     * @private
     * @function Highcharts.Pointer#drag
     *
     * @param {Highcharts.PointerEventObject} e
     */
    drag: function (e) {

        var chart = this.chart,
            chartOptions = chart.options.chart,
            chartX = e.chartX,
            chartY = e.chartY,
            zoomHor = this.zoomHor,
            zoomVert = this.zoomVert,
            plotLeft = chart.plotLeft,
            plotTop = chart.plotTop,
            plotWidth = chart.plotWidth,
            plotHeight = chart.plotHeight,
            clickedInside,
            size,
            selectionMarker = this.selectionMarker,
            mouseDownX = this.mouseDownX,
            mouseDownY = this.mouseDownY,
            panKey = chartOptions.panKey && e[chartOptions.panKey + 'Key'];

        // If the device supports both touch and mouse (like IE11), and we are
        // touch-dragging inside the plot area, don't handle the mouse event.
        // #4339.
        if (selectionMarker && selectionMarker.touch) {
            return;
        }

        // If the mouse is outside the plot area, adjust to cooordinates
        // inside to prevent the selection marker from going outside
        if (chartX < plotLeft) {
            chartX = plotLeft;
        } else if (chartX > plotLeft + plotWidth) {
            chartX = plotLeft + plotWidth;
        }

        if (chartY < plotTop) {
            chartY = plotTop;
        } else if (chartY > plotTop + plotHeight) {
            chartY = plotTop + plotHeight;
        }

        // determine if the mouse has moved more than 10px
        this.hasDragged = Math.sqrt(
            Math.pow(mouseDownX - chartX, 2) +
            Math.pow(mouseDownY - chartY, 2)
        );

        if (this.hasDragged > 10) {
            clickedInside = chart.isInsidePlot(
                mouseDownX - plotLeft,
                mouseDownY - plotTop
            );

            // make a selection
            if (
                chart.hasCartesianSeries &&
                (this.zoomX || this.zoomY) &&
                clickedInside &&
                !panKey
            ) {
                if (!selectionMarker) {
                    this.selectionMarker = selectionMarker =
                        chart.renderer.rect(
                            plotLeft,
                            plotTop,
                            zoomHor ? 1 : plotWidth,
                            zoomVert ? 1 : plotHeight,
                            0
                        )
                        .attr({
                            
                            fill: (
                                chartOptions.selectionMarkerFill ||
                                color('#335cad')
                                    .setOpacity(0.25).get()
                            ),
                            
                            'class': 'highcharts-selection-marker',
                            'zIndex': 7
                        })
                        .add();
                }
            }

            // adjust the width of the selection marker
            if (selectionMarker && zoomHor) {
                size = chartX - mouseDownX;
                selectionMarker.attr({
                    width: Math.abs(size),
                    x: (size > 0 ? 0 : size) + mouseDownX
                });
            }
            // adjust the height of the selection marker
            if (selectionMarker && zoomVert) {
                size = chartY - mouseDownY;
                selectionMarker.attr({
                    height: Math.abs(size),
                    y: (size > 0 ? 0 : size) + mouseDownY
                });
            }

            // panning
            if (clickedInside && !selectionMarker && chartOptions.panning) {
                chart.pan(e, chartOptions.panning);
            }
        }
    },

    /**
     * On mouse up or touch end across the entire document, drop the selection.
     *
     * @private
     * @function Highcharts.Pointer#drop
     *
     * @param {global.Event} e
     */
    drop: function (e) {
        var pointer = this,
            chart = this.chart,
            hasPinched = this.hasPinched;

        if (this.selectionMarker) {
            var selectionData = {
                    originalEvent: e, // #4890
                    xAxis: [],
                    yAxis: []
                },
                selectionBox = this.selectionMarker,
                selectionLeft = selectionBox.attr ?
                    selectionBox.attr('x') :
                    selectionBox.x,
                selectionTop = selectionBox.attr ?
                    selectionBox.attr('y') :
                    selectionBox.y,
                selectionWidth = selectionBox.attr ?
                    selectionBox.attr('width') :
                    selectionBox.width,
                selectionHeight = selectionBox.attr ?
                    selectionBox.attr('height') :
                    selectionBox.height,
                runZoom;

            // a selection has been made
            if (this.hasDragged || hasPinched) {

                // record each axis' min and max
                each(chart.axes, function (axis) {
                    if (
                        axis.zoomEnabled &&
                        defined(axis.min) &&
                        (
                            hasPinched ||
                            pointer[{
                                xAxis: 'zoomX',
                                yAxis: 'zoomY'
                            }[axis.coll]]
                        )
                    ) { // #859, #3569
                        var horiz = axis.horiz,
                            minPixelPadding = e.type === 'touchend' ?
                                axis.minPixelPadding :
                                0, // #1207, #3075
                            selectionMin = axis.toValue(
                                (horiz ? selectionLeft : selectionTop) +
                                minPixelPadding
                            ),
                            selectionMax = axis.toValue(
                                (
                                    horiz ?
                                        selectionLeft + selectionWidth :
                                        selectionTop + selectionHeight
                                ) - minPixelPadding
                                );

                        selectionData[axis.coll].push({
                            axis: axis,
                            // Min/max for reversed axes
                            min: Math.min(selectionMin, selectionMax),
                            max: Math.max(selectionMin, selectionMax)
                        });
                        runZoom = true;
                    }
                });
                if (runZoom) {
                    fireEvent(
                        chart,
                        'selection',
                        selectionData,
                        function (args) {
                            chart.zoom(
                                extend(
                                    args,
                                    hasPinched ? { animation: false } : null
                                )
                            );
                        }
                    );
                }

            }

            if (isNumber(chart.index)) {
                this.selectionMarker = this.selectionMarker.destroy();
            }

            // Reset scaling preview
            if (hasPinched) {
                this.scaleGroups();
            }
        }

        // Reset all. Check isNumber because it may be destroyed on mouse up
        // (#877)
        if (chart && isNumber(chart.index)) {
            css(chart.container, { cursor: chart._cursor });
            chart.cancelClick = this.hasDragged > 10; // #370
            chart.mouseIsDown = this.hasDragged = this.hasPinched = false;
            this.pinchDown = [];
        }
    },

    /**
     * @private
     * @function Highcharts.Pointer#onContainerMouseDown
     *
     * @param {global.Event} e
     */
    onContainerMouseDown: function (e) {
        // Normalize before the 'if' for the legacy IE (#7850)
        e = this.normalize(e);

        if (e.button !== 2) {

            this.zoomOption(e);

            // issue #295, dragging not always working in Firefox
            if (e.preventDefault) {
                e.preventDefault();
            }

            this.dragStart(e);
        }
    },

    /**
     * @private
     * @function Highcharts.Pointer#onDocumentMouseUp
     *
     * @param {global.Event} e
     */
    onDocumentMouseUp: function (e) {
        if (charts[H.hoverChartIndex]) {
            charts[H.hoverChartIndex].pointer.drop(e);
        }
    },

    /**
     * Special handler for mouse move that will hide the tooltip when the mouse
     * leaves the plotarea. Issue #149 workaround. The mouseleave event does not
     * always fire.
     *
     * @private
     * @function Highcharts.Pointer#onDocumentMouseMove
     *
     * @param {Highcharts.PointerEventObject} e
     */
    onDocumentMouseMove: function (e) {
        var chart = this.chart,
            chartPosition = this.chartPosition;

        e = this.normalize(e, chartPosition);

        // If we're outside, hide the tooltip
        if (
            chartPosition &&
            !this.inClass(e.target, 'highcharts-tracker') &&
            !chart.isInsidePlot(
                e.chartX - chart.plotLeft,
                e.chartY - chart.plotTop
            )
        ) {
            this.reset();
        }
    },

    /**
     * When mouse leaves the container, hide the tooltip.
     *
     * @private
     * @function Highcharts.Pointer#onContainerMouseLeave
     *
     * @param {global.Event} e
     */
    onContainerMouseLeave: function (e) {
        var chart = charts[H.hoverChartIndex];
        // #4886, MS Touch end fires mouseleave but with no related target
        if (chart && (e.relatedTarget || e.toElement)) {
            chart.pointer.reset();
            // Also reset the chart position, used in #149 fix
            chart.pointer.chartPosition = null;
        }
    },

    /**
     * The mousemove, touchmove and touchstart event handler
     *
     * @private
     * @function Highcharts.Pointer#onContainerMouseMove
     *
     * @param {Highcharts.PointerEventObject} e
     */
    onContainerMouseMove: function (e) {

        var chart = this.chart;

        if (
            !defined(H.hoverChartIndex) ||
            !charts[H.hoverChartIndex] ||
            !charts[H.hoverChartIndex].mouseIsDown
        ) {
            H.hoverChartIndex = chart.index;
        }

        e = this.normalize(e);
        e.returnValue = false; // #2251, #3224

        if (chart.mouseIsDown === 'mousedown') {
            this.drag(e);
        }

        // Show the tooltip and run mouse over events (#977)
        if (
            (
                this.inClass(e.target, 'highcharts-tracker') ||
                chart.isInsidePlot(
                    e.chartX - chart.plotLeft,
                    e.chartY - chart.plotTop
                )
            ) &&
            !chart.openMenu
        ) {
            this.runPointActions(e);
        }
    },

    /**
     * Utility to detect whether an element has, or has a parent with, a
     * specificclass name. Used on detection of tracker objects and on deciding
     * whether hovering the tooltip should cause the active series to mouse out.
     *
     * @function Highcharts.Pointer#inClass
     *
     * @param {Highcharts.SVGDOMElement|Highcharts.HTMLDOMElement} element
     *        The element to investigate.
     *
     * @param {string} className
     *        The class name to look for.
     *
     * @return {boolean}
     *         True if either the element or one of its parents has the given
     *         class name.
     */
    inClass: function (element, className) {
        var elemClassName;
        while (element) {
            elemClassName = attr(element, 'class');
            if (elemClassName) {
                if (elemClassName.indexOf(className) !== -1) {
                    return true;
                }
                if (elemClassName.indexOf('highcharts-container') !== -1) {
                    return false;
                }
            }
            element = element.parentNode;
        }
    },

    /**
     * @private
     * @function Highcharts.Pointer#onTrackerMouseOut
     *
     * @param {global.Event} e
     */
    onTrackerMouseOut: function (e) {
        var series = this.chart.hoverSeries,
            relatedTarget = e.relatedTarget || e.toElement;

        this.isDirectTouch = false;

        if (
            series &&
            relatedTarget &&
            !series.stickyTracking &&
            !this.inClass(relatedTarget, 'highcharts-tooltip') &&
            (
                !this.inClass(
                    relatedTarget,
                    'highcharts-series-' + series.index
                ) || // #2499, #4465
                !this.inClass(relatedTarget, 'highcharts-tracker') // #5553
            )
        ) {
            series.onMouseOut();
        }
    },

    /**
     * @private
     * @function Highcharts.Pointer#onContainerClick
     *
     * @param {Highcharts.PointerEventObject} e
     */
    onContainerClick: function (e) {
        var chart = this.chart,
            hoverPoint = chart.hoverPoint,
            plotLeft = chart.plotLeft,
            plotTop = chart.plotTop;

        e = this.normalize(e);

        if (!chart.cancelClick) {

            // On tracker click, fire the series and point events. #783, #1583
            if (hoverPoint && this.inClass(e.target, 'highcharts-tracker')) {

                // the series click event
                fireEvent(hoverPoint.series, 'click', extend(e, {
                    point: hoverPoint
                }));

                // the point click event
                if (chart.hoverPoint) { // it may be destroyed (#1844)
                    hoverPoint.firePointEvent('click', e);
                }

            // When clicking outside a tracker, fire a chart event
            } else {
                extend(e, this.getCoordinates(e));

                // fire a click event in the chart
                if (
                    chart.isInsidePlot(e.chartX - plotLeft, e.chartY - plotTop)
                ) {
                    fireEvent(chart, 'click', e);
                }
            }


        }
    },

    /**
     * Set the JS DOM events on the container and document. This method should
     * contain a one-to-one assignment between methods and their handlers. Any
     * advanced logic should be moved to the handler reflecting the event's
     * name.
     *
     * @private
     * @function Highcharts.Pointer#setDOMEvents
     */
    setDOMEvents: function () {

        var pointer = this,
            container = pointer.chart.container,
            ownerDoc = container.ownerDocument;

        container.onmousedown = function (e) {
            pointer.onContainerMouseDown(e);
        };
        container.onmousemove = function (e) {
            pointer.onContainerMouseMove(e);
        };
        container.onclick = function (e) {
            pointer.onContainerClick(e);
        };
        this.unbindContainerMouseLeave = addEvent(
            container,
            'mouseleave',
            pointer.onContainerMouseLeave
        );
        if (!H.unbindDocumentMouseUp) {
            H.unbindDocumentMouseUp = addEvent(
                ownerDoc,
                'mouseup',
                pointer.onDocumentMouseUp
            );
        }
        if (H.hasTouch) {
            container.ontouchstart = function (e) {
                pointer.onContainerTouchStart(e);
            };
            container.ontouchmove = function (e) {
                pointer.onContainerTouchMove(e);
            };
            if (!H.unbindDocumentTouchEnd) {
                H.unbindDocumentTouchEnd = addEvent(
                    ownerDoc,
                    'touchend',
                    pointer.onDocumentTouchEnd
                );
            }
        }

    },

    /**
     * Destroys the Pointer object and disconnects DOM events.
     *
     * @function Highcharts.Pointer#destroy
     */
    destroy: function () {
        var pointer = this;

        if (pointer.unDocMouseMove) {
            pointer.unDocMouseMove();
        }

        this.unbindContainerMouseLeave();

        if (!H.chartCount) {
            if (H.unbindDocumentMouseUp) {
                H.unbindDocumentMouseUp = H.unbindDocumentMouseUp();
            }
            if (H.unbindDocumentTouchEnd) {
                H.unbindDocumentTouchEnd = H.unbindDocumentTouchEnd();
            }
        }

        // memory and CPU leak
        clearInterval(pointer.tooltipTimeout);

        H.objectEach(pointer, function (val, prop) {
            pointer[prop] = null;
        });
    }
};