-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcanvas-chart.ts
85 lines (74 loc) · 1.55 KB
/
canvas-chart.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { mainloop, WindowCanvas } from "../ext/canvas.ts";
import {
Chart,
registerables,
} from "https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.esm.js";
// deno-lint-ignore ban-ts-comment
// @ts-ignore
Chart.register(...registerables);
const win = new WindowCanvas({
title: "Skia Canvas",
width: 800,
height: 600,
resizable: true,
});
win.window.setSizeLimits(400, 300, 1600, 1200);
const data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
label: "Looping tension",
data: [65, 59, 80, 81, 26, 55, 40],
fill: false,
borderColor: "rgb(75, 192, 192)",
}],
};
const options = {
type: "line",
data: data,
options: {
animations: {
tension: {
duration: 1000,
easing: "linear",
from: 1,
to: 0,
loop: true,
},
},
scales: {
y: {
min: 0,
max: 100,
},
},
},
plugins: [
{
id: "custom_canvas_background_color",
beforeDraw: (chart: Chart) => {
const { ctx } = chart;
ctx.save();
ctx.fillStyle = "white";
ctx.fillRect(0, 0, chart.width, chart.height);
ctx.restore();
},
},
],
};
let chart = new Chart(win.canvas, options);
win.onContextLoss = () => {
chart.destroy();
chart = new Chart(win.canvas, options);
};
addEventListener("click", () => {
console.log("click");
});
addEventListener("dblclick", () => {
console.log("dblclick");
});
win.onDraw = () => {
chart.render();
};
await mainloop(() => {
win.draw();
});