I'm trying to use Firebase real time data to receive form data, then I want to read it to graph it. My problem is that when I graph the data, it comes along with the key, and my graph x-axis displays the key rather than the name. I've been at this for a day. How do I get the key to bugger off and have the graph display the fields that I want?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Attachment Style Data</title>
<!-- Firebase JavaScript SDK -->
<script type="module">
// Import the necessary functions from the Firebase JavaScript
SDK
import { initializeApp } from
"https://www.gstatic.com/firebasejs/10.9.0/firebase-app.js";
import { getAnalytics } from
"https://www.gstatic.com/firebasejs/10.9.0/firebase-
analytics.js";
import { getDatabase, ref, onValue } from
"https://www.gstatic.com/firebasejs/10.9.0/firebase-
database.js";
// Your Firebase configuration
const firebaseConfig = {
apiKey: "xxx",
authDomain: "redacted.firebaseapp.com",
databaseURL: "redacted-
rtdb.firebaseio.com",
projectId: "redacted",
storageBucket: "redacted.appspot.com",
messagingSenderId: "13834881828",
appId: "1:13834881828:web:redacted",
measurementId: "G-2LTCCF8CRE"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
// Get a reference to the Firebase database
const database = getDatabase();
// Function to fetch data from Firebase and display it as a
graph
function fetchDataAndDisplayGraph() {
const dataRef = ref(database, 'attachmentStyles');
onValue(dataRef, (snapshot) => {
const data = snapshot.val(); // Get the data snapshot
const users = Object.entries(data); // Convert data to an array
of [name, answers] pairs
// Prepare arrays to store data for Chart.js
const labels = [];
const q1Data = [];
const q2Data = [];
const q3Data = [];
// Iterate over each user
users.forEach(([name, answers]) => {
labels.push(name); // Add user's name to labels
q1Data.push(answers.Question_1); // Add answer to Q1 data
q2Data.push(answers.Question_2); // Add answer to Q2 data
q3Data.push(answers.Question_3); // Add answer to Q3 data
});
// Display the data as a bar graph
const ctx = document.getElementById('chart').getContext('2d');
const myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: 'Question 1',
data: q1Data,
backgroundColor: 'rgba(255, 99, 132, 0.2)',
borderColor: 'rgba(255, 99, 132, 1)',
borderWidth: 1
},
{
label: 'Question 2',
data: q2Data,
backgroundColor: 'rgba(54, 162, 235, 0.2)',
borderColor: 'rgba(54, 162, 235, 1)',
borderWidth: 1
},
{
label: 'Question 3',
data: q3Data,
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderColor: 'rgba(75, 192, 192, 1)',
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
});
}
// Call fetchDataAndDisplayGraph function when the page loads
window.onload = fetchDataAndDisplayGraph;
</script>
</head>
<body>
<h1>Attachment Style Data</h1>
<canvas id="chart" width="800" height="400"></canvas>
<!-- Include Chart.js library -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</body>
</html>