显示叶面: <%@ Page Language="vb" %> <%@ Import Namespace="System.Data"%> <%@ Import Namespace="System.Data.Sqlclient"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Display_Chart</title> <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.0"> <meta name="CODE_LANGUAGE" content="Visual Basic 7.0"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </head> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <table width="100%" height="100%"> <tr> <td align="middle"> <IMG Src="http://wujiahong/Bjbys/manage_platform/stu_job/I_Charts.aspx?table=<%=request.querystring("table")%>"> </td> </tr> </table> </form> </body> </html> 画图叶面: Imports System.Drawing Imports System.Drawing.Imaging Imports System.Data Imports System.Data.SqlClient Public Class Charts Inherits System.Web.UI.Page Dim Values As ArrayList = New ArrayList() Dim Captions As ArrayList = New ArrayList() Dim Connection As SqlConnection Dim DR As SqlDataReader Dim Command As SqlCommand Dim TableName As String Dim SumNum As Int32 Dim Item_Count As Integer #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub#End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TableName = Request.QueryString("table") Connection = New SqlConnection(Application("SqlConString")) '统计数据到两个数组 ReadData() '生成饼图 DisplayPieChart() End Sub 'This function returns a color for the bar and pie charts. Private Function GetColor(ByVal itemIndex As Integer) As Color Dim objColor As Color Select Case itemIndex Case 0 objColor = Color.Blue Case 1 objColor = Color.Red Case 2 objColor = Color.Yellow Case 3 objColor = Color.Purple Case 4 objColor = Color.Orange Case 5 objColor = Color.Brown Case 6 objColor = Color.Gray Case 7 objColor = Color.Maroon Case Else objColor = Color.Green End Select Return objColor End Function Sub ReadData() Dim SqlString As String Dim I As Integer Dim Num_Temp As Int16 SumNum = 0 I = 0 SqlString = "Select r1,count(r1) as cc from " + TableName + " group by r1" Command = New SqlCommand(SqlString, Connection) Command.Connection.Open() DR = Command.ExecuteReader While DR.Read Captions.Add(Convert.ToString(DR.GetString(0))) Values.Add(Convert.ToInt16(DR("cc"))) Item_Count += 1 SumNum = SumNum + Convert.ToInt16(DR("cc")) End While DR.Close() Command.Connection.Close() End Sub Sub DisplayPieChart() '定义显示的字体 Dim FontLeg As Font Dim FontTitle As Font FontLeg = New Font("宋体", 10) FontTitle = New Font("宋体", 15) '确定图片的大小,及其各个元素的大小,宽度都一样 Dim ImageWidth As Integer Dim ImageHeight As Integer Dim ChartHeight As Integer Dim LegendHeight As Integer Dim TitleHeight As Integer Dim BufferSpace As Integer = 15 ImageWidth = 300 LegendHeight = FontLeg.Height * (Item_Count + 1) + BufferSpace TitleHeight = FontTitle.Height + BufferSpace ChartHeight = ImageWidth ImageHeight = ImageWidth + LegendHeight + TitleHeight + BufferSpace '画出底图 Dim WjhBitmap As Bitmap Dim WjhGraphic As Graphics Dim WjhBrush As Brush WjhBitmap = New Bitmap(ImageWidth, ImageHeight) WjhGraphic = Graphics.FromImage(WjhBitmap) WjhBrush = New SolidBrush(Color.Black) WjhGraphic.FillRectangle(New SolidBrush(Color.White), 0, 0, ImageWidth, ImageHeight) ' 画出饼图 Dim PieRec As Rectangle PieRec = New Rectangle(0, TitleHeight, ImageWidth, ChartHeight) Dim CurrentDegree As Single = 0.0F Dim I As Integer For I = 0 To Item_Count - 1 WjhGraphic.FillPie(New SolidBrush(GetColor(I)), PieRec, CurrentDegree, Convert.ToSingle(Values(I) / SumNum * 360)) CurrentDegree = CurrentDegree + Convert.ToSingle(Values(I) / SumNum * 360) Next '画出标题 Dim TitleRecF As RectangleF TitleRecF = New RectangleF(0, 0, ImageWidth, TitleHeight) Dim WjhFormat As StringFormat = New StringFormat() WjhFormat.Alignment = StringAlignment.Center WjhFormat.LineAlignment = StringAlignment.Center WjhGraphic.DrawString("统计结果", FontTitle, WjhBrush, TitleRecF, WjhFormat) '画出图例 WjhGraphic.DrawRectangle(New Pen(Color.Black, 2), 0, ImageHeight - LegendHeight, ImageWidth, LegendHeight) For I = 0 To Item_Count - 1 WjhGraphic.FillRectangle(New SolidBrush(GetColor(I)), 5, ImageHeight - LegendHeight + FontLeg.Height * I + 5, 10, 10) WjhGraphic.DrawString(Captions(I) + " ---- " + Convert.ToString(Values(I)), FontLeg, WjhBrush, 20, ImageHeight - LegendHeight + FontLeg.Height * I + 1) Next '画总计 WjhGraphic.DrawString("总计: " + Str(SumNum), FontLeg, WjhBrush, 5, ImageHeight - FontLeg.Height - 5) WjhBitmap.Save(Response.OutputStream, ImageFormat.Jpeg) End Sub End Class
<%@ import namespace="system.drawing" %>
<%@ import namespace="system.drawing.imaging" %>
<%@ import namespace="system.drawing.drawing2d" %>
<%
dim strFilename as string
dim i as System.Drawing.Image
strFilename = server.mappath("./chris-fsck.jpg")
i = System.Drawing.Image.FromFile(strFilename)
dim b as New system.drawing.bitmap(i.width, i.height, pixelformat.format24bpprgb)
dim g as graphics = graphics.fromimage(b)
g.clear(color.blue)
'旋转图片
i.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipX)
g.drawimage(i,New point(0,0))
i.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipY)
g.RotateTransform(10)
g.drawimage(i,New point(0,0))
g.RotateTransform(10)
g.drawimage(i,New point(20,20))
g.RotateTransform(10)
g.drawimage(i,New point(40,40))
g.RotateTransform(10)
g.drawimage(i,New point(40,40))
g.RotateTransform(-40)
g.RotateTransform(90)
g.drawimage(i,New rectangle(100,-400,100,50),New rectangle(20,20,i.width-20,i.height-20),GraphicsUnit.Pixel)
g.RotateTransform(-90)
' 拉伸图片
g.drawimage(i,New rectangle(10,10,50,50),New rectangle(20,20,i.width-20,i.height-20),GraphicsUnit.Pixel)
g.drawimage(i,New rectangle(50,10,90,50),New rectangle(20,20,i.width-20,i.height-20),GraphicsUnit.Pixel)
g.drawimage(i,New rectangle(110,10,150,50),New rectangle(20,20,i.width-20,i.height-20),GraphicsUnit.Pixel)
'切割图片
g.drawimage(i,50,100,New rectangle(180,80,60,110),GraphicsUnit.Pixel)
g.drawimage(i,140,100,New rectangle(180,80,60,110),GraphicsUnit.Pixel)
'旋转图片
i.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipX)
g.drawimage(i,230,100,New rectangle(180,110,60,110),GraphicsUnit.Pixel)
response.contenttype="image/jpeg"
b.save(response.outputstream, imageformat.jpeg)
b.dispose()
%>
综合起来,可以总结出以下几点:1.利用ASP.NET技术,可以在不使用第三方组件的情况下,画出理想的图形。2.画图核心是构造一个BitMap(位图)对象,它为要创建的图形提供了内存空间。然后,利用有关namespace提供的类和方法画出图形。最后就可以调用Bitmap对象的“Save”方法,将其发送到任何.NET的输出流中,这里是直接将图形的内容发送到浏览器,而没有将其保存到磁盘中。这不是我写的,作者是:bben_h。主要是save之后到一个输出流,基本原理就是这个。
System.Drawing.Bitmap bm = new System.Drawing.Bitmap(800,100);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bm);
g.Clear(System.Drawing.Color.Blue);
g.DrawString("中华人民共和国",new Font("Tahoma",72),Brushes.Black,new PointF(5,5));
bm.Save(Response.OutputStream,ImageFormat.Gif);
bm.Dispose();
g.Dispose();
}嗬嗬,给我分吧!你作有向图干什么?
UP! :)
<%@ Page Language="vb" %>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.Sqlclient"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Display_Chart</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table width="100%" height="100%">
<tr>
<td align="middle">
<IMG Src="http://wujiahong/Bjbys/manage_platform/stu_job/I_Charts.aspx?table=<%=request.querystring("table")%>">
</td>
</tr>
</table>
</form>
</body>
</html>
画图叶面:
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Data
Imports System.Data.SqlClient
Public Class Charts
Inherits System.Web.UI.Page
Dim Values As ArrayList = New ArrayList()
Dim Captions As ArrayList = New ArrayList()
Dim Connection As SqlConnection
Dim DR As SqlDataReader
Dim Command As SqlCommand
Dim TableName As String
Dim SumNum As Int32
Dim Item_Count As Integer
#Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TableName = Request.QueryString("table")
Connection = New SqlConnection(Application("SqlConString"))
'统计数据到两个数组
ReadData()
'生成饼图
DisplayPieChart()
End Sub
'This function returns a color for the bar and pie charts.
Private Function GetColor(ByVal itemIndex As Integer) As Color
Dim objColor As Color
Select Case itemIndex
Case 0
objColor = Color.Blue
Case 1
objColor = Color.Red
Case 2
objColor = Color.Yellow
Case 3
objColor = Color.Purple
Case 4
objColor = Color.Orange
Case 5
objColor = Color.Brown
Case 6
objColor = Color.Gray
Case 7
objColor = Color.Maroon
Case Else
objColor = Color.Green
End Select
Return objColor
End Function
Sub ReadData()
Dim SqlString As String
Dim I As Integer
Dim Num_Temp As Int16
SumNum = 0
I = 0
SqlString = "Select r1,count(r1) as cc from " + TableName + " group by r1"
Command = New SqlCommand(SqlString, Connection)
Command.Connection.Open()
DR = Command.ExecuteReader
While DR.Read
Captions.Add(Convert.ToString(DR.GetString(0)))
Values.Add(Convert.ToInt16(DR("cc")))
Item_Count += 1
SumNum = SumNum + Convert.ToInt16(DR("cc"))
End While
DR.Close()
Command.Connection.Close()
End Sub
Sub DisplayPieChart()
'定义显示的字体
Dim FontLeg As Font
Dim FontTitle As Font
FontLeg = New Font("宋体", 10)
FontTitle = New Font("宋体", 15) '确定图片的大小,及其各个元素的大小,宽度都一样
Dim ImageWidth As Integer
Dim ImageHeight As Integer Dim ChartHeight As Integer
Dim LegendHeight As Integer
Dim TitleHeight As Integer Dim BufferSpace As Integer = 15
ImageWidth = 300 LegendHeight = FontLeg.Height * (Item_Count + 1) + BufferSpace
TitleHeight = FontTitle.Height + BufferSpace
ChartHeight = ImageWidth
ImageHeight = ImageWidth + LegendHeight + TitleHeight + BufferSpace '画出底图
Dim WjhBitmap As Bitmap
Dim WjhGraphic As Graphics
Dim WjhBrush As Brush
WjhBitmap = New Bitmap(ImageWidth, ImageHeight)
WjhGraphic = Graphics.FromImage(WjhBitmap)
WjhBrush = New SolidBrush(Color.Black)
WjhGraphic.FillRectangle(New SolidBrush(Color.White), 0, 0, ImageWidth, ImageHeight) ' 画出饼图
Dim PieRec As Rectangle
PieRec = New Rectangle(0, TitleHeight, ImageWidth, ChartHeight)
Dim CurrentDegree As Single = 0.0F
Dim I As Integer
For I = 0 To Item_Count - 1
WjhGraphic.FillPie(New SolidBrush(GetColor(I)), PieRec, CurrentDegree, Convert.ToSingle(Values(I) / SumNum * 360))
CurrentDegree = CurrentDegree + Convert.ToSingle(Values(I) / SumNum * 360)
Next '画出标题
Dim TitleRecF As RectangleF
TitleRecF = New RectangleF(0, 0, ImageWidth, TitleHeight)
Dim WjhFormat As StringFormat = New StringFormat()
WjhFormat.Alignment = StringAlignment.Center
WjhFormat.LineAlignment = StringAlignment.Center
WjhGraphic.DrawString("统计结果", FontTitle, WjhBrush, TitleRecF, WjhFormat) '画出图例
WjhGraphic.DrawRectangle(New Pen(Color.Black, 2), 0, ImageHeight - LegendHeight, ImageWidth, LegendHeight)
For I = 0 To Item_Count - 1
WjhGraphic.FillRectangle(New SolidBrush(GetColor(I)), 5, ImageHeight - LegendHeight + FontLeg.Height * I + 5, 10, 10)
WjhGraphic.DrawString(Captions(I) + " ---- " + Convert.ToString(Values(I)), FontLeg, WjhBrush, 20, ImageHeight - LegendHeight + FontLeg.Height * I + 1)
Next '画总计
WjhGraphic.DrawString("总计: " + Str(SumNum), FontLeg, WjhBrush, 5, ImageHeight - FontLeg.Height - 5)
WjhBitmap.Save(Response.OutputStream, ImageFormat.Jpeg)
End Sub
End Class