score:5
Please 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.
I have set sample example you can see here.
Please refer below code example:
api.php
$arrLabels = array("January","February","March","April","May","June","July"); $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')); $arrReturn = array(array('labels' => $arrLabels, 'datasets' => $arrDatasets)); print (json_encode($arrReturn));
example.html
$.ajax({ type: 'POST', url: 'api.php', success: function (data) { lineChartData = data;//alert(JSON.stringify(data)); var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Line(lineChartData); var ctx = document.getElementById("canvas").getContext("2d"); window.myLine = new Chart(ctx).Line(lineChartData, {responsive: true}); } });
Please note that you should pass value of randomScalingFactor()
at api.php
.
Please check and let me know if you require any further help.
score:0
This is based on the answer above with a couple of changes.
php:
include 'db.php';
$query = "SELECT month, COUNT(*) count FROM customer WHERE month='march' GROUP BY month";
if ($stmt = $conn->prepare($query)) {
$stmt->execute();
$stmt->bind_result($month, $count);
$labels = array();
$data = array();
while ($stmt->fetch()) {
$labels[] = $month;
$data[] = $count;
}
$stmt->close();
}
$datasets = array('label'=>"timer",'data'=> $data);
$data = array('labels'=>$labels, 'datasets'=> array($datasets));
echo json_encode($data);
I had to use JSON.pare() on the passed-in array.
Javascript:
$.ajax({
type: 'POST',
url: 'api.php ',
datatype: 'json',
success: function (result) {
var ctx = document.getElementById("chart").getContext("2d");
var mychart = new Chart(ctx,
{
type: 'bar',
data: JSON.parse(result),
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
})
}
})
html:
<canvas id="chart" width="200" height="200"></canvas>
score:6
First get your data into suitable data structures using PHP
$months = array("january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december");
$monthvalues = array();
foreach ($months as $month) {
$monthvalues[$month] = 0;
}
$result = mysql_query("SELECT month, count(*) FROM customer group by month") or die(mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$monthvalues[$row[0]] = (int)$row[1];
}
Below that, just plug in those data structures into your Javascript
var randomScalingFactor = function(){ return Math.round(Math.random()*100)};
var lineChartData = {
labels : <?=json_encode($months);?>,
datasets : [
{
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 : <?=json_encode(array_values($monthvalues));?>
}
]
}
assuming the the window.onload and the HTML for the canvas element are in their proper places.
Source: stackoverflow.com
Related Query
- Chart.js - Getting data from database using mysql and php
- Grouped Bar Chart from mySQL database using ChartJS and PHP
- How to displays item names from the database using a monthly or yearly chart PHP MySQL
- How to display data from database into line graph using php and mysql?
- Chart.js not rendering chart with data from mySQL database
- I need a PHP query to grab data from the database and use in my bar charts for results
- getting additional value fields from data source for dx.chartjs doughnut chart
- How to take data from an API and create a chart on that using Chart.js and Angular 8?
- How to generate bubble chart (Chart.js) using data from database FILTERED by user in Django?
- pie chart in php with mysql data using chart.js
- Chartjs random colors for each part of pie chart with data dynamically from database
- Filter ChartJS using data from PHP
- Using data from API with Chart JS
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- How to create chartjs chart with data from database C#
- Configure Pie Chart Colors Using Chart.js and PHP
- chart.js not getting linked with online database and php
- How to get chart from data points (arrays) with inconsistent time intervals and chart.js?
- How do I customize y-axis labels and randomly pick the value from the data range for x-axis in Chart js
- How to plot chart from external JSON and format X-AXIS to show time using Chart.JS?
- Update Chart js chart dynamically in angular 2 from data getting from nodejs service
- create Chart using Chartjs and PHP
- How to get Data from API to display chart using chartjs in Vuejs
- daily activity chart using PHP MySQL
- Send data from php to chart with ajax
- How can make my chart.js dynamic using data from database
- Using number/text input field to set the data values in ChartJs stops the chart from being displayed
- Use data from SQL database in .js file with PHP
- Chart.js using json data from MySQL
- Having trouble fetching data from database for chart in ASP .NET Core
More Query from same tag
- Showing only entire units on ChartJS
- ChartJS, updating chart type does not update xAxis properly
- Who knows why I can't plot my chart properly?
- chartjs chart spilling out of container
- React chart.js dynamic data update not rendering
- chart.js? Specifically Radar Charta and Negative Numbers
- how to show chart using chart js
- Chart.update is not a function typeError
- Chart.js gap between points
- Chart.js remove gridline
- Chart.js - tooltips issue with multiple charts
- Limit data in Chart.js with Flask/SQLAlchemy in python
- Chart.js - scaleType='date' not working
- How to show tooltip on legend item in chart.js
- Javascript highcharts code similar to image
- Chart.js Pie Chart: How to set background image for segment
- Reduce spacing between bars in horizontal bar chart (chart.js)
- Update Chart JS data dynamically and add new data, remove old data to create timeline "like" chart
- Do not draw line on condition in ChartJS line chart
- How to call back one js file from another js file?
- Charts.js graph not scaling to canvas size
- Want to draw data using chartjs in django
- Update chartjs chart within random data once per second
- How can I remove the white border from Chart.js pie chart when all legends are hidden?
- Dynamic Chart Data
- chartjs: bars smaller than actual column, tooltip doesn't display
- Chart.js show data in chronological order
- fill: 'origin' property does not work for my chart
- Printing a list by clicking chart Chart js + react
- Chart.js updating-animations of radar-charts