0

I am using google charts ,found syntax error

Unexpected Token {

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['name', 'score'],
<?php 
global $wpdb;
$query = $wpdb->get_results("select t2.name, count(t1.id) as score from wp3_wpsp_custom_status as t2 left join wp3_wpsp_ticket as t1 on t2.name =   t1.status group by t2.name");
var_dump($query);
foreach($query as $row){
$object_array =(array)$row;
echo "['".$object_array['name']."',".$object_array['score']."],";
}
?>
]);
var options = {
title: 'Date wise visits'
};
var chart = new google.visualization.ColumnChart(document.getElementById("columnchart"));
chart.draw(data, options);
}
</script>

The above code is the basic syntax for creating a column chart. Look at the values after “var data = google.visualization.arrayToDataTable([” This one has static values. But in order to show our stats, we need to dynamically load these values from our database with php.

<body>
<h3>Column Chart</h3>
<div id="columnchart" style="width: 900px; height: 500px;"></div>
</body>

Error[![enter image description here][1]][1]

R9102
  • 687
  • 1
  • 9
  • 32
  • @Armin i have edited you can look into my code now – R9102 Feb 14 '17 at 14:47
  • Can you also add your generated code after you run the script? – Armin Feb 14 '17 at 14:49
  • @Armin i have posted my screenshot also – R9102 Feb 14 '17 at 14:50
  • I meant your js generated code. When you go to "view-source" what is your generated js? – Armin Feb 14 '17 at 14:52
  • 2
    It's almost impossible to debug this without seeing the rendered Javascript. As a general suggestion, though: if your values can be a valid JSON structure, build the structure in PHP and call `json_encode` on it. That will be much more reliable in producing valid Javascript. – lonesomeday Feb 14 '17 at 14:57
  • @Armin i have given screen shot of view source – R9102 Feb 14 '17 at 15:01
  • I tested your code, it should work with proper values. I suppose your var_dump messed it up – Armin Feb 14 '17 at 15:02
  • Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Félix Adriyel Gagnon-Grenier Aug 26 '17 at 18:58

1 Answers1

2

Remove var_dump($query) from your code.

To add color, add third parameter. Example from their site:

var data = google.visualization.arrayToDataTable([
        ["Element", "Density", { role: "style" } ],
        ["Copper", 8.94, "#b87333"],
        ["Silver", 10.49, "silver"],
        ["Gold", 19.30, "gold"],
        ["Platinum", 21.45, "color: #e5e4e2"]
      ]);
Armin
  • 1,158
  • 1
  • 7
  • 16
  • How add color to each one of them – R9102 Feb 14 '17 at 15:06
  • Check updated explanation – Armin Feb 14 '17 at 15:08
  • it just show score as legend....how to add up legends ...Thanks for your answers – R9102 Feb 14 '17 at 15:12
  • I am not sure what do you mean. But anyway I would suggest either updating this question, or asking a new one if you have more questions. Otherwise we could end up with incorrect answer to your question, or with an answer that provides more information but people won't see it because the question does not ask for it. – Armin Feb 14 '17 at 15:17