I'm trying to create multiple Google Charts recursively with the code below. I'm using AJAX
to get the chart data and to store in the array histogramData
.
With this code I can create the multiple i_histogram
charts, but apparently, on each loop interaction, the previous chart is overwritten by the last one.
HTML code:
<div id="histogramCharts"></div>
Javascript code:
var histogramChartElement = document.getElementById('histogramCharts');
// remove histogram chart element children
while (histogramChartElement.hasChildNodes()) {
histogramChartElement.removeChild(histogramChartElement.childNodes[0]);
}
for (var i = 0; i < histogramLabels.length; i++) {
var label = histogramLabels[i];
var node = document.createElement('div');
node.setAttribute('id', i + '_histogram');
histogramChartElement.appendChild(node);
var dataTable = new google.visualization.DataTable();
dataTable.addColumn('string', 'A');
dataTable.addColumn('number', 'B');
dataTable.addRows(histogramData[i]);
google.charts.setOnLoadCallback(function () {
var options = {
title: histogramLabels[i],
legend: {position: 'none'},
histogram: {bucketSize: 0.1}
};
var histogramChart = new google.visualization.Histogram(node);
histogramChart.draw(dataTable, options);
});
}
I got this result:
<div id="histogramCharts">
<div id="0_histogram"></div>
<div id="1_histogram"></div>
<div id="2_histogram">
<div style="position: relative;">....</div>
</div>
</div>