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)

Drawing A Comparative Bar Graph - Page 19

November 12, 2001

Let's say that you've deployed the script above so that your sales manager can track online sales. That's great, but now the President of the company wants to be able to track online sales in comparison to catalog sales. "Of course" you say. With GD::Graph, adding an addition row is only a matter of adding a new array to our @data array on line 7 below. We also change the title on line 15 to reflect the contents of this new chart. We'll also need to create a color coded legend to identify which bar represents online sales and which represents catalog sales. The set_legend() method on line 22 assigns a title for the bar in the order that they occurred in the @data array. The first data array (the array after the labels array ) represents online sales, while the last array represents catalog sales.

We've also added two additional settings on lines 17 and 18. When the long_ticks parameter is set to a positive value, it draws a grid behind the graph to make it easier to match a value in the y axis with the height of the bar. The show_values parameters writes the value directly above each point or bar as can be seen in the image below.

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  	    [58,55,62,48,45,44,41,35,43]
8  	    );
9
10  my $graph = new GD::Graph::bars;
11
12  $graph->set(
13    x_label         => 'Month',
14    y_label         => 'Revenue ($1,000s)',
15    title           => 'Monthly Online vs. Catalog Sales for 2001',
16    bar_spacing     => 3,
17    long_ticks      => 1,
18    show_values     => 1,
19  )
20  or warn $graph->error;
21
22  $graph->set_legend( 'Online', 'Catalog' );
23
24  $graph->plot(\@data) or die $graph->error;
25
26  open(GRAPH,">graph2.jpg") || die "Cannot open graph2.jpg: $!\n";
27  print GRAPH $graph->gd->jpeg(100);

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

This graph provides the information that the President of the company was looking for. The online sales increased the first half of the year while catalog sales remained stagnant and even decreased a bit. This could have been a result of sagging sales or could represent a certain portion of the customers who have switched to online ordering over catalogs. It also shows a heavy drop-off in online sales in September.

Drawing a stacked bar graph

The sales manager and President have been impressed with your ability to develop dynamic these dynamic sales graphs for the company. Now that they know you have this skill, they would like you to create a new graph that represents combined online and catalog sales. We can do this with a stacked bar chart. The code for this graph looks almost identical to the previous example with the exception of line 18, the cumulate parameter, which stacks the bars one on top of another.

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  	    [58,55,62,48,45,44,41,35,43]
8  	    );
9
10  my $graph = new GD::Graph::bars;
11
12  $graph->set(
13      x_label         => 'Month',
14      y_label         => 'Revenue ($1,000s)',
15      title           => 'Monthly Revenue Totals for 2001',
16      bar_spacing     => 3,
17      long_ticks      => 1,
18      cumulate        => 1
19  )
20  or warn $graph->error;
21
22  $graph->set_legend( 'Online', 'Catalog' );
23
24  $graph->plot(\@data) or die $graph->error;
25
26  open(GRAPH,">graph3.jpg") || die "Cannot open graph3.jpg: $!\n";
27  print GRAPH $graph->gd->jpeg(100);

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

The stacked graph above shows us the total sales for each month, but also color codes the portion of each bar based upon the portion of the total that represents online or catalog sales.

Automating Image Manipulation with GD - Page 18
Weaving Magic With Regular Expressions
Drawing a line chart with GD::Graph::lines - Page 20


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