OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 |
| 6 var NumberGraph = (function () { |
| 7 "use strict"; |
| 8 |
| 9 function NumberGraph(title) { |
| 10 |
| 11 this.element = document.createElement('canvas'); |
| 12 this.context = this.element.getContext('2d'); |
| 13 |
| 14 this.barWidth = 1; |
| 15 |
| 16 this.previousDataValues = []; |
| 17 |
| 18 // Indexed by the position from the left |
| 19 this.recording = []; |
| 20 this.hoverAt = 0; |
| 21 } |
| 22 |
| 23 NumberGraph.prototype.setSize = function (width, height) { |
| 24 this.element.width = width; |
| 25 this.element.height = height; |
| 26 this.width = width; |
| 27 this.height = height; |
| 28 |
| 29 this.maxPoints = this.width / this.barWidth; |
| 30 |
| 31 this.setup(); |
| 32 }; |
| 33 |
| 34 NumberGraph.prototype.setup = function () { |
| 35 this.element.onmousedown = function (event) { |
| 36 this.hoverAt = event.offsetX; |
| 37 this.drawWith(this.previousDataValues); |
| 38 }.bind(this); |
| 39 }; |
| 40 |
| 41 NumberGraph.prototype.getRelativePos = function (min, max, value) { |
| 42 var totalDist = Math.abs(max - min); |
| 43 var posFromBottom = Math.abs(value - min); |
| 44 var posPercent = posFromBottom / totalDist; |
| 45 return Math.floor(posPercent * this.height); |
| 46 }; |
| 47 |
| 48 NumberGraph.prototype.drawWith = function (dataValues_passed) { |
| 49 this.previousDataValues = dataValues_passed.slice(0); |
| 50 // Reverse it so we can traverse from newest to oldest |
| 51 var dataValues = dataValues_passed.slice(0).reverse(); |
| 52 this.recording = []; |
| 53 |
| 54 this.context.fillStyle = "white"; |
| 55 this.context.fillRect(0, 0, this.width, this.height); |
| 56 // We only want values that are greater than 20 milliseconds apart |
| 57 var using = []; |
| 58 var last = {time: -1000}; |
| 59 var i = 0; |
| 60 var len = dataValues.length; |
| 61 |
| 62 // We only want the last |maxPoints| values. |
| 63 for (i = 0; i < Math.min(this.maxPoints, dataValues.length); i++) { |
| 64 var val = dataValues[i]; |
| 65 if (Math.abs(val.time - last.time) >= 20) { |
| 66 using.unshift(val); |
| 67 last = val; |
| 68 this.recording.unshift(val); |
| 69 } |
| 70 } |
| 71 |
| 72 var usingValues = using.map(function (t) { |
| 73 return t.value; |
| 74 }); |
| 75 var maximum = Math.max.apply(null, usingValues); |
| 76 var minimum = Math.min.apply(null, usingValues); |
| 77 var placeZero = this.height - this.getRelativePos(minimum, maximum, 0); |
| 78 |
| 79 |
| 80 this.context.fillStyle = "rgb(70, 70, 130)"; |
| 81 var posw = 0; |
| 82 |
| 83 using.forEach(function (v) { |
| 84 var position = this.getRelativePos(minimum, maximum, v.value); |
| 85 this.context.fillRect(posw, placeZero, this.barWidth, |
| 86 this.height - (position + placeZero)); |
| 87 posw += this.barWidth; |
| 88 }, this); |
| 89 |
| 90 // Draw the horizontal '0' bar |
| 91 this.context.fillStyle = "rgb(90, 90, 90)"; |
| 92 this.context.fillRect(0, placeZero - 1, this.width, 2); |
| 93 |
| 94 // Draw the mouse bar |
| 95 this.context.fillStyle = "red"; |
| 96 this.context.fillRect(this.hoverAt, 0, 1, this.height); |
| 97 |
| 98 var normalizedPosition = Math.floor(this.hoverAt / this.barWidth); |
| 99 var dataAtCursor = this.recording[normalizedPosition]; |
| 100 if (dataAtCursor) { |
| 101 this.context.fillText(dataAtCursor.value.toFixed(4), 20, 20); |
| 102 this.context.fillText(goog.time.millisToString(dataAtCursor.time), |
| 103 20, 40); |
| 104 } |
| 105 if (usingValues.length >= this.maxPoints) { |
| 106 this.hoverAt -= this.barWidth; |
| 107 } |
| 108 }; |
| 109 |
| 110 return NumberGraph; |
| 111 }()); |
OLD | NEW |