When using AmChart's makeChart
method, it is essential that the ID specified in the first parameter exists in the DOM; otherwise, the method will not function as expected. In this case, the initialization of AmCharts is being called before the information window is attached to the DOM. To address this issue, a domready
event listener should be added to the infowindow so that the makeChart
can be invoked there. Additionally, a close
event listener is recommended to clear out the chart instances and conserve resources. It is important to clone the chart data during the domready
event to ensure that the clear function does not remove them entirely.
The following code demonstrates how to add and clear the chart while preserving the data. I have removed the shift calls which appeared unnecessary and caused issues when reopening the infowindow:
var chartLine;
var chartWa;
google.maps.event.addListener(infowindow, 'domready', function() {
var line_data_copy = JSON.parse(JSON.stringify(item.line_data_chart));
var wa_data_copy = JSON.parse(JSON.stringify(item.wa_data_chart));
chartLine = initAmChart("divChartLine", colors_line, line_data_copy);
chartWa = initAmChart("divChartWa", colors_wa, wa_data_copy);
chartLine.allLabels[0].text = item.line_ratio+"%";
chartLine.validateData();
chartWa.allLabels[0].text = item.wa_ratio+"%";
chartWa.validateData();
});
google.maps.event.addListener(infowindow, 'close', function() {
chartLine.clear();
chartWa.clear();
})
Check out the updated code on CodePen