I am trying to do some basic Google Charts filled by databse via PHP/PDO/MSSQL passed via AJAX to the .js
I'm having a very basic problem that i feel is encoding.
I am following the tutorial https://developers-dot-devsite-v2-prod.appspot.com/chart/interactive/docs/gallery/linechart
They hardcode the data into the google chart but i send mine over via Ajax. My data from the Ajax looks like this:
$data = "['Year', 'Sales', 'Expenses'],['2004', 1000, 400],['2005', 1170, 460], ['2006', 660, 1120],['2007', 1030, 540]";
echo (json_encode($data));
resulting in:
"['Year', 'Sales', 'Expenses'],['2004', 1000, 400],['2005', 1170, 460], ['2006', 660, 1120],['2007', 1030, 540]"
However when I run the chart i get the error "Error: Data for arrayToDataTable is not an array."
I'm sending the exact same data verbatim but via AJAX so what am I missing?
Instead of the google sample:
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Year', 'Sales', 'Expenses'],
['2004', 1000, 400],
['2005', 1170, 460],
['2006', 660, 1120],
['2007', 1030, 540]
]);
var options = {
title: 'Company Performance',
curveType: 'function',
legend: { position: 'bottom' }
};
var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
I'm using:
function drawChart() {
var jsonData = $.ajax({
url: "reports/GoogleCharts/ajax_StationExpenses.php",
dataType: "json"
}).responseText;
var data = new google.visualization.arrayToDataTable(jsonData);
var options = {
title: 'Station Utility Expense Over $250 by Month',
curveType: 'function',
legend: { position: 'bottom' },
width: 800,
height: 600,
animation:{ startup: true, duration: 1700, easing: 'in' }
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
What am I missing, why does it take it hardcoded but not in what appears to be the exact same format pushed by AJAX from my php page?
Your $data value is a string, not an array.
$data = "anything here"; //a string
$data = "[['also','a','string'],...]"; //also a string
JSON encoding a string will not produce valid JSON.
$data = array(
array("Year","Sales","Expenses"),
array("2004",1000,400),
array("2005",1170,460),
array("2006",660,1120),
array("2007",1030,540)
);
The above is a valid PHP array.
$data = [
["Year","Sales","Expenses"],
["2004",1000,400],
["2005",1170,460],
["2006",660,1120],
["2007",1030,540]
];
.. of course you can also use the PHP shorthand for writing arrays.
$json = json_encode($data);
The above is a valid JSON encoded array.
Add the JSON header:
header('Content-type: application/json');
exit( $json );
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.