Click here to Skip to main content
6,595,444 members and growing! (19,300 online)
Email Password   helpLost your password?
Web Development » Charts, Graphs and Images » Images and multimedia     Intermediate License: The Code Project Open License (CPOL)

Generating Bar Chart in ASP.NET

By J A Srikanth

This article demonstrates how to create chart in ASP.NET using System.Drawing namespace.
VB, Windows, .NET, ASP.NET, IIS, Visual Studio, GDI+, WebForms, Dev
Posted:8 Nov 2004
Views:88,586
Bookmarked:54 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
19 votes for this article.
Popularity: 4.88 Rating: 3.82 out of 5
1 vote, 5.3%
1

2
5 votes, 26.3%
3
6 votes, 31.6%
4
7 votes, 36.8%
5

Introduction

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()
aMonths.Add("January")
aMonths.Add("February")
aMonths.Add("March")
aMonths.Add("April")
aMonths.Add("May")
aMonths.Add("June")
aMonths.Add("July")
aMonths.Add("August")
aMonths.Add("September")
aMonths.Add("October")
aMonths.Add("November")
aMonths.Add("December")

aProfits.Add(240500)
aProfits.Add(220950)
aProfits.Add(283500)
aProfits.Add(340000)
aProfits.Add(325750)
aProfits.Add(123456)
aProfits.Add(235621)
aProfits.Add(345235)
aProfits.Add(290451)
aProfits.Add(152345)
aProfits.Add(653456)
aProfits.Add(785620)

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
    Next

    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)
    Next
    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)
    objGraphics.Dispose()
    objBitmap.Dispose()
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)

License

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


Member

Occupation: Web Developer
Location: United States United States

Other popular Charts, Graphs and Images articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 20 of 20 (Total in Forum: 20) (Refresh)FirstPrevNext
Generalchart not displayed in firefox PinmemberRajaprathapdhana0:15 7 Mar '09  
QuestionNot applying for the masterpage PinmemberMember 51870131:33 4 Mar '09  
GeneralDoesnt work Pinmemberbone krusher15:33 31 Jan '09  
GeneralNo Workie! PinmemberCrazyTasty22:25 27 Feb '08  
GeneralProblem with code PinmemberMember 412528012:50 24 Feb '08  
General3D in bar charts PinmemberHasanNazeer4:13 16 Jan '08  
GeneralRe: 3D in bar charts Pinmembersidbaruah23:21 28 Apr '08  
Generalerror when compiling with visual studio 2005 PinmemberMeali00711:42 28 Nov '07  
QuestionOther Controls on the Page? PinmemberJohn Tyce5:30 5 Oct '07  
GeneralCentering the labels Pinmembertmmy_cat12:05 28 Aug '07  
GeneralBind the array to a datasource? Pinmemberajwelch23:41 10 Apr '07  
GeneralThis code in C# Pinmemberlauradonaghy5:39 20 Mar '07  
GeneralRe: This code in C# Pinmembermalinwick4:28 19 Jul '07  
GeneralRe: This code in C# Pinmembermalinwick5:59 19 Jul '07  
Generaldoes not work in Firefox Pinmemberwebber1234566:48 9 Mar '06  
GeneralRe: does not work in Firefox PinmemberJinx10111:24 18 Oct '06  
GeneralRe: does not work in Firefox Pinmembertmmy_cat11:15 28 Aug '07  
GeneralCode Doesn't Work Pinmembersap_032111:02 5 Apr '05  
GeneralThanks! PinmemberFrank Sandersen13:28 26 Nov '04  
Generalexample image PinmemberTsjaar22:15 9 Nov '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Nov 2004
Editor: Smitha Vijayan
Copyright 2004 by J A Srikanth
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project