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 function getTimeString(timePast) { | |
Daniel Erat
2014/01/06 23:02:41
timePast -> secPast
Siva Chandra
2014/01/07 01:19:19
Done.
| |
7 var d = new Date(); | |
8 d.setSeconds(d.getSeconds() - timePast); | |
9 return d.toLocaleTimeString(); | |
10 } | |
11 | |
12 /** | |
13 * Plot a line graph of data versus time on a HTML canvas element. | |
14 * | |
15 * @param {DOMElement} canvas The canvas on which the line graph is drawn. | |
arv (Not doing code reviews)
2014/01/06 23:29:57
Invalid type. Do you mean {Element} or maybe more
Siva Chandra
2014/01/07 01:19:19
Done.
| |
16 * @param {Array.integer} tdata The time (in seconds) in the past when the | |
arv (Not doing code reviews)
2014/01/06 23:29:57
Array.<number>
JS only has one number type
Siva Chandra
2014/01/07 01:19:19
Done.
| |
17 * corresponding data in plots was sampled. | |
18 * @param {Array.<Object>} plots An array of objects with fields 'data' and | |
19 * 'color'. The field 'data' is an array of samples to be plotted as a | |
20 * line graph with 'color'. The elements in the 'data' array are ordered | |
21 * corresponding to their sampling time in the argument 'tdata'. Also, | |
22 * the number of elements in the data array should be the same as in the | |
23 * time array 'tdata' above. | |
24 * @param {number} ymin Minimum bound of y-axis | |
25 * @param {number} ymax Maximum bound of y-axis. | |
26 * @param {integer} yprecision An integer value representing the number of | |
27 * digits of precision the y-axis data should be printed with. | |
28 */ | |
29 function plotLineGraph(canvas, tdata, plots, ymin, ymax, yprecision) { | |
arv (Not doing code reviews)
2014/01/06 23:29:57
tdata -> tData or maybe timeData?
ymax -> yMax
.
Siva Chandra
2014/01/07 01:19:19
Done.
| |
30 var textFont = '12px Arial'; | |
31 var textHeight = 12; | |
32 var padding = 5; // Pixels | |
33 var gridColor = '#CCC'; | |
arv (Not doing code reviews)
2014/01/06 23:29:57
#ccc
Siva Chandra
2014/01/07 01:19:19
Done.
| |
34 var ctx = canvas.getContext('2d'); | |
35 var size = tdata.length; | |
36 | |
37 function printErrorText(text) { | |
38 ctx.clearRect(0, 0, canvas.scrollWidth, canvas.scrollHeight); | |
arv (Not doing code reviews)
2014/01/06 23:29:57
canvas.width and height is more idiomatic
Siva Chandra
2014/01/07 01:19:19
Done.
| |
39 ctx.font = textFont; | |
40 ctx.fillText(text, 15, 15); | |
Daniel Erat
2014/01/06 23:02:41
move these "15" magic numbers into an "errorOffset
Siva Chandra
2014/01/07 01:19:19
Done.
| |
41 } | |
42 | |
43 if (size < 2) { | |
44 printErrorText(loadTimeData.getString('notEnoughDataAvailableYet')); | |
45 return; | |
46 } | |
47 | |
48 for (var count in plots) { | |
arv (Not doing code reviews)
2014/01/06 23:29:57
Don't use for-in loops over arrays. Stick to plain
Siva Chandra
2014/01/07 01:19:19
Done.
| |
49 if (plots[count].data.length != size) { | |
50 printErrorText(loadTimeData.getString('timeAndPlotDataMismatch')); | |
51 return; | |
52 } | |
53 } | |
54 | |
55 function valueToString(value) { | |
arv (Not doing code reviews)
2014/01/06 23:29:57
i18n?
Why not use Intl for all your number to str
Siva Chandra
2014/01/07 01:19:19
How to i18n-ize numbers from within a js chrome re
arv (Not doing code reviews)
2014/01/07 15:23:54
Intl is part of JavaScript and it provides locale
Siva Chandra
2014/01/07 22:05:16
I am tempted to say i18n for numbers does not matt
| |
56 return Number(value).toPrecision(yprecision); | |
57 } | |
58 | |
59 function getTextWidth(text) { | |
60 ctx.font = textFont; | |
61 return ctx.measureText(text).width + 2; | |
Daniel Erat
2014/01/06 23:02:41
why +2?
(i.e. please add a comment)
Siva Chandra
2014/01/07 01:19:19
Done.
| |
62 } | |
63 | |
64 function drawText(text, x, y) { | |
Daniel Erat
2014/01/06 23:02:41
move this up and make printErrorText() use it
Siva Chandra
2014/01/07 01:19:19
Done.
| |
65 ctx.font = textFont; | |
66 ctx.fillStyle = '#000'; | |
67 ctx.fillText(text, x, y); | |
68 } | |
69 | |
70 function drawHighlightText(text, x, y, color) { | |
71 ctx.strokeStyle = '#000'; | |
72 ctx.strokeRect(x, y - textHeight, getTextWidth(text), textHeight); | |
73 ctx.fillStyle = color; | |
74 ctx.fillRect(x, y - textHeight, getTextWidth(text), textHeight); | |
75 ctx.fillStyle = '#FFF'; | |
76 ctx.fillText(text, x, y); | |
77 } | |
78 | |
79 function drawLine(x1, y1, x2, y2, color) { | |
80 var colorBackup = ctx.strokeStyle; | |
arv (Not doing code reviews)
2014/01/06 23:29:57
ctx.save()
...
ctx.restore()
Siva Chandra
2014/01/07 01:19:19
Done.
| |
81 ctx.beginPath(); | |
82 ctx.moveTo(x1, y1); | |
83 ctx.lineTo(x2, y2); | |
84 ctx.strokeStyle = color; | |
85 ctx.stroke(); | |
86 ctx.strokeStyle = colorBackup; | |
87 } | |
88 | |
89 // The strokeRect method of the 2d context of a canvas draws a bounding | |
90 // rectangle with an offset origin and greater dimensions. Hence, use this | |
91 // function to draw a rect at the desired location with desired dimensions. | |
92 function drawRect(x, y, width, height, color) { | |
arv (Not doing code reviews)
2014/01/06 23:29:57
Is there a reason why this (and other functions) a
Siva Chandra
2014/01/07 01:19:19
I am viewing this as a pseudo plotting object whic
| |
93 drawLine(x, y, x + width - 1, y, color); | |
94 drawLine(x, y, x, y + height - 1, color); | |
95 drawLine(x, y + height - 1, x + width - 1, y + height - 1, color); | |
96 drawLine(x + width - 1, y, x + width - 1, y + height - 1, color); | |
97 } | |
98 | |
99 var yminStr = valueToString(ymin); | |
100 var ymaxStr = valueToString(ymax); | |
101 var yhalfStr = valueToString((ymax + ymin) / 2); | |
102 var yminWidth = getTextWidth(yminStr); | |
103 var ymaxWidth = getTextWidth(ymaxStr); | |
104 var yhalfWidth = getTextWidth(yhalfStr); | |
105 | |
106 var xminStr = tdata[0]; | |
107 var xmaxStr = tdata[size - 1]; | |
108 var xminWidth = getTextWidth(xminStr); | |
109 var xmaxWidth = getTextWidth(xmaxStr); | |
110 | |
111 var xorigin = padding + Math.max(yminWidth, ymaxWidth, xminWidth / 2); | |
112 var yorigin = padding + textHeight; | |
113 var width = canvas.scrollWidth - xorigin - xmaxWidth / 2 - padding; | |
114 if (width < size) { | |
115 canvas.width += canvas.width + size - width; | |
116 width = size; | |
117 } | |
118 var height = canvas.scrollHeight - yorigin - textHeight - padding; | |
119 | |
120 function drawPlots() { | |
121 // Start fresh. | |
122 ctx.clearRect(0, 0, canvas.scrollWidth, canvas.scrollHeight); | |
123 | |
124 // Draw the bounding rectangle. | |
125 drawRect(xorigin, yorigin, width, height, gridColor); | |
126 | |
127 // Draw the x and y bound values. | |
128 drawText(ymaxStr, xorigin - ymaxWidth, yorigin + textHeight); | |
129 drawText(yminStr, xorigin - yminWidth, yorigin + height); | |
130 drawText(xminStr, xorigin - xminWidth / 2, yorigin + height + textHeight); | |
131 drawText(xmaxStr, | |
132 xorigin + width - xmaxWidth / 2, | |
133 yorigin + height + textHeight); | |
134 | |
135 // Draw y-level (horizontal) lines. | |
136 drawLine(xorigin, yorigin + height / 4, | |
137 xorigin + width, yorigin + height / 4, | |
138 gridColor); | |
139 drawLine(xorigin, yorigin + height / 2, | |
140 xorigin + width, yorigin + height / 2, gridColor); | |
141 drawLine(xorigin, yorigin + 3 * height / 4, | |
142 xorigin + width, yorigin + 3 * height / 4, | |
143 gridColor); | |
144 | |
145 // Draw half-level value. | |
146 drawText(yhalfStr, | |
147 xorigin - yhalfWidth, | |
148 yorigin + height / 2 + textHeight / 2); | |
149 | |
150 // Draw the plots. | |
151 var yvalRange = ymax - ymin; | |
152 for (var count in plots) { | |
153 var plot = plots[count]; | |
154 var ydata = plot.data; | |
155 ctx.strokeStyle = plot.color; | |
156 ctx.beginPath(); | |
157 for (var i = 0; i < size; ++i) { | |
arv (Not doing code reviews)
2014/01/06 23:29:57
prefer i++ for consistency
Siva Chandra
2014/01/07 01:19:19
Done.
| |
158 var xpos = xorigin + Math.floor(i / (size - 1) * (width - 1)); | |
159 var val = ydata[i]; | |
160 var ypos = yorigin + height - 1 - | |
161 Math.round((val - ymin) / yvalRange * (height - 1)); | |
162 if (i == 0) { | |
163 ctx.moveTo(xpos, ypos); | |
164 } else { | |
165 ctx.lineTo(xpos, ypos); | |
166 } | |
167 } | |
168 ctx.stroke(); | |
169 } | |
170 } | |
171 | |
172 function drawTimeGuide(tdataIndex) { | |
173 var x = xorigin + tdataIndex / (size - 1) * (width - 1); | |
174 drawLine(x, yorigin, x, yorigin + height - 1, '#000'); | |
175 drawText(tdata[tdataIndex], | |
176 x - getTextWidth(tdata[tdataIndex]) / 2, | |
177 yorigin - 2); | |
178 | |
179 for (var count in plots) { | |
180 var ydata = plots[count].data; | |
181 | |
182 // Draw small black square on the plot where the time guide intersects | |
183 // it. | |
184 var val = ydata[tdataIndex]; | |
185 var ypos = yorigin + height - 1 - | |
186 Math.round((val - ymin) / (ymax - ymin) * (height - 1)); | |
187 ctx.fillStyle = '#000'; | |
188 ctx.fillRect(x - 2, ypos - 2, 4, 4); | |
189 | |
190 // Draw the val to right of the intersection. | |
191 var valStr = valueToString(val); | |
192 var yloc; | |
193 if (ypos - textHeight / 2 < yorigin) { | |
194 yloc = yorigin + textHeight; | |
195 } else if (ypos + textHeight / 2 >= ypos + height) { | |
196 yloc = yorigin + height - 1; | |
197 } else { | |
198 yloc = ypos + textHeight / 2; | |
199 } | |
200 drawHighlightText( | |
201 valStr, x + 5, ypos + textHeight / 2, plots[count].color); | |
202 } | |
203 } | |
204 | |
205 function onMouseOverOrMove(event) { | |
206 drawPlots(); | |
207 | |
208 var x = event.clientX - canvas.offsetLeft + canvas.parentNode.scrollLeft; | |
arv (Not doing code reviews)
2014/01/06 23:29:57
Maybe use canvas.getBoundingClientRect() instead?
Siva Chandra
2014/01/07 01:19:19
Done.
| |
209 var y = event.clientY - canvas.offsetTop + canvas.parentNode.scrollTop; | |
210 if (x < xorigin || x >= xorigin + width || | |
211 y < yorigin || y >= yorigin + height) { | |
212 return; | |
213 } | |
214 | |
215 if (width == size) { | |
216 drawTimeGuide(x - xorigin); | |
217 } else { | |
218 drawTimeGuide(Math.round((x - xorigin) / (width - 1) * (size - 1))); | |
219 } | |
220 } | |
221 | |
222 function onMouseOut(event) { | |
Daniel Erat
2014/01/06 23:02:41
use canvas.addEventListener('mouseout', function(e
Siva Chandra
2014/01/07 01:19:19
Done.
| |
223 drawPlots(); | |
224 } | |
225 | |
226 drawPlots(); | |
227 canvas.onmouseover = onMouseOverOrMove; | |
arv (Not doing code reviews)
2014/01/06 23:29:57
Do you need touch events too?
Siva Chandra
2014/01/07 01:19:19
Single touch events are already captured is it not
| |
228 canvas.onmousemove = onMouseOverOrMove; | |
Daniel Erat
2014/01/06 23:02:41
use addEventListener for these too
Siva Chandra
2014/01/07 01:19:19
Done.
| |
229 canvas.onmouseout = onMouseOut; | |
230 } | |
231 | |
232 /** | |
233 * Display the battery charge vs time on a line graph. | |
234 * | |
235 * @param {Array.<Object>} power_supply_array An array of objects with fields | |
236 * representing the battery charge, time when the charge measurement was | |
237 * taken, and whether there was external power connected at that time. | |
238 */ | |
239 function showBatteryChargeData(power_supply_array) { | |
Daniel Erat
2014/01/06 23:02:41
powerSupplyArray
arv (Not doing code reviews)
2014/01/06 23:29:57
no underscores in js
Siva Chandra
2014/01/07 01:19:19
Done.
Siva Chandra
2014/01/07 01:19:19
Done.
| |
240 canvas = $('battery-charge-canvas'); | |
241 tdata = new Array(); | |
arv (Not doing code reviews)
2014/01/06 23:29:57
tdata = [];
Siva Chandra
2014/01/07 01:19:19
Done.
| |
242 plot = new Array(); | |
243 for (var i in power_supply_array) { | |
244 tdata[i] = getTimeString(power_supply_array[i].seconds_past); | |
245 plot[i] = power_supply_array[i].battery_percent; | |
246 } | |
247 | |
248 plots = new Array(); | |
arv (Not doing code reviews)
2014/01/06 23:29:57
plots = [
{
color: ...
}
];
Siva Chandra
2014/01/07 01:19:19
Done.
| |
249 plots[0] = { | |
250 color: '#0000FF', | |
251 data: plot | |
252 }; | |
253 plotLineGraph(canvas, tdata, plots, 0.00, 100.00, 3); | |
254 } | |
255 | |
256 function requestBatteryChargeData() { | |
257 chrome.send('requestBatteryChargeData'); | |
258 } | |
259 | |
260 var PowerUI = { | |
arv (Not doing code reviews)
2014/01/06 23:29:57
Only classes/constructors should be capitalized.
Siva Chandra
2014/01/07 01:19:19
Done.
| |
261 showBatteryChargeData: showBatteryChargeData | |
262 }; | |
263 | |
264 document.addEventListener('DOMContentLoaded', function() { | |
265 requestBatteryChargeData(); | |
266 $('battery-charge-reload-button').onclick = requestBatteryChargeData; | |
267 }); | |
OLD | NEW |