I desperately need a way to pass just a string of data

29 views
Skip to first unread message

Pops Vlogs

unread,
Feb 14, 2016, 11:36:32 AM2/14/16
to Google Visualization API
I need a way to pass in a string that is formatted as you'll see in the code below into the chart. if anybody could help out that will be amazing!!!!








var points = "['Mon', 20, 28, 38, 45],['Tue', 31, 38, 55, 66],['Wed', 50, 55, 77, 80],['Thu', 77, 77, 66, 50],['Fri', 68, 66, 22, 15]"

google.setOnLoadCallback(drawChart);

function drawChart($points) {
    var data = google.visualization.arrayToDataTable([
      $points
      // Treat first row as data as well.
    ], true);

    var options = {
      legend:'none'
    };

    var chart = new google.visualization.CandlestickChart(document.getElementById('chart_div'));

    chart.draw(data, options);
}


the highlighted bit is the string I cant get to work...

Sergey Grabkovsky

unread,
Feb 16, 2016, 10:20:54 AM2/16/16
to Google Visualization API
Hi Pops,

Is this all JavaScript?

Firstly, you're declaring that $points is an argument to drawChart, but are never actually passing it in. You'll have better luck just using your global variable directly and figuring out how to generalize later.

Secondly, you can't just interpolate a string and turn it into code. You'll need to take some explicit action, like calling JSON.parse (to parse the JSON that you specified).

Third, you're not going to be able to get anywhere with your "points" string as is. Currently, it is not a valid object expression, and the comments will simply cause the last thing to be executed (or maybe just cause an error). You need to surround your entire points string in brackets to make it an array, like so:
var points = "[['Mon', 20, 28, 38, 45],['Tue', 31, 38, 55, 66],['Wed', 50, 55, 77, 80],['Thu', 77, 77, 66, 50],['Fri', 68, 66, 22, 15]]";

Which would also mean that you don't need the extra brackets around arrayToDataTable's argument. So putting it all together, you would get:
var data = google.visualization.arrayToDataTable(JSON.parse(points), true);



--
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-visualizati...@googlegroups.com.
To post to this group, send email to google-visua...@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/3233955b-580a-4f4e-b333-56c61c960339%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

unnamed.gif

Sergey Grabkovsky

Software Engineer

Google, Inc

gra...@google.com


Reply all
Reply to author
Forward
0 new messages