I am creating a simple polling website using HTML & JavaScript (through Glitch) and also Firebase, I followed a tutorial to do it. I have it all working and the voting works fine and displays the results as intended. I now want to take the results and use them to display a chart on my HTML page. I understand how this works, just not how to get the voting results variable into the JS code for the chart. I am using charts.js and the code for this is towards the bottom. The y values should be a variable reading the total votes, but it isn’t working. Any suggestions?
Thanks
JavaScript
x
48
48
1
var myStates = [];
2
var myTimes = [];
3
4
// Variables to hold the count for each state
5
var TrumpCount = 0;
6
var BidenCount = 0;
7
8
// Define database connection to correct child branch, MyTemperature
9
var myDBConn = firebase.database().ref("USvote");
10
11
// Function that acts when a 'new child is added to the DB' - i.e. new data is added this function runs.
12
myDBConn.on("child_added", function(data, prevChildKey) {
13
TrumpCount = 0;
14
BidenCount = 0;
15
16
// The data returned from the branch is put into a variable, dataPoint
17
var dataPoint = data.val();
18
19
// Populate the lists with the various data from the database
20
myStates.push(dataPoint.USvote);
21
myTimes.push(dataPoint.Time);
22
23
// add 1 to the appropriate counter
24
for (i = 0; i < myStates.length; i++) {
25
if (myStates[i] == "Trump") {
26
TrumpCount = TrumpCount + 1;
27
}
28
if (myStates[i] == "Biden") {
29
BidenCount = BidenCount + 1;
30
}
31
}
32
33
// Update the page elements with the results of each count
34
document.getElementById("TrumpCount").innerHTML = TrumpCount;
35
document.getElementById("BidenCount").innerHTML = BidenCount;
36
});
37
38
39
// JS code for using charts
40
JSC.Chart("chartDiv", {
41
type: "column",
42
series: [
43
{
44
points: [{ x: "Biden", y: BidenCount}, { x: "Trump", y: TrumpCount}]
45
}
46
]
47
});
48
Advertisement
Answer
Try placing the chart.js code only when firebase has loaded and performed its needed actions.
Try this:
JavaScript
1
48
48
1
var myStates = [];
2
var myTimes = [];
3
4
// Variables to hold the count for each state
5
var TrumpCount = 0;
6
var BidenCount = 0;
7
8
// Define database connection to correct child branch, MyTemperature
9
var myDBConn = firebase.database().ref("USvote");
10
11
// Function that acts when a 'new child is added to the DB' - i.e. new data is added this function runs.
12
myDBConn.on("child_added", function(data, prevChildKey) {
13
TrumpCount = 0;
14
BidenCount = 0;
15
16
// The data returned from the branch is put into a variable, dataPoint
17
var dataPoint = data.val();
18
19
// Populate the lists with the various data from the database
20
myStates.push(dataPoint.USvote);
21
myTimes.push(dataPoint.Time);
22
23
// add 1 to the appropriate counter
24
for (i = 0; i < myStates.length; i++) {
25
if (myStates[i] == "Trump") {
26
TrumpCount = TrumpCount + 1;
27
}
28
if (myStates[i] == "Biden") {
29
BidenCount = BidenCount + 1;
30
}
31
}
32
33
// Update the page elements with the results of each count
34
document.getElementById("TrumpCount").innerHTML = TrumpCount;
35
document.getElementById("BidenCount").innerHTML = BidenCount;
36
37
38
// JS code for using charts
39
JSC.Chart("chartDiv", {
40
type: "column",
41
series: [
42
{
43
points: [{ x: "Biden", y: BidenCount}, { x: "Trump", y: TrumpCount}]
44
}
45
]
46
});
47
});
48