Load the Google Charts library once, request the package you need, and draw into a dedicated container after the loader callback fires.
<div id="chart_div" style="width:100%;min-height:380px"></div>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script>
google.charts.load('current', {packages: ['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
const data = google.visualization.arrayToDataTable([
['Language', 'Tutorials'],
['PHP', 300],
['MySQL', 100],
['JavaScript', 200]
]);
const chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, {title: 'Tutorial distribution'});
}
</script>Current example first; the original chart examples below are retained and cleaned up for continued learning.
<div id="chart_div"></div>
Copy above JavaScript code and html code and save as .html page. The above Pie chart will be displayed on your page. You should have internet connection to connect to Google Chart library.
var options = {'title':'Tutorials at plus2net.com',
'width':400,
'height':300};
is3D=true, by default its value is false.
var options = {title:'plus2net.com :Nos of tutorials',
width:600,
height:500,
legend:'left',
is3D:true};
$php_data_array first and then transfer the data to JavaScript array my_2d to create the graph.
var chart_div = document.getElementById('chart_div');
var chart = new google.visualization.PieChart(chart_div);
google.visualization.events.addListener(chart, 'ready', function () {
chart_div.innerHTML = '<img src="' + chart.getImageURI() + '">';
console.log(chart_div.innerHTML);
});
chart.draw(data, options);
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.