Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | 03bf84a | 2013-03-23 22:11:48 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 5 | // Maximum number of labels placed vertically along the sides of the graph. |
Philipp Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 6 | const MAX_VERTICAL_LABELS = 6; |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 7 | |
| 8 | // Vertical spacing between labels and between the graph and labels. |
Philipp Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 9 | const LABEL_VERTICAL_SPACING = 4; |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 10 | // Horizontal spacing between vertically placed labels and the edges of the |
| 11 | // graph. |
Philipp Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 12 | const LABEL_HORIZONTAL_SPACING = 3; |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 13 | // Horizintal spacing between two horitonally placed labels along the bottom |
| 14 | // of the graph. |
Philipp Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 15 | const LABEL_LABEL_HORIZONTAL_SPACING = 25; |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 16 | |
| 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 Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 19 | const Y_AXIS_TICK_LENGTH = 10; |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 20 | |
Philipp Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 21 | const GRID_COLOR = '#CCC'; |
| 22 | const TEXT_COLOR = '#000'; |
| 23 | const BACKGROUND_COLOR = '#FFF'; |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 24 | |
Philipp Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 25 | const MAX_DECIMAL_PRECISION = 3; |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 26 | |
[email protected] | 03bf84a | 2013-03-23 22:11:48 | [diff] [blame] | 27 | /** |
| 28 | * A TimelineGraphView displays a timeline graph on a canvas element. |
| 29 | */ |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 30 | export class TimelineGraphView { |
| 31 | constructor(divId, canvasId) { |
[email protected] | 03bf84a | 2013-03-23 22:11:48 | [diff] [blame] | 32 | this.scrollbar_ = {position_: 0, range_: 0}; |
| 33 | |
Rebekah Potter | 8148f85 | 2022-11-29 18:04:37 | [diff] [blame] | 34 | // 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] | 03bf84a | 2013-03-23 22:11:48 | [diff] [blame] | 40 | |
| 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] | ca00194 | 2014-02-18 18:10:54 | [diff] [blame] | 51 | // Horizontal scale factor, in terms of milliseconds per pixel. |
| 52 | this.scale_ = 1000; |
| 53 | |
[email protected] | 03bf84a | 2013-03-23 22:11:48 | [diff] [blame] | 54 | // Initialize the scrollbar. |
| 55 | this.updateScrollbarRange_(true); |
| 56 | } |
| 57 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 58 | setScale(scale) { |
| 59 | this.scale_ = scale; |
| 60 | } |
[email protected] | ca00194 | 2014-02-18 18:10:54 | [diff] [blame] | 61 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 62 | // Returns the total length of the graph, in pixels. |
| 63 | getLength_() { |
Philipp Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 64 | const timeRange = this.endTime_ - this.startTime_; |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 65 | // 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 | } |
rbpotter | d93b3e8 | 2021-03-02 03:49:13 | [diff] [blame] | 69 | |
| 70 | /** |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 71 | * Returns true if the graph is scrolled all the way to the right. |
rbpotter | d93b3e8 | 2021-03-02 03:49:13 | [diff] [blame] | 72 | */ |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 73 | graphScrolledToRightEdge_() { |
| 74 | return this.scrollbar_.position_ === this.scrollbar_.range_; |
| 75 | } |
Evan Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 76 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 77 | /** |
| 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 Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 83 | let scrollbarRange = this.getLength_() - this.canvas_.width; |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 84 | if (scrollbarRange < 0) { |
| 85 | scrollbarRange = 0; |
rbpotter | d93b3e8 | 2021-03-02 03:49:13 | [diff] [blame] | 86 | } |
rbpotter | d93b3e8 | 2021-03-02 03:49:13 | [diff] [blame] | 87 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 88 | // 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 Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 92 | } |
rbpotter | d93b3e8 | 2021-03-02 03:49:13 | [diff] [blame] | 93 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 94 | this.scrollbar_.range_ = scrollbarRange; |
| 95 | if (resetPosition) { |
| 96 | this.scrollbar_.position_ = scrollbarRange; |
| 97 | this.repaint(); |
| 98 | } |
| 99 | } |
Evan Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 100 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 101 | /** |
| 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 Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 108 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 109 | // Safety check. |
| 110 | if (this.endTime_ <= this.startTime_) { |
| 111 | this.startTime_ = this.endTime_ - 1; |
| 112 | } |
Evan Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 113 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 114 | this.updateScrollbarRange_(true); |
| 115 | } |
Evan Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 116 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 117 | /** |
| 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 Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 127 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 128 | getStartDate() { |
| 129 | return new Date(this.startTime_); |
| 130 | } |
Evan Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 131 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 132 | /** |
| 133 | * Replaces the current TimelineDataSeries with |dataSeries|. |
| 134 | */ |
| 135 | setDataSeries(dataSeries) { |
| 136 | // Simply recreates the Graph. |
| 137 | this.graph_ = new Graph(); |
Philipp Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 138 | for (let i = 0; i < dataSeries.length; ++i) { |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 139 | this.graph_.addDataSeries(dataSeries[i]); |
| 140 | } |
| 141 | this.repaint(); |
| 142 | } |
Evan Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 143 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 144 | /** |
| 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 Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 154 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 155 | /** |
| 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 Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 162 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 163 | this.repaintTimerRunning_ = false; |
Evan Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 164 | |
Philipp Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 165 | const width = this.canvas_.width; |
| 166 | let height = this.canvas_.height; |
| 167 | const context = this.canvas_.getContext('2d'); |
Evan Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 168 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 169 | // Clear the canvas. |
| 170 | context.fillStyle = BACKGROUND_COLOR; |
| 171 | context.fillRect(0, 0, width, height); |
Evan Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 172 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 173 | // Try to get font height in pixels. Needed for layout. |
Philipp Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 174 | const fontHeightString = context.font.match(/([0-9]+)px/)[1]; |
| 175 | const fontHeight = parseInt(fontHeightString); |
Evan Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 176 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 177 | // 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 Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 182 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 183 | // Save current transformation matrix so we can restore it later. |
| 184 | context.save(); |
Evan Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 185 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 186 | // 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 Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 190 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 191 | // Figure out what time values to display. |
Philipp Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 192 | let position = this.scrollbar_.position_; |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 193 | // 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 Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 198 | const visibleStartTime = this.startTime_ + position * this.scale_; |
Evan Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 199 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 200 | // Make space at the bottom of the graph for the time labels, and then |
| 201 | // draw the labels. |
Philipp Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 202 | const textHeight = height; |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 203 | height -= fontHeight + LABEL_VERTICAL_SPACING; |
| 204 | this.drawTimeLabels(context, width, height, textHeight, visibleStartTime); |
Evan Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 205 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 206 | // Draw outline of the main graph area. |
| 207 | context.strokeStyle = GRID_COLOR; |
| 208 | context.strokeRect(0, 0, width - 1, height - 1); |
Evan Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 209 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 210 | 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 Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 215 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 216 | // Draw the lines of all graphs, and then draw their labels. |
| 217 | this.graph_.drawLines(context); |
| 218 | this.graph_.drawLabels(context); |
| 219 | } |
Evan Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 220 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 221 | // Restore original transformation matrix. |
| 222 | context.restore(); |
| 223 | } |
Evan Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 224 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 225 | /** |
| 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 Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 232 | const timeStep = 1000 * 60; |
Evan Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 233 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 234 | // Find the time for the first label. This time is a perfect multiple of |
| 235 | // timeStep because of how UTC times work. |
Philipp Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 236 | let time = Math.ceil(startTime / timeStep) * timeStep; |
Evan Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 237 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 238 | context.textBaseline = 'bottom'; |
| 239 | context.textAlign = 'center'; |
| 240 | context.fillStyle = TEXT_COLOR; |
| 241 | context.strokeStyle = GRID_COLOR; |
Evan Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 242 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 243 | // Draw labels and vertical grid lines. |
| 244 | while (true) { |
Philipp Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 245 | const x = Math.round((time - startTime) / this.scale_); |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 246 | if (x >= width) { |
| 247 | break; |
| 248 | } |
Philipp Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 249 | const text = (new Date(time)).toLocaleTimeString(); |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 250 | 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 Shrubsole | b9464e5 | 2021-03-03 18:01:10 | [diff] [blame] | 258 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 259 | 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 | */ |
| 277 | class 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 | */ |
| 289 | class 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 Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 315 | for (let i = 0; i < this.dataSeries_.length; ++i) { |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 316 | 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 Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 347 | 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]); |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 351 | if (!values) { |
| 352 | continue; |
| 353 | } |
Philipp Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 354 | for (let j = 0; j < values.length; ++j) { |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 355 | if (values[j] > max) { |
| 356 | max = values[j]; |
| 357 | } else if (values[j] < min) { |
| 358 | min = values[j]; |
Dan Beam | bdd7d82 | 2019-01-05 00:59:42 | [diff] [blame] | 359 | } |
[email protected] | 03bf84a | 2013-03-23 22:11:48 | [diff] [blame] | 360 | } |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 361 | } |
[email protected] | 03bf84a | 2013-03-23 22:11:48 | [diff] [blame] | 362 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 363 | this.layoutLabels_(min, max); |
| 364 | } |
[email protected] | 03bf84a | 2013-03-23 22:11:48 | [diff] [blame] | 365 | |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 366 | /** |
| 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 Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 379 | const units = ['', 'k', 'M', 'G', 'T', 'P']; |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 380 | // Units to use for labels. 0 is '1', 1 is K, etc. |
| 381 | // We start with 1, and work our way up. |
Philipp Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 382 | let unit = 1; |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 383 | 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 Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 395 | for (let i = 0; i < this.labels_.length; ++i) { |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 396 | 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 Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 411 | const range = maxValue - minValue; |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 412 | // 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 Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 421 | const minLabelSpacing = 2 * this.fontHeight_ + LABEL_VERTICAL_SPACING; |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 422 | |
| 423 | // The + 1 is for the top label. |
Philipp Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 424 | let maxLabels = 1 + this.height_ / minLabelSpacing; |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 425 | 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 Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 432 | let stepSize = Math.pow(10, -maxDecimalDigits); |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 433 | // Number of digits to the right of the decimal of |stepSize|. |
| 434 | // Used for formatting label strings. |
Philipp Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 435 | let stepSizeDecimalDigits = maxDecimalDigits; |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 436 | |
| 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 Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 471 | for (let label = this.max_; label >= this.min_; label -= stepSize) { |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 472 | 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 Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 480 | const x1 = this.width_ - 1; |
| 481 | const x2 = this.width_ - 1 - Y_AXIS_TICK_LENGTH; |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 482 | |
| 483 | context.fillStyle = GRID_COLOR; |
| 484 | context.beginPath(); |
Philipp Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 485 | for (let i = 1; i < this.labels_.length - 1; ++i) { |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 486 | // The rounding is needed to avoid ugly 2-pixel wide anti-aliased |
| 487 | // lines. |
Philipp Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 488 | const y = Math.round(this.height_ * i / (this.labels_.length - 1)); |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 489 | 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 Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 501 | let scale = 0; |
| 502 | const bottom = this.height_ - 1; |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 503 | 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 Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 509 | for (let i = this.dataSeries_.length - 1; i >= 0; --i) { |
| 510 | const values = this.getValues(this.dataSeries_[i]); |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 511 | if (!values) { |
| 512 | continue; |
| 513 | } |
| 514 | context.strokeStyle = this.dataSeries_[i].getColor(); |
| 515 | context.beginPath(); |
Philipp Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 516 | for (let x = 0; x < values.length; ++x) { |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 517 | // 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 Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 532 | const x = this.width_ - LABEL_HORIZONTAL_SPACING; |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 533 | |
| 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 Hancke | 0635d314 | 2021-03-16 20:02:09 | [diff] [blame] | 545 | const step = (this.height_ - 1) / (this.labels_.length - 1); |
| 546 | for (let i = 1; i < this.labels_.length; ++i) { |
rbpotter | 5796b00 | 2021-03-10 18:49:21 | [diff] [blame] | 547 | context.fillText(this.labels_[i], x, step * i); |
| 548 | } |
| 549 | } |
| 550 | } |