Can't get linechart working

75 views
Skip to first unread message

Eric Mulder

unread,
Feb 3, 2017, 7:58:12 AM2/3/17
to Google Visualization API
I am fairly new to the Google Visualization and I am trying to start off with an easy linechart for 5 value sets (i.e. every record has 5 values).
My PHP script queries a database who is providing me with the raw data. This data is manipulated into a workable JSON encoded array and output as script result like so:

data.php:
[["V1","V2","V3","V4","V5"],
[21,18.5,630.96,18.5,21],
[21,18.5,631.56,18.5,21],
[21,18.5,632.13,18.5,21],
[21,18.5,632.75,18.5,21],
[21,18.5,633.3,18.5,21],
[21,18.5,633.94,18.5,21],
[21,18.5,634.49,18.5,21],
[21,18.5,635.12,18.5,21],
[21,18.5,625.6,18.5,21]]

My HTML file view.html:
<html>
 
<head>
   
<title>Test chart</title>
   
<!-- Load jQuery -->
   
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
   
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   
<script type="text/javascript">
        google
.charts.load('current', {packages: ['corechart', 'line']});
        google
.charts.setOnLoadCallback(drawCurveTypes);

       
function drawCurveTypes() {
           
var jsonData = $.getJSON("data.php");
           
var obj = JSON.stringify(jsonData);
           
var data = google.visualization.arrayToDataTable(obj);

           
var options = {
                hAxis
: {
                    title
: 'Time'
               
},
                vAxis
: {
                    title
: 'Values'
               
},
                series
: {
                   
1: {curveType: 'function'}
               
}
           
};

           
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
            chart
.draw(data, options);
       
}
   
</script>
 
</head>
 
<body>
   
<div id="chart_div"></div>
 
</body>
</html>


Whatever I do and whatever I try, nothing is showing up. When I take the example from the Google site it works find (with the embedded test data). I have added a few lines to retrieve the live data (the $.getJSON line, JSON.stringify line and arrayToDataTable line) and transform it into a Google dataset.
When debugging with Chrome the console tells me this:

Uncaught Error: Not an array
    at gvjs_zba
(jsapi_compiled_default_module.js:81)
    at
Object.gvjs_sm [as arrayToDataTable] (jsapi_compiled_default_module.js:83)
    at drawCurveTypes
(view.php:14)
    at
Object.google.a.c.xc (loader.js:152)
    at
Object.google.a.c.Oa (loader.js:152)
    at f
(loader.js:149)
    at
Object.google.l.m.kj (loader.js:229)
    at
Object.google.l.m.ce (loader.js:229)
    at loader
.js:228

I have no clue where things go wrong and I don't know where else to look. Any help pointing me in the right direction would be very appreciated.

Daniel LaLiberte

unread,
Feb 3, 2017, 9:02:04 AM2/3/17
to Google Visualization API
Hi Eric,

The value of your obj variable is going to be a string, not an array of arrays.  arrayToDataTable() requires an array of arrays.

Depending on whether the JSON in that string is properly formatted, you may want to instead use new google.visualization.DataTable().  See https://developers.google.com/chart/interactive/docs/reference#datatable-class

--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-visualization-api+unsub...@googlegroups.com.
To post to this group, send email to google-visualization-api@googlegroups.com.
Visit this group at https://groups.google.com/group/google-visualization-api.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-visualization-api/71314d71-2daa-4569-a365-c44b4e43a7e6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Eric Mulder

unread,
Feb 3, 2017, 9:47:13 PM2/3/17
to Google Visualization API
Tried a little more and now got the line chart semi-working using the DataTable method.
Now i just need to work out how to use a timestamp for the x axis if I supply it with the result of each row.
Thanks!
Reply all
Reply to author
Forward
0 new messages