How to Display Data In Pie Chart Using PHP And jQuery

In this post we will make simple pie chart by using Google chart library with php script and mysql database. This advance topic in web development with php programming. For making pie chart we will use Google chart library for making pie chart and we have pass mysql data by using php programming in json format. You may also like Multiple file upload in Codeigniter and curl request in udemy api using php

PHP Code

<?php
$rating_data = array(
    array('Employee', 'Rating'),
    array('Suresh', 20),
    array('Amit', 56),
    array('Mitul', 56),
    array('Rahul', 37),
    
);
$encoded_data = json_encode($rating_data);
?>

HTML & jQuery Code

<html>
    <head>
        <script type="text/javascript" src="https://www.google.com/jsapi"></script>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        
        <script type="text/javascript">
            google.load("visualization", "1", {packages: ["corechart"]});
            google.setOnLoadCallback(drawChart);
            function drawChart()
            {
                var data = google.visualization.arrayToDataTable(
                <?php echo $encoded_data; ?>
                );
                var options = {
                    title: "Employee Ratings"
                };
                var chart = new google.visualization.PieChart(document.getElementById("employee_piechart"));
                chart.draw(data, options);
            }
        </script>
    </head>
    <body>
        <div id="employee_piechart" style="width: 900px; height: 500px;"></div>
    </body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *