-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
73 lines (62 loc) · 1.58 KB
/
app.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
// streaming-api/app.js
const APP_KEY = 'PUSHER_APP_KEY';
const APP_CLUSTER = 'PUSHER_APP_CLUSTER';
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: [],
datasets: [
{
label: '# of Tweets',
data: [],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 159, 64, 0.2)',
],
borderWidth: 1,
},
],
},
options: {
scales: {
yAxes: [
{
ticks: {
beginAtZero: true,
},
},
],
},
},
});
function updateChart(data) {
let iterationCount = 0;
for (const key in data) {
if (!myChart.data.labels.includes(key)) {
myChart.data.labels.push(key);
}
myChart.data.datasets.forEach(dataset => {
dataset.data[iterationCount] = data[key];
});
iterationCount++;
myChart.update();
}
}
axios
.get('http://localhost:1500/polls', {})
.then(res => {
updateChart(res.data);
})
.catch(err => {
console.log('Could not retrieve information from the backend');
console.error(err);
});
const pusher = new Pusher(APP_KEY, {
cluster: APP_CLUSTER,
});
const channel = pusher.subscribe('twitter-votes');
channel.bind('options', data => {
updateChart(data);
});