PHP is one of the most common server-side scripting languages developers worldwide use for web development. When it comes to creating a PHP chart using data contained in a database, most developers prefer the MySQL database. MySQL, an open-source RDBMS (Relational Database Management System), helps automate the process of retrieving data. Using PHP and MySQL together, developers can build just about any type of website, simple or complex. For example, you can create a pie chart in PHP and MySQL to display proportion/relative data on your website/web app. Are you looking to create a PHP chart? While charts are a great way to make sense of raw data, the general perception is that creating a PHP chart using data from the MySQL database is a difficult and time-consuming process. But it isn’t as difficult as you think if you’re using an efficient charting library. In this article, we’ll show you how to create a pie chart in PHP MySQL and why FusionCharts is the best PHP chart tool.

Why Create A Pie Chart In PHP And MySQL?

A simple pie chart in PHP MySQL A pie chart is essentially a circular graph, where the circle is divided into different slices or sectors. Each slice in the chart shows a proportion of the whole. You can use these charts to represent categorical data and a parts-to-whole relationship. When it comes to creating a PHP pie chart, you can obtain the data for the chart statically by defining the data during the creation of the chart. However, the efficient way is to retrieve data dynamically from another source like MySQL. This way, your chart will always show up-to-date data. While there are several databases for PHP-based web development, MySQL is the most popular DBMS for PHP. Here are a few reasons why most PHP developers prefer MySQL:
  • Provides an easy way to save website data
  • Helps automate the process of retrieving data
  • Create highly interactive web apps
  • Cost-effective and functional
  • Superior performance
  • Open source
  • Fast and easy maintenance
  • Compatible with almost every modern server, such as Apache and IIS

Why Use FusionCharts To Create A Pie Chart In PHP and MySQL?

There are many open-source JavaScript chart libraries that you can use to create a pie chart in PHP MySQL. However, most of them are difficult to use. Additionally, most charting tools only support static images of the charts with low resolution. If you’re looking for a powerful PHP chart library that is easy to use, FusionCharts is the best option. FusionCharts is a leading JavaScript charting library that is capable of rendering beautiful and interactive charts/graphs in the browser. If you want to create a JavaScript pie chart in PHP, you can use the PHP module for FusionCharts. FusionCharts also supports a wide range of front-end integrations. Hence, you can also use it to create an angular pie chart or a React pie chart. Here are the main features of the PHP module for FusionCharts:

Can You Connect Charts Directly To Any Database Of Your Choice?

With the PHP module for FusionCharts, you don’t have to worry about JavaScript methods and objects. You can simply write your app in native PHP or any other major PHP frameworks, such as Laravel, Symphony, or CodeIgniter, and easily fetch data from your SQL databases, including MySQL, and directly create dynamic and interactive charts.

Does It Support a Wide Range of Charts and Graphs?

FusionCharts’ PHP module allows you to create more than 150 functional charts for your web apps. These include pie charts, line charts, bar charts, column charts, area charts, Gantt charts, and many more. The PHP module also comes with event support for keyboard, mouse, and more. This feature allows you to add charts and graphs during any lifecycle phase of your app.

Does It Allow You To Create Interactive Charts?

Interactive charts make visualization even easier to understand, which in turn helps make informed decisions. With FusionCharts, you can create highly interactive charts that are responsive on all screen sizes. FusionCharts supports a number of user interactions:
  • Zooming (zoom in and out)
  • Panning
  • Dragable charts
  • Enable users to switch between values on a chart or graph
  • Add drill-downs to your charts
  • Show or hide data series in column charts and many more

Can You Create Live Charts With PHP Module For FusionCharts?

The PHP module for FusionCharts supports real-time updates or live charts. These charts update automatically after a specific interval to show real-time data.

Does It Allow You To Export Charts and Dashboards?

While most charting libraries don’t allow you to export full dashboards, FusionCharts PHP chart generator offers this feature. It allows you to export full live dashboards in different formats, such as PDF and image. You can also easily add any new elements to your exported dashboards, such as logos, colors, and tables.

Which Type of PHP Pie Charts Can I Create With FusionCharts?

Whether you want to create a financial pie chart or a 2D/3D pie chart, FusionCharts supports a wide range of pie charts:

Pie in 2D and 3D

A 2D or 3D pie chart is great for representing the share of constituents as part of the whole. A 3D pie chart essentially adds depth to a plain pie chart and makes it visually appealing.

Nested Pie in 2D

nested pie chart A nested 2D pie chart is a multi-level pie chart. You can you this PHP graph to represent symmetrical and asymmetrical tree structures.

Donut in 2D/3D

A donut 2D or 3D pie chart is great for depicting the share of constituents as part of a whole. However, it also allows users to slice out different components/sectors of the pie chart.

How Can I Create A Pie Chart In PHP and MySQL Using FusionCharts?

With FusionCharts, you can create a pie chart in PHP MySQL by using the data string method or data URL method. Here, we’ll create a sample pie chart for representing “Production by Factory” using the data string method. The database contains only two tables, namely “Factory_Master” for storing the name and ID of factories and “Factory_Output” for storing the number of units produced by factories for a specific date. Here are the main steps for creating a pie chart in PHP MySQL :
  1. First, we need to include the FusionCharts.js JavaScript class and FusionCharts.php. This will help embed charts easily.
  2. Next, include DBConn.php to connect to the database.
  3. Generate XML document and store it in a strXML variable.
  4. Render the Pie 3D Chart with data from $strXML.
Here is the sample code for creating the pie chart:
      //We've included ../Includes/FusionCharts.php and ../Includes/DBConn.php, which contains
      //functions to help us easily embed the charts and connect to a database.
        <TITLE>FusionCharts - Database Example</TITLE>
   //In this example, we show how to connect FusionCharts to a database.
   //For the sake of ease, we've used a MySQL database containing two
   //Connect to the DB
   $link = connectToDB();
   //$strXML will be used to store the entire XML document generated
   //Generate the chart element
     $strXML = "<chart caption='Factory Output report' subCaption='By Quantity' pieSliceDepth='30' showBorder='1' formatNumberScale='0' numberSuffix=' Units'>";
   //Fetch all factory records
     $strQuery = "select * from Factory_Master";
     $result = mysql_query($strQuery) or die(mysql_error());
   //Iterate through each factory
       if ($result) {
          while($ors = mysql_fetch_array($result)) {
          //Now create a second query to get details for this factory
           $strQuery = "select sum(Quantity) as TotOutput from Factory_Output where FactoryId=" . $ors['FactoryId'];
           $result2 = mysql_query($strQuery) or die(mysql_error()); 
           $ors2 = mysql_fetch_array($result2); 
          //Generate <set label='..' value='..'/>
           $strXML .= "<set label='" . $ors['FactoryName'] . "' value='" . $ors2['TotOutput'] . "' />";
          //free the resultset
           //Finally, close <chart> element
           $strXML .= "</chart>";
           //Create the chart - Pie 3D Chart with data from $strXML
           echo renderChart("../../FusionCharts/Pie3D.swf", "", $strXML, "FactorySum", 600, 300, false, true);
The above code will render the following chart: create a pie chart in PHP MySQL using FusionCharts If you want to learn how to create a PHP MySQL chart using the data URL method, check out this article.   Ready to create a pie chart in PHP MySQL with ease? Head over to FusionCharts and create more than 150 PHP charts!

Take your data visualization to a whole new level

From column to donut and radar to gantt, FusionCharts provides with over 100+ interactive charts & 2,000+ data-driven maps to make your dashboards and reports more insightful

Explore FusionCharts

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.