ASP.NET - bar chart using asp.net

Asked By Bala Marish
04-Feb-12 12:28 AM


how to generate bar chart in asp.net c#..................
  D Company replied to Bala Marish
04-Feb-12 12:50 AM
hello friend,

first of all see the silverlight tutorials,best examples to do this,than,to accomplish this use some graphical feature provided in .net like Create a bitmap image using Bitmap class

FillRectangle() and DrawRectangle() are the two methods, you can use to draw the appropriate graphics

than draw the bar chart as below

private void DrawBars()
{
    float availableXSpace = width - rightPadding -
        leftPadding - Chart.AxisWidth;
    float spacePerItem = availableXSpace / items.Count;
    float spacing = 0F; // 1F;
    spacePerItem -= spacing;

    using (Brush b = new SolidBrush(this.seriesColor))
    {
        for (int i = 0; i < items.Count; i++)
        {
            ChartItem item = items[i];
            PointF itemPoint = ConvertItemToPoint(
                item, i + 1, items.Count);
            RectangleF barRect = new RectangleF(
                itemPoint.X - spacePerItem, itemPoint.Y,
                spacePerItem, height - bottomPadding -
                    itemPoint.Y - Chart.AxisWidth);

            g.FillRectangle(b, barRect);
        }
    }
}

private void DrawArea()
{
    PointF[] newPoints = new PointF[this.points.Length + 2];
    newPoints[0] = new PointF(leftPadding, height -
        bottomPadding - Chart.AxisWidth);
    newPoints[newPoints.Length - 1] = new PointF(width -
        rightPadding, height - bottomPadding - Chart.AxisWidth);
    System.Array.Copy(this.points, 0, newPoints, 1, this.points.Length);

    using (Brush b = new SolidBrush(this.seriesColor))
    {
        g.FillPolygon(b, newPoints);
    }
}

private void DrawLegend()
{
    if (this.legendTitle != null)
    {
        SizeF legendStringSize = g.MeasureString(this.legendTitle,
            this.stdTextFont);
        PointF legendPoint = new PointF(width - Chart.TextPadding -
            legendStringSize.Width, height / 3F);

        using (Brush b = new SolidBrush(this.seriesColor))
        {
            g.FillRectangle(b, legendPoint.X - 8, legendPoint.Y +
                (legendStringSize.Height / 3F), 5, 5);
        }

        g.DrawString(this.legendTitle, this.stdTextFont,
            this.stdTextBrush, legendPoint);
    }
}
Regards
D

  Suchit shah replied to Bala Marish
04-Feb-12 06:53 AM
Bar chart created in the below example illustrates the profit of a company for each month from January through December.

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.
Create New Account
help
Dynamically generation of pie chart and bar chart on my website Hi I want to generate dynamically pie chart as well as bar diagram according to the user input and some calculation. . please help from the MSDN site which brings the data from the database and constructs a pie chart . . . which satisfies your requirement . . go through the article. . For more Info refere the below link from Database Data Now that you know how to create a simple image from an ASP.NET Web page, you can create more complex (and useful) images. For the remainder of this ll look at how to use the .NET Framework drawing classes to create a pie chart from database information. I'll build all of this functionality into a set of functions in an ASP.NET Web page that will end up streaming the dynamically created pie chart's binary
Tree / Hierarchy In this article, you'll learn to use GDI+ and the Bitmap, Graphic, SolidBrush, and FillRectangle classes to draw a hierarchy as a graphic that has user interface interactivity. Download C# Source Code I went looking for a chart control or code sample that would render an image of my hierarchical data tree the sample code is a windows form application, the classes are suited for use in an ASP.NET application. The code sample contained in this article was initially inspired by the Microsoft Research code to populate / / the .BackGroundImage if desired. Rectangle bgImg = new Rectangle(0, 0, width, height); SolidBrush bgBrush = new SolidBrush(Color. White ); g.FillRectangle(bgBrush, bgImg); bgBrush.Dispose(); / / Draw the node hierarchy use the root if we want / / to see the entire thing in a browser. bm. Save ( "test.gif" , ImageFormat.Gif); / / Get a quick peek on the windows forms. this .pictureBox1.Width = bm.Width; this GetMarkerCenterY(int position) { return position + (int)(MarkerHeight * .5); } public void DrawLineConnector(Graphics g, DataNode node) { float parentX = 0; float parentY = 0; float childX = 0; float childY = 0; if (node.Parent = = null
Build an ASP.NET Stock Chart Class with GDI+ Build an ASP.NET Stock Chart Class with .NET and GDI+ By Peter A. Bromberg, Ph.D. Printer - Friendly Version Peter it would be fun to see if I could put together a web - based historical chart class for quotes from Yahoo. The result is what you see here. Some of the basis for this was some line chart example stuff from another author (I'm sorry I can't remember his name, but
drawing line how to draw a line in asp.net. . .? hi, Try to use <hr> HTML element . Assign a style property to it and change LineChart { public Bitmap Bitmapb; public string strTitle = "Default Title"; public ArrayList ArrayListchartValues = new ArrayList(); public float ftXorigin = 0, ftYorigin = 0; public float ftScaleX, ftScaleY; public float ftXdivs = 2, ftYdivs = 2; private int intWidth, intHeight; private Graphics Graphicsg; private Page Pagep; struct datapoint { public float ftx; public float fty; public bool bolvalid; } / / initialize public LineChart(int intmyWidth, int intmyHeight, Page myPage) { intWidth = intmyWidth intx; myPoint.fty = inty; myPoint.bolvalid = true; ArrayListchartValues.Add(myPoint); } public void Draw() { int inti; float ftx, fty, ftx0, fty0; string strmyLabel; Pen penblackPen = new Pen(Color.Black, 1); Brush BrushblackBrush