-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
186 lines (166 loc) · 7.14 KB
/
index.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
185
186
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Статистика подписчиков Веб-стандартов</title>
</head>
<body>
<canvas id="canvas" width="400" height="200"></canvas>
<button id="absolute" type="button">Абсолютное число</button>
<button id="relative" type="button">Относительное число</button>
<button id="month" type="button">Прирост по месяцам</button>
<script src="./node_modules/chart.js/dist/Chart.bundle.js"></script>
<script>
(async () => {
const data = await fetch('./data.json').then(response => response.json());
const labels = ['vk', 'twitter', 'telegram', 'facebook'];
const colors = {
twitter: 'rgb(0, 191, 255, 0.5)',
facebook: 'rgb(72, 61, 139, 0.5)',
vk: 'rgb(127, 255, 212, 0.5)',
telegram: 'rgb(255, 99, 71, 0.5)',
};
function generateDataset(data) {
return labels.map(label => {
return {
label: label,
borderColor: colors[label],
backgroundColor: colors[label],
pointStyle: 'rectRot',
pointRadius: 7,
pointBorderColor: 'rgb(0, 0, 0)',
data: data.map(item => item[label]),
};
});
}
function getLabels(data) {
return data.map(item => item.date);
}
function absoluteProcessor(data) {
return data;
}
function relativeProcessor(data) {
return data.map((item, i) => {
const result = {};
labels.forEach(label => {
result[label] = item[label] - data[i > 0 ? i - 1 : i][label];
});
result.date = item.date;
return result;
});
}
function monthProcessor(data) {
const months = {};
data.forEach(item => {
const monthLabel = item.date.slice(0, 7);
if (!months[monthLabel]) {
months[monthLabel] = {};
labels.forEach(label => {
months[monthLabel][label] = 0;
});
}
labels.forEach(label => {
months[monthLabel][label] = item[label];
});
});
const result = [];
for (month in months) {
result.push({
...months[month],
date: month,
});
}
return relativeProcessor(result);
}
const processed = relativeProcessor(data);
const config = {
type: 'line',
data: {
labels: processed.map(item => item.date),
datasets: generateDataset(processed),
},
options: {
responsive: true,
legend: {
labels: {
usePointStyle: true,
},
},
title: {
display: true,
text: 'Статистика по группам в соц. сетях',
},
tooltips: {
mode: 'index',
intersect: false,
callbacks: {
footer: function (tooltipItems, data) {
var sum = 0;
tooltipItems.forEach(function (tooltipItem) {
sum +=
data.datasets[tooltipItem.datasetIndex].data[
tooltipItem.index
];
});
return 'Sum: ' + sum;
},
},
footerFontStyle: 'normal',
},
hover: {
mode: 'index',
intersect: false,
},
scales: {
xAxes: [
{
type: 'time',
time: {
unit: 'week',
},
scaleLabel: {
display: true,
labelString: 'Дни',
},
},
],
yAxes: [
{
stacked: true,
scaleLabel: {
display: true,
labelString: 'Подписчиков',
},
},
],
},
},
};
const ctx = document.getElementById('canvas').getContext('2d');
window.wstChart = new Chart(ctx, config);
[
{
selector: '#relative',
processor: relativeProcessor,
},
{
selector: '#absolute',
processor: absoluteProcessor,
},
{
selector: '#month',
processor: monthProcessor,
},
].forEach(button => {
document.querySelector(button.selector).addEventListener('click', e => {
const processed = button.processor(data);
window.wstChart.data.datasets = generateDataset(processed);
window.wstChart.data.labels = getLabels(processed);
window.wstChart.update();
});
});
})();
</script>
</body>
</html>