Click here to Skip to main content
15,071,729 members
Articles / Web Development / HTML
Posted 5 Oct 2006


72 bookmarked

HTML Vertical Bar Chart

Rate me:
Please Sign up or sign in to vote.
3.66/5 (32 votes)
3 Jul 20076 min read
HTML Vertical Bar Chart generated using HTML Table, DIVs and Javascript. This mechanism can be clubbed with any web development tool like ASP, ASP.NET, JSP etc. to generate effective charts.


Recently, I came across a requirement of displaying colorful bar charts in our web application. Since I am using ASP.NET 1.1, a lot of wonderful ideas came to me, such as using MS Office web office components, using third party controls to generate charts, finding some great ways of using System.Drawing things to display charts, etc. One of the approaches that struck me was using just plain HTML, a bit of DHTML and JavaScript to do the same thing. At first glance, it could seem to be a horrifying idea, but working with basics to achieving big requirements is never bad. Let me keep it very short and simple and see how this can be achieved.

Inside look at the script

Here we are going to look at the single and double vertical bar charts, which look somewhat like this:

Image 1

Single vertical bar chart

Let me explain in a very simple way how this colorful chart is generated. This bar chart is generated using an HTML table and <DIV>s. It is basically a set of <DIV>s smartly placed inside the <TD>s of HTML table <TR>s. This chart table needs only two rows: one to contain <DIV>s that represent values and another one to show X-axis labels. Let us cook this chart up step by step.

  1. Take an HTML table.
  2. Determine how many columns you need in the table by determining the number of items you want to have on the chart. Let us say you got colNum as the number of columns you need. In the example attached, it is 24.
  3. Take two rows in that table.
  4. In the first row, add the colNum number of <TD>s having <DIV>s with appropriate width and height.
  5. In the second row, add the colNum number of <TD>s having <DIV>s with labels for the X-axis.

This generates the skeleton for your chart. Now to represent the values on the chart, we need to place <DIV>s inside each <TD> of the first row (Step 4) , with the appropriate height and width. Say tdwd is the width of each bar and h is the height that you would need for this bar. Then it would be:

    <div style='background-color:blue; width:" + (tdwd-5) + 
        "; height:" + h + ";' />

Each of us can have our own logic for determining the tdwd and h. For instance, if we have a fixed area for the chart then we can determine the tdwd by dividing the chart width by number of columns we want. The one that is used here is:

tdwd = parseInt((chartwidth-(HorX.length*4))/HorX.length);
h = parseInt(VerY[i] / (vmax / 200));

Have a look at the attached code to understand it completely.

Double vertical bar chart

Now it won't be too hard to understand how to generate a double bar chart. In the same <TD> of Step 4, rather than adding one <DIV> we need to place two <DIV>s. The height of the second <DIV> should be guided by the second array of information. That's it. To better format the stuff, you can actually add another table inside the <TD> with one row and two columns, each column having a <DIV> representing the value. Let us see how the HTML code would look:

    <table cellpadding=0 cellspacing=0 border=0 
        width=" + (tdwd*2/3) + ">
            <td align=center valign=bottom width=50%>
                <font face=arial size='-2'> + VerY[i] + 
                <div style='background-color:blue; height:" + h + 
                    "px; writing-mode:tb-rl;'></div>    
             <td align=center valign=bottom width=50%>
                 <font face=arial size='-2'> + VerY2[i] + </font>
                 <div style='background-color:green; height:" + h2 + 
                     "px; writing-mode:tb-rl;'></div>

Here, h2 = parseInt(VerY2[i] / (vmax / 200)); can be used to determine the height of the second <DIV> and <font face=arial size='-2'> + VerY[i] + </font> writes the value of the bar above the bar.

Printing the charts

There is an Internet Explorer setting that determines whether or not to allow printing of the background color of <DIV>s and background Images. By default, IE settings do not allow the printing of background colors. To change this setting in IE 6, go to the Tools menu of IE -> Select "Internet Options..." -> Select the "Advanced" tab. Now scroll down to the "Printing" section and check the checkbox for "Print Background Color and Images." We must have the equivalent for them in IE 7 and other good browsers.

IE stores and picks this information from the registry. So, if you want to enable the printing of background colors and images with the script, add the following code in the HTML part of the landing page:

<script language="vbscript">
    on error resume next
    'Change IE Print Settings to 
    'Print Background Color and Images
    Set WSHShell = CreateObject("WScript.Shell")
    WSHShell.RegWrite "HKCU\Software\Microsoft\" + 
    "Internet Explorer\Main\Print_Background", "yes"
    Set WSHShell = Nothing


Charts developed in such ways using basic HTML can be used very effectively by clubbing them with web development tools and technology like ASP, ASP.NET, JSP, etc. All that's needed is an array of values to be displayed in the graphical manner. This works with Netscape as well, so scope is further increased automatically.

Version history

Version 1.1

Fixed issue in demo: Chart not getting displayed in Netscape browser when user selects "Show Chart in same Window."

Version 1.2

Feature included: Y-axis scale can now be displayed, guided by user settings. Presently, the following settings are made in the demo, which would display the Y-axis scale as shown in the 3rd chart in the figure above:

var chartHeight = 500;
var chartYAxisInterval = 100; 
var displayYAxisScale = true;

Version 1.3

Added demo: Added a sample demo to show how HTML vertical bar charts can be generated very easily in ASP.NET, with DataTable being the data source. The complete solution can be downloaded here. Key notes for Version 1.3: ASP.NET Version 1.1 is used in the demo and can be used in similar ways with ASP.NET 2.0.

The texts of the X-axis labels are now displayed in the vertical way to avoid issues with the alignments when the X-axis label text is too long. This works fine with IE 6, SP 2 on the Windows XP platform and also with Netscape 8.04 if the options configured properly. To make this work in Netscape 8.04, set the rendering engine to IE by going to Menu -> Tools-> Options-> Site Controls -> Site List Tab. The writing-mode property is presently supported by IE only. With this setting done, when Netscape comes across any property which it does not support, it goes to the IE rendering engine and checks whether it is supported by IE. If IE supports it, Netscape uses its feature and renders it as per the IE rendering engine.

For User convenience, the HTML demo contains the script with X-axis horizontal text. The ASP.NET demo contains the script with X-axis vertical text.

Version 1.4

Included support for X-axis and Y-axis labels on demand in the base demo. Also, limited the number of columns in the demo from 24 to 12 for better understanding of it.


Basic building blocks of HTML/DHTML with coating can create wonders when used smartly. User experience is best guided by UI. This is just one of the HTML charts that I am putting across; there are more to come. If interested, click here to view all my published articles.


  • 5 October, 2006 -- Original version posted
  • 2 January, 2007 -- Article moved
  • 19 March, 2007 -- Updated
  • 3 July, 2007 -- Updated


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Neeraj Saluja
Web Developer
United States United States
.NET Professional, working with a leading global firm.

Primarily works in .NET using C# with Oracle and MS SQL Server 2000 as backend.

Learning .Net ...

[ My Linked In Profile ^ ]

Comments and Discussions

GeneralNice article! Pin
Ashim Malgope18-Sep-14 20:41
professionalAshim Malgope18-Sep-14 20:41 
QuestionHTML code for bar chart Pin
Member 109258764-Jul-14 22:04
MemberMember 109258764-Jul-14 22:04 
SuggestionNice Works! Pin
Member 855673514-May-12 17:42
MemberMember 855673514-May-12 17:42 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey27-Mar-12 22:26
professionalManoj Kumar Choubey27-Mar-12 22:26 
GeneralUI Effect Pin
GautamBSharma9-Apr-10 1:00
MemberGautamBSharma9-Apr-10 1:00 
GeneralPRINT Pin
scorpioyas8-May-07 0:12
Memberscorpioyas8-May-07 0:12 
GeneralRe: PRINT Pin
Neeraj Saluja8-May-07 1:25
MemberNeeraj Saluja8-May-07 1:25 
Generalwidth of bars Pin
scorpioyas24-Apr-07 2:09
Memberscorpioyas24-Apr-07 2:09 
GeneralRe: width of bars [modified] Pin
Neeraj Saluja24-Apr-07 2:14
MemberNeeraj Saluja24-Apr-07 2:14 
GeneralHTML Horizontal Bar Chart Pin
Neeraj Saluja9-Apr-07 1:40
MemberNeeraj Saluja9-Apr-07 1:40 
Generaltriple bar chart or tetra(4 lines) Pin
scorpioyas9-Apr-07 1:19
Memberscorpioyas9-Apr-07 1:19 
GeneralRe: triple bar chart or tetra(4 lines) Pin
Neeraj Saluja9-Apr-07 1:35
MemberNeeraj Saluja9-Apr-07 1:35 
QuestionHTML Vertical Bar Chart Pin
sheenysadath7-Nov-06 22:46
Membersheenysadath7-Nov-06 22:46 
AnswerRe: HTML Vertical Bar Chart Pin
Neeraj Saluja7-Nov-06 22:59
MemberNeeraj Saluja7-Nov-06 22:59 
GeneralRe: HTML Vertical Bar Chart Pin
sheenysadath8-Nov-06 20:58
Membersheenysadath8-Nov-06 20:58 
GeneralRe: HTML Vertical Bar Chart Pin
Neeraj Saluja8-Nov-06 23:17
MemberNeeraj Saluja8-Nov-06 23:17 
GeneralRe: HTML Vertical Bar Chart Pin
sheenysadath8-Nov-06 23:20
Membersheenysadath8-Nov-06 23:20 
GeneralRe: HTML Vertical Bar Chart Pin
Neeraj Saluja8-Nov-06 23:45
MemberNeeraj Saluja8-Nov-06 23:45 
GeneralRe: HTML Vertical Bar Chart Pin
sheenysadath12-Nov-06 22:02
Membersheenysadath12-Nov-06 22:02 
GeneralRe: HTML Vertical Bar Chart [modified] Pin
Neeraj Saluja12-Nov-06 22:23
MemberNeeraj Saluja12-Nov-06 22:23 
GeneralRe: HTML Vertical Bar Chart Pin
sheenysadath12-Nov-06 22:26
Membersheenysadath12-Nov-06 22:26 
GeneralRe: HTML Vertical Bar Chart Pin
sheenysadath12-Nov-06 22:53
Membersheenysadath12-Nov-06 22:53 
GeneralRe: HTML Vertical Bar Chart Pin
Neeraj Saluja12-Nov-06 22:58
MemberNeeraj Saluja12-Nov-06 22:58 
GeneralRe: HTML Vertical Bar Chart Pin
Neeraj Saluja13-Nov-06 23:34
MemberNeeraj Saluja13-Nov-06 23:34 
GeneralRe: HTML Vertical Bar Chart Pin
Shaheena.kareem22-Nov-06 2:50
MemberShaheena.kareem22-Nov-06 2:50 

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

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