We have already completed the customized pie chart and bar chart classes. We will apply these classes below.
Use vs.net to create a web application named insight_cs Program And add it to the Insight Project. Delete the default webform1.aspx file and create a new file named saleschart. aspx. Open this file and go Code Mode, replace the first line:
<% @ Page contenttype = "image/GIF" Language = "C #" autoeventwireup = "false" codebehind = "saleschart. aspx. cs" inherits = "insight_cs.saleschart" %>
Open the saleschart. aspx. CS file. The Code is as follows:
Using system;
Using system. Data;
Using system. Web;
Using system. IO;
Using system. Data. sqlclient;
Using insight_cs.webcharts; // This is a custom namespace
Namespace insight_cs
{
Public class saleschart: system. Web. UI. Page
{
Public saleschart ()
{
Page. init + = new system. eventhandler (page_init );
}
Private void page_load (Object sender, system. eventargs E)
{
// Obtain data from the database for drawing
String SQL = "select" + "Year (SA. ord_date) as [year], "+" sum (SA. qty) as [qty] "+" from "+" sales SA "+" inner join stores st on (SA. stor_id = ST. stor_id) "+" group by "+" Year (SA. ord_date) "+" order by "+" [year] ";
String connectstring = "Password = Ben; user id = sa; database = pubs; Data Source = localhost ";
Sqldataadapter da = new sqldataadapter (SQL, connectstring );
Dataset DS = new dataset ();
Int rows = da. Fill (DS, "chartdata ");
// Set the Graph Generation type (pie or bar)
String type = "";
If (null = request ["type"])
{
Type = "pie ";
}
Else
{
Type = request ["type"]. tostring (). toupper ();
}
// Set the image size
Int width = 0;
If (null = request ["width"])
{
Length = 400;
}
Else
{
Width = convert. toint32 (request ["width"]);
}
Int Height = 0;
If (null = request ["height"])
{
Height = 400;
}
Else
{
Height = convert. toint32 (request ["height"]);
}
// Set the chart title
String title = "";
If (null! = Request ["title"])
{
Title = request ["title"]. tostring ();
}
String subtitle = "";
If (null! = Request ["subtitle"])
{
Subtitle = request ["subtitle"]. tostring ();
}
If (0 <rows)
{
Switch (type)
{
Case "pie ":
Piechart Pc = new piechart ();
PC. Render (title, subtitle, width, height, DS, response. outputstream );
Break;
Case "bar ":
Barchart BC = new barchart ();
BC. Render (title, subtitle, width, height, DS, response. outputstream );
Break;
Default:
Break;
}
}
}
Private void page_init (Object sender, eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
}
# Region web form designer generated code
/// <Summary>
/// Required method for designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
}
}
The above code is not difficult, so we will not analyze it here.
In vs.net, open insight_cs solution, right-click reference, and add reference to the component File insight_cs.webcharts.dll to make it the namespace in the project.
Now we can view the results.
First, create a demochart. aspx file and add the following content to the Code:
Src = "saleschart. aspx? Type = pie & width = 300 & Height = 30
0 & Title = Sales + by + Year & subtitle = books ">
Src = "saleschart. aspx? Type = Bar & width = 300 & Height = 30
0 & Title = Sales + by + Year & subtitle = books ">
Type indicates the type of the graph, whether it is a pie chart pie or a bar chart.
Width and height indicate the image size.
Title indicates the title text.
Subtitle indicates the title text.
The result shows 1. Article ASP. NET graphic introduction (I).
As a result, we have completed the process of drawing using Asp.net technology.
The following points can be summarized: 1. Using ASP. NET technology, you can draw an ideal image without using third-party components. 2. The core of drawing is to construct a bitmap object, which provides memory space for the image to be created. Then, draw a graph using the classes and methods provided by the namespace. Finally, you can call the "save" method of the bitmap object and send it to any. in the net output stream, the image content is directly sent to the browser without being saved to the disk.
Abstract: http://www.phome.net/document/net/200503/net11106132211044.html