Commit ee58a6ee by Axel Pirek Committed by Axel Pirek

Speed up tooltip value retrieval

parent a2e6408b
......@@ -34,13 +34,22 @@ function ($, core) {
};
this.findHoverIndexFromData = function(posX, series) {
var len = series.data.length;
for (var j = 0; j < len; j++) {
if (series.data[j][0] > posX) {
return Math.max(j - 1, 0);
var lower = 0;
var upper = series.data.length - 1;
var middle;
while (true) {
if (lower > upper) {
return Math.max(upper, 0);
}
middle = Math.floor((lower + upper) / 2);
if (series.data[middle][0] === posX) {
return middle;
} else if (series.data[middle][0] < posX) {
lower = middle + 1;
} else { //if (series.data[middle][0] > posX) {
upper = middle - 1;
}
}
return j - 1;
};
this.renderAndShow = function(absoluteTime, innerHtml, pos, xMode) {
......
......@@ -40,6 +40,31 @@ function describeSharedTooltip(desc, fn) {
});
}
describe("findHoverIndexFromData", function() {
var tooltip = new GraphTooltip(elem, dashboard, scope);
var series = { data: [[100, 0], [101, 0], [102, 0], [103, 0], [104, 0], [105, 0], [106, 0], [107, 0]] };
it("should return 0 if posX out of lower bounds", function() {
var posX = 99;
expect(tooltip.findHoverIndexFromData(posX, series)).to.be(0);
});
it("should return n - 1 if posX out of upper bounds", function() {
var posX = 108;
expect(tooltip.findHoverIndexFromData(posX, series)).to.be(series.data.length - 1);
});
it("should return i if posX in series", function() {
var posX = 104;
expect(tooltip.findHoverIndexFromData(posX, series)).to.be(4);
});
it("should return i if posX not in series and i + 1 > posX", function() {
var posX = 104.9;
expect(tooltip.findHoverIndexFromData(posX, series)).to.be(4);
});
});
describeSharedTooltip("steppedLine false, stack false", function(ctx) {
ctx.setup(function() {
ctx.data = [
......@@ -168,6 +193,3 @@ describeSharedTooltip("steppedLine false, stack true, individual true", function
expect(ctx.results[1].value).to.be(2);
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment