From 048bb0d06c2d5b4e7ad2cade323d4ef62832fd47 Mon Sep 17 00:00:00 2001 From: "vladimir%pobox.com" Date: Wed, 15 Aug 2007 16:45:34 +0000 Subject: [PATCH] b=392057, incorporate graph server fixes from vlad's tree, r=alice -- Make cursor in big graph snap to nearest point when moving the mouse git-svn-id: svn://10.0.0.236/trunk@232130 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/webtools/new-graph/js/GraphCanvas.js | 43 +++++++++++++++++++- mozilla/webtools/new-graph/js/graph.js | 6 ++- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/mozilla/webtools/new-graph/js/GraphCanvas.js b/mozilla/webtools/new-graph/js/GraphCanvas.js index 147f727eda5..77d5effe9a3 100755 --- a/mozilla/webtools/new-graph/js/GraphCanvas.js +++ b/mozilla/webtools/new-graph/js/GraphCanvas.js @@ -86,6 +86,7 @@ Graph.prototype = { cursorColor: "rgba(200,200,0,0.7)", cursorTime: null, cursorValue: null, + cursorSnapsToPoints: false, markerColor: "rgba(200,0,0,0.4)", markersVisible: true, @@ -962,8 +963,46 @@ Graph.prototype = { var secondsPerPixel = (this.endTime - this.startTime + this.offsetTime) / this.frontBuffer.width; var valuesPerPixel = 1.0 / this.yScale; - this.cursorTime = (event.pageX - pos[0]) * secondsPerPixel + this.startTime; - this.cursorValue = (this.frontBuffer.height - (event.pageY - pos[1])) * valuesPerPixel + this.yOffset; + var pointTime = (event.pageX - pos[0]) * secondsPerPixel + this.startTime; + var pointValue = (this.frontBuffer.height - (event.pageY - pos[1])) * valuesPerPixel + this.yOffset; + + if (this.cursorSnapsToPoints && this.dataSets.length > 0) { + // find the nearest point to (pointTime, pointValue) in all the datasets + var distanceSquared = -1; + var nearestDSIndex, nearestPointIndex; + + for (var i = 0; i < this.dataSets.length; i++) { + var lastDistance = -1; + for (var j = this.dataSetIndices[i][0]; + j < this.dataSetIndices[i][1]; + j++) + { + var t = this.dataSets[i].data[j*2]; + var v = this.dataSets[i].data[j*2+1]; + var d = Math.abs(pointTime*pointTime - t*t) + Math.abs(pointValue*pointValue - v*v); + + // stop looking at this ds if we're getting further away from the point + if (lastDistance != -1 && d > lastDistance) + break; + + if (distanceSquared == -1 || + d < distanceSquared) + { + nearestDSIndex = i; + nearestPointIndex = j; + distanceSquared = d; + } + + lastDistance = d; + } + } + + pointTime = this.dataSets[nearestDSIndex].data[nearestPointIndex*2]; + pointValue = this.dataSets[nearestDSIndex].data[nearestPointIndex*2 + 1]; + } + + this.cursorTime = pointTime; + this.cursorValue = pointValue; //for adding extra_data variable to the status line var extra_data = ""; diff --git a/mozilla/webtools/new-graph/js/graph.js b/mozilla/webtools/new-graph/js/graph.js index ee8c0be3fcd..465c74b61ab 100644 --- a/mozilla/webtools/new-graph/js/graph.js +++ b/mozilla/webtools/new-graph/js/graph.js @@ -78,17 +78,19 @@ function loadingDone(graphTypePref) { Tinderbox = new TinderboxData(); SmallPerfGraph = new CalendarTimeGraph("smallgraph"); BigPerfGraph = new CalendarTimeGraph("graph"); + BigPerfGraph.cursorSnapsToPoints = true; onDataLoadChanged(); } else if (graphType == DATA_GRAPH) { Tinderbox = new ExtraDataTinderboxData(); SmallPerfGraph = new CalendarTimeGraph("smallgraph"); BigPerfGraph = new CalendarTimeGraph("graph"); - } - else { + BigPerfGraph.cursorSnapsToPoints = true; + } else { Tinderbox = new DiscreteTinderboxData(); Tinderbox.raw = 1; SmallPerfGraph = new DiscreteGraph("smallgraph"); BigPerfGraph = new DiscreteGraph("graph"); + BigPerfGraph.cursorSnapsToPoints = true; onDiscreteDataLoadChanged(); }