How to Draw Charts in PHP

Written by mark stansberry
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Draw Charts in PHP
Create 3D pie charts with PHP's graphic library. (Ryan McVay/Photodisc/Getty Images)

Draw colourful 2D and 3D charts with the open source PHP scripting language. Code pie charts, line charts, scatter charts and bar charts with PHP's GD library of graphic design functions. Use fonts from PHP's font library to add text for professional online business and technical presentations. Create your first 3D pie chart with PHP in just a few minutes and a few lines of code. With the knowledge you gain, you will be able to not only create more types of PHP charts, but also be able to confidently use and edit other free PHP chart-making scripts.

Skill level:
Moderate

Other People Are Reading

Things you need

  • Web hosting service that supports PHP scripting

Show MoreHide

Instructions

  1. 1

    Create a text file with your text editor to create the file to store your PHP 3D chart-making script. . Save the file with a PHP file extension as "3DpieChart.php."

  2. 2

    On the first line in your text file type the code below to indicate to the Web browser that the file contains a PHP script.

    <?php

  3. 3

    Type on the next line of the text editor the PHP function to create a drawing area that has a width of 200 pixels and height of 200 pixels and store it a PHP variable with a name of $image.

    $image = ImageCreate(200, 200);

  4. 4

    Type the code to define variables for the colours you will use in the PHP script. Code the first colour to be white and to be used as the colour fill for the drawing area, $image. Specify the other colours to be used in the arguments of the ImageColorAllocate function using the RGB (Red Green Blue) format. Make colours you allocate darker with lower RGB numbers and brighter with higher RGB numbers. Don't use negative numbers, decimal numbers or numbers greater than 255 for the arguments of this function.

    $white = ImageColorAllocate($image, 255, 255, 255);

    $grey = ImageColorAllocate($image, 200, 200, 200);

    $darkgray = ImageColorAllocate($image, 150, 150, 150);

    $blue = ImageColorAllocate($image, 0, 0, 250);

    $darkblue = ImageColorAllocate($image, 0, 0, 150);

    $red = ImageColorAllocate($image, 255, 0, 0);

    $darkred = ImageColorAllocate($image, 200, 0, 0);

  5. 5

    Type the code to consecutively draw 20 arc curves such that each new arc will have a centre that is 1 pixel higher than the previous arc. Specify the arc x centre coordinate, y centre coordinate, measure, starting angle, ending angle and colour of each section of the pie chart in the arguments separate ImageArc functions.

    for ($I = 120; $I > 100; $I--) {

    ImageArc($image, 100, $I, 100, 50, 0, 60, $darkblue);

    ImageArc($image, 100, $I, 100, 50, 60, 90 , $darkgray);

    ImageArc($image, 100, $I, 100, 50, 90, 360 , $darkred);

    }

  6. 6

    Type the code to draw three colour filled arcs that have a centre at the Web page coordinates of (100,100) and have the same arc measures and centre location as the last arc curve drawn with the ImageArc code (100 pixels) to create the 2D pie chart.

    ImageFilledArc($image, 100, 100, 100, 50, 0, 60, $blue, IMG_ARC_PIE);

    ImageFilledArc($image, 100, 100, 100, 50, 60, 90 , $grey, IMG_ARC_PIE);

    ImageFilledArc($image, 100, 100, 100, 50, 90, 360 , $red, IMG_ARC_PIE);

  7. 7

    Type the code to render the 3D pie chart as a PNG graphic file, clear the memory used to create the image and end the PHP script.

    header ("Content-type: image/png");

    ImagePNG($image);

    ImageDestroy($image);

    ?>

  8. 8

    Copy and paste the code into your text editor to ensure that the 3D pie chart program has no syntax errors and runs correctly.

    <?php

    $image = ImageCreate(200, 200);

    $white = ImageColorAllocate($image, 255, 255, 255);

    $grey = ImageColorAllocate($image, 200, 200, 200);

    $darkgray = ImageColorAllocate($image, 150, 150, 150);

    $blue = ImageColorAllocate($image, 0, 0, 250);

    $darkblue = ImageColorAllocate($image, 0, 0, 150);

    $red = ImageColorAllocate($image, 255, 0, 0);

    $darkred = ImageColorAllocate($image, 200, 0, 0);

    for ($I = 120; $I > 100; $I--) {

    ImageArc($image, 100, $I, 100, 50, 0, 60, $darkblue);

    ImageArc($image, 100, $I, 100, 50, 60, 90 , $darkgray);

    ImageArc($image, 100, $I, 100, 50, 90, 360 , $darkred);

    }

    ImageFilledArc($image, 100, 100, 100, 50, 0, 60, $blue, IMG_ARC_PIE);

    ImageFilledArc($image, 100, 100, 100, 50, 60, 90 , $grey, IMG_ARC_PIE);

    ImageFilledArc($image, 100, 100, 100, 50, 90, 360 , $red, IMG_ARC_PIE);

    header ("Content-type: image/png");

    ImagePNG($image);

    ImageDestroy($image);

    ?>

  9. 9

    Save the PHP file and upload it to your Web server. Paste the URL where the PHP file is located in your browser to view the 3D pie chart.

Tips and warnings

  • Temporary problems on the server may keep your code from rendering. To detect if your code is not rendering correctly because of a server problem you have to include error handlers. Other problems could be a result of your Web server not supporting the PHP GD graphic library extensions or is using a version of PHP that does not support the PHP functions you are using.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.