I am using chart.js to display stats on data pushed to php with the queries. I would like create a php/sql filter to display data week by week starting on the last 7 days. I am using mysql(MariaDB).
In this scenario I have salespersons with unique IDs, and the records of each sale are being stored in the DB, including: sale id, sale date, the number of items sold, and the ID of the salesperson associated with the sale.
I used SQL query to set up a filter, which displays the number of items sold by each of the salespersons in the last 7 days.
This is my php code:
$spID = ''; //this will hold asalesperson ID
$spStats=''; //this will hold a total number of items sold by the specific salesperson in the range of 7 days
//get performance of the salesperson
$get_perf = "SELECT cast(sDate AS date) As date, spID, SUM(itemsSaled) AS spStats
FROM sales
WHERE sDate >= DATE_ADD(NOW(), INTERVAL -7 DAY) AND sDate <=NOW()
GROUP BY
date, spID";
$agent_perf = mysqli_query($db_conn, $get_perf);
while ($row = mysqli_fetch_array($agent_perf)) {
$spID = $spID. '"'. $row['spID'].'",'; //label
$spStats = $spStats. '"'. $row['spStats'].'",'; //dataset
}
$spStats= trim($spStats, ",");
And this is my js script to represent the above as a pie chart
var ctx = document.getElementById("chart4").getContext('2d');
var myChart4 = new Chart(ctx, {
type: 'pie',
data: {
datasets: [{
label: 'date', //this is not being displayed
data: [<?php echo $spStats; ?>],
backgroundColor: pointBackgroundColors //takes colours from function
}],
//labels: [<?php echo $agent_id; ?>]
labels: [<?php echo $spID; ?>]
},
options: {
scales: {scales:{yAxes: [{beginAtZero: false}], xAxes: [{autoskip: true, maxTicketsLimit: 20}]}},
tooltips:{mode: 'index'},
legend:{display: true, position: 'top', labels: {fontColor: 'black', fontSize: 16}}
}
});
So right now my graph displays my desired data for the last seven days. I would like to have a drop-down list, which will be associated with my PHP so I could scroll down to the desired data range (always 7 days), but I can't figure out of how to make the process of selecting 7 days range automatic instead of the need to assign the dates in a query. (And how will that change my js code?)