score:0
The main idea is that you should format your data in your model, then return JSON to the front end via json_encode
. After that, you would parse the json in your ajax returns and then pass the appropriate data to the chart.
score:0
You can send JSON
data from php
Try this:
Php:
public function dossier(){
$stmt = "SELECT count(*) FROM dossier GROUP BY typedossier";
$stmt = $this->db->prepare($stmt);
$rows=$stmt->execute();
$rows = $stmt->fetch(PDO::FETCH_NUM);
exit(json_encode(array('counts' => $rows)));
}
After ajax successfully complete you need to initialize chart plugin inside success callback like below:
Ajax:
$.ajax({ url: 'stats',
data: {method: 'dossierRepartitionType'},
type: 'post',
datatype: 'json',
success: function(output) {
if (output.counts) {
dataDossierRepartitionType=output.counts.join();
alert(dataDossierRepartitionType)
initCharts(dataDossierRepartitionType);
}
},
error: function () {
alert("Oops there is an error.");
}});
Finally wrap chart initialization code inside callback function
Chart:
function initCharts(dataDossierRepartitionType){
var ctx = document.getElementById("myChart");
ctx.width = 400;
ctx.height = 400;
data = {
datasets: [{
data: [dataDossierRepartitionType, 20],
backgroundColor: [
'rgb(255, 99, 132)',
'rgb(54, 162, 235)',
],
borderColor: [
'white',
'white',
],
borderWidth: 1
}],
// These labels appear in the legend and in the tooltips when hovering different arcs
labels: [
'Red',
'Blue',
]
};
var myDoughnutChart = new Chart(ctx, {
type: 'doughnut',
data: data,
options: {
legend: {
labels: {
fontColor: "white",
fontSize: 18
}
},
maintainAspectRatio: false,
responsive: false
}
});
}
score:0
it's very easy, you need to modify your php code like this:
public function dossier(){
$stmt = "SELECT count(*) as total FROM dossier";
$stmt = $this->db->prepare($stmt);
$rows=$stmt->execute();
$number_of_rows = $rows->fetchColumn();
return json_encode(["total" => $number_of_rows]);
In your ajax petition you are specifying a "json" return so in your script php need's return a json.
$.ajax({ url: 'stats',
data: {method: 'dossierRepartitionType'},
type: 'post',
datatype: 'json',
success: function(output) {
dataDossierRepartitionType=output.total;
},
error: function () {
alert("Oops there is an error.");
}});
You should to receive a json from php with this structure
{total: rows_total}
so in your ajax response you'll receive that answer and you can get the data like this:
dataDossierRepartitionType=output.total;
Sorry for my english, hope can help you
Source: stackoverflow.com
Related Query
- ChartJS and Ajax calls
- ChartJS AJAX load labels and data
- Many Flots using ChartJS and Ajax get data values
- ChartJS - Returning labels and data from server on different calls
- Draw a Chart.js with ajax data and responsive. A few problems and questions
- Chartjs v2 - format tooltip for multiple data sets (bar and line)
- ChartJS canvas not displaying rgba colors in IE, Safari and Firefox
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- chartjs datalabels change font and color of text displaying inside pie chart
- Django and ChartJS
- Change point size and color on hover in chartjs
- ChartJS and jsPDF - why the background is black?
- how to change background in chartjs and remove background lines?
- chartjs - top and bottom padding of a chart area
- How to change the color of legend in chartjs and be able to add one more legend?
- Usage of ChartJS v3 with TypeScript and Webpack
- Category scale on Y-axis and time on x-axis in bubble chart in Chartjs
- How to remove gridlines and grid labels in Chartjs Radar?
- How to feed hour and minute data into chartJS
- Chartjs Datasets overlapping and z-index
- Create a rounded bar graph with Angular and chartJS
- Displaying mixed types of legends (bar and lines) with Chartjs
- Chartjs not rendering chart and no error thrown
- ChartJS Line chart cut off at the top and bottom
- How to get onClick Event for a Label in ChartJS and React?
- Update charts in chartjs and angular
- ChartJS click on bar and change it's background color
- unable to add background color to the canvas using jspdf and chartjs
- Chartjs adding icon to tooltip and label
- Removing ChartJS 2 border and shadow from point style legend
More Query from same tag
- Puppeteer and PDF generation with canvas
- Chart js - Get bar width after render
- How to fill my chart (chart.js) with my own data from API
- How to make bar charts and scatter dots appear underneath each other?
- Bar Chart With 2 Different Attribute Of The Same Variable [Chart.Js]
- Chart.js 3.3.0 - Draw text on top of chart
- ChartJS Only Show Large Font Size for a Specific Tick
- ChartJs Horizontal line
- Unable to ES6 import ChartJS plugin into Aurelia
- How to include adapters and plugins with ChartJS
- Charts not rendering properly in Angular
- How to json_encode associative php array to [{x: key1, y: value1}, {x: key2, y: value2}, ...]?
- Chart.js scatter/bubble pointstyle custom text
- Changing the base of the logarithmic scale in Chart.js
- Chart.js time series showing empty plot
- How can I display both values as labels when hovering over chart.js
- How To Format Scale Numbers in Chart.js v2
- Chart JS Dynamic number of datasets
- Can you draw a graph without rendering it?
- Keeping scales in sync across multiple graphs or dynamically changing scales
- ChartJS TypeError: document.getElementById(...).getContext is not a function
- How to display chart in android tv using web view(chart.js)
- Chartjs line chart gets all scrambled up when an x value coincides with a label
- using react js to create a website that could read .csv file and show in chartjs
- Display Date Label in axes - chart js/ng2 charts
- Completely hide the Y-axis on chartjs when data is hidden
- char.js overlapping bar columns when using type: 'time'
- Change size of bubble radius without changing r value
- chart js onclick function call
- Chart Piechart in only show percentage on hover but it shows data and label with percentage both i just want to show percentage only percentage