1741 | | var data = series[i].data, |
---|
1742 | | axisx = series[i].xaxis, |
---|
1743 | | axisy = series[i].yaxis, |
---|
1744 | | lowestDistance = maxDistance * maxDistance + 1, |
---|
1745 | | |
---|
1746 | | // precompute some stuff to make the loop faster |
---|
1747 | | mx = axisx.c2p(mouseX), |
---|
1748 | | my = axisy.c2p(mouseY), |
---|
1749 | | maxx = maxDistance / axisx.scale, |
---|
1750 | | maxy = maxDistance / axisy.scale, |
---|
1751 | | checkbar = series[i].bars.show, |
---|
1752 | | checkpoint = !(series[i].bars.show && !(series[i].lines.show || series[i].points.show)), |
---|
1753 | | barLeft = series[i].bars.align == "left" ? 0 : -series[i].bars.barWidth/2, |
---|
1754 | | barRight = barLeft + series[i].bars.barWidth, |
---|
1755 | | bj = binary_search(data, mx, data.length-1, 0), |
---|
1756 | | j_start, j_end; |
---|
1757 | | if(options.grid.hoverXOnly) { |
---|
1758 | | j_start = bj.index; |
---|
1759 | | j_end = bj.index + 1; |
---|
1760 | | if(!bj.found) j_end++; |
---|
1761 | | } else { |
---|
1762 | | j_start = 0; |
---|
1763 | | j_end = data.length; |
---|
1764 | | } |
---|
1765 | | for (var j = j_start; j < j_end; ++j) { |
---|
1766 | | if (data[j] == null) |
---|
1767 | | continue; |
---|
1768 | | |
---|
1769 | | var x = data[j][0], y = data[j][1]; |
---|
1770 | | if(series[i].dataManip) y = series[i].dataManip(y); |
---|
1771 | | |
---|
1772 | | if (checkbar) { |
---|
1773 | | // For a bar graph, the cursor must be inside the bar |
---|
1774 | | // and no other point can be nearby |
---|
1775 | | if (!foundPoint && mx >= x + barLeft && |
---|
1776 | | mx <= x + barRight && |
---|
1777 | | my >= Math.min(0, y) && my <= Math.max(0, y)) |
---|
1778 | | item = result(i, j); |
---|
1779 | | } |
---|
1780 | | |
---|
1781 | | if (checkpoint) { |
---|
1782 | | // For points and lines, the cursor must be within a |
---|
1783 | | // certain distance to the data point |
---|
1784 | | |
---|
1785 | | // if the hoverxonly option is true, y doesn't matter |
---|
1786 | | if(options.grid.hoverXOnly) my = y; |
---|
1787 | | |
---|
1788 | | // check bounding box first |
---|
1789 | | if ((x - mx > maxx || x - mx < -maxx) || |
---|
1790 | | (y - my > maxy || y - my < -maxy)) |
---|
1791 | | continue; |
---|
1792 | | |
---|
1793 | | // We have to calculate distances in pixels, not in |
---|
1794 | | // data units, because the scale of the axes may be different |
---|
1795 | | var dx = Math.abs(axisx.p2c(x) - mouseX), |
---|
1796 | | dy = Math.abs(axisy.p2c(y) - mouseY), |
---|
1797 | | dist; |
---|
1798 | | dist = dx * dx; |
---|
1799 | | if(!options.grid.hoverXOnly) dist += dy * dy; |
---|
1800 | | if (dist < lowestDistance) { |
---|
1801 | | lowestDistance = dist; |
---|
1802 | | foundPoint = true; |
---|
1803 | | item = result(i, j); |
---|
1804 | | } |
---|
1805 | | } |
---|
1806 | | } |
---|
1807 | | if(item) items.push(item); |
---|
1808 | | item = null; |
---|
1809 | | } |
---|
1810 | | |
---|
| 1741 | if(series[i].bars.show || series[i].lines.show || series[i].points.show){ |
---|
| 1742 | var data = series[i].data, |
---|
| 1743 | axisx = series[i].xaxis, |
---|
| 1744 | axisy = series[i].yaxis, |
---|
| 1745 | lowestDistance = maxDistance * maxDistance + 1, |
---|
| 1746 | |
---|
| 1747 | // precompute some stuff to make the loop faster |
---|
| 1748 | mx = axisx.c2p(mouseX), |
---|
| 1749 | my = axisy.c2p(mouseY), |
---|
| 1750 | maxx = maxDistance / axisx.scale, |
---|
| 1751 | maxy = maxDistance / axisy.scale, |
---|
| 1752 | checkbar = series[i].bars.show, |
---|
| 1753 | checkpoint = !(series[i].bars.show && !(series[i].lines.show || series[i].points.show)), |
---|
| 1754 | barLeft = series[i].bars.align == "left" ? 0 : -series[i].bars.barWidth/2, |
---|
| 1755 | barRight = barLeft + series[i].bars.barWidth, |
---|
| 1756 | bj = binary_search(data, mx, data.length-1, 0), |
---|
| 1757 | j_start, j_end; |
---|
| 1758 | if(options.grid.hoverXOnly) { |
---|
| 1759 | j_start = bj.index; |
---|
| 1760 | j_end = bj.index + 1; |
---|
| 1761 | if(!bj.found) j_end++; |
---|
| 1762 | } else { |
---|
| 1763 | j_start = 0; |
---|
| 1764 | j_end = data.length; |
---|
| 1765 | } |
---|
| 1766 | for (var j = j_start; j < j_end; ++j) { |
---|
| 1767 | if (data[j] == null) |
---|
| 1768 | continue; |
---|
| 1769 | |
---|
| 1770 | var x = data[j][0], y = data[j][1]; |
---|
| 1771 | if(series[i].dataManip) y = series[i].dataManip(y); |
---|
| 1772 | |
---|
| 1773 | if (checkbar) { |
---|
| 1774 | // For a bar graph, the cursor must be inside the bar |
---|
| 1775 | // and no other point can be nearby |
---|
| 1776 | if (!foundPoint && mx >= x + barLeft && |
---|
| 1777 | mx <= x + barRight && |
---|
| 1778 | my >= Math.min(0, y) && my <= Math.max(0, y)) |
---|
| 1779 | item = result(i, j); |
---|
| 1780 | } |
---|
| 1781 | |
---|
| 1782 | if (checkpoint) { |
---|
| 1783 | // For points and lines, the cursor must be within a |
---|
| 1784 | // certain distance to the data point |
---|
| 1785 | |
---|
| 1786 | // if the hoverxonly option is true, y doesn't matter |
---|
| 1787 | if(options.grid.hoverXOnly) my = y; |
---|
| 1788 | |
---|
| 1789 | // check bounding box first |
---|
| 1790 | if ((x - mx > maxx || x - mx < -maxx) || |
---|
| 1791 | (y - my > maxy || y - my < -maxy)) |
---|
| 1792 | continue; |
---|
| 1793 | |
---|
| 1794 | // We have to calculate distances in pixels, not in |
---|
| 1795 | // data units, because the scale of the axes may be different |
---|
| 1796 | var dx = Math.abs(axisx.p2c(x) - mouseX), |
---|
| 1797 | dy = Math.abs(axisy.p2c(y) - mouseY), |
---|
| 1798 | dist; |
---|
| 1799 | dist = dx * dx; |
---|
| 1800 | if(!options.grid.hoverXOnly) dist += dy * dy; |
---|
| 1801 | if (dist < lowestDistance) { |
---|
| 1802 | lowestDistance = dist; |
---|
| 1803 | foundPoint = true; |
---|
| 1804 | item = result(i, j); |
---|
| 1805 | } |
---|
| 1806 | } |
---|
| 1807 | } |
---|
| 1808 | if(item) items.push(item); |
---|
| 1809 | item = null; |
---|
| 1810 | } |
---|
| 1811 | } |
---|