HTML と JavaScript (Glitch 経由) と Firebase を使用して単純なポーリング Web サイトを作成しています。チュートリアルに従ってそれを行いました。私はそれをすべて機能させ、投票は正常に機能し、意図したとおりに結果を表示します。結果を取得し、それらを使用して HTML ページにグラフを表示したいと考えています。これがどのように機能するかは理解していますが、投票結果変数をチャートの JS コードに取り込む方法は理解していません。私は charts.js を使用していますが、このコードは一番下にあります。y 値は総投票数を読み取る変数である必要がありますが、機能していません。助言がありますか?
ありがとう
var myStates = [];
var myTimes = [];
// Variables to hold the count for each state
var TrumpCount = 0;
var BidenCount = 0;
// Define database connection to correct child branch, MyTemperature
var myDBConn = firebase.database().ref("USvote");
// Function that acts when a 'new child is added to the DB' - i.e. new data is added this function runs.
myDBConn.on("child_added", function(data, prevChildKey) {
TrumpCount = 0;
BidenCount = 0;
// The data returned from the branch is put into a variable, dataPoint
var dataPoint = data.val();
// Populate the lists with the various data from the database
myStates.push(dataPoint.USvote);
myTimes.push(dataPoint.Time);
// add 1 to the appropriate counter
for (i = 0; i < myStates.length; i++) {
if (myStates[i] == "Trump") {
TrumpCount = TrumpCount + 1;
}
if (myStates[i] == "Biden") {
BidenCount = BidenCount + 1;
}
}
// Update the page elements with the results of each count
document.getElementById("TrumpCount").innerHTML = TrumpCount;
document.getElementById("BidenCount").innerHTML = BidenCount;
});
// JS code for using charts
JSC.Chart("chartDiv", {
type: "column",
series: [
{
points: [{ x: "Biden", y: BidenCount}, { x: "Trump", y: TrumpCount}]
}
]
});