iNET Interactive - Online Advertising Agency
          
   Home    Authors    About    Login    Contact Us
   Search:   
Advanced Search     
  Articles

  ASP (26)
  ASP.NET (19)
  C and C++ (4)
  CFML (2)
  CGI and Perl (16)
  Flash (2)
  Java (7)
  JavaScript (28)
  PHP (92)
  MySQL (13)
  MSSQL (3)
  HTML (34)
  SEO (9)
  Visual Basic (12)
  CSS (13)
  SSI (5)
  XML (12)
  C# (14)

  Developer News

May 15, 2008
Reader Question - Would you host your client's work on your website?
About
 
May 15, 2008
How to Create an Ajax Autocomplete Text Field: Part 6
WebReference.com
 
May 14, 2008
Poll: Are the browser safe colors still needed?
About
 
May 14, 2008
Google Doctype launched
About
 
May 14, 2008
Web Editor Reviews - 6 New Reviews
About
 
May 14, 2008
Build Beautiful Buttons in Photoshop, Part I
SitePoint
 
Courtesy of moreover.com
 
Want to receive new articles via e-mail? Click here!
/Home /CGI and Perl

Creating a pie-chart using GD::Graph 

  Views:    12528
  Votes:    3
by Temzupin de Rabzentalf 12/16/03 Rating: 

Synopsis:

Nowadays, to meet the needs of whimsial users practically every Internet resource is based on dynamically displayed content. Today we will examine only one of these possibilities, in particular — realization of the pie-chart.
Pages: 
The Article

Nowadays, to meet the needs of whimsial users practically every Internet resource is based on dynamically displayed content. Today we will examine only one of these possibilities, in particular — realization of the pie-chart.

Perl offers a vast toolkit to create, modify and manage content of a web-site. Creation of a pie-chart is available to everyone using Perl module GD::Graph.

For example, your boss wants to get a comparative graph, which displays dynamically online sales, conventional shop sales and sales by distributors. The graph should be based on CSV-file (Comma Separated Values), which is exported weekly from Microsoft Excel and is allocated in a separate folder in LAN. So we need to make a script, which will automatically generate pie-chart based on available CSV-data.

Listing 1

1  use strict;
2  use GD::Graph::pie;
3  use Text::CSV_XS;
4
5  my @data;
6
7  my $csv = new Text::CSV_XS;
8  open(FILE,"excel.csv") || die "Cannot open excel.csv: $!\n";
9  while (my $line = <FILE>) {
10      $csv->parse($line);
11      my @col = $csv->fields;
12      push(@data,\@col);
13  }
14
15  my $graph = new GD::Graph::pie(300, 300);
16
17  $graph->set(
18      title  =>  'Pie-chart Caption',
19      label  =>  'Comments',
20      axislabelclr  =>  'black',
21      '3d'  =>  1,
22      start_angle  =>  90,
23      suppress_angle  =>  5,
24  )
25  or warn $graph->error;
26
27  $graph->set_title_font("/usr/share/fonts/ttf/windows/times.ttf",18);
28  $graph->set_value_font("/usr/share/fonts/ttf/windows/times.ttf",12);
29  $graph->set_label_font("/usr/share/fonts/ttf/windows/times.ttf",14);
30
31  $graph->plot(\@data) or die $graph->error;
32
33  open(GRAPH,">graph.jpg") || die "Cannot open graph.jpg: $!\n";
34  print GRAPH $graph->gd->jpeg(100); 

In line 3 of the listing we load module Text::CSV_XS, which can be downloaded for free from CPAN (Comprehensive Perl Archive Network, www.cpan.org). In line 7 we creat a new object of the class Text::CSV_XS, open CSV-file in line 8 and load data from the file to array @data (lines 9-13). Line 10 calls method parse(), which splits the strings from the file to a columns. The columns are returned by method fields() in line 11 and appended to the array @data as a row (line 12). In other words, instead of organizing array @data manually, we can create it from a file (in our case — CSV).
Next (line 15), we’ll create an object for a chart of neccessary sort (in this case — pie) with given dimensions of the image.
Third parameter in line 21 is equal 1, which means 3D pie-chart (default value “0” makes chart 2D). Then we need to determine starting point of the chart, from which the script begins to count the sectors of the graph. The value is specified in degrees and is equal “0” by default (we set it “90”, line 22). We also can assign a value to the parameter suppress_angle, which determines minimum size of the sector in degrees.
You can use TrueType fonts instead of built-in, if you want. In lines 27-29 the names of the fonts used for caption are given, sectors and comments to graph, accordingly. Numbers after them mean font size.
In line 33 the image of the pie-chart is output to a file in JPEG (or PNG) format. In the next line the parameter of the method jpeg() assigns the quality level of the image (in percentage).
In our case the image of the chart is brought to a file, but we always can put it to STDOUT. This can be useful for creation of charts of dynamically changed data “on-the-fly”. For that case we need to change the code of the script slightly.

Listing 2

32  print "Content-type: image/jpeg\n\n";
33  binmode(STDOUT);
34  print $graph->gd->jpeg(100);

To display pie-chart on a web-page, you need to insert following construction in desired location of your HTML-document:

Listing 3

<img src="/cgi-bin/graph.cgi" width="300" height="300" border="0" alt="Chart">

Pages: 



 
  Sponsors