blob: ad4fab89c5bc1e5d727eb28006dd1b55fac6a0b0 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2013 The Chromium Authors
[email protected]03bf84a2013-03-23 22:11:482// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rbpotter5796b002021-03-10 18:49:215// Maximum number of labels placed vertically along the sides of the graph.
Philipp Hancke0635d3142021-03-16 20:02:096const MAX_VERTICAL_LABELS = 6;
rbpotter5796b002021-03-10 18:49:217
8// Vertical spacing between labels and between the graph and labels.
Philipp Hancke0635d3142021-03-16 20:02:099const LABEL_VERTICAL_SPACING = 4;
rbpotter5796b002021-03-10 18:49:2110// Horizontal spacing between vertically placed labels and the edges of the
11// graph.
Philipp Hancke0635d3142021-03-16 20:02:0912const LABEL_HORIZONTAL_SPACING = 3;
rbpotter5796b002021-03-10 18:49:2113// Horizintal spacing between two horitonally placed labels along the bottom
14// of the graph.
Philipp Hancke0635d3142021-03-16 20:02:0915const LABEL_LABEL_HORIZONTAL_SPACING = 25;
rbpotter5796b002021-03-10 18:49:2116
17// Length of ticks, in pixels, next to y-axis labels. The x-axis only has
18// one set of labels, so it can use lines instead.
Philipp Hancke0635d3142021-03-16 20:02:0919const Y_AXIS_TICK_LENGTH = 10;
rbpotter5796b002021-03-10 18:49:2120
Philipp Hancke0635d3142021-03-16 20:02:0921const GRID_COLOR = '#CCC';
22const TEXT_COLOR = '#000';
23const BACKGROUND_COLOR = '#FFF';
rbpotter5796b002021-03-10 18:49:2124
Philipp Hancke0635d3142021-03-16 20:02:0925const MAX_DECIMAL_PRECISION = 3;
rbpotter5796b002021-03-10 18:49:2126
[email protected]03bf84a2013-03-23 22:11:4827/**
28 * A TimelineGraphView displays a timeline graph on a canvas element.
29 */
rbpotter5796b002021-03-10 18:49:2130export class TimelineGraphView {
31 constructor(divId, canvasId) {
[email protected]03bf84a2013-03-23 22:11:4832 this.scrollbar_ = {position_: 0, range_: 0};
33
Rebekah Potter8148f852022-11-29 18:04:3734 // Disable getElementById restriction here, since |divId| and |canvasId| are
35 // not always valid selectors.
36 // eslint-disable-next-line no-restricted-properties
37 this.graphDiv_ = document.getElementById(divId);
38 // eslint-disable-next-line no-restricted-properties
39 this.canvas_ = document.getElementById(canvasId);
[email protected]03bf84a2013-03-23 22:11:4840
41 // Set the range and scale of the graph. Times are in milliseconds since
42 // the Unix epoch.
43
44 // All measurements we have must be after this time.
45 this.startTime_ = 0;
46 // The current rightmost position of the graph is always at most this.
47 this.endTime_ = 1;
48
49 this.graph_ = null;
50
[email protected]ca001942014-02-18 18:10:5451 // Horizontal scale factor, in terms of milliseconds per pixel.
52 this.scale_ = 1000;
53
[email protected]03bf84a2013-03-23 22:11:4854 // Initialize the scrollbar.
55 this.updateScrollbarRange_(true);
56 }
57
rbpotter5796b002021-03-10 18:49:2158 setScale(scale) {
59 this.scale_ = scale;
60 }
[email protected]ca001942014-02-18 18:10:5461
rbpotter5796b002021-03-10 18:49:2162 // Returns the total length of the graph, in pixels.
63 getLength_() {
Philipp Hancke0635d3142021-03-16 20:02:0964 const timeRange = this.endTime_ - this.startTime_;
rbpotter5796b002021-03-10 18:49:2165 // Math.floor is used to ignore the last partial area, of length less
66 // than this.scale_.
67 return Math.floor(timeRange / this.scale_);
68 }
rbpotterd93b3e82021-03-02 03:49:1369
70 /**
rbpotter5796b002021-03-10 18:49:2171 * Returns true if the graph is scrolled all the way to the right.
rbpotterd93b3e82021-03-02 03:49:1372 */
rbpotter5796b002021-03-10 18:49:2173 graphScrolledToRightEdge_() {
74 return this.scrollbar_.position_ === this.scrollbar_.range_;
75 }
Evan Shrubsoleb9464e52021-03-03 18:01:1076
rbpotter5796b002021-03-10 18:49:2177 /**
78 * Update the range of the scrollbar. If |resetPosition| is true, also
79 * sets the slider to point at the rightmost position and triggers a
80 * repaint.
81 */
82 updateScrollbarRange_(resetPosition) {
Philipp Hancke0635d3142021-03-16 20:02:0983 let scrollbarRange = this.getLength_() - this.canvas_.width;
rbpotter5796b002021-03-10 18:49:2184 if (scrollbarRange < 0) {
85 scrollbarRange = 0;
rbpotterd93b3e82021-03-02 03:49:1386 }
rbpotterd93b3e82021-03-02 03:49:1387
rbpotter5796b002021-03-10 18:49:2188 // If we've decreased the range to less than the current scroll position,
89 // we need to move the scroll position.
90 if (this.scrollbar_.position_ > scrollbarRange) {
91 resetPosition = true;
Evan Shrubsoleb9464e52021-03-03 18:01:1092 }
rbpotterd93b3e82021-03-02 03:49:1393
rbpotter5796b002021-03-10 18:49:2194 this.scrollbar_.range_ = scrollbarRange;
95 if (resetPosition) {
96 this.scrollbar_.position_ = scrollbarRange;
97 this.repaint();
98 }
99 }
Evan Shrubsoleb9464e52021-03-03 18:01:10100
rbpotter5796b002021-03-10 18:49:21101 /**
102 * Sets the date range displayed on the graph, switches to the default
103 * scale factor, and moves the scrollbar all the way to the right.
104 */
105 setDateRange(startDate, endDate) {
106 this.startTime_ = startDate.getTime();
107 this.endTime_ = endDate.getTime();
Evan Shrubsoleb9464e52021-03-03 18:01:10108
rbpotter5796b002021-03-10 18:49:21109 // Safety check.
110 if (this.endTime_ <= this.startTime_) {
111 this.startTime_ = this.endTime_ - 1;
112 }
Evan Shrubsoleb9464e52021-03-03 18:01:10113
rbpotter5796b002021-03-10 18:49:21114 this.updateScrollbarRange_(true);
115 }
Evan Shrubsoleb9464e52021-03-03 18:01:10116
rbpotter5796b002021-03-10 18:49:21117 /**
118 * Updates the end time at the right of the graph to be the current time.
119 * Specifically, updates the scrollbar's range, and if the scrollbar is
120 * all the way to the right, keeps it all the way to the right. Otherwise,
121 * leaves the view as-is and doesn't redraw anything.
122 */
123 updateEndDate(opt_date) {
124 this.endTime_ = opt_date || (new Date()).getTime();
125 this.updateScrollbarRange_(this.graphScrolledToRightEdge_());
126 }
Evan Shrubsoleb9464e52021-03-03 18:01:10127
rbpotter5796b002021-03-10 18:49:21128 getStartDate() {
129 return new Date(this.startTime_);
130 }
Evan Shrubsoleb9464e52021-03-03 18:01:10131
rbpotter5796b002021-03-10 18:49:21132 /**
133 * Replaces the current TimelineDataSeries with |dataSeries|.
134 */
135 setDataSeries(dataSeries) {
136 // Simply recreates the Graph.
137 this.graph_ = new Graph();
Philipp Hancke0635d3142021-03-16 20:02:09138 for (let i = 0; i < dataSeries.length; ++i) {
rbpotter5796b002021-03-10 18:49:21139 this.graph_.addDataSeries(dataSeries[i]);
140 }
141 this.repaint();
142 }
Evan Shrubsoleb9464e52021-03-03 18:01:10143
rbpotter5796b002021-03-10 18:49:21144 /**
145 * Adds |dataSeries| to the current graph.
146 */
147 addDataSeries(dataSeries) {
148 if (!this.graph_) {
149 this.graph_ = new Graph();
150 }
151 this.graph_.addDataSeries(dataSeries);
152 this.repaint();
153 }
Evan Shrubsoleb9464e52021-03-03 18:01:10154
rbpotter5796b002021-03-10 18:49:21155 /**
156 * Draws the graph on |canvas_| when visible.
157 */
158 repaint() {
159 if (this.canvas_.offsetParent === null) {
160 return; // do not repaint graphs that are not visible.
161 }
Evan Shrubsoleb9464e52021-03-03 18:01:10162
rbpotter5796b002021-03-10 18:49:21163 this.repaintTimerRunning_ = false;
Evan Shrubsoleb9464e52021-03-03 18:01:10164
Philipp Hancke0635d3142021-03-16 20:02:09165 const width = this.canvas_.width;
166 let height = this.canvas_.height;
167 const context = this.canvas_.getContext('2d');
Evan Shrubsoleb9464e52021-03-03 18:01:10168
rbpotter5796b002021-03-10 18:49:21169 // Clear the canvas.
170 context.fillStyle = BACKGROUND_COLOR;
171 context.fillRect(0, 0, width, height);
Evan Shrubsoleb9464e52021-03-03 18:01:10172
rbpotter5796b002021-03-10 18:49:21173 // Try to get font height in pixels. Needed for layout.
Philipp Hancke0635d3142021-03-16 20:02:09174 const fontHeightString = context.font.match(/([0-9]+)px/)[1];
175 const fontHeight = parseInt(fontHeightString);
Evan Shrubsoleb9464e52021-03-03 18:01:10176
rbpotter5796b002021-03-10 18:49:21177 // Safety check, to avoid drawing anything too ugly.
178 if (fontHeightString.length === 0 || fontHeight <= 0 ||
179 fontHeight * 4 > height || width < 50) {
180 return;
181 }
Evan Shrubsoleb9464e52021-03-03 18:01:10182
rbpotter5796b002021-03-10 18:49:21183 // Save current transformation matrix so we can restore it later.
184 context.save();
Evan Shrubsoleb9464e52021-03-03 18:01:10185
rbpotter5796b002021-03-10 18:49:21186 // The center of an HTML canvas pixel is technically at (0.5, 0.5). This
187 // makes near straight lines look bad, due to anti-aliasing. This
188 // translation reduces the problem a little.
189 context.translate(0.5, 0.5);
Evan Shrubsoleb9464e52021-03-03 18:01:10190
rbpotter5796b002021-03-10 18:49:21191 // Figure out what time values to display.
Philipp Hancke0635d3142021-03-16 20:02:09192 let position = this.scrollbar_.position_;
rbpotter5796b002021-03-10 18:49:21193 // If the entire time range is being displayed, align the right edge of
194 // the graph to the end of the time range.
195 if (this.scrollbar_.range_ === 0) {
196 position = this.getLength_() - this.canvas_.width;
197 }
Philipp Hancke0635d3142021-03-16 20:02:09198 const visibleStartTime = this.startTime_ + position * this.scale_;
Evan Shrubsoleb9464e52021-03-03 18:01:10199
rbpotter5796b002021-03-10 18:49:21200 // Make space at the bottom of the graph for the time labels, and then
201 // draw the labels.
Philipp Hancke0635d3142021-03-16 20:02:09202 const textHeight = height;
rbpotter5796b002021-03-10 18:49:21203 height -= fontHeight + LABEL_VERTICAL_SPACING;
204 this.drawTimeLabels(context, width, height, textHeight, visibleStartTime);
Evan Shrubsoleb9464e52021-03-03 18:01:10205
rbpotter5796b002021-03-10 18:49:21206 // Draw outline of the main graph area.
207 context.strokeStyle = GRID_COLOR;
208 context.strokeRect(0, 0, width - 1, height - 1);
Evan Shrubsoleb9464e52021-03-03 18:01:10209
rbpotter5796b002021-03-10 18:49:21210 if (this.graph_) {
211 // Layout graph and have them draw their tick marks.
212 this.graph_.layout(
213 width, height, fontHeight, visibleStartTime, this.scale_);
214 this.graph_.drawTicks(context);
Evan Shrubsoleb9464e52021-03-03 18:01:10215
rbpotter5796b002021-03-10 18:49:21216 // Draw the lines of all graphs, and then draw their labels.
217 this.graph_.drawLines(context);
218 this.graph_.drawLabels(context);
219 }
Evan Shrubsoleb9464e52021-03-03 18:01:10220
rbpotter5796b002021-03-10 18:49:21221 // Restore original transformation matrix.
222 context.restore();
223 }
Evan Shrubsoleb9464e52021-03-03 18:01:10224
rbpotter5796b002021-03-10 18:49:21225 /**
226 * Draw time labels below the graph. Takes in start time as an argument
227 * since it may not be |startTime_|, when we're displaying the entire
228 * time range.
229 */
230 drawTimeLabels(context, width, height, textHeight, startTime) {
231 // Draw the labels 1 minute apart.
Philipp Hancke0635d3142021-03-16 20:02:09232 const timeStep = 1000 * 60;
Evan Shrubsoleb9464e52021-03-03 18:01:10233
rbpotter5796b002021-03-10 18:49:21234 // Find the time for the first label. This time is a perfect multiple of
235 // timeStep because of how UTC times work.
Philipp Hancke0635d3142021-03-16 20:02:09236 let time = Math.ceil(startTime / timeStep) * timeStep;
Evan Shrubsoleb9464e52021-03-03 18:01:10237
rbpotter5796b002021-03-10 18:49:21238 context.textBaseline = 'bottom';
239 context.textAlign = 'center';
240 context.fillStyle = TEXT_COLOR;
241 context.strokeStyle = GRID_COLOR;
Evan Shrubsoleb9464e52021-03-03 18:01:10242
rbpotter5796b002021-03-10 18:49:21243 // Draw labels and vertical grid lines.
244 while (true) {
Philipp Hancke0635d3142021-03-16 20:02:09245 const x = Math.round((time - startTime) / this.scale_);
rbpotter5796b002021-03-10 18:49:21246 if (x >= width) {
247 break;
248 }
Philipp Hancke0635d3142021-03-16 20:02:09249 const text = (new Date(time)).toLocaleTimeString();
rbpotter5796b002021-03-10 18:49:21250 context.fillText(text, x, textHeight);
251 context.beginPath();
252 context.lineTo(x, 0);
253 context.lineTo(x, height);
254 context.stroke();
255 time += timeStep;
256 }
257 }
Evan Shrubsoleb9464e52021-03-03 18:01:10258
rbpotter5796b002021-03-10 18:49:21259 getDataSeriesCount() {
260 if (this.graph_) {
261 return this.graph_.dataSeries_.length;
262 }
263 return 0;
264 }
265
266 hasDataSeries(dataSeries) {
267 if (this.graph_) {
268 return this.graph_.hasDataSeries(dataSeries);
269 }
270 return false;
271 }
272}
273
274/**
275 * A Label is the label at a particular position along the y-axis.
276 */
277class Label {
278 constructor(height, text) {
279 this.height = height;
280 this.text = text;
281 }
282}
283
284/**
285 * A Graph is responsible for drawing all the TimelineDataSeries that have
286 * the same data type. Graphs are responsible for scaling the values, laying
287 * out labels, and drawing both labels and lines for its data series.
288 */
289class Graph {
290 constructor() {
291 this.dataSeries_ = [];
292
293 // Cached properties of the graph, set in layout.
294 this.width_ = 0;
295 this.height_ = 0;
296 this.fontHeight_ = 0;
297 this.startTime_ = 0;
298 this.scale_ = 0;
299
300 // The lowest/highest values adjusted by the vertical label step size
301 // in the displayed range of the graph. Used for scaling and setting
302 // labels. Set in layoutLabels.
303 this.min_ = 0;
304 this.max_ = 0;
305
306 // Cached text of equally spaced labels. Set in layoutLabels.
307 this.labels_ = [];
308 }
309
310 addDataSeries(dataSeries) {
311 this.dataSeries_.push(dataSeries);
312 }
313
314 hasDataSeries(dataSeries) {
Philipp Hancke0635d3142021-03-16 20:02:09315 for (let i = 0; i < this.dataSeries_.length; ++i) {
rbpotter5796b002021-03-10 18:49:21316 if (this.dataSeries_[i] === dataSeries) {
317 return true;
318 }
319 }
320 return false;
321 }
322
323 /**
324 * Returns a list of all the values that should be displayed for a given
325 * data series, using the current graph layout.
326 */
327 getValues(dataSeries) {
328 if (!dataSeries.isVisible()) {
329 return null;
330 }
331 return dataSeries.getValues(this.startTime_, this.scale_, this.width_);
332 }
333
334 /**
335 * Updates the graph's layout. In particular, both the max value and
336 * label positions are updated. Must be called before calling any of the
337 * drawing functions.
338 */
339 layout(width, height, fontHeight, startTime, scale) {
340 this.width_ = width;
341 this.height_ = height;
342 this.fontHeight_ = fontHeight;
343 this.startTime_ = startTime;
344 this.scale_ = scale;
345
346 // Find largest value.
Philipp Hancke0635d3142021-03-16 20:02:09347 let max = 0;
348 let min = 0;
349 for (let i = 0; i < this.dataSeries_.length; ++i) {
350 const values = this.getValues(this.dataSeries_[i]);
rbpotter5796b002021-03-10 18:49:21351 if (!values) {
352 continue;
353 }
Philipp Hancke0635d3142021-03-16 20:02:09354 for (let j = 0; j < values.length; ++j) {
rbpotter5796b002021-03-10 18:49:21355 if (values[j] > max) {
356 max = values[j];
357 } else if (values[j] < min) {
358 min = values[j];
Dan Beambdd7d822019-01-05 00:59:42359 }
[email protected]03bf84a2013-03-23 22:11:48360 }
rbpotter5796b002021-03-10 18:49:21361 }
[email protected]03bf84a2013-03-23 22:11:48362
rbpotter5796b002021-03-10 18:49:21363 this.layoutLabels_(min, max);
364 }
[email protected]03bf84a2013-03-23 22:11:48365
rbpotter5796b002021-03-10 18:49:21366 /**
367 * Lays out labels and sets |max_|/|min_|, taking the time units into
368 * consideration. |maxValue| is the actual maximum value, and
369 * |max_| will be set to the value of the largest label, which
370 * will be at least |maxValue|. Similar for |min_|.
371 */
372 layoutLabels_(minValue, maxValue) {
373 if (maxValue - minValue < 1024) {
374 this.layoutLabelsBasic_(minValue, maxValue, MAX_DECIMAL_PRECISION);
375 return;
376 }
377
378 // Find appropriate units to use.
Philipp Hancke0635d3142021-03-16 20:02:09379 const units = ['', 'k', 'M', 'G', 'T', 'P'];
rbpotter5796b002021-03-10 18:49:21380 // Units to use for labels. 0 is '1', 1 is K, etc.
381 // We start with 1, and work our way up.
Philipp Hancke0635d3142021-03-16 20:02:09382 let unit = 1;
rbpotter5796b002021-03-10 18:49:21383 minValue /= 1024;
384 maxValue /= 1024;
385 while (units[unit + 1] && maxValue - minValue >= 1024) {
386 minValue /= 1024;
387 maxValue /= 1024;
388 ++unit;
389 }
390
391 // Calculate labels.
392 this.layoutLabelsBasic_(minValue, maxValue, MAX_DECIMAL_PRECISION);
393
394 // Append units to labels.
Philipp Hancke0635d3142021-03-16 20:02:09395 for (let i = 0; i < this.labels_.length; ++i) {
rbpotter5796b002021-03-10 18:49:21396 this.labels_[i] += ' ' + units[unit];
397 }
398
399 // Convert |min_|/|max_| back to unit '1'.
400 this.min_ *= Math.pow(1024, unit);
401 this.max_ *= Math.pow(1024, unit);
402 }
403
404 /**
405 * Same as layoutLabels_, but ignores units. |maxDecimalDigits| is the
406 * maximum number of decimal digits allowed. The minimum allowed
407 * difference between two adjacent labels is 10^-|maxDecimalDigits|.
408 */
409 layoutLabelsBasic_(minValue, maxValue, maxDecimalDigits) {
410 this.labels_ = [];
Philipp Hancke0635d3142021-03-16 20:02:09411 const range = maxValue - minValue;
rbpotter5796b002021-03-10 18:49:21412 // No labels if the range is 0.
413 if (range === 0) {
414 this.min_ = this.max_ = maxValue;
415 return;
416 }
417
418 // The maximum number of equally spaced labels allowed. |fontHeight_|
419 // is doubled because the top two labels are both drawn in the same
420 // gap.
Philipp Hancke0635d3142021-03-16 20:02:09421 const minLabelSpacing = 2 * this.fontHeight_ + LABEL_VERTICAL_SPACING;
rbpotter5796b002021-03-10 18:49:21422
423 // The + 1 is for the top label.
Philipp Hancke0635d3142021-03-16 20:02:09424 let maxLabels = 1 + this.height_ / minLabelSpacing;
rbpotter5796b002021-03-10 18:49:21425 if (maxLabels < 2) {
426 maxLabels = 2;
427 } else if (maxLabels > MAX_VERTICAL_LABELS) {
428 maxLabels = MAX_VERTICAL_LABELS;
429 }
430
431 // Initial try for step size between consecutive labels.
Philipp Hancke0635d3142021-03-16 20:02:09432 let stepSize = Math.pow(10, -maxDecimalDigits);
rbpotter5796b002021-03-10 18:49:21433 // Number of digits to the right of the decimal of |stepSize|.
434 // Used for formatting label strings.
Philipp Hancke0635d3142021-03-16 20:02:09435 let stepSizeDecimalDigits = maxDecimalDigits;
rbpotter5796b002021-03-10 18:49:21436
437 // Pick a reasonable step size.
438 while (true) {
439 // If we use a step size of |stepSize| between labels, we'll need:
440 //
441 // Math.ceil(range / stepSize) + 1
442 //
443 // labels. The + 1 is because we need labels at both at 0 and at
444 // the top of the graph.
445
446 // Check if we can use steps of size |stepSize|.
447 if (Math.ceil(range / stepSize) + 1 <= maxLabels) {
448 break;
449 }
450 // Check |stepSize| * 2.
451 if (Math.ceil(range / (stepSize * 2)) + 1 <= maxLabels) {
452 stepSize *= 2;
453 break;
454 }
455 // Check |stepSize| * 5.
456 if (Math.ceil(range / (stepSize * 5)) + 1 <= maxLabels) {
457 stepSize *= 5;
458 break;
459 }
460 stepSize *= 10;
461 if (stepSizeDecimalDigits > 0) {
462 --stepSizeDecimalDigits;
463 }
464 }
465
466 // Set the min/max so it's an exact multiple of the chosen step size.
467 this.max_ = Math.ceil(maxValue / stepSize) * stepSize;
468 this.min_ = Math.floor(minValue / stepSize) * stepSize;
469
470 // Create labels.
Philipp Hancke0635d3142021-03-16 20:02:09471 for (let label = this.max_; label >= this.min_; label -= stepSize) {
rbpotter5796b002021-03-10 18:49:21472 this.labels_.push(label.toFixed(stepSizeDecimalDigits));
473 }
474 }
475
476 /**
477 * Draws tick marks for each of the labels in |labels_|.
478 */
479 drawTicks(context) {
Philipp Hancke0635d3142021-03-16 20:02:09480 const x1 = this.width_ - 1;
481 const x2 = this.width_ - 1 - Y_AXIS_TICK_LENGTH;
rbpotter5796b002021-03-10 18:49:21482
483 context.fillStyle = GRID_COLOR;
484 context.beginPath();
Philipp Hancke0635d3142021-03-16 20:02:09485 for (let i = 1; i < this.labels_.length - 1; ++i) {
rbpotter5796b002021-03-10 18:49:21486 // The rounding is needed to avoid ugly 2-pixel wide anti-aliased
487 // lines.
Philipp Hancke0635d3142021-03-16 20:02:09488 const y = Math.round(this.height_ * i / (this.labels_.length - 1));
rbpotter5796b002021-03-10 18:49:21489 context.moveTo(x1, y);
490 context.lineTo(x2, y);
491 }
492 context.stroke();
493 }
494
495 /**
496 * Draws a graph line for each of the data series.
497 */
498 drawLines(context) {
499 // Factor by which to scale all values to convert them to a number from
500 // 0 to height - 1.
Philipp Hancke0635d3142021-03-16 20:02:09501 let scale = 0;
502 const bottom = this.height_ - 1;
rbpotter5796b002021-03-10 18:49:21503 if (this.max_) {
504 scale = bottom / (this.max_ - this.min_);
505 }
506
507 // Draw in reverse order, so earlier data series are drawn on top of
508 // subsequent ones.
Philipp Hancke0635d3142021-03-16 20:02:09509 for (let i = this.dataSeries_.length - 1; i >= 0; --i) {
510 const values = this.getValues(this.dataSeries_[i]);
rbpotter5796b002021-03-10 18:49:21511 if (!values) {
512 continue;
513 }
514 context.strokeStyle = this.dataSeries_[i].getColor();
515 context.beginPath();
Philipp Hancke0635d3142021-03-16 20:02:09516 for (let x = 0; x < values.length; ++x) {
rbpotter5796b002021-03-10 18:49:21517 // The rounding is needed to avoid ugly 2-pixel wide anti-aliased
518 // horizontal lines.
519 context.lineTo(x, bottom - Math.round((values[x] - this.min_) * scale));
520 }
521 context.stroke();
522 }
523 }
524
525 /**
526 * Draw labels in |labels_|.
527 */
528 drawLabels(context) {
529 if (this.labels_.length === 0) {
530 return;
531 }
Philipp Hancke0635d3142021-03-16 20:02:09532 const x = this.width_ - LABEL_HORIZONTAL_SPACING;
rbpotter5796b002021-03-10 18:49:21533
534 // Set up the context.
535 context.fillStyle = TEXT_COLOR;
536 context.textAlign = 'right';
537
538 // Draw top label, which is the only one that appears below its tick
539 // mark.
540 context.textBaseline = 'top';
541 context.fillText(this.labels_[0], x, 0);
542
543 // Draw all the other labels.
544 context.textBaseline = 'bottom';
Philipp Hancke0635d3142021-03-16 20:02:09545 const step = (this.height_ - 1) / (this.labels_.length - 1);
546 for (let i = 1; i < this.labels_.length; ++i) {
rbpotter5796b002021-03-10 18:49:21547 context.fillText(this.labels_[i], x, step * i);
548 }
549 }
550}