I am trying to make a Google chart which pulls the data from a hidden field on a web page.
If I add the data in directly via data.addRows it works fine:
google.charts.load('current', {packages: ['corechart', 'line']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Rainfall');
data.addRows([[new Date(2000, 8, 5), 56], [new Date(2000, 8, 6), 22], [new Date(2000, 8, 7), 44], [new Date(2000, 8, 8), 40], [new Date(2000, 8, 9), 62]]);
var options = {
title: 'Rainfall'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
However, if I try and create a string which contains the array and then add it using addRows it doesn't work:
google.charts.load('current', {packages: ['corechart', 'line']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Rainfall');
var myData = "[[new Date(2000, 8, 5), 56], [new Date(2000, 8, 6), 22], [new Date(2000, 8, 7), 44], [new Date(2000, 8, 8), 40], [new Date(2000, 8, 9), 62]]"
data.addRows(myData);
var options = {
title: 'Rainfall'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
How can I add the data in the string to the DataTable?
JSFiddle: https://jsfiddle.net/oo5w6ghf/4/