Skip to content

Commit 2dc90bb

Browse files
committed
Add a function to draw a legend
1 parent 69171b7 commit 2dc90bb

File tree

1 file changed

+72
-41
lines changed

1 file changed

+72
-41
lines changed

src/cargo/core/compiler/timings.js

Lines changed: 72 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,61 @@ function render_pipeline_graph() {
197197
ctx.restore();
198198
}
199199

200+
// Draw a legend at the current position of the ctx.
201+
// entries should be an array of objects with the following scheme:
202+
// {
203+
// "name": <name of the legend entry> [string],
204+
// "color": <color of the legend entry> [string],
205+
// "line": <should the entry be a thin line or a rectangle> [bool]
206+
// }
207+
function draw_legend(ctx, width, entries) {
208+
const entry_height = 20;
209+
const height = entries.length * entry_height + 2; // Add a bit of margin to the bottom
210+
211+
// Draw background
212+
ctx.fillStyle = BG_COLOR;
213+
ctx.strokeStyle = TEXT_COLOR;
214+
ctx.lineWidth = 1;
215+
ctx.textBaseline = 'middle';
216+
ctx.textAlign = 'start';
217+
ctx.beginPath();
218+
ctx.rect(0, 0, width, height);
219+
ctx.stroke();
220+
ctx.fill();
221+
222+
ctx.lineWidth = 2;
223+
224+
// Dimension of a block
225+
const block_height = 15;
226+
const block_width = 30;
227+
228+
// Margin from the left edge
229+
const x_start = 5;
230+
// Width of the "mark" section (line/block)
231+
const mark_width = 45;
232+
233+
// Draw legend entries
234+
let y = 10;
235+
for (const entry of entries) {
236+
ctx.beginPath();
237+
238+
if (entry.line) {
239+
ctx.strokeStyle = entry.color;
240+
ctx.moveTo(x_start, y);
241+
ctx.lineTo(x_start + mark_width, y);
242+
ctx.stroke();
243+
} else {
244+
ctx.fillStyle = entry.color;
245+
ctx.fillRect(x_start + (mark_width - block_width) / 2, y - (block_height / 2), block_width, block_height);
246+
}
247+
248+
ctx.fillStyle = TEXT_COLOR;
249+
ctx.fillText(entry.name, x_start + mark_width + 4, y + 1);
250+
251+
y += entry_height;
252+
}
253+
}
254+
200255
// Determine the color of a section block based on the section name.
201256
function get_section_color(name) {
202257
if (name === "codegen") {
@@ -325,47 +380,23 @@ function render_timing_graph() {
325380
ctx.restore();
326381
ctx.save();
327382
ctx.translate(canvas_width-200, MARGIN);
328-
// background
329-
ctx.fillStyle = BG_COLOR;
330-
ctx.strokeStyle = TEXT_COLOR;
331-
ctx.lineWidth = 1;
332-
ctx.textBaseline = 'middle'
333-
ctx.textAlign = 'start';
334-
ctx.beginPath();
335-
ctx.rect(0, 0, 150, 82);
336-
ctx.stroke();
337-
ctx.fill();
338-
339-
ctx.fillStyle = TEXT_COLOR;
340-
ctx.beginPath();
341-
ctx.lineWidth = 2;
342-
ctx.strokeStyle = 'red';
343-
ctx.moveTo(5, 10);
344-
ctx.lineTo(50, 10);
345-
ctx.stroke();
346-
ctx.fillText('Waiting', 54, 11);
347-
348-
ctx.beginPath();
349-
ctx.strokeStyle = 'blue';
350-
ctx.moveTo(5, 30);
351-
ctx.lineTo(50, 30);
352-
ctx.stroke();
353-
ctx.fillText('Inactive', 54, 31);
354-
355-
ctx.beginPath();
356-
ctx.strokeStyle = 'green';
357-
ctx.moveTo(5, 50);
358-
ctx.lineTo(50, 50);
359-
ctx.stroke();
360-
ctx.fillText('Active', 54, 51);
361-
362-
ctx.beginPath();
363-
ctx.fillStyle = cpuFillStyle
364-
ctx.fillRect(15, 60, 30, 15);
365-
ctx.fill();
366-
ctx.fillStyle = TEXT_COLOR;
367-
ctx.fillText('CPU Usage', 54, 71);
368-
383+
draw_legend(ctx, 150, [{
384+
name: "Waiting",
385+
color: "red",
386+
line: true
387+
}, {
388+
name: "Inactive",
389+
color: "blue",
390+
line: true
391+
}, {
392+
name: "Active",
393+
color: "green",
394+
line: true
395+
}, {
396+
name: "CPU Usage",
397+
color: cpuFillStyle,
398+
line: false
399+
}]);
369400
ctx.restore();
370401
}
371402

0 commit comments

Comments
 (0)