HTML Vertical Bar Chart


By Neeraj Saluja
Printer Friendly Version
View My Articles

  

HTML Vertical Bar Chart can be easily generated smartly placing Divs in HTML Tableusing javascript.



Download demo project - 3 Kb

Introduction

Recently I came across a requirement of displaying colorful bar charts in our web application. Since I am using ASP.NET 1.1, lot of wonderful ideas came to me like using MS Office web office components, using third party controls to generate charts, finding some great ways using System.Drawing things to display charts etc etc. And one of the approach that struck me was using just plain HTML, a bit of DHTML and javascript to do the same thing. Well, at first glance it could seem to be a  horrifying idea, but working with basics to achieving big requirement 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 looks somewhat like this

Single Vertical Bar Chart

Let me explain in very simple way, how this colorful chart is generated. This bar chart is generated using HTML table and DIVs. It is basically a set of divs smartly placed inside the TDs of HTML table TR. This chart table needs only two rows one to contain DIVs to represent values and another one to show X-Axis labels. Let us cook this chart step by step.

1. Take a HTML table

2. Determine how many columns you need in the table by determining the number of items you want to have on chart. Let us say you got colNum as no. of columns you need. In example attached it is 24.

3. Take two rows in that table.

4. In the First Row, Add colNum number of TDs having Divs with appropriate Width and Height in it.

5. In the second Row, add colNum number of TDs having DIVs with labels for X axis.

This generates the skeleton for your chart.

Now to represent the values on the chart, we need to place DIVs inside each TD of First Row (Step 4) , with the appropriate height and width. Say tdwd is the width of your each bar and h is the height that you would need for this bar then it would be

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

Each of us can have a our own logic for determining the tdwd and h. Like if we have a fix area for the chart, then we can detremine the tdwd by dividing the chartwidth by number of columns you 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

And now it won't be too hard to understand how to generate two bar chart. In the same TD, of Step 4, rather than adding one DIV we need to place two DIVs. The height of the second div should be guided by the second array of information. And thats it. Or 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 it would the HTML code would look like

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

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

Benefits

The charts developed in such ways using basic HTML stuff can be used very effectively by clubbing it with any web development tools/technology like ASP, ASP.NET, JSP etc. All it needs is a array of values that needs to be displayed in the graphical manner. It works with Netscape as well, so scope is further increased automatically.

Conclusion

Basic building blocks of HTML/HTML with coating when used smartly can create wonders. User experience is best guided by UI and for UI to be facinating, you always do not need hi-fi things. This is just one of the HTML charts that I am putting across, there are more to come.



Biography
Microsoft Talks

button
 
Article Discussion: HTML Verticle Bar Chart can be easily generated smartly placing Divs in HTML Tableusing javascript.
Neeraj Saluja posted at 06-Oct-06 02:29
Original Article

promotion
Silverlight    WPF    WCF    WWF    LINQ   
JavaScript    AJAX    ASP.NET    XAML   
C#    VB.NET    VB 6.0    GDI+    IIS    XML   
.NET Generics    Anonymous Methods    Delegate   
Visual Studio .NET    Expression Blend    Virus   
Windows Vista    Windows XP    Windows Update   
Windows 2003 Server    Windows 2008 Server   
SQL Server    Microsoft Excel    Microsoft Word   
SharePoint    BizTalk    Virtual Earth   
.NET Compact Framework    Web Service   

"Everything" RSS / ATOM Feed Parser
How to send and receive messages through message queuing in .Net
How to Read text file as database
SQL Server 2005 Paging Performance Tip
Display code of web page.
Fully Scalable Excel File Importer class for .net using Microsoft Jet driver
Generic Chart Color Manager class that can be used for any charts
Helper class to style the infragistics wingrid
Using Reflection to detemine as Assembly Info in and out.
Helper class to play with Window (Owners and position)
Resolving displayname from the culture using the XmlLanguage and LanguageSpecificStringDictionary class
Manipulate file attributes in VB.NET
Forms Based Authentication Filtered Content Editor for SharePoint
How to create a Tree View of the Windows Folder and extract all the file-folder info.
How to use AssemblyInfo.cs file in win forms to provide much needed information on Assemblies
Sorting In Datagrid
Helper class to work with NativeMethods in the native api's
Silverlight Line Of Business Applications With Offline WPF Versions
C# : Database monitoring system using XML file
C# : Adding ComboBox to ListView SubItem
Sum of Numbers Captcha: Keeping it Simple
C# Create a Piechart for the specified Hard Disk Drive Utilization
Extension Methods for DataSet and DataTable that makes tasks easier
Accessing IIS Hosted WCF Services from PHP
Helper class that provides most commonly used Extension Methods for DateTime object
Helper class to work with a Status Bar in WPF.
Finding Unmatched Records in Dataset Tables Using Linq
Silverlight Toolkit: Autocomplete TextBox Stock Symbols and Chart
COOL Auto Complete textbox using javascript
Creating a Serializable Log Entry for Microsoft Enterprise Library to log to a Database
ASP.NET Searching Values in Datagrid