Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
 Discussion Forums
 HTML, XML, JavaScript...
 Software Reviews
 Editors,Others...
 Top100
 JavaScript Tutorials, ...
 Tutorials
 ASP, CSS, Databases...
 Discussion List
 FAQ, Roundup, Configure ...
 Authoring
 HTML, JavaScript, CSS...
 Design
 Layout, Navigation,...
 Graphics
 Tools, Colors, Images...
 Software
 Browsers, Editors, XML...
 Internet
 Domains, E-Commerce, ...
 WDVL Resources
  Intermdiate, Tutorials,...
 WDVL
 Discussion Lists, Top 100,...
 Technology Jobs


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Top 10 Articles
  1. Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
  2. JavaScript Tutorial for Programmers
  3. Design
  4. JavaScript Tutorial for Programmers - Objects
  5. JavaScript Tutorial for Programmers - JavaScript Grammar
  6. JavaScript Tutorial for Programmers - Versions of JavaScript
  7. Cascading Style Sheets
  8. JavaScript Tutorial for Programmers - Embedding JavaScript
  9. JavaScript Tutorial for Programmers - Functions
  10. Authoring JavaScript
Domain Name Lookup
Search to find the availability of a domain name. Just enter the complete domain name with extension (.com, .net, .edu)

Automating Image Manipulation with GD - Page 18

November 12, 2001

In the last article, we learned how to use the the GD library to draw borders, add text, and create thumbnails. In this article, we will learn how to create charts and graphs with the GD::Graph module.

Drawing Charts and Graphs with GD::Graph

Creating a chart or graph within a spreadsheet like Excel is simple enough, however, if the chart needs to be created on a daily basis from a data set that changes regularly, this can be a tedious repetitive task that should be automated. And let's say that the charts and graphs need to be accessible from the Web. Well, we just described a perfect scenario for using the GD::GRaph module to automate this type of task. With it, we can create several different types of graphs including line charts, bar charts, pie charts, point charts, and area charts. This can be useful for reporting on bits of data like system status, network traffic, Web server traffic and revenue reports. What the GD::GRaph doesn't do for you is grab and tabulate the information that will be used to create the chart. You have to do that yourself. You will need to create a multidimensional array whose first array contains the list of labels that will be used in the chart and whose subsequent arrays will contain the data to graph. For the majority of the charts you will likely generate, the first array will represent the X axis labels. In the case of a pie chart, the labels are the pie slice labels. Subsequent arrays contain the data that will be used in the chart. For example, if we wanted to report monthly revenues for January through September in a bar chart, we would need to create a data structure like the one below:

my @data = (
["Jan-01","Feb-01","Mar-01","Apr-01","May-01",
 "Jun-01","Jul-01","Aug-01","Sep-01"],
[21,25,33,39,49,48,40,45,15]
);

[The colored lines above are one line. They have been split for formatting purposes.]

The GD::Graph module has too many features to list in this article, however, in the examples below, you will discover that the primary method for controlling chart output is the set() method. The plot() method accepts a data structure like @data above and creates the chart image, which can then be saved to disk and otherwise manipulated with the GD library. We will learn how to create 5 different types of charts below using monthly sales information as the data set.

Draw a bar graph with GD::Graph::bars

The GD::Graph::bars class provides the capability for creating bar graphs like the one below. The first thing we need to do is create the data structure that will be used to draw the chart. On lines 4 through 7, we create an array that contains two anonymous arrays. The first anonymous array contains the X axis labels that will identify the values in the second anonymous array, which contains the online sales figures in thousands of dollars.

On line 9, we create a new instance of the GD::Graph::bars class and then set the chart parameters with the set() method on lines 11 through 17. The x_label parameter on line 12 sets the X axis label that describes the type of information that we will display in the bars. The y_label parameter on line 13 contains the description of the number values in the Y axis, which in this case, is revenue in thousands of dollars. The value of the title parameter on line 14 is centered and drawn across the top of the image. The bar_spacing parameter on line 15 is the number of pixels that should exist between the bars in the image.

Once we've set the chart parameters and created the proper data structure, we pass them to the plot() method on line 19, which actually creates the image based upon the previous settings. Then we can save the image as a JPEG on lines 21 and 22.

1  use strict;
2  use GD::Graph::bars;
3
4  my @data = (
5  ["Jan-01","Feb-01","Mar-01", "Apr-01","May-01",
 "Jun-01","Jul-01","Aug-01","Sep-01"],
6  [21,25,33,39,49,48,40,45,15]
7  	    );
8
9  my $graph = new GD::Graph::bars;
10
11  $graph->set(
12      x_label         => 'Month',
13      y_label         => 'Revenue ($1,000s)',
14      title           => 'Monthly Online Sales for 2001',
15      bar_spacing     => 10
16  )
17  or warn $graph->error;
18
19  $graph->plot(\@data) or die $graph->error;
20
21  open(GRAPH,">graph1.jpg") || die "Cannot open graph1.jpg: $!\n";
22  print GRAPH $graph->gd->jpeg(100);

[The colored lines above are one line. They have been split for formatting purposes.]

As you can see in the image above, our chart maps out monthly online sales to date for 2001 given the information in the @data array.

Creating Thumbnail Images - Page 17
Weaving Magic With Regular Expressions
Drawing A Comparative Bar Graph - Page 19


Up to => Home / Authoring / Languages / Perl / Weave




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers