-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbar-growth.html
184 lines (173 loc) · 5.26 KB
/
bar-growth.html
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Bar growth chart</title>
<style>
* {
margin: 0;
padding: 0;
font-family: sans-serif;
}
.chartCard {
width: 100vw;
height: calc(100vh - 40px);
background: rgba(75, 192, 192, 0.2);
display: flex;
align-items: center;
justify-content: center;
}
.chartBox {
width: 700px;
padding: 20px;
border-radius: 20px;
border: solid 3px rgba(75, 192, 192, 1);
background: white;
}
</style>
</head>
<body>
<div class="chartCard">
<div class="chartBox">
<canvas id="myChart"></canvas>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
const data = {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [
{
label: "Red Bar",
data: [
[0, 12],
[0, 19],
[0, 3],
[0, 5],
[0, 2],
[0, 3],
],
backgroundColor: "rgba(255, 99, 132, 0.2)",
borderColor: "rgba(255, 99, 132, 1)",
borderWidth: 1,
},
{
label: "Arrow Bar",
data: [
[12, 19],
[19, 3],
[3, 5],
[5, 2],
[2, 3],
],
backgroundColor: "rgba(0, 0, 0, 1)",
borderColor: "rgba(0, 0, 0, 1)",
barPercentage: 0.05,
},
],
};
const barGrowthIndicator = {
id: "barGrowthIndicator",
afterDatasetsDraw(chart, scales, options) {
const {
ctx,
scales: { x, y },
} = chart;
const deltaPercentage = [];
for (let i = 0; i < chart._metasets[0]._parsed.length - 1; i++) {
let z = 1 + i;
const basis = chart._metasets[0]._parsed[i].y;
const delta = chart._metasets[0]._parsed[z].y;
let percentage = (delta / basis) * 100;
percentage = percentage - 100;
deltaPercentage.push(percentage.toFixed(1));
}
console.log(chart._metasets[1].hidden);
if (chart._metasets[1].hidden !== true) {
for (let a = 0; a < deltaPercentage.length; a++) {
const start = chart._metasets[1]._parsed[a]._custom.start;
const end = chart._metasets[1]._parsed[a]._custom.end;
if (end >= start) {
ctx.beginPath();
ctx.moveTo(
chart.getDatasetMeta(1).data[a].x,
chart.getDatasetMeta(1).data[a].y - 2
);
ctx.lineTo(
chart.getDatasetMeta(1).data[a].x - 5,
chart.getDatasetMeta(1).data[a].y + 5
);
ctx.lineTo(
chart.getDatasetMeta(1).data[a].x + 5,
chart.getDatasetMeta(1).data[a].y + 5
);
ctx.fillStyle = "black";
ctx.fill();
ctx.restore();
ctx.font = "10px Arial";
ctx.fillStyle = "green";
ctx.textAlign = "center";
ctx.fillText(
deltaPercentage[a] + "%",
chart.getDatasetMeta(1).data[a].x + 2.5,
chart.getDatasetMeta(1).data[a].y - 10
);
ctx.restore();
}
if (end < start) {
let yStart = a + 1;
ctx.beginPath();
ctx.moveTo(
chart.getDatasetMeta(1).data[a].x,
chart.getDatasetMeta(0).data[yStart].y + 3
);
ctx.lineTo(
chart.getDatasetMeta(1).data[a].x - 5,
chart.getDatasetMeta(0).data[yStart].y - 5
);
ctx.lineTo(
chart.getDatasetMeta(1).data[a].x + 5,
chart.getDatasetMeta(0).data[yStart].y - 5
);
ctx.fillStyle = "black";
ctx.fill();
ctx.restore();
ctx.font = "10px Arial";
ctx.fillStyle = "red";
ctx.textAlign = "center";
ctx.fillText(
deltaPercentage[a] + "%",
chart.getDatasetMeta(1).data[a].x + 2.5,
chart.getDatasetMeta(0).data[yStart].y + 12
);
ctx.restore();
}
}
}
},
};
const config = {
type: "bar",
data: data,
options: {
plugins: {
tooltip: {
filter: (tooltipItem) => {
return tooltipItem.datasetIndex === 0;
},
},
},
scales: {
y: {
grace: "5%",
beginAtZero: true,
},
},
},
plugins: [barGrowthIndicator],
};
const myChart = new Chart(document.getElementById("myChart"), config);
</script>
</body>
</html>