Reputation: 867
I have the following present in chart.js
:
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
I have a table called monthly_flagged
which stores the total amount of flagged posts for each month. I am trying to create a line chart to show the fluctuation between number of flagged posts every month.
Using PHP I have got the following data:
$statement = mysqli_prepare ($connect, "SELECT * FROM monthly_flagged");
mysqli_stmt_execute($statement);
$get_data = mysqli_fetch_assoc ($statement);
$month = $get_data['month'];
$year = $get_data['year'];
$total_flaggged = $get_data['total_flagged'];
mysqli_stmt_close($statement);
The thing is, I am unsure on how I can apply this data to JS. Assume my monthly_flagged
table has one row:
id:1
month: April
year: 2016
total_flagged:28
Using the data obtained in $month
and $total_flagged
, I want to update the Apr
on my x-axis.
Currently, this is how data is displayed (using test numbers):
series: [{
name: 'Flagged posts',
data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
}
But I basically need it to go something like this:
series: [{
name: 'Flagged posts',
data: [$monthJan, $monthFeb, $monthApr, ...]
}
But again, unsure how to do this.
Upvotes: 3
Views: 688
Reputation: 752
Try storing your data in an array,
$statement = mysqli_prepare ($connect, "SELECT * FROM monthly_flagged");
mysqli_stmt_execute($statement);
$months = array();
$data = array();
$result = mysqli_stmt_get_result($statement);
while($get_data = mysqli_fetch_assoc ($result)){
$months[] = $get_data['month'];
$data[] = $get_data['total_flagged'];
}
mysqli_stmt_close($statement);
Then on your javascript:
xAxis: {
categories: [<?php echo json_encode($months); ?>]
},
And finally:
series: [{
name: 'Flagged posts',
data: [<?php echo json_encode($data); ?>]
}
Upvotes: 2