-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (85 loc) · 3.4 KB
/
index.js
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
// fetch Data from json file. It is alwasys good a approach to fetch data from file.
getData();
async function getData() {
const response = await fetch('./data.json');
console.log(response);
const data = await response.json();
console.log(data);
length = data.length;
console.log(length);
labels = [];
values = [];
for (i = 0; i < length; i++) {
labels.push(data[i].day);
values.push(data[i].amount);
}
// After fetching data, generate Chart
function generateChart() {
const info = {
labels: data.map((chart) => chart.day),
datasets: [
{
data: data.map((chart) => chart.amount),
// backgroundColor: "hsl(10, 79%, 65%)",
backgroundColor: color => {
console.log(color)
let colors= color.index === 2 ? ' rgb(118, 181, 188,1)': 'hsl(10, 79%, 65%,1)' ;
return colors;
},
borderRadius: 5,
borderSkipped: false,
hoverBackgroundColor: color => {
console.log(color)
let colors= color.index === 2 ? 'hsl(186, 34%, 60%,0.6)': 'hsl(10, 79%, 65%,0.6)' ;
return colors;
},
},
],
};
const titleTooltip = (e) => `$${e[0].formattedValue}`;
const labelTooltip = (e) => "";
const options = {
scales: {
y: {
ticks: {
display: false,
},
grid: {
display: false,
drawTicks: false,
drawBorder: false,
},
},
x: {
grid: {
display: false,
drawBorder: false,
},
},
},
plugins: {
legend: { display: false },
tooltip: {
yAlign: "bottom",
xAlign: "center",
titleAlign: 'center',
caretSize: 0,
cornerRadius: 3,
displayColors: false,
backgroundColor: 'hsl(25, 47%, 15%)',
callbacks: {
title: titleTooltip,
label: labelTooltip,
},
},
},
};
const config = {
type: "bar",
data: info,
options,
};
new Chart(document.getElementById("bar-chart"), config);
}
generateChart();
}