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;
}
}
|