Dear I am with my next question. I am working with a fix. The first part is more than anything the query with its respective array.
include "db.php";
$sentencia =("SELECT UPPER (MONTHNAME(fecha)) AS FECHA,SUM(cantidad) AS TOTAL
FROM barchart
WHERE YEAR(fecha)=YEAR(NOW()) AND tipo='AGUA'
GROUP BY MONTH(fecha),tipo");
$datos = $conn->query($sentencia)->fetchAll(PDO::FETCH_ASSOC);
echo json_encode( array('datos1'=>$datos) );
This is how the data is displayed.
{"datos1":[{"FECHA":"AUGUST","TOTAL":15000},{"FECHA":"SEPTEMBER","TOTAL":42000}]}
The idea is that it shows me the data like this. Any ideas how I can fix my data to display.
$datos1 = array(
array("label"=> "AUGUST", "y"=> 15000),
array("label"=> "SEPTEMBER", "y"=> 14000)
);
Code of the example that I am using. But it doesn't show me the chart. I'm trying to fix it, but I have to have some error that I can't see yet.
I think the error comes from the query when wanting to view the graph
<?php
include "db.php";
$sentencia =("SELECT UPPER (MONTHNAME(fecha)) AS FECHA,SUM(cantidad) AS TOTAL
FROM barchart
WHERE YEAR(fecha)=YEAR(NOW()) AND tipo='AGUA'
GROUP BY MONTH(fecha),tipo");
$datos = $conn->query($sentencia)->fetchAll(PDO::FETCH_ASSOC);
print_r(array('datos1'=>$datos));
?>
<!DOCTYPE HTML>
<html>
<head>
<script>
window.onload = function () {
var chart = new CanvasJS.Chart("chartContainer", {
animationEnabled: true,
theme: "light2",
title:{
text: "Average Amount Spent on Real and Artificial X-Mas Trees in U.S."
},
axisY:{
includeZero: true
},
legend:{
cursor: "pointer",
verticalAlign: "center",
horizontalAlign: "right",
itemclick: toggleDataSeries
},
data: [{
type: "column",
name: "Real Trees",
indexLabel: "{y}",
yValueFormatString: "$#0.##",
showInLegend: true,
dataPoints: <?php echo json_encode($datos1); ?>
},{
type: "column",
name: "Artificial Trees",
indexLabel: "{y}",
yValueFormatString: "$#0.##",
showInLegend: true,
dataPoints: <?php echo json_encode($datos1); ?>
}]
});
chart.render();
function toggleDataSeries(e){
if (typeof(e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
e.dataSeries.visible = false;
}
else{
e.dataSeries.visible = true;
}
chart.render();
}
}
</script>
</head>
<body>
<div id="chartContainer" style="height: 370px; width: 1200px;"></div>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
</body>
</html>
I would really appreciate if you can help me.
Bro I was testing locally and you just have to change the last line of your code. Change the echo to a print_r() and remove the json encode as follows:
Documentation: https://www.php.net/manual/es/function.print-r.php