JSFiddle - React, Tailwind, and code Playground
by kapantzak
HTML
<script src="https://www.gstatic.com/charts/loader.js"></script>
<p id="loading">Loading wrapper...</p>
<div id="wrapper">
<div id="piechart"></div>
</div>
CSS
#wrapper {
width: 100%;
display: none;
border: 1px solid #d8d8d8;
}
JavaScript
setTimeout(() => {
document.getElementById("loading").style.display = "none";
document.getElementById("wrapper").style.display = "block";
}, 2000);
google.charts.load('current', {'packages':['corechart']});
waitVisible(document.getElementById("wrapper"), drawChart);
function elementVisible(elem) {
return !!( elem.offsetWidth || elem.offsetHeight || elem.getClientRects().length );
}
function waitVisible(elem, callback, timeout) {
let timer = setInterval(() => {
if (elementVisible(elem)) {
callback();
clearInterval(timer);
timer = null;
}
}, 10);
const tm = timeout || 5000;
setTimeout(() => {
if (timer) {
clearInterval(timer);
}
}, tm);
}
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 11],
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]);
var options = {
title: 'My Daily Activities'
};
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}