Generate pie and column chart instances in asp.net, and asp.net bar chart instances

Source: Internet
Author: User

Generate pie and column chart instances in asp.net, and asp.net bar chart instances

This article describes how to generate a pie chart and a bar chart in asp.net. Share it with you for your reference. The specific method is as follows:

1. common methods for generating images:
Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Text;
//
// Using System. Data;
// Using System. Web. UI. WebControls;
//
Using System. Drawing;
Using System. Drawing. Imaging;
 
Namespace Tools
{
Public static class OWCImageHelp
{
/// <Summary>
/// Dynamically generate the column chart and pie chart
/// </Summary>
/// <Param name = "arrValueNames"> fields to be displayed in the line coordinate </param>
/// <Param name = "arrValues"> number to be displayed in the Y coordinate </param>
/// <Param name = "title"> title </param>
Public static void GetZBImage (string [] arrValueNames, int [] arrValues, string title)
{
Bitmap objBitMap = new Bitmap (650,300 );
Graphics objGraphics;
ObjGraphics = Graphics. FromImage (objBitMap );
ObjGraphics. Clear (Color. White );
// Int [] arrValues = {40000,320 00, 24000,300 00, 36000,280 00 };
// String [] arrValueNames = new string [] {"first", "second", "third", "fourth", "Fifth ", "Sixth "};
ObjGraphics. DrawString (title, new System. Drawing. Font ("", 16), Brushes. Blue, new PointF (5, 5 ));
PointF symbolLeg = new PointF (335, 20 );
PointF descLeg = new PointF (360, 16 );
// Draw the illustration
For (int I = 0; I <arrValueNames. Length; I ++)
{
ObjGraphics. FillRectangle (new SolidBrush (GetColor (I), symbolLeg. X, symbolLeg. Y, 20, 10 );
ObjGraphics. DrawRectangle (Pens. Black, symbolLeg. X, symbolLeg. Y, 20, 10 );
ObjGraphics. DrawString (arrValueNames [I]. ToString (), new System. Drawing. Font ("", 10), Brushes. Black, descLeg );
SymbolLeg. Y + = 15;
DescLeg. Y + = 15;
}
Float TotalValues = 0;
For (int I = 0; I <= arrValues. Length-1; I ++)
{
TotalValues + = arrValues [I];
}
// Draw a rectangle.
Float Rectangleheight = 0;
PointF recLeg = new PointF (12,200-arrValues [0]/TotalValues * 300 );
For (int I = 0; I <arrValues. Length; I ++)
{
Rectangleheight = arrValues [I]/TotalValues * 300;
ObjGraphics. FillRectangle (new SolidBrush (GetColor (I), (I * 35) + 15,200-Rectangleheight, 20, Rectangleheight + 50 );
ObjGraphics. DrawRectangle (Pens. Black, (I * 35) + 15,200-Rectangleheight, 20, Rectangleheight + 50 );
RecLeg. Y = 200-Rectangleheight-14;
ObjGraphics. DrawString (arrValues [I]. ToString (), new System. Drawing. Font ("", 10), Brushes. Blue, recLeg );
RecLeg. X + = 35;
}
// Draw a circular chart.
Float sglCurrentAngle = 0;
Float sglTotalAngle = 0;
For (int I = 0; I <arrValues. Length; I ++)
{
SglCurrentAngle = arrValues [I]/TotalValues * 360;
ObjGraphics. FillPie (new SolidBrush (GetColor (I), 220, 95,100,100, sglTotalAngle, sglCurrentAngle );
ObjGraphics. DrawPie (Pens. Black, 220, 95,100,100, sglTotalAngle, sglCurrentAngle );
SglTotalAngle + = sglCurrentAngle;
}
ObjBitMap. Save (System. Web. HttpContext. Current. Response. OutputStream, ImageFormat. Gif );
}
// Define the color.
Private static Color GetColor (int itemIndex)
{
Color objColor;
If (itemIndex = 0)
{
ObjColor = Color. Maroon;
}
Else if (itemIndex = 1)
{
ObjColor = Color. Red;
}
Else if (itemIndex = 2)
{
ObjColor = Color. Gray;
}
Else if (itemIndex = 3)
{
ObjColor = Color. Blue;
}
Else if (itemIndex = 4)
{
ObjColor = Color. Orange;
}
Else if (itemIndex = 5)
{
ObjColor = Color. Cyan;
}
Else if (itemIndex = 6)
{
ObjColor = Color. Bisque;
}
Else if (itemIndex = 7)
{
ObjColor = Color. Maroon;
}
Else if (itemIndex = 8)
{
ObjColor = Color. Maroon;
}
Else
{
ObjColor = Color. Blue;
}
Return objColor;
}
}
}
Ii. Create and generate a pie bar chart BZImage. aspx:
Background:
Copy codeThe Code is as follows: using System;
Using System. Data;
Using System. Configuration;
Using System. Collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using BLL;
Using Models;
Public partial class GridViewDemo_BZImage: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
GetBZIamge ();
}
/// <Summary>
/// Generate a pie column chart
/// </Summary>
Public void GetBZIamge ()
{
DataTable dt = BLL. StudentBLL. SelAllStudent ();
String [] rows = new string [dt. Rows. Count];
Int [] columns = new int [dt. Rows. Count];
For (int I = 0; I <dt. Rows. Count; I ++)
{
Rows [I] = dt. Rows [I] ["Student name"]. ToString ();
Columns [I] = Convert. ToInt32 (dt. Rows [I] ["salary"]. ToString ());
}
Tools. OWCImageHelp. GetZBImage (rows, columns, "Student salary query ");
}
}
3. Page showing the pie bar chart:
Front-end:
Copy codeThe Code is as follows: <table style = "width: 600px" onMouseOver = "over ()" onMouseOut = "out ()">
<Tr>
<Td style = "height: 21px; width: 35px;" align = "center">

</Td>
</Tr>
</Table>

I hope this article will help you design your asp.net program.

Related Article

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.