Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Microsoft.Office.Interop
Public Class getchart
Inherits System.Web.UI.Page
Protected WithEvents ChartSpace1 As Owc.ChartSpace
Private Sub Page_Load(ByVal Sender As System.Object, _
ByVal E As System.EventArgs) Handles MyBase.Load
Response.Buffer = TRUE
Response.ContentType = "image/gif"
'SQL Server连接字符串
Dim ConnectionString As String = "连接字符串"
'计算数据点个数的SQL命令
Dim CountText As String = "SELECT COUNT(*) From OWCDATA"
'获取数据点的SQL命令
Dim CommandText As String = "SELECT X, Y From OWCDATA ORDER BY X"
'定义数据库连接对象
Dim myConnection As New SqlConnection(ConnectionString)
'定义一个命令对象,用于计算数据点个数
Dim myCount As New SqlCommand(CountText, myConnection)
'定义一个获取数据点的命令对象
Dim myCommand As New SqlCommand(CommandText, myConnection)
'定义一个DataReader对象
Dim DataReader1 As SqlDataReader
'i = 索引变量(用于填写数组)
'NumPoints = 表示数据库中数据点个数的整数
'aX = 存放X值的数组
'aY = 存放Y值的数组
Dim i, NumPoints, aX, aY
'定义图表对象、数据系列对象,OWC绘图必需
Dim Chart1, Chart1_Series1
'打开数据库连接
myConnection.Open()
'步骤一:计算数据点个数,结果保存在
'NumPoints变量中。
NumPoints = myCount.ExecuteScalar()
'调试信息
'Response.Write(NumPoints & "#")
'根据数据点个数,调整数组的大小
ReDim aX(NumPoints - 1)
ReDim aY(NumPoints - 1)
'步骤二:获取数据点,在aX和aY数组中返回X和
'Y值
DataReader1 = myCommand.ExecuteReader()
i = 0
While DataReader1.Read
aX(i) = DataReader1.GetValue(0)
aY(i) = DataReader1.GetValue(1)
i = i + 1
End While
DataReader1.Close()
'调试信息
'For i = 0 to NumPoints - 1
' Response.Write(aX(i) & "|" & aY(i) & "#")
'Next i
'关闭数据库连接
myConnection.Close()
'新建一个绘图空间
ChartSpace1 = new Owc.ChartSpace()
'在ChartSpace1绘图空间中新建一个图表
Chart1 = Chartspace1.Charts.Add(0)
'在Chart1图表中加入一个数据系列
Chart1_Series1 = Chart1.SeriesCollection.Add(0)
'将Chart1_Series1数据系列定义成XY散点图,
'带连线和数据点标记
Chart1_Series1.Type = _
Chartspace1.Constants.chChartTypeScatterLineMarkers
'命名数据系列(名称将在图例中显示出来)
Chart1_Series1.SetData (OWC.ChartDimensionsEnum.chDimSeriesNames, _
OWC.ChartSpecialDataSourcesEnum.chDataLiteral, "Series1")
'将数组中的数据植入图表
Chart1_Series1.SetData (OWC.ChartDimensionsEnum.chDimXValues, _
OWC.ChartSpecialDataSourcesEnum.chDataLiteral, aX)
Chart1_Series1.SetData (OWC.ChartDimensionsEnum.chDimYValues, _
OWC.ChartSpecialDataSourcesEnum.chDataLiteral, aY)
'设置绘图空间格式
With ChartSpace1
'.Border.Color = Chartspace1.Constants.chColorNone
End With
'设置图表格式
With Chart1
'.SeriesCollection(0).Interior.Color = "Rosybrown"
'.PlotArea.Interior.Color = "Wheat"
.HasLegend = true
.Legend.Position = _
OWC.ChartLegendPositionEnum.chLegendPositionBottom
.HasTitle = true
.Title.Caption = "XY散点图示例"
.Axes(0).HasTitle = true
.Axes(0).Title.Caption = "Y轴"
.Axes(1).HasTitle = true
.Axes(1).Title.Caption = "X轴"
End With
'以GIF图形的形式返回图表
Response.BinaryWrite(Chartspace1.GetPicture ("gif", 500, 400))
Response.End
End Sub
End Class
|