spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / charts To page 1current pageTo page 3To page 4To page 5To page 6To page 7
[previous] [next]

Drawing Charts with JavaScript

Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

Getting Started

Ok, most of you caught me. I actually used two images on the previous page. Two 1-pixel gifs that are 44 bytes each. We can use 20 different colors for our charts and still remain under 1k!

The trick to drawing charts involves a few steps:

Here's what the HTML for our chart might look like (if we were plotting only two students):

<table bgcolor="#cccccc" cellpadding=0 cellspacing=2 width=355><tr><td>
  <table bgcolor="#cccccc" cellpadding=0 cellspacing=2 width=355>
  <tr>
    <td>John</td>
    <td><img src="red.gif" width=250 height=10 hspace=2></td>
  </tr>
  <tr>
    <td>Mary</td>
    <td><img src="yellow.gif" width=270 height=10 hspace=2></td>
  </tr>
  </table>
</td></tr></table>

Notice that we have nested two tables. This is to prevent Netscape 4.x from showing lines between our table cells.

Note too, that we have set a width of 355 pixels for the table, even though, as you will see next, the plottable area of the chart is specified as 275 pixels wide. This is to accommodate the <td> that will hold the student's names. We'll also alternate bar colors for each row in our table.

At this point, let's look at the entire script to make the chart on the previous page. We'll put in plenty of comments to walk us through:

<script><!--

// Even though they are only 44 bytes, let's preload our images
imRed = new Image();
imYellow = new Image();
imRed.src = "red.gif";
imYellow.src = "yellow.gif";

strImage = "";    // used for alternating colors

strToWrite = "";  // We'll dynamically build a table to hold
                  // our chart, then write it to the screen when ready

// Store the student scores, and their names
arScores = new Array(90,100,78,86,52,99,99,100);
arStudents = new Array("Alan", "Bob", "Joe", "Mary", "Dunce", "Sally", "Allison", "Jacob");

chartWidth = 275; // the width of the chart
maxWidth = 0;

// now find the maximum value to chart
for(i = 0; i < arScores.length; i++) {
 if(arScores[i] > maxWidth)
    maxWidth = arScores[i];
}

// start building the output string
strToWrite += "<table bgcolor='#cccccc' width=" + (chartWidth+80) + "><tr><td>";
strToWrite += "<table bgcolor='#cccccc' width=" + (chartWidth+80) + "><tr><td>";

// continue building the HTML to output
// by looping through our arrays
for(i = 0; i < arScores.length; i++) {
  if(i % 2 == 0)  // alternate images by using the modulus operator
     strImage = "red.gif";
  else
     strImage = "yellow.gif";
 
  strToWrite += "<tr><td align=right>" + arStudents[i] + "</td>"
  strToWrite += "<td><img src='" + strImage + "' width=" + parseInt((arScores[i] / maxWidth) * chartWidth) + " height=10 hspace=2>" + arScores[i] + "</td></tr>";
}

strToWrite += "</table>";
strToWrite += "</td></tr></table>";
// -->
</script>

I've bolded the formula for drawing bar charts above. This formula, in any language, is what makes drawing this type of chart possible.

Now, wherever we want the chart to appear in our page, we can simply write out the value strToWrite. This will work in Version 3 browsers and up.

<html>
<body>

<script><!--
  document.write(strToWrite);
// -->
</script>

</body>
</html>

Got it? Good. Let's look at a vertical bar chart and then start getting interactive.


home / programming / javascript / charts To page 1current pageTo page 3To page 4To page 5To page 6To page 7
[previous] [next]

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Crucial Triples Up With New Three-Channel DDR3 Kits · Meet the Finalists: Excellence in Technology Awards · Tealeaf Offers Insight to Mobile Customer Behavior

Created: March 6, 2002
Revised: March 6, 2002

URL: http://webreference.com/programming/javascript/charts/2.html