原出處
下載Microsoft.Office.Interop.Owc11
---------------------------
1.必在加入參考 (COM) Microsoft.Office.Interop.Owc11
2.程式參考範例:
======== vb.net寫法 ===========
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Data.SqlClient
'引用添加數據操作
Imports Microsoft.Office.Interop.Owc11
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strSeriesName As String = "圖表"
Dim ConnectString As String = "workstation id=server;packet size=4096;user id=xxx;data source=xxx.xxx.1.5;persist security info=True;initial catalog=xxxxdata;password=xxxxx"
Dim Sql As String = "SELECT top 12 ENGI_ACODE,ENGI_TOTAL FROM ContractDetail"
Dim myConn As New SqlConnection(ConnectString)
myConn.Open()
Dim Da As New SqlDataAdapter(Sql, myConn)
Dim ds As New DataSet()
Da.Fill(ds)
'存放月
Dim MonNum As String() = New String(ds.Tables(0).Rows.Count) {}
'存放資料
Dim MonCount As String() = New String(ds.Tables(0).Rows.Count) {}
'为数组赋值
For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
MonNum(i) = ds.Tables(0).Rows(i)(0).ToString()
MonCount(i) = ds.Tables(0).Rows(i)(1).ToString()
Next
'為x軸指定特定字符串,以便顯示數據
Dim strXdata As String = [String].Empty
For Each strData As String In MonNum
strXdata += strData & vbTab
Next
Dim strYdata As String = [String].Empty
'為y軸指定特定的字符串,以便與x軸相對應
For Each strValue As String In MonCount
strYdata += strValue & vbTab
Next
'建立ChartSpace來放置圖表
Dim laySpace As ChartSpace = New ChartSpaceClass()
'在ChartSpace對像中添加圖表
Dim InsertChart As ChChart = laySpace.Charts.Add(0)
'指定繪製圖表的類型。類型可以通過OWC.ChartChartTypeEnum值得到
'主要圖形形狀
InsertChart.Type = ChartChartTypeEnum.chChartTypeLine '折線圖
'InsertChart.Type = ChartChartTypeEnum.chChartTypeArea '面積圖
'InsertChart.Type = ChartChartTypeEnum.chChartTypeBarClustered '條形圖
'指定圖表是否需要圖例標註
InsertChart.HasLegend = False
InsertChart.HasTitle = True
'為圖表添加標題
InsertChart.Title.Caption = "清單"
'為x,y軸添加圖示說明
InsertChart.Axes(0).HasTitle = True
InsertChart.Axes(0).Title.Caption = "数量"
'月份
InsertChart.Axes(1).HasTitle = True
'200數據區隔劃分
'InsertChart.Axes(1).Scaling.SplitMinimum = 200
InsertChart.Axes(1).Title.Caption = "月份"
'添加一個series系列
InsertChart.SeriesCollection.Add(0)
'給定series系列的名字
InsertChart.SeriesCollection(0).SetData(ChartDimensionsEnum.chDimSeriesNames, +CInt(ChartSpecialDataSourcesEnum.chDataLiteral), strSeriesName)
'指定分類
InsertChart.SeriesCollection(0).SetData(ChartDimensionsEnum.chDimCategories, +CInt(ChartSpecialDataSourcesEnum.chDataLiteral), strXdata)
'給定值
InsertChart.SeriesCollection(0).SetData(ChartDimensionsEnum.chDimValues, CInt(ChartSpecialDataSourcesEnum.chDataLiteral), strYdata)
'顯示每個點之數據
InsertChart.SeriesCollection(0).DataLabelsCollection.Add()
InsertChart.SeriesCollection(0).DataLabelsCollection(0).HasValue = True
'輸出文件
Dim strAbsolutePath As String = (Server.MapPath(".")) & "\ShowData.gif"
laySpace.ExportPicture(strAbsolutePath, "GIF", 720, 250)
'創建GIF文件的相對路徑
Dim strRelativePath As String = "./ShowData.gif"
'把圖片添加到placeholder中,並在頁面上顯示
Dim strImageTag As String = " "
Me.PlaceHolder1.Controls.Add(New LiteralControl(strImageTag))
End Sub
End Class
沒有留言:
張貼留言