To display the totals of each package in nursing school and nursing prep using a Bootstrap Pie Chart, follow the below steps. This involves integrating the counts you've already fetched in your PHP file with JavaScript and Chart.js (a widely used JavaScript library for creating charts). Make sure you have Bootstrap and Chart.js included in your project. If not, you can add them by linking to their CDN in the <head> section of your HTML.
- Include Chart.js in Your HTML File
<head>
<!-- Include Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<!-- Include Chart.js JavaScript -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
- PHP Section to Pass Data to JavaScript
At the end of your PHP code (or in another appropriate section within the HTML body), you'll need to pass the PHP variables to JavaScript to use them for the chart.
<script>
// Pass PHP variables to JavaScript
var nursingPackages = [
<?php echo $selNursingSM['totNursingSM']; ?>,
<?php echo $selNursingS6M['totNursingS6M']; ?>,
<?php echo $selNursingS1Y['totNursingS1Y']; ?>,
<?php echo $selNursingS2Y['totNursingS2Y']; ?>
];
var prepPackages = [
<?php echo $selPrepCM['totPrepCM']; ?>,
<?php echo $selPrepC6M['totPrepC6M']; ?>,
<?php echo $selPrepC1Y['totPrepC1Y']; ?>
];
</script>
- Creating the Pie Chart with Chart.js
In the same script tag, or a new one, add the JavaScript needed to render the pie chart using the data passed from PHP.
<script>
var ctx = document.getElementById('chart-area').getContext('2d');
var myChart = new Chart(ctx, {
type: 'pie',
data: {
labels: [
'Nursing School Monthly',
'Nursing School 6 Months',
'Nursing School 1 Year',
'Nursing School 2 Years',
'Prep Courses Monthly',
'Prep Courses 6 Months',
'Prep Courses 1 Year'
],
datasets: [{
label: '# of Votes',
data: [...nursingPackages, ...prepPackages],
backgroundColor: [
'rgba(255, 99, 132, 0.2)', 'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)', 'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)', 'rgba(255, 159, 64, 0.2)',
'rgba(255, 99, 132, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)', 'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)', 'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)', 'rgba(255, 159, 64, 1)',
'rgba(255, 99, 132, 1)'
],
borderWidth: 1
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
}
});
</script>
- Ensure Chart.js Canvas is Sized Properly
Since Chart.js can sometimes be tricky to size appropriately, ensure your canvas container is styled in a way that the chart fits nicely. You can control the size via CSS or inline styling. Example:
<style>
.chart-container {
position: relative;
margin: auto;
height: 40vh;
width: 80vw;
}
</style>
<div class="chart-container">
<canvas id="chart-area"></canvas>
</div>
This setup will display a pie chart that visualizes the distribution of students across nursing school and nursing prep packages using the counts you've calculated in your PHP script.