-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlineChart.js
More file actions
99 lines (82 loc) · 1.95 KB
/
lineChart.js
File metadata and controls
99 lines (82 loc) · 1.95 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
let canvas = document.querySelector('canvas');
canvas.width=1000;
canvas.height=500;
let xGrid = 10;
let yGrid = 10;
let cellSize = 10;
let ctx = canvas.getContext('2d');
let data = {
1910 : 1000,
1920 : 2700,
1930 : 500,
1940 : 2100,
1950 : 2100,
1960 : 3000,
1970 : 100,
1980 : 2500,
1990 : 1250,
2000 : 1120,
2010 : 1950,
2020 : 3100,
2030 : 3000,
2040 : 2400
}
const entries = Object.entries(data);
function drawGrids() {
ctx.beginPath();
while(xGrid < canvas.height) {
ctx.moveTo(0, xGrid);
ctx.lineTo(canvas.width, xGrid);
xGrid+=cellSize;
}
while(yGrid < canvas.width) {
ctx.moveTo(yGrid, 0);
ctx.lineTo(yGrid, canvas.height);
yGrid+=cellSize;
}
ctx.strokeStyle = "gray";
ctx.stroke();
}
function blocks(count) {
return count*cellSize;
}
function drawAxis() {
let xPlot = 5;
let yPlot = 40;
let years = 1900;
let stock = 0;
ctx.beginPath();
ctx.strokeStyle = "black";
ctx.moveTo(blocks(5), blocks(5));
ctx.lineTo(blocks(5), blocks(40));
ctx.lineTo(blocks(80), blocks(40));
for(let i = 1; i <= 15; i++) { // x축 label
ctx.strokeText(years, blocks(xPlot), blocks(41));
ctx.arc(blocks(xPlot), blocks(40), 2, 0, Math.PI*2, true);
xPlot += 5;
years += 10;
}
for(let i = 1; i <= 10; i++) { // y축 label
ctx.strokeText(stock, blocks(2), blocks(yPlot));
yPlot -= 5;
stock += 500;
}
ctx.stroke();
}
function drawChart() {
ctx.beginPath();
ctx.strokeStyle = "black";
ctx.moveTo(blocks(5), blocks(40));
var xPlot = 10;
for(const [years, stock] of entries) {
var stockInBlocks = stock/100; // 현재 블록 하나가 100이므로. 일단 임시로.
ctx.strokeText(years, blocks(xPlot), blocks(40-stockInBlocks-1));
ctx.lineTo(blocks(xPlot), blocks(40-stockInBlocks));
ctx.arc(blocks(xPlot), blocks(40-stockInBlocks), 2, 0, Math.PI*2, true);
xPlot += 5;
}
ctx.stroke();
}
drawGrids();
drawAxis();
drawChart();