chart js getting data from database using mysql and php

Solutions on MaxInterview for chart js getting data from database using mysql and php by the best coders in the world

showing results for - "chart js getting data from database using mysql and php"
Livia
01 Sep 2020
1Please place your php code into another file called api.php and use $.ajax to get these data with JSON format. To convert data into JSON format data you should use json_encode() php function.
2
3I have set sample example you can see here.
4
5Please refer below code example:
6
7api.php
8
9$arrLabels = array("January","February","March","April","May","June","July");
10$arrDatasets = array('label' => "My First dataset",'fillColor' => "rgba(220,220,220,0.2)", 'strokeColor' => "rgba(220,220,220,1)", 'pointColor' => "rgba(220,220,220,1)", 'pointStrokeColor' => "#fff", 'pointHighlightFill' => "#fff", 'pointHighlightStroke' => "rgba(220,220,220,1)", 'data' => array('28', '48', '40', '19', '86', '27', '90'));
11
12$arrReturn = array(array('labels' => $arrLabels, 'datasets' => $arrDatasets));
13
14print (json_encode($arrReturn));
15example.html
16
17$.ajax({
18type: 'POST',
19url: 'api.php',
20success: function (data) {
21lineChartData = data;//alert(JSON.stringify(data));
22var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Line(lineChartData);
23
24var ctx = document.getElementById("canvas").getContext("2d");
25window.myLine = new Chart(ctx).Line(lineChartData, {responsive: true});
26} 
27});
28Please note that you should pass value of randomScalingFactor() at api.php.
29
30Please check and let me know if you require any further help.