Chromium Code Reviews| Index: chrome/browser/resources/chromeos/power.js |
| diff --git a/chrome/browser/resources/chromeos/power.js b/chrome/browser/resources/chromeos/power.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bcd6555193c809485540f063bc74f70594ef95df |
| --- /dev/null |
| +++ b/chrome/browser/resources/chromeos/power.js |
| @@ -0,0 +1,182 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +/** |
| + * Plot a line graph of data versus time on a HTML canvas element. |
| + * |
| + * @param {DOMElement} canvas The canvas on which the line graph is drawn. |
| + * @param {Array.Integer} tdata The time (in seconds) in the past when the |
| + * 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.
|
| + * @param {Array.<Object>} plots An array of objects with fields 'data' and |
| + * 'color'. The field 'data' is an array of samples to be plotted as a |
| + * 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.
|
| + * @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.
|
| + * to be shown on the y-axis. |
| + * @param {number} ymax A number representing the maximum of the values to |
| + * to be shown on the y-axis. |
| + * @param {integer} yprecision An integer value representing the number of |
| + * digits of precision the y-axis data should be printed with. |
| + */ |
| +function plotLineGraph(canvas, tdata, plots, ymin, ymax, yprecision) { |
| + var indexFont = '12px Arial'; |
| + var indexHeight = 12; |
| + var padding = 5; // Pixels |
| + 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.
|
| + var gridColor = '#CCCCCC'; // A lighter shade of grey |
| + var ctx = canvas.getContext('2d'); |
| + var size = tdata.length; |
| + |
| + if (size < 2) { |
| + ctx.clearRect(0, 0, canvas.scrollWidth, canvas.scrollHeight); |
| + ctx.font = indexFont; |
| + 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.
|
| + return; |
| + } |
| + |
| + 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.
|
| + var indexNum = Number(index); |
| + return indexNum.toPrecision(yprecision); |
| + } |
| + |
| + function getIndexWidth(index) { |
| + ctx.font = indexFont; |
| + return ctx.measureText(index).width + 2; |
| + } |
| + |
| + function drawIndex(index, x, y) { |
| + ctx.font = indexFont; |
| + ctx.fillStyle = '#000000'; // Black |
| + ctx.fillText(index, x, y); |
| + } |
| + |
| + function getTimeString(timePast) { |
| + var d = new Date(); |
| + d.setSeconds(-timePast); |
| + return d.toLocaleTimeString(); |
| + } |
| + |
| + function drawLine(x1, y1, x2, y2, color) { |
| + var colorBackup = ctx.strokeStyle; |
| + ctx.beginPath(); |
| + ctx.moveTo(x1, y1); |
| + ctx.lineTo(x2, y2); |
| + ctx.strokeStyle = color; |
| + ctx.stroke(); |
| + ctx.strokeStyle = colorBackup; |
| + } |
| + |
| + var yminStr = yDataToString(ymin); |
| + var ymaxStr = yDataToString(ymax); |
| + var yhalfStr = yDataToString((ymax + ymin) / 2); |
| + var yminWidth = getIndexWidth(yminStr); |
| + var ymaxWidth = getIndexWidth(ymaxStr); |
| + var yhalfWidth = getIndexWidth(yhalfStr); |
| + |
| + var xminStr = getTimeString(tdata[0]); |
| + var xmaxStr = getTimeString(tdata[size - 1]); |
| + var xminWidth = getIndexWidth(xminStr); |
| + var xmaxWidth = getIndexWidth(xmaxStr); |
| + |
| + var xorigin = padding + Math.max(yminWidth, ymaxWidth, xminWidth / 2); |
| + var yorigin = padding + indexHeight / 2; |
| + var width = canvas.scrollWidth - xorigin - xmaxWidth / 2 - padding; |
| + var height = canvas.scrollHeight - yorigin - indexHeight - padding; |
| + |
| + // Start fresh. |
| + ctx.clearRect(0, 0, canvas.scrollWidth, canvas.scrollHeight); |
| + |
| + // Draw the bounding rectangle. |
| + ctx.strokeStyle = rectColor; |
| + ctx.strokeRect(xorigin, yorigin, width, height); |
| + |
| + // Draw the x and y bound values. |
| + drawIndex(ymaxStr, xorigin - ymaxWidth, yorigin + indexHeight / 2); |
| + drawIndex(yminStr, xorigin - yminWidth, yorigin + height); |
| + drawIndex(xminStr, xorigin - xminWidth / 2, yorigin + height + indexHeight); |
| + drawIndex(xmaxStr, |
| + xorigin + width - xmaxWidth / 2, |
| + yorigin + height + indexHeight); |
| + |
| + // Draw y-level (horizontal) lines. |
| + drawLine(xorigin, yorigin + height / 4, |
| + xorigin + width, yorigin + height / 4, |
| + gridColor); |
| + drawLine(xorigin, yorigin + height / 2, |
| + xorigin + width, yorigin + height / 2, gridColor); |
| + drawLine(xorigin, yorigin + 3 * height / 4, |
| + xorigin + width, yorigin + 3 * height / 4, |
| + gridColor); |
| + |
| + // Draw half-level value. |
| + drawIndex(yhalfStr, |
| + xorigin - yhalfWidth, |
| + yorigin + height / 2 + indexHeight / 2); |
| + |
| + // Draw the plots. |
| + for (var count in plots) { |
| + var plot = plots[count]; |
| + var ydata = plot.data; |
| + ctx.strokeStyle = plot.color; |
| + ctx.beginPath(); |
| + if (width < size) { |
| + for (var i = 0; i < width; ++i) { |
| + var xindex = i / (width - 1) * size; |
| + var val = ydata[xindex]; |
| + if (i == 0) { |
| + ctx.moveTo(xorigin + i, yorigin + height - val / ymax * height); |
| + } else { |
| + ctx.lineTo(xorigin + i, yorigin + height - val / ymax * height); |
| + } |
| + } |
| + } else { |
| + for (var i = 0; i < size; ++i) { |
| + var xpos = xorigin + i / (size - 1) * width; |
| + var val = ydata[i]; |
| + if (i == 0) { |
| + ctx.moveTo(xpos, yorigin + height - val / ymax * height); |
| + } else { |
| + ctx.lineTo(xpos, yorigin + height - val / ymax * height); |
| + } |
| + } |
| + } |
| + } |
| + ctx.stroke(); |
| +} |
| + |
| +/** |
| + * Display the battery charge vs time on a line graph. |
| + * |
| + * @param {Array.<Object>} power_supply_array An array of objects with fields |
| + * representing the battery charge, time when the charge measurement was |
| + * taken, and whether there was external power connected at that time. |
| + */ |
| +function showBatteryChargeData(power_supply_array) { |
| + canvas = $('battery-charge-canvas'); |
| + tdata = new Array(); |
| + plot = new Array(); |
| + for (var i in power_supply_array) { |
| + tdata[i] = power_supply_array[i].time_past; |
| + plot[i] = power_supply_array[i].battery_percent; |
| + } |
| + |
| + plots = new Array(); |
| + plots[0] = { |
| + 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.
|
| + data: plot |
| + }; |
| + plotLineGraph(canvas, tdata, plots, 0.00, 100.00, 3); |
| +} |
| + |
| +function requestBatteryChargeData() { |
| + chrome.send('requestBatteryChargeData'); |
| +} |
| + |
| +var PowerUI = { |
| + showBatteryChargeData: showBatteryChargeData |
| +}; |
| + |
| +document.addEventListener('DOMContentLoaded', function() { |
| + requestBatteryChargeData(); |
| + $('battery-charge-reload-button').onclick = requestBatteryChargeData; |
| +}); |