-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_money_graph.js
91 lines (64 loc) · 1.95 KB
/
async_money_graph.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
var surfing_user = document.getElementById("user_surfing").getAttribute("value")
let username;
let spinner;
if (surfing_user !== "AnonymousUser") {
username = document.getElementsByClassName("profile_username")[0].textContent
spinner = document.getElementsByClassName("spinner")[0]
if (document.getElementById('earnings-button') !== null){
const money_graphSocket = new WebSocket(
'ws://' + '0.0.0.0:8000' +
'/ws/money_earned/'
);
money_graphSocket.onopen = function(e) {
console.log("money_graph WebSocket connection established!");
const earningsButton = document.getElementById('earnings-button');
const chartWrapper = document.getElementById('chart-wrapper');
let ctx = document.getElementById("myChart").getContext("2d");
let chartData = {
labels: [],
datasets: [
{
label: `${username}'s Earnings`,
backgroundColor: "#79AEC8",
borderColor: "#417690",
data: []
}
]
};
let chartOptions = {
responsive: true,
title: {
text: "Gross Volume in 2020",
display: true
}
};
let chart = new Chart(ctx, {
type: "line",
data: chartData,
options: chartOptions
});
earningsButton.addEventListener('click', function() {
spinner.style.display = "block"
if (earningsButton.textContent !== "Hide Earnings") {
money_graphSocket.send(JSON.stringify({
"message": "get_earnings_report",
}));
money_graphSocket.onmessage = function(e) {
var data = JSON.parse(e.data)
spinner.style.display = "None"
// Update the chart data and options with the fetched data.
chartData.labels = data.message.labels;
chartData.datasets[0].data = data.message.data;
chart.update();
chartWrapper.style.display = 'flex';
earningsButton.textContent = "Hide Earnings"
}
} else {
chartWrapper.style.display = '';
earningsButton.textContent = "Your Earnings"
spinner.style.display = "None"
}
});
}
}
}