I'm searching data and mounting a string to go to the graphics api of google but I'm having trouble, I'm doing it like this:
var variavel = "['Data', 'Membros', 'Visitantes'],";
for(var b = 0; b < pontos.length; b++){
var obj = pontos[b][0];
variavel = variavel + "['" + obj.data + "', " + obj.membros + ", " + obj.visitantes + "],"
}
variavel = variavel.replace(/'/g, '"');
var parsVal = JSON.parse(variavel);
In this part I'm creating this string.
The problem you're experiencing is because of ,
the last time it passes in.
In the console the following error was plotted:
Uncaught SyntaxError: Unexpected token,
After this I'm doing is plotting on the div the graph:
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable(parsVal);
var options = {
title: 'Membros x Visitantes',
curveType: 'function',
legend: { position: 'bottom' }
};
var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
chart.draw(data, options);
}
My chart is not appearing on the page.
In the documentation, let me do it this way:
<script type="text/javascript">
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
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'));
chart.draw(data, options);
}
</script>
But what I need to do is do it dynamically, so the data is taken when the page loads. This graphic is getting here:
https://developers.google.com/chart/interactive/docs/gallery/linechart