Tutorials : Poor Man's Graphics :
Contents
Introduction
Horizontal and Vertical Bars
Using Javabeans to Produce the Bars
Scaling Your Chart

Scaling Your Chart

One extra feature that you would almost certainly need to have is the ability to "scale" the bars. Say that the numbers for NBC, CBS, and CNN were 3000, 2000, and 3200, then you could not directly use these numbers as the number of pixels for the bar lengths. What you would have to define is how much one pixel should represent. For the latter numbers you could define the scale like this: 1 pixel equals 10 of your units. Thereby the value 3000 would become 300 pixels. A new bean-method could handle this:

public void setScale(float scale) 

In the example above we would use it like this:

<%=graph.setScale(10)%>

Note that the scale is of type "float", which allows us to specify a scale like "0.1F", meaning that one of our units equals 10 pixels.

Adding Other Features

It's very easy to add some simple effects to the bars by utilizing well-known HTML-features:

  • a "tool-tip" can be added to a bar by using the title-option on the img-tag. This could be used to give further information on the poll results.
  • mouseover/mouseout effects can be added to the bars. This allows you to call JavaScript functions which could be used for other visual effects.

To support older Explorer and Netscape versions you would have to make a hyperlink around the bar-image and then apply the title-option and mouse-events to this hyperlink.

Improving the Bar Charts Further

In the next article I'll show how to make the charts more professional, e.g. by adding an axis, and also how to facilitate using them by making some high-level methods that completely hide the table structures.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.