Draw a bar chart in Asp.net

Source: Internet
Author: User
There are not many classes involved in drawing images. If you want to draw images, especially those dynamically changing data sources, controlling the coordinates is critical.
This demo uses the Oracle database as the Data source:
 

Create Table ivan_test
(
Major varchar2 (50 ),
Grade number,
ID number (10)
)

Add in HTML:

<Asp: button id = "btncolumn" runat = "server" text = "drawcolumn" onclick = "btncolumn_click"/> <br/>
<Asp: Image id = "imgcolumn" runat = "server"/>

CS :() protected void btncolumn_click (Object sender, eventargs E)
...{
Bitmap IMG = new Bitmap (300,300 );
Graphics G = graphics. fromimage (IMG );
Stringformat drawformat = new system. Drawing. stringformat (stringformatflags. directionvertical );
Stringformat drawformat1 = new system. Drawing. stringformat (stringformatflags. displayformatcontrol );
G. Clear (colortranslator. fromhtml ("# f0f0f0 "));
G. drawstring ("student grade column Chart", new font ("Arial", 1, fontstyle. Bold), brushes. Black, 100, 0, drawformat1 );

// Get datasource
String SQL = "select * From ivan_test ";
Dal. isdapp01 cdal = New DAL. isdapp01 ();
Dataset DS = cdal. executequery (SQL );


For (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
...{
Int grade = convert. toint32 (Ds. Tables [0]. Rows [I] [1]); // grade
String major = Ds. Tables [0]. Rows [I] [0]. tostring ();

// Pay attention to coordinate control
G. fillrectangle (New solidbrush (getcolor (I), (I * 35) + 15,150-grade, 20,
Grade + 50 );

G. drawrectangle (new pen (color. Black), (I * 35) + 15,150-grade, 20,
Grade + 50 );

G. drawstring (Major, new font ("Arial", 12, fontstyle. Bold), brushes. Red, (I * 35) + 20,200, drawformat );

G. drawstring (grade. tostring (), new font ("Arial", 12, fontstyle. Bold), brushes. Red, (I * 35) + 20,130-grade );
 
}
// Show chart

String filepath = server. mappath ("Images") + "//" + "column.jpg ";
If (file. exists (filepath ))
...{
File. Delete (filepath );
}
IMG. Save (filepath, imageformat. JPEG );
IMG. Dispose ();
G. Dispose ();
This. imgcolumn. imageurl = filepath;
}

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.