-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharts.js
222 lines (197 loc) · 6.17 KB
/
charts.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// mpn65 qualitative palette
const COLORS = [
"#ff0029",
"#377eb8",
"#66a61e",
"#984ea3",
"#00d2d5",
"#ff7f00",
"#af8d00",
"#7f80cd",
"#b3e900",
"#c42e60",
"#a65628",
"#f781bf",
"#8dd3c7",
"#bebada",
"#fb8072",
"#80b1d3",
"#fdb462",
"#fccde5",
"#bc80bd",
"#ffed6f",
];
const FIRST_DATE = new Date(2020, 8, 1);
const LAST_DATE = Date.now();
function getDaysArray() {
let dates = [];
for (let dt = new Date(FIRST_DATE); dt <= LAST_DATE; dt.setDate(dt.getDate() + 1)) {
dates.push(new Date(dt));
}
return dates;
}
function generateLineGraphConfig(canvas) {
let data = $(canvas).data("data");
let labels = $(canvas).data("labels");
let is_general_chart = labels.includes("VVD");
let config = {
type: 'line',
data: {
labels: getDaysArray(),
datasets: []
},
options: {
animation: false,
datasets: {
line: {
pointRadius: 0,
}
},
scales: {
x: {
type: "time",
time: {
tooltipFormat: "YYYY-MM-DD",
},
min: FIRST_DATE,
max: LAST_DATE,
},
y: {
stacked: !is_general_chart,
ticks: {
callback: function (value, index, values) {
return canvas.id.includes("spending") ? "€" + value : value;
}
}
}
},
plugins: {
title: {
display: true,
text: $(canvas).data("title"),
font: {
size: 16,
},
},
legend: {
position: "bottom",
},
tooltip: {
mode: 'index',
intersect: false,
callbacks: {
label: function (context) {
let sum = context.chart.config._config.data.datasets.reduce(
(total, arg) => total + arg.data[context.dataIndex], 0
);
let percentage = (context.raw / sum * 100).toFixed(2);
let value = canvas.id.includes("spending") ? "€" + context.raw.toFixed(2) : context.raw;
return context.dataset.label + ": " + value + " (" + percentage + "%)";
}
}
},
zoom: {
zoom: {
drag:{
enabled: true,
},
mode: 'x',
},
},
}
}
}
labels.forEach(function (item, index) {
config.data.datasets.push(
{
label: item,
data: data[index],
backgroundColor: COLORS[index],
borderColor: COLORS[index],
borderWidth: 1,
fill: !is_general_chart,
});
});
return config
}
function generateBarChart(canvas) {
return {
type: 'bar',
data: {
labels: $(canvas).data("labels"),
datasets: [{
data: $(canvas).data("data"),
backgroundColor: COLORS,
maxBarThickness: 30,
}],
},
options: {
animation: false,
indexAxis: 'y',
aspectRatio: 1,
scales: {
x: {
ticks: {
callback: function (value, index, values) {
return canvas.id.includes("spending") ? "€" + value : value;
},
}
}
},
plugins: {
title: {
display: true,
text: $(canvas).data("title"),
font: {
size: 16
},
},
legend: {
display: false,
},
tooltip: {
mode: "y",
intersect: false,
callbacks: {
label: function (context) {
let sum = context.dataset.data.reduce((a, b) => a + b, 0);
let percentage = (context.raw / sum * 100).toFixed(2);
let value = canvas.id.includes("spending") ? "€" + context.raw.toFixed(2) : context.raw;
return value + " (" + percentage + "%)";
}
}
}
}
}
}
}
$(document).ready(function () {
$("canvas").each(function (index, canvas) {
if (canvas.id.includes("daily")) {
new Chart(canvas, generateLineGraphConfig(canvas));
} else {
new Chart(canvas, generateBarChart(canvas));
}
}
);
$(".update-charts").on("click", function (){
let divId = $(this).data("id");
let data = $(this).data("data");
let party = $(this).data("party");
let theme = $(this).data("theme")
$("#" + divId + " canvas").each(function (index, canvas){
let chart = Chart.getChart(canvas)
if(party){
chart.config.options.plugins.title.text = chart.config.options.plugins.title.text.replace(/ by \w+/, " by " + party);
}
if(theme){
chart.config.options.plugins.title.text = chart.config.options.plugins.title.text.replace(/ about [\w &]+? Distributed/, " about " + theme + " Distributed");
}
chart.config.data.datasets[0].data = data[index];
chart.update();
})
});
$(".first-button").each(function (){
$(this).click();
});
});