PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 232: How to draw a simple bar graph using GD2?
Written by kksou   
Tuesday, 15 May 2007
Problem

You would like to draw a simple bar graph from a set of data as shown below:

How to draw a simple bar graph using GD2?


Solution

We can draw a simple bar graph using the GD2 library. For those of you who are not familiar with GD2, please refer to the PHP documentation at: http://www.php.net/gd

To run the following example, make sure you php-gtk2 is compiled with GD2 library.

Note: The Gnope Installer version DOES NOT support GD2. Please download the latest php-gtk2 binary from the official PHP-GTK site as outlined in the article How to install php gtk2 on windows?


Sample Code
1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
40   
41   
42   
43   
<?php
$window = new GtkWindow();
$window->set_title($argv[0]);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Draw a simple Bar Graph");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$title->set_justify(Gtk::JUSTIFY_CENTER);
$alignment = new GtkAlignment(0.5, 0, 0, 0);
$alignment->add($title);
$vbox->pack_start($alignment, 0, 0);
$vbox->pack_start(new GtkLabel(), 0, 0);

draw_bargraph($vbox); // note 1

$window->show_all();
Gtk::main();

function draw_bargraph($vbox) {
    $data = array('120','160','300','240');
    $im = imagecreate(360, 200);

    $white = imagecolorallocate($im,255,255,255);
    $black = imagecolorallocate($im,0,0,0);
    $blue = imagecolorallocate($im,0,0,255);
    //
    // create background box
    //imagerectangle($im, 1, 1, 319, 239, $black);
    //draw X, Y Co-Ordinate
    imageline($im, 10, 5, 10, 180, $black );
    imageline($im, 10, 180, 340, 180, $black );
    imagestring($im,6,100,25,"Simple Bar Graph", $black);

    // what next draw the bars
    $x = 30;
    $y = 180;
    $w = 40;
    // get into some meat now, cheese for vegetarians;
    for ($i=0;$i<count($data);$i++){
  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.
Explanation
  1. Draw the bar graph using GD2.
  2. Load the graph into GdkPixbuf.
  3. Create a new GtkImage from the pixbuf.
  4. Resize the GtkImage so that the entire bar graph is displayed.

Related Links
 

Add comment


Security code
Refresh

< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Links - Classes - Social Business - BPM - Web - General
Copyright © 2006-2013. kksou.com. All Rights Reserved