HTML5 Zone is brought to you in partnership with:

My name is Jean-Baptiste Jung and I’m a 29 years old web developer and professional blogger. I was born and raised in Paris, France and I now live in Belgium with my wife and our adorable cat. I first used the internet in 1998, built my first website in 2001 and finally started to work as a professional web developer in 2005. In 2010, I left my job and created my own web development studio. jb is a DZone MVB and is not an employee of DZone and has posted 55 posts at DZone. You can read more from them at their website. View Full User Profile

How to easily create charts using jQuery and HTML5

10.04.2011
| 13230 views |
  • submit to reddit

For years, Flash was the only solution to display a dynamic chart on a website. But thanks to modern techniques, the dying Flash isn’t needed anymore. In this tutorial, I’ll show you how easy it is to transform a basic HTML table into a profesionnal looking chart using visualize.js, a very useful jQuery plugin.

Step 1: Preparing files

Here we go! The first thing to do is obviously to create a directory on your Mac (or PC, nobody’s perfect ;) ). You should name it chart for example. Once done, download the required Javascript and CSS files and save it on your hard drive.

Now, create a new html document, named charts.html. Copy the html structure below, and paste it into your newly created charts.html file.

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
	<title>Charts!</title>       

</head>

<body>

</body>
</html>

Did you notice that I used the <!DOCTYPE html> doctype? It is the right doctype to use, as the javscript code we’ll add later will turn a html table into a HTML 5 canvas.


Step 2: Adding data

Now that we downloaded the javascript files and created an html document, it’s time to add data. Copy the code below and paste it within the <body> and </body> tags of your charts.html file.

<table>
	<caption>Visits from August 16 to August 21</caption>
	<thead>
		<tr>
			<td></td>
			<th scope="col">Monday</th>
			<th scope="col">Tuesday</th>
			<th scope="col">Wednesday</th>
			<th scope="col">Thursday</th>
			<th scope="col">Friday</th>
			<th scope="col">Saturday</th>
			<th scope="col">Sunday</th>
		</tr>
	</thead>
	<tbody>

		<tr>
			<th scope="row">CatsWhoCode.com</th>
			<td>12541</td>
			<td>11204</td>
			<td>11354</td>
			<td>10058</td>
			<td>9871</td>
			<td>8254</td>
			<td>5477</td>
		</tr>
		<tr>
			<th scope="row">WpRecipes.com</th>
			<td>9855</td>
			<td>8870</td>
			<td>8731</td>
			<td>7488</td>
			<td>8159</td>
			<td>6547</td>
			<td>4512</td>
		</tr>
		<tr>
			<th scope="row">CatsWhoBlog.com</th>
			<td>3241</td>
			<td>2544</td>
			<td>2597</td>
			<td>3108</td>
			<td>2114</td>
			<td>2045</td>
			<td>950</td>
		</tr>
	</tbody>
</table>

Of course, feel free to add your own data to make the example more interesting for you.

Step 3: Let the magic happen

Alright, now we have a bunch of downloaded files and an html document. It’s time to merge them all together and finally generate the chart.
Unzip the downloaded file and open the extracted directory. Copy the following files into your chart directory.

  • charting/css/basic.css
  • charting/css/visualize.css
  • charting/css/visualize-light.css
  • charting/js/visualize.js

Once done, we obviously have to link the css and javascript files to our document. Paste the following after the <title> tag of the document:

<link href="basic.css" type="text/css" rel="stylesheet" />
<link href="visualize.css" type="text/css" rel="stylesheet" />
<link href="visualize-light.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="visualize.js"></script>

It’s time to give life to our chart. Paste the final piece of code after the script calls:

<script type="text/javascript">
	$(function(){
		$('table').visualize();
	});
</script>

Once you saved the file, your HTML table should be displayed along with a good looking chart. If you don’t want the table to be visible, simply hide it using the display:none css property.

Additional explanations

Generating bar charts is definitely great, but what if your preference goes to a pie chart? No problem, visualize.js allows 4 different type of charts: Bar, area, pie and line.

Changing the default type is pretty easy: Just add the type parameter as shown below:

$('table').visualize({type: 'pie'});

Cool, isn’t it? Visualize.js accepts even more parameters to make sure your chart will look exactly how you want. Here are the parameters which can be used:

  • type: string. Accepts ‘bar’, ‘area’, ‘pie’, ‘line’. Default: ‘bar’.
  • width: number. Width of chart. Defaults to table width
  • height: number. Height of chart. Defaults to table height
  • appendTitle: boolean. Add title to chart. Default: true.
  • title: string. Title for chart. Defaults to text of table’s Caption element.
  • appendKey: boolean. Adds the color key to the chart. Default: true.
  • colors: array. Array items are hex values, used in order of appearance. Default: ['#be1e2d','#666699','#92d5ea','#ee8310','#8d10ee','#5a3b16','#26a4ed','#f45a90','#e9e744']
  • textColors: array. Array items are hex values. Each item corresponds with colors array. null/undefined items will fall back to CSS text color. Default: [].
  • parseDirection: string. Direction to parse the table data. Accepts ‘x’ and ‘y’. Default: ‘x’.
  • pieMargin: number. Space around outer circle of Pie chart. Default: 20.
  • pieLabelPos: string. Position of text labels in Pie chart. Accepts ‘inside’ and ‘outside’. Default: ‘inside’.
  • lineWeight: number. Stroke weight for lines in line and area charts. Default: 4.
  • barGroupMargin: number. Space around each group of bars in a bar chart. Default: 10.
  • barMargin: number. Creates space around bars in bar chart (added to both sides of each bar). Default: 1

That’s all for today. Have fun with the charts :)

References
Published at DZone with permission of jb j, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Nabeel Manara replied on Fri, 2012/01/27 - 10:55am

HTML file is very easy to use, I got some reviews on the new tags and I think I can write code with about 50% less typing. It is extremely easy especially for embedding videos and great a proper styles and proper text formating.

Xi Huang replied on Mon, 2014/07/28 - 2:18pm

I followed your codes, and it is working as I wanted. However, it generates all 4 charts pie bar and all. However, as my data only makes sense with a line chart(as in is displaying a trend) how do I remove the other three charts? 

I tried to enter

<script type="text/javascript">$(function(){$('table').visualize({type: 'line'});});</script>


the following codes however it does not seem to display that chart first, or it only. Please guide me if possible.


A Student here by the way.


Thank you.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.