September 14, 2014 1:33 pm

How to Create charts with PHP

Charts give a pictorial representation of information. Using PHP we can create beautiful charts to provide the user with graphical representation of the data. In this article you will see how we can create bar, line and pie charts using PHP pcharts library. Pchart is a php class oriented framework designed to create charts directly from the web server.

Create charts with PHP

What is pchart library?

Pchart is a php class oriented framework designed to create charts directly from the web server. It requires GD library so make sure your server supports it. Using pcharts you can draw various kinds of charts like bar, line, area, pie, scatter, split, stacked. It has also feature to draw barcodes. Cool isn’t it.

Steps to install pcharts in your server

Installation of pcharts is very simple. Go to its download page to download the latest version, currently 2.1.4. After the download extract the zip/tar/tar.gz file in your local/online server. The file contains the class files and various examples to get you started. Create a folder with any name to store the chart images. I have used charts_images folder to store temporarily chart images.

Creating a bar chart

The first step to create a bar chart is to include the libraries.

Then you have to create an object of the pdata class. You can add multiple data for comparison or single data based on your requirements.

Drawing the chart and displaying on browser

Creating a pie chart

To create a pie chart use the code below, it is very similar like bar charts with minor changes. You can create 2d as well as 3d pie charts. You have to call different methods for using them.

Creating a bar code

To create a barcode see the code below. It’s very simple all you have to do is include the required class file and then create an object. Set the string that needs to be coded and finally display the barcode.

Check the live demo for working sample. More example to create line charts, area charts other different kind of charts are included in the source code.

I hope this article helps you creating charts, Feel free to give your comment below.

Author Shahrukh Khan

Shahrukh Khan is a software engineer, blogger and a web lover. He loves to develop custom web solution and in between his free time blogs at thesoftwareguy.in. When he is not infront of the his computer (jenny) loves to hang around with friends. You can follow him on facebook, twitter or googleplus.


Tutorial Categories: