ASP. Net bar chart and graph example

Source: Internet
Author: User
ASP. Net bar chart and graph example

Public class WebForm1: System. Web. UI. Page
{
Private void Page_Load (object sender, System. EventArgs e)
{
// This is the interface to outer usage
Int widthValue = 800;
Int heightValue = 500;
String xTitle, yTitle;
Int offsetX = 20;
Int offsetY = 20;
// Spacing between bar charts
Int spaceSpan = 15;

XTitle = "Time ";
YTitle = "Sum ";

Bitmap imgBitmap = new Bitmap (widthValue + 5 * offsetX, heightValue + offsetY * 2, PixelFormat. Format32bppPArgb );
Graphics g = Graphics. FromImage (imgBitmap );
G. Clear (Color. Snow );
Pen linePen = new Pen (Color. Black );
LinePen. Width = 2;
// Draw Title
Font txtFont = new Font ("Courier", 10, GraphicsUnit. Point );
String title = "This is the title to be displayed ";
G. DrawString (title, txtFont, Brushes. Blue, new PointF (imgBitmap. Width/2, 0 ));
Point original = new Point (offsetX, heightValue + offsetY );
Point xEndPonit = new Point (widthValue + offsetX, heightValue + offsetY );
Point yEndPonit = new Point (offsetX, 0 );
// X coordinate
G. DrawLine (linePen, original, xEndPonit );
// Y Coordinate
G. DrawLine (linePen, original, yEndPonit );
// Draw arrows
Point leftArrowStart = new Point (offsetX * 3/4, offsetY/3 );
Point rightArrowEnd = new Point (offsetX * 5/4, offsetY/3 );
G. DrawLine (linePen, leftArrowStart, yEndPonit );
G. DrawLine (linePen, yEndPonit, rightArrowEnd );

Point topArrowStart = new Point (widthValue + offsetX * 3/4, heightValue + offsetY * 3/4 );
Point bottomArrowEnd = new Point (widthValue + offsetX * 3/4, heightValue + offsetY * 5/4 );
G. DrawLine (linePen, topArrowStart, xEndPonit );
G. DrawLine (linePen, xEndPonit, bottomArrowEnd );


// Draw X-Title
G. DrawString (xTitle, txtFont, Brushes. Blue, new PointF (bottomArrowEnd. X-g.MeasureString (xTitle, txtFont). Width, topArrowStart. Y + 10 ));
StringFormat f = new StringFormat ();
// F. Alignment = StringAlignment. Center;
// Draw Y-Title
F. LineAlignment = StringAlignment. Center;
G. DrawString (yTitle, txtFont, Brushes. Blue, new PointF (offsetX * 5/3, 15), f );

// Code for drawing a single bar chart

// Test Data
/**
Int [] staticData = new int [] {200,120, 200,120, 140 };

Int maxValue = MaxValue (staticData );

Int ratio = heightValue/maxValue;
Int groupCount = staticData. Length; // Number of bar chart groups
ResetValue (staticData, ratio );

Int rectWidth = (widthValue-(groupCount + 1) * spaceSpan)/groupCount;
Rectangle rect;
Int startX = offsetX + spaceSpan; // start coordinate point
For (int I = 0; I
{
Rect = new Rectangle (startX, heightValue + offsetY-staticData, RectWidth, staticData);
G. DrawRectangle (Pens. Green, rect );
G. FillRectangle (Brushes. Green, rect );
StartX = startX + rectWidth + spaceSpan;
}
**/
// Type t = dataList. GetType ();
// If (t. IsArray)
//{
//}
// Group bar chart code
ArrayList dataList = new ArrayList ();
Int [] data1 = new int [] {20, 30, 50,100 };
Int [] data2 = new int [] {50, 60, 30, 30 };
Int [] data3 = new int [] {80, 50, 60, 85 };
Int [] data4 = new int [] {20, 30, 90, 58 };
Int [] data5 = new int [] {50, 60, 30, 30 };
Int [] data6 = new int [] {80, 50, 60, 85 };
Int [] data7 = new int [] {20, 30, 90, 58 };
DataList. Add (data1 );
DataList. Add (data2 );
DataList. Add (data3 );
DataList. Add (data4 );
DataList. Add (data5 );
DataList. Add (data6 );
DataList. Add (data7 );
Int maxValue = MaxValue (dataList );
Int ratio = heightValue/maxValue;
Int groupCount = dataList. Count; // Number of bar chart groups
ResetValue (dataList, ratio );

// Draw coordinates based on ratio
// Draw X-Grids, The height value is divided into 10 parts
Int heightStep = heightValue/10;
Point point1, point2;
For (int I = 1; I <= 10; I ++)
{
Point1 = new Point (offsetX * 3/4, offsetY + heightValue-heightStep * I );
Point2 = new Point (offsetX, point1.Y );
G. DrawLine (Pens. Black, point1, point2 );
String text = maxValue/10 * I + "";
G. DrawString (text, txtFont, Brushes. Blue, new PointF (-2, point1.Y ));
}
// Draw the coordinates to end
Int rectWidth = (widthValue-(groupCount + 1) * spaceSpan)/groupCount;
Int innerGroupCount = GetInnerGroupCount (dataList );
Int innerWidth = rectWidth/innerGroupCount;
Rectangle rect;
Int startX = offsetX + spaceSpan; // start coordinate point
For (int I = 0; I
{
Int [] staticData = (int []) dataList;
For (int j = 0; j
{
Rect = new Rectangle (startX, heightValue + offsetY-staticData [j], innerWidth, staticData [j]);
G. DrawRectangle (GetPenColor (j), rect );
G. FillRectangle (GetBrushColor (j), rect );
StartX = startX + innerWidth;
}
StartX = startX + spaceSpan;
}
// Draw a curve
Point pnt1 = new Point (200 + offsetX, heightValue + offsetY-300 );
Point pnt2 = new Point (300 + offsetX, heightValue + offsetY-500 );
Point pnt3 = new Point (400 + offsetX, heightValue + offsetY-300 );
Point pnt4 = new Point (600 + offsetX, heightValue + offsetY-600 );
G. DrawCurve (Pens. Purple, new Point [] {pnt1, pnt2, pnt3, pnt4 });
// Pie chart
G. DrawPie (Pens. Red, 50, 50,150,150, 0, 30 );
ImgBitmap. Save (Response. OutputStream, ImageFormat. Gif );
}

The functions involved in the Code are as follows:
Private int MaxValue (int [] data)
{
Int maxValue = 0;
For (int I = 0; I
{
If (data> MaxValue)
MaxValue = data;
}
Return maxValue;
}
Private int MaxValue (ArrayList dataList)
{
Int maxValue = 0;
For (int I = 0; I
{
Int newMaxValue = MaxValue (int []) dataList);
If (newMaxValue> maxValue)
MaxValue = newMaxValue;
}
Return maxValue;
}
Private void ResetValue (int [] data, int ratio)
{
For (int I = 0; I
Data* = Ratio;
}
Private void ResetValue (ArrayList dataList, int ratio)
{
For (int I = 0; I
{
ResetValue (int []) dataList, Ratio );
}
}
Private int GetInnerGroupCount (ArrayList dataList)
{
Int groupCount = 0;
For (int I = 0; I
{
Int count = (int []) dataList). Length;
If (count> groupCount)
GroupCount = count;
}
Return groupCount;
}
Private Pen GetPenColor (int index)
{
Pen linePen = new Pen (Color. Black );
LinePen. Width = 1;
LinePen. CompoundArray = new float [] {0.0F, 0.2F };
Switch (index)
{
Case 0:
LinePen. Color = Color. Gray;
Break;
Case 1:
LinePen. Color = Color. Green;
Break;
Case 2:
LinePen. Color = Color. Blue;
Break;
Case 3:
LinePen. Color = Color. Purple;
Break;
}
Return linePen;
}
Private Brush GetBrushColor (int index)
{
Switch (index)
{
Case 0:
Return Brushes. Gray;
Case 1:
Return Brushes. Green;
Case 2:
Return Brushes. Blue;
Case 3:
Return Brushes. Purple;
}
Return Brushes. Red;
}
------------------------------------------------------------

More communication!

 

TOP

 

Send Short Message

View Public Information

Find all posts for this Member

  • UID10177
  • Excellent2
  • Prestige7 points
  • Money$6.5
  • From
  • StatusOffline

    Dgjdgj

    • Group[Student]
    • GenderConfidentiality
    • Points[3]
    • Post3
    • Registration Time2008-10-06
    Dgjdgj Tree Type | favorites | small medium large 2 #~ Asp.net troubleshooting hands-on ~

    Reply: ASP. Net bar chart and graph example

    Yes, it seems that some obvious code is missing and debugging is as follows:

    Public partial class Default2: System. Web. UI. Page
    {
    Protected void Page_Load (object sender, EventArgs e)
    {
    Int widthvalue = 800;
    Int heightvalue = 500;
    String xTile, yTitle;
    Int offsetX = 20;
    Int offsetY = 20;
    // The spacing between the columns
    Int spaceSpan = 15;

    XTile = "Title ";
    YTitle = "Sum ";

    Bitmap imgbitmap = new Bitmap (widthvalue + 5 * offsetX, heightvalue + 2 * offsetY, PixelFormat. Format32bppArgb );
    Graphics g = Graphics. FromImage (imgbitmap );
    G. Clear (Color. Snow );
    Pen linePen = new Pen (Color. Black );
    LinePen. Width = 2;
    // Draw Title
    Font txtFont = new Font ("Courier", 10, GraphicsUnit. Point );
    String title = "This is the title to be displayed ";
    G. DrawString (title, txtFont, Brushes. Blue, new PointF (imgbitmap. Width/2, 0 ));
    Point original = new Point (offsetX, heightvalue + offsetY );
    Point xEndPonit = new Point (widthvalue + offsetX, heightvalue + offsetY );
    Point yEndPonit = new Point (offsetX, 0 );
    // X coordinate
    G. DrawLine (linePen, original, xEndPonit );
    // Y Coordinate
    G. DrawLine (linePen, original, yEndPonit );
    // Draw arrows
    Point leftArrowStart = new Point (offsetX * 3/4, offsetY/3 );
    Point rightArrowEnd = new Point (offsetX * 5/4, offsetY/3 );
    G. DrawLine (linePen, leftArrowStart, yEndPonit );
    G. DrawLine (linePen, yEndPonit, rightArrowEnd );

    Point topArrowStart = new Point (widthvalue + offsetX * 3/4, heightvalue + offsetY * 3/4 );
    Point bottomArrowEnd = new Point (widthvalue + offsetX * 3/4, heightvalue + offsetY * 5/4 );
    G. DrawLine (linePen, topArrowStart, xEndPonit );
    G. DrawLine (linePen, xEndPonit, bottomArrowEnd );

    // Draw X-Title
    G. DrawString (xTile, txtFont, Brushes. Blue, new PointF (bottomArrowEnd. X-g. MeasureString (xTile, txtFont). Width, topArrowStart. Y + 10 ));
    StringFormat f = new StringFormat ();
    // F. Alignment = StringAlignment. Center;
    // Draw Y-Title
    F. LineAlignment = StringAlignment. Center;
    G. DrawString (yTitle, txtFont, Brushes. Blue, new PointF (offsetX * 5/3, 15), f );

    // Code for drawing a single bar chart

    // Test Data
    /**
    Int [] staticData = new int [] {200,120, 200,120, 140 };

    Int maxValue = MaxValue (staticData );

    Int ratio = heightvalue/maxValue;
    Int groupCount = staticData. Length; // Number of bar chart groups
    ResetValue (staticData, ratio );

    Int rectWidth = (widthvalue-(groupCount + 1) * spaceSpan)/groupCount;
    Rectangle rect;
    Int startX = offsetX + spaceSpan; // start coordinate point
    For (int I = 0; I
    {
    Rect = new Rectangle (startX, heightvalue + offsetY-staticData, RectWidth, staticData);
    G. DrawRectangle (Pens. Green, rect );
    G. FillRectangle (Brushes. Green, rect );
    StartX = startX + rectWidth + spaceSpan;
    }
    **/
    // Type t = dataList. GetType ();
    // If (t. IsArray)
    //{
    //}
    // Group bar chart code
    ArrayList dataList = new ArrayList ();
    Int [] data1 = new int [] {20, 30, 50,100 };
    Int [] data2 = new int [] {50, 60, 30, 30 };
    Int [] data3 = new int [] {80, 50, 60, 85 };
    Int [] data4 = new int [] {20, 30, 90, 58 };
    Int [] data5 = new int [] {50, 60, 30, 30 };
    Int [] data6 = new int [] {80, 50, 60, 85 };
    Int [] data7 = new int [] {20, 30, 90, 58 };
    DataList. Add (data1 );
    DataList. Add (data2 );
    DataList. Add (data3 );
    DataList. Add (data4 );
    DataList. Add (data5 );
    DataList. Add (data6 );
    DataList. Add (data7 );
    Int maxValue = MaxValue (dataList );
    Int ratio = heightvalue/maxValue;
    Int groupCount = dataList. Count; // Number of bar chart groups
    ResetValue (dataList, ratio );

    // Draw coordinates based on ratio
    // Draw X-Grids, The height value is divided into 10 parts
    Int heightStep = heightvalue/10;
    Point point1, point2;
    For (int I = 1; I <= 10; I ++)
    {
    Point1 = new Point (offsetX * 3/4, offsetY + heightvalue-heightStep * I );
    Point2 = new Point (offsetX, point1.Y );
    G. DrawLine (Pens. Black, point1, point2 );
    String text = maxValue/10 * I + "";
    G. DrawString (text, txtFont, Brushes. Blue, new PointF (-2, point1.Y ));
    }

    // Draw the coordinates to end
    Int rectWidth = (widthvalue-(groupCount + 1) * spaceSpan)/groupCount;
    Int innerGroupCount = GetInnerGroupCount (dataList );
    Int innerWidth = rectWidth/innerGroupCount;
    Rectangle rect;
    Int startX = offsetX + spaceSpan; // start coordinate point
    For (int I = 0; I <dataList. Count; I ++)
    {
    Int [] staticData = (int []) dataList;
    For (int j = 0; j <staticData. Length; j ++)
    {
    Rect = new Rectangle (startX, heightvalue + offsetY-staticData [j], innerWidth, staticData [j]);
    G. DrawRectangle (GetPenColor (j), rect );
    G. FillRectangle (GetBrushColor (j), rect );
    StartX = startX + innerWidth;
    }
    StartX = startX + spaceSpan;
    }

    // Draw a curve
    Point pnt1 = new Point (200 + offsetX, heightvalue + offsetY-300 );
    Point pnt2 = new Point (300 + offsetX, heightvalue + offsetY-500 );
    Point pnt3 = new Point (400 + offsetX, heightvalue + offsetY-300 );
    Point pnt4 = new Point (600 + offsetX, heightvalue + offsetY-600 );
    G. DrawCurve (Pens. Purple, new Point [] {pnt1, pnt2, pnt3, pnt4 });
    // G. DrawCurve (Pens. Purple, new Point [] {pnt1, pnt2, pnt3, pnt4 });
    // Pie chart
    G. DrawPie (Pens. Red, 50, 50,150,150, 0, 30 );
    Imgbitmap. Save (Response. OutputStream, ImageFormat. Gif );
    }

    Private int MaxValue (int [] data)
    {
    Int maxValue = 0;
    For (int I = 0; I <data. Length; I ++)
    {
    If (data> MaxValue)
    MaxValue = data;
    }
    Return maxValue;
    }
    Private int MaxValue (ArrayList dataList)
    {
    Int maxValue = 0;
    For (int I = 0; I <dataList. Count; I ++)
    {
    Int newMaxValue = MaxValue (int []) dataList);
    If (newMaxValue> maxValue)
    MaxValue = newMaxValue;
    }
    Return maxValue;
    }
    Private void ResetValue (int [] data, int ratio)
    {
    For (int I = 0; I <data. Length; I ++)
    Data* = Ratio;
    }
    Private void ResetValue (ArrayList dataList, int ratio)
    {
    For (int I = 0; I <dataList. Count; I ++)
    {
    ResetValue (int []) dataList, Ratio );
    }
    }
    Private int GetInnerGroupCount (ArrayList dataList)
    {
    Int groupCount = 0;
    For (int I = 0; I <dataList. Count; I ++)
    {
    Int count = (int []) dataList). Length;
    If (count> groupCount)
    GroupCount = count;
    }
    Return groupCount;
    }
    Private Pen GetPenColor (int index)
    {
    Pen linePen = new Pen (Color. Black );
    LinePen. Width = 1;
    LinePen. CompoundArray = new float [] {0.0F, 0.2F };
    Switch (index)
    {
    Case 0:
    LinePen. Color = Color. Gray;
    Break;
    Case 1:
    LinePen. Color = Color. Green;
    Break;
    Case 2:
    LinePen. Color = Color. Blue;
    Break;
    Case 3:
    LinePen. Color = Color. Purple;
    Break;
    }
    Return linePen;
    }
    Private Brush GetBrushColor (int index)
    {
    Switch (index)
    {
    Case 0:
    Return Brushes. Gray;
    Case 1:
    Return Brushes. Green;
    Case 2:
    Return Brushes. Blue;
    Case 3:
    Return Brushes. Purple;
    }
    Return Brushes. Red;
    }
    }

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.