Click here to Skip to main content
13,344,765 members (62,273 online)
Click here to Skip to main content
Add your own
alternative version


68 bookmarked
Posted 8 Nov 2004

Generating Bar Chart in ASP.NET

, 8 Nov 2004
Rate this:
Please Sign up or sign in to vote.
This article demonstrates how to create chart in ASP.NET using System.Drawing namespace.


The following example demonstrates how to generate bar-charts for any business information available on a web page. This uses the classes which are provided in the .NET System.Drawing namespace to generate the chart.

Bar chart created in the below example illustrates the profit of a company for each month from January through December.

Generating the Bar chart

Data which is to be displayed on X axis and Y axis is stored in ArrayLists, and then the data is read from these ArrayLists for creating the required bar chart.

First, the ArrayLists are populated as follows:

Dim aMonths As ArrayList = New ArrayList()
aProfits As ArrayList = New ArrayList()


Once the data is populated, the chart can be generated by calling the method DrawBarGraph:

DrawBarGraph("Profits!", aMonths, aProfits)

DrawBarGraph is defined as follows:

Sub DrawBarGraph(ByVal strTitle As String, _
        ByVal aX As ArrayList, ByVal aY As ArrayList)
    Const iColWidth As Integer = 60, iColSpace As Integer = 25, _
          iMaxHeight As Integer = 400, iHeightSpace As Integer = 25, _
          iXLegendSpace As Integer = 30, iTitleSpace As Integer = 50
    Dim iMaxWidth As Integer = (iColWidth + iColSpace) * aX.Count + iColSpace, _
          iMaxColHeight As Integer = 0, _
          iTotalHeight As Integer = iMaxHeight + iXLegendSpace + iTitleSpace

    Dim objBitmap As Bitmap = New Bitmap(iMaxWidth, iTotalHeight)
    Dim objGraphics As Graphics = Graphics.FromImage(objBitmap)
    objGraphics.FillRectangle(New SolidBrush(Color.White), _
                0, 0, iMaxWidth, iTotalHeight)
    objGraphics.FillRectangle(New SolidBrush(Color.Ivory), _
                0, 0, iMaxWidth, iMaxHeight)

    ' find the maximum value
    Dim iValue As Integer
    For Each iValue In aY
        If iValue > iMaxColHeight Then iMaxColHeight = iValue

    Dim iBarX As Integer = iColSpace, iCurrentHeight As Integer
    Dim objBrush As SolidBrush = New SolidBrush(Color.FromArgb(70, 20, 20))
    Dim fontLegend As Font = New Font("Arial", 11), _
        fontValues As Font = New Font("Arial", 8), _
        fontTitle As Font = New Font("Arial", 24)

     ' loop through and draw each bar
    Dim iLoop As Integer
    For iLoop = 0 To aX.Count - 1
        iCurrentHeight = ((Convert.ToDouble(aY(iLoop)) / _
               Convert.ToDouble(iMaxColHeight)) * _
               Convert.ToDouble(iMaxHeight - iHeightSpace))
        objGraphics.FillRectangle(objBrush, iBarX, _
        iMaxHeight - iCurrentHeight, iColWidth, iCurrentHeight)
        objGraphics.DrawString(aX(iLoop), fontLegend, _
           objBrush, iBarX, iMaxHeight)
        objGraphics.DrawString(Format(aY(iLoop), "#,###"), _
           fontValues, objBrush, iBarX, iMaxHeight - iCurrentHeight - 15)
        iBarX += (iColSpace + iColWidth)
    objGraphics.DrawString(strTitle, fontTitle, objBrush, _
          (iMaxWidth / 2) - strTitle.Length * 6, iMaxHeight + iXLegendSpace)
    'objBitmap.Save("C:\inetpub\wwwroot\graph.gif", ImageFormat.GIF) 

    objBitmap.Save(Response.OutputStream, ImageFormat.Gif)
End Sub

This code will generate the bar chart and display it on the screen. Also, if required, the bar chart can be saved as an image, just uncomment the line below in the above code:

objBitmap.Save("C:\inetpub\wwwroot\graph.gif", ImageFormat.GIF)


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


About the Author

J A Srikanth
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionIt works fine. Pin
arun733111-Nov-11 1:14
memberarun733111-Nov-11 1:14 
GeneralMy vote of 1 Pin
G.santosh priya13-Oct-11 22:56
memberG.santosh priya13-Oct-11 22:56 
Generalchart not displayed in firefox Pin
Rajaprathapdhana7-Mar-09 0:15
memberRajaprathapdhana7-Mar-09 0:15 
QuestionNot applying for the masterpage Pin
Member 51870134-Mar-09 1:33
memberMember 51870134-Mar-09 1:33 
Its working but i cannot apply the masterpage to this page...

Can anyone tell me the solution to apply the masterpage to this aspx page...

Thanks ,
GeneralDoesnt work Pin
bone krusher31-Jan-09 15:33
memberbone krusher31-Jan-09 15:33 
GeneralNo Workie! Pin
CrazyTasty27-Feb-08 22:25
memberCrazyTasty27-Feb-08 22:25 
GeneralProblem with code Pin
Member 412528024-Feb-08 12:50
memberMember 412528024-Feb-08 12:50 
General3D in bar charts Pin
HasanNazeer16-Jan-08 4:13
memberHasanNazeer16-Jan-08 4:13 
GeneralRe: 3D in bar charts Pin
sidbaruah28-Apr-08 23:21
membersidbaruah28-Apr-08 23:21 
Generalerror when compiling with visual studio 2005 Pin
Meali00728-Nov-07 11:42
memberMeali00728-Nov-07 11:42 
QuestionOther Controls on the Page? Pin
John Tyce5-Oct-07 5:30
memberJohn Tyce5-Oct-07 5:30 
GeneralCentering the labels Pin
tmmy_cat28-Aug-07 12:05
membertmmy_cat28-Aug-07 12:05 
QuestionBind the array to a datasource? Pin
ajwelch10-Apr-07 23:41
memberajwelch10-Apr-07 23:41 
GeneralThis code in C# Pin
lauradonaghy20-Mar-07 5:39
memberlauradonaghy20-Mar-07 5:39 
GeneralRe: This code in C# Pin
malinwick19-Jul-07 4:28
membermalinwick19-Jul-07 4:28 
GeneralRe: This code in C# Pin
malinwick19-Jul-07 5:59
membermalinwick19-Jul-07 5:59 
Generaldoes not work in Firefox Pin
webber1234569-Mar-06 6:48
memberwebber1234569-Mar-06 6:48 
GeneralRe: does not work in Firefox Pin
Jinx10118-Oct-06 11:24
memberJinx10118-Oct-06 11:24 
GeneralRe: does not work in Firefox Pin
tmmy_cat28-Aug-07 11:15
membertmmy_cat28-Aug-07 11:15 
GeneralCode Doesn't Work Pin
sap_03215-Apr-05 11:02
membersap_03215-Apr-05 11:02 
GeneralThanks! Pin
Frank Sandersen26-Nov-04 13:28
memberFrank Sandersen26-Nov-04 13:28 
Generalexample image Pin
Tsjaar9-Nov-04 22:15
memberTsjaar9-Nov-04 22:15 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 9 Nov 2004
Article Copyright 2004 by J A Srikanth
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid