使用Office2000中的图表控件绘制图形~

      示例中使用 ADO 记录集中的数据创建图表。首先根据返回的记录集数据创建以制表符分隔的字符串,然后通过 SetData 方法并使用此字符串设置图表数据。
       下面用的数据结构如图:

这个,可以从SQL Server里面的自带的数据库导过来。

<object id="ChartSpace1" classid="CLSID:0002E500-0000-0000-C000-000000000046" style="width:80%;height:350"></object>
<object id="ADOConnection1" runat="server" classid="CLSID:00000514-0000-0010-8000-00AA006D2EA4"></object>
这里的ADOConnection1可以通过Server.CreateObject("ADODB.Connection")来创建,不过ChartSpace在服务器端的创建,我太清楚,所以就用客户端的创建方式。
<%
 Dim rs, categories, values
 categories = ""
 values = ""
 
 '打开连接执行sql查询 (建立“nwind.mdb”库,并创建名为“Category Sales for 1997”的表)
 ADOConnection1.ConnectionString = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("nwind.mdb")
 ADOConnection1.Open
 
 Set rs = ADOConnection1.Execute("SELECT * FROM [Category Sales for 1997]")
 '对于每一字段记录产生一个由制表符分隔的字符串
 rs.MoveFirst
 Do while Not rs.EOF
  categories = categories & rs.Fields(0).Value & Chr(9)
  values = values & rs.Fields(1).Value & Chr(9)
  rs.MoveNext
 Loop
 rs.Close
 ADOConnection1.Close

 '删去字符串末尾的分隔符
 categories = Left(categories, Len(categories) - 1)
 values = Left(values, Len(values) - 1)
%>
<Script Language="VBScript">
 chChartTypeCombo = -1
 chChartTypeColumnClustered = 0
 chChartTypeColumnStacked = 1
 chChartTypeColumnStacked100 = 2
 chChartTypeBarClustered = 3
 chChartTypeBarStacked = 4
 chChartTypeBarStacked100 = 5
 chChartTypeLine = 6
 chChartTypeLineMarkers = 7
 chChartTypeLineStacked = 8
 chChartTypeLineStackedMarkers = 9
 chChartTypeLineStacked100 = 10
 chChartTypeLineStacked100Markers = 11
 chChartTypeSmoothLine = 12
 chChartTypeSmoothLineMarkers = 13
 chChartTypeSmoothLineStacked = 14
 chChartTypeSmoothLineStackedMarkers = 15
 chChartTypeSmoothLineStacked100 = 16
 chChartTypeSmoothLineStacked100Markers = 17
 chChartTypePie = 18
 chChartTypePieExploded = 19
 chChartTypePieStacked = 20
 chChartTypeScatterMarkers = 21
 chChartTypeScatterSmoothLineMarkers = 22
 chChartTypeScatterSmoothLine = 23
 chChartTypeScatterLineMarkers = 24
 chChartTypeScatterLine = 25
 chChartTypeScatterLineFilled = 26
 chChartTypeBubble = 27
 chChartTypeBubbleLine = 28
 chChartTypeArea = 29
 chChartTypeAreaStacked = 30
 chChartTypeAreaStacked100 = 31
 chChartTypeDoughnut = 32
 chChartTypeDoughnutExploded = 33
 chChartTypeRadarLine = 34
 chChartTypeRadarLineMarkers = 35
 chChartTypeRadarLineFilled = 36
 chChartTypeRadarSmoothLine = 37
 chChartTypeRadarSmoothLineMarkers = 38
 chChartTypeStockHLC = 39
 chChartTypeStockOHLC = 40
 chChartTypePolarMarkers = 41
 chChartTypePolarLine = 42
 chChartTypePolarLineMarkers = 43
 chChartTypePolarSmoothLine = 44
 chChartTypePolarSmoothLineMarkers = 45
 '创建一个系列
 ChartSpace1.Clear
 ChartSpace1.Charts.Add
 ChartSpace1.Charts(0).SeriesCollection.Add
 ChartSpace1.Charts(0).SeriesCollection(0).Caption = "Sales"

 '使用recordset生成的字符串设置系列的类型和值
 Set c = ChartSpace1.Constants
 ChartSpace1.Charts(0).SeriesCollection(0).SetData c.chDimCategories, c.chDataLiteral, "<%=categories%>"
 ChartSpace1.Charts(0).SeriesCollection(0).SetData c.chDimValues, c.chDataLiteral, "<%=values%>"

 '设置图表工作区的标题,并将图表工作区的图例放置于工作区的右边。
 ChartSpace1.HasChartSpaceTitle = True
 With ChartSpace1.ChartSpaceTitle
  .Caption = "Monthly Sales Data"
  .Font.Size = 12
  .Font.Color = "#FF0000"
  .Font.Bold = True
 End with

 ChartSpace1.HasChartSpaceLegend = True
 With ChartSpace1.ChartSpaceLegend
  .Position = c.chLegendPositionright
  .Font.Color = "#009999"
  .Font.Size = 9
 End with

 '设置图表类型(具体样式见附录)
 ChartSpace1.Charts(0).Type = c.chChartTypeBarClustered
 '轴的坐标格式、样式(有坐标轴时才设)
 With ChartSpace1.Charts(0).Axes(c.chAxisPositionBottom)
  .NumberFormat = "#,##0"
  .Font.Size = 9
 End with

 With ChartSpace1.Charts(0).Axes(c.chAxisPositionLeft)
  .Font.Color = "#0000ff"
  .Font.Size = 9
 End with
</Script>
运行效果:

你可能感兴趣的:(使用Office2000中的图表控件绘制图形~)