Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 * Plot a line graph of data versus time on a HTML canvas element. | |
| 7 * | |
| 8 * @param {DOMElement} canvas The canvas on which the line graph is drawn. | |
| 9 * @param {Array.Integer} tdata The time (in seconds) in the past when the | |
| 10 * corresponding data in plots was sampled. | |
|
Daniel Erat
2014/01/03 16:40:47
can you just store the timestamps in |plots| as we
Siva Chandra
2014/01/03 23:34:59
Do you mean like this:
plots = {
tdata, //
Daniel Erat
2014/01/04 00:02:17
no, i meant something more like:
lines = [
{
Siva Chandra
2014/01/06 22:36:57
Discussion about this is present elsewhere.
| |
| 11 * @param {Array.<Object>} plots An array of objects with fields 'data' and | |
| 12 * 'color'. The field 'data' is an array of samples to be plotted as a | |
| 13 * line graph with 'color'. | |
|
Daniel Erat
2014/01/03 16:40:47
document that this is ordered by ascending time
Siva Chandra
2014/01/03 23:34:59
Done.
| |
| 14 * @param {number} ymin A number representing the minimum of the values to | |
|
Daniel Erat
2014/01/03 16:40:47
instead of passing in the min and max values, can
Siva Chandra
2014/01/03 23:34:59
I agree. But the purpose here is to have the flexi
Daniel Erat
2014/01/04 00:02:17
sure, makes sense. can you update the comments to
Siva Chandra
2014/01/06 22:36:57
Done.
| |
| 15 * to be shown on the y-axis. | |
| 16 * @param {number} ymax A number representing the maximum of the values to | |
| 17 * to be shown on the y-axis. | |
| 18 * @param {integer} yprecision An integer value representing the number of | |
| 19 * digits of precision the y-axis data should be printed with. | |
| 20 */ | |
| 21 function plotLineGraph(canvas, tdata, plots, ymin, ymax, yprecision) { | |
| 22 var indexFont = '12px Arial'; | |
| 23 var indexHeight = 12; | |
| 24 var padding = 5; // Pixels | |
| 25 var rectColor = '#888888'; // A darker shade of grey | |
|
Daniel Erat
2014/01/03 16:40:47
nit: '#888', '#ccc'. don't think the comments are
Siva Chandra
2014/01/03 23:34:59
Done.
| |
| 26 var gridColor = '#CCCCCC'; // A lighter shade of grey | |
| 27 var ctx = canvas.getContext('2d'); | |
| 28 var size = tdata.length; | |
| 29 | |
| 30 if (size < 2) { | |
| 31 ctx.clearRect(0, 0, canvas.scrollWidth, canvas.scrollHeight); | |
| 32 ctx.font = indexFont; | |
| 33 ctx.fillText('Enough data not available yet.', 15, 15); | |
|
Daniel Erat
2014/01/03 16:40:47
shouldn't this be a string resource?
also, "Not e
Siva Chandra
2014/01/03 23:34:59
Done.
| |
| 34 return; | |
| 35 } | |
| 36 | |
| 37 function yDataToString(index) { | |
|
Daniel Erat
2014/01/04 00:02:17
i don't understand the use of the word "index" in
Siva Chandra
2014/01/06 22:36:57
Done.
| |
| 38 var indexNum = Number(index); | |
| 39 return indexNum.toPrecision(yprecision); | |
| 40 } | |
| 41 | |
| 42 function getIndexWidth(index) { | |
| 43 ctx.font = indexFont; | |
| 44 return ctx.measureText(index).width + 2; | |
| 45 } | |
| 46 | |
| 47 function drawIndex(index, x, y) { | |
| 48 ctx.font = indexFont; | |
| 49 ctx.fillStyle = '#000000'; // Black | |
| 50 ctx.fillText(index, x, y); | |
| 51 } | |
| 52 | |
| 53 function getTimeString(timePast) { | |
| 54 var d = new Date(); | |
| 55 d.setSeconds(-timePast); | |
| 56 return d.toLocaleTimeString(); | |
| 57 } | |
| 58 | |
| 59 function drawLine(x1, y1, x2, y2, color) { | |
| 60 var colorBackup = ctx.strokeStyle; | |
| 61 ctx.beginPath(); | |
| 62 ctx.moveTo(x1, y1); | |
| 63 ctx.lineTo(x2, y2); | |
| 64 ctx.strokeStyle = color; | |
| 65 ctx.stroke(); | |
| 66 ctx.strokeStyle = colorBackup; | |
| 67 } | |
| 68 | |
| 69 var yminStr = yDataToString(ymin); | |
| 70 var ymaxStr = yDataToString(ymax); | |
| 71 var yhalfStr = yDataToString((ymax + ymin) / 2); | |
| 72 var yminWidth = getIndexWidth(yminStr); | |
| 73 var ymaxWidth = getIndexWidth(ymaxStr); | |
| 74 var yhalfWidth = getIndexWidth(yhalfStr); | |
| 75 | |
| 76 var xminStr = getTimeString(tdata[0]); | |
| 77 var xmaxStr = getTimeString(tdata[size - 1]); | |
| 78 var xminWidth = getIndexWidth(xminStr); | |
| 79 var xmaxWidth = getIndexWidth(xmaxStr); | |
| 80 | |
| 81 var xorigin = padding + Math.max(yminWidth, ymaxWidth, xminWidth / 2); | |
| 82 var yorigin = padding + indexHeight / 2; | |
| 83 var width = canvas.scrollWidth - xorigin - xmaxWidth / 2 - padding; | |
| 84 var height = canvas.scrollHeight - yorigin - indexHeight - padding; | |
| 85 | |
| 86 // Start fresh. | |
| 87 ctx.clearRect(0, 0, canvas.scrollWidth, canvas.scrollHeight); | |
| 88 | |
| 89 // Draw the bounding rectangle. | |
| 90 ctx.strokeStyle = rectColor; | |
| 91 ctx.strokeRect(xorigin, yorigin, width, height); | |
| 92 | |
| 93 // Draw the x and y bound values. | |
| 94 drawIndex(ymaxStr, xorigin - ymaxWidth, yorigin + indexHeight / 2); | |
| 95 drawIndex(yminStr, xorigin - yminWidth, yorigin + height); | |
| 96 drawIndex(xminStr, xorigin - xminWidth / 2, yorigin + height + indexHeight); | |
| 97 drawIndex(xmaxStr, | |
| 98 xorigin + width - xmaxWidth / 2, | |
| 99 yorigin + height + indexHeight); | |
| 100 | |
| 101 // Draw y-level (horizontal) lines. | |
| 102 drawLine(xorigin, yorigin + height / 4, | |
| 103 xorigin + width, yorigin + height / 4, | |
| 104 gridColor); | |
| 105 drawLine(xorigin, yorigin + height / 2, | |
| 106 xorigin + width, yorigin + height / 2, gridColor); | |
| 107 drawLine(xorigin, yorigin + 3 * height / 4, | |
| 108 xorigin + width, yorigin + 3 * height / 4, | |
| 109 gridColor); | |
| 110 | |
| 111 // Draw half-level value. | |
| 112 drawIndex(yhalfStr, | |
| 113 xorigin - yhalfWidth, | |
| 114 yorigin + height / 2 + indexHeight / 2); | |
| 115 | |
| 116 // Draw the plots. | |
| 117 for (var count in plots) { | |
| 118 var plot = plots[count]; | |
| 119 var ydata = plot.data; | |
| 120 ctx.strokeStyle = plot.color; | |
| 121 ctx.beginPath(); | |
| 122 if (width < size) { | |
| 123 for (var i = 0; i < width; ++i) { | |
| 124 var xindex = i / (width - 1) * size; | |
| 125 var val = ydata[xindex]; | |
| 126 if (i == 0) { | |
| 127 ctx.moveTo(xorigin + i, yorigin + height - val / ymax * height); | |
| 128 } else { | |
| 129 ctx.lineTo(xorigin + i, yorigin + height - val / ymax * height); | |
| 130 } | |
| 131 } | |
| 132 } else { | |
| 133 for (var i = 0; i < size; ++i) { | |
| 134 var xpos = xorigin + i / (size - 1) * width; | |
| 135 var val = ydata[i]; | |
| 136 if (i == 0) { | |
| 137 ctx.moveTo(xpos, yorigin + height - val / ymax * height); | |
| 138 } else { | |
| 139 ctx.lineTo(xpos, yorigin + height - val / ymax * height); | |
| 140 } | |
| 141 } | |
| 142 } | |
| 143 } | |
| 144 ctx.stroke(); | |
| 145 } | |
| 146 | |
| 147 /** | |
| 148 * Display the battery charge vs time on a line graph. | |
| 149 * | |
| 150 * @param {Array.<Object>} power_supply_array An array of objects with fields | |
| 151 * representing the battery charge, time when the charge measurement was | |
| 152 * taken, and whether there was external power connected at that time. | |
| 153 */ | |
| 154 function showBatteryChargeData(power_supply_array) { | |
| 155 canvas = $('battery-charge-canvas'); | |
| 156 tdata = new Array(); | |
| 157 plot = new Array(); | |
| 158 for (var i in power_supply_array) { | |
| 159 tdata[i] = power_supply_array[i].time_past; | |
| 160 plot[i] = power_supply_array[i].battery_percent; | |
| 161 } | |
| 162 | |
| 163 plots = new Array(); | |
| 164 plots[0] = { | |
| 165 color: '#0000FF', // Blue color. | |
|
Daniel Erat
2014/01/04 00:02:17
nit: indent two spaces instead of 4
Siva Chandra
2014/01/06 22:36:57
Done.
| |
| 166 data: plot | |
| 167 }; | |
| 168 plotLineGraph(canvas, tdata, plots, 0.00, 100.00, 3); | |
| 169 } | |
| 170 | |
| 171 function requestBatteryChargeData() { | |
| 172 chrome.send('requestBatteryChargeData'); | |
| 173 } | |
| 174 | |
| 175 var PowerUI = { | |
| 176 showBatteryChargeData: showBatteryChargeData | |
| 177 }; | |
| 178 | |
| 179 document.addEventListener('DOMContentLoaded', function() { | |
| 180 requestBatteryChargeData(); | |
| 181 $('battery-charge-reload-button').onclick = requestBatteryChargeData; | |
| 182 }); | |
| OLD | NEW |