Click here to Skip to main content
Click here to Skip to main content
Go to top

JavaScript Chart

, 2 May 2007
Rate this:
Please Sign up or sign in to vote.
Add a stylish chart to your Web page using only client-side JavaScript.
Screenshot - jschart.gif

Introduction

Wouldn't it be nice to be able to add a stylish and colorful chart to your Web page with minimal effort and still avoid slow-loading Java applets or server side generated images? Then the JavaScript Chart is just what you need!

The JavaScript Chart supports:

  • Several charts on the same page
  • No limit on the number of data items in one chart (as long as you have enough space)
  • Pass the chart data as a string in the following format: Item A;1;Item B;2;Item C;3.
  • Gridlines are automatically created and spaced with appropriate increments depending on the highest value in the data range
  • Choose if item values should be displayed or not on each individual chart
  • Choose if the chart legend should be displayed or not
  • Each data item will automatically get a different matching color
  • Each chart can be rendered in debug mode, showing some useful information if the chart is not displayed as expected
  • Modification of some of the layout parameters with CSS

This small utility will simply take a semi-colon delimited data string (the delimiter can be changed) and generate a chart for you. You can add several charts to the same page and there is no limit to how many data items you can have in one chart, just as long as you have enough space in your page.

This code is by no means perfect and I'm sure there is lots of room for improvements, but hopefully you will find it useful.

Background

I was searching around to find a client-side JavaScript chart without much success, so I decided to write my own. Then, when coming here to The Code Project to upload my solution, I stumbled across the excellent article "Cool Graph Object to Plot Column and Line Graphs in your Web Pages." by Wagner DosAnjos. Ah, well... Even though the functionality is very similar, I guess there are also some differences, so I'll post my solution here anyway.

Using the Code

Include the JavaScript file in your Web page HEAD section:

<script language="javascript" type="text/javascript" src="jschart.js"></script>

Create a DIV element in your Web page and give it an ID attribute. This will be the container for your chart.

If you do not specify any width or height attributes, the following default values will be used: Width: 100%, Height: 300px. You can override those default values with your DIV element and you can also position the element if you'd like.

<div id="myChart" style="width:60%;"></div>

Now we need to create the actual chart object and assign it to the container. In the sample, I created a small function that is called from the onLoad-event.

Imagine that the chart data string is generated from a database SQL query, a Web Service, an Ajax call to some datasource, or whatever you decide.

<script language="javascript" type="text/javascript">
    
    // This is the chart data as a semi-colon delimited string.
    var chartData = 
        "Windows;150;Linux;370;HP-UX;600;VMS;211;Unix;544;NT;111;Solaris;480;AIX;96";

    function initCharts() { 
        var chart = Chart;
        // Initialize the chart with the id of the container DIV element.
        chart.init("myChart"); 
        chart.title = "Column chart" // Set the chart title.
        chart.type = "column"; // Specify the type of chart - "column" or "bar".
        chart.debug = false; // Debug. False if omitted.
        chart.showLegend = true; // Legend. True if omitted.
        chart.showValues = true; // Data item value labels. True if omitted.
        chart.setData(chartData); // Set the chart data.
        chart.render();
    }
</script>

Now add the function to the BODY onLoad-event.

<body onload="initChart()">

Refer to the file jschart_demo.html (available for download with this article) for a complete sample.

History

  • 1.1, 2007-May-01: Handle errors for incorrect data and more debug information
  • 1.0, 2007-Apr-01: Article created

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Magnus Lundahl
Web Developer Know IT Stockholm
Europe Europe
Software developer @Know IT Stockholm:
Java/JEE/JMS/JSF
JavaScript

Comments and Discussions

 
GeneralMy vote of 5 PinmemberReza Samadabadi10-Jul-11 20:46 
GeneralMy vote of 5 PinmemberVixanna27-Oct-10 11:51 
GeneralLine chart PinmemberClayton Bonelli3-Sep-09 2:34 
GeneralRe: Line chart PinmemberMagnus Lundahl3-Sep-09 9:17 
Generalvery good Pinmemberjingjing24-Nov-08 0:34 
GeneralReading Chart Data from a .CSV FIle: Client-Side Chart PinmemberM TURNER27-Feb-08 14:11 
I need to plot an (on-the-fly) simple time-series line chart of a particular data series held in a simple .CSV file. Can your .JS code be adapted to read the chart data from this datafile rather than from the "hard-coded" "chartData" variable in the HTML code
 
Thanks
MT
GeneralRe: Reading Chart Data from a .CSV FIle: Client-Side Chart PinmemberMagnus Lundahl14-Mar-08 22:21 
Generalreally good and simple chart script Pinmemberwizera21-Feb-08 8:40 
GeneralRe: really good and simple chart script PinmemberMagnus Lundahl14-Mar-08 22:40 
GeneralThe JS chart appears different in Safari Pinmembervictorbos16-Aug-07 4:29 
GeneralRe: The JS chart appears different in Safari PinmemberMagnus Lundahl16-Aug-07 21:51 
GeneralThanks Pinmembervictorbos14-Aug-07 12:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 2 May 2007
Article Copyright 2007 by Magnus Lundahl
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid