I am using Laravel 10, and Chart.JS for graphs on my site.
I’ve got the stats loaded from the database, however, when creating the graph it shows me (https://i.stack.imgur.com/QT0s7.jpg) the graph lines, and the numbers up the side correctly but no bars or labels.
To get the information:
$stats = UserCountStats::query()
->start(now()->subMonths(2))
->end(now()->subSecond())
->groupByWeek()
->get();
My code to generate the graph:
<script>
// Retrieve the statistical data from laravel-stats
var statsData = {!! json_encode($stats) !!};
// Process the data and create the chart
var labels = statsData.map(stat => stat.period);
var values = statsData.map(stat => stat.value);
// Create the Chart.js bar chart
var ctx = document.getElementById('userCountChart').getContext('2d');
var userCountChart = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: 'User Count',
data: values,
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderColor: 'rgba(75, 192, 192, 1)',
borderWidth: 1
}]
},
options: {
scales: {
x: {
grid: {
display: true
}
},
y: {
beginAtZero: true // Ensure the y-axis starts at 0
}
},
plugins: {
legend: {
display: false // Hide the legend
},
tooltip: {
callbacks: {
label: function(context) {
return 'User Count: ' + context.parsed.y;
}
}
}
}
}
});
</script>
Stats passed
[{“start”:”2023-06-05T00:00:00.000000Z”,”end”:”2023-06-12T00:00:00.000000Z”,”value”:0,”increments”:0,”decrements”:0,”difference”:0},{“start”:”2023-06-12T00:00:00.000000Z”,”end”:”2023-06-19T00:00:00.000000Z”,”value”:0,”increments”:0,”decrements”:0,”difference”:0},{“start”:”2023-06-19T00:00:00.000000Z”,”end”:”2023-06-26T00:00:00.000000Z”,”value”:0,”increments”:0,”decrements”:0,”difference”:0},{“start”:”2023-06-26T00:00:00.000000Z”,”end”:”2023-07-03T00:00:00.000000Z”,”value”:0,”increments”:0,”decrements”:0,”difference”:0},{“start”:”2023-07-03T00:00:00.000000Z”,”end”:”2023-07-10T00:00:00.000000Z”,”value”:0,”increments”:0,”decrements”:0,”difference”:0},{“start”:”2023-07-10T00:00:00.000000Z”,”end”:”2023-07-17T00:00:00.000000Z”,”value”:0,”increments”:0,”decrements”:0,”difference”:0},{“start”:”2023-07-17T00:00:00.000000Z”,”end”:”2023-07-24T00:00:00.000000Z”,”value”:0,”increments”:0,”decrements”:0,”difference”:0},{“start”:”2023-07-24T00:00:00.000000Z”,”end”:”2023-07-31T00:00:00.000000Z”,”value”:4,”increments”:4,”decrements”:0,”difference”:4},{“start”:”2023-07-31T00:00:00.000000Z”,”end”:”2023-08-07T00:00:00.000000Z”,”value”:7,”increments”:3,”decrements”:0,”difference”:3},{“start”:”2023-08-07T00:00:00.000000Z”,”end”:”2023-08-14T00:00:00.000000Z”,”value”:11,”increments”:4,”decrements”:0,”difference”:4}]
I’ve tried for a while, and can’t figure this out.