下面请follow me 编写我们的组件。 1.new project , 请选择activex dll,在project explorer面板上选择project1,然后在属性面板上修改其name为chinaaspchart。同样把里面的class modules修改为pie 2.保存该project,将project存为chinaaspchart.vbp,将class1.cls存为pie.cls。 3.菜单project,选择菜单项references,然后请把microsoft active server pages ojbect library、microsoft excel 9.0 object library、com+ services type library选上。 注意:在nt4/win98上没有com+ service type library这个东东,应该选microsoft transaction server type library 4.编辑pie.cls,代码如下: '------------------------------------------------------------------------------- dim xl dim m_chartname dim m_chartdata() dim m_charttype dim m_filename public errmsg public founderr dim icount type m_value label as string value as double end type dim tvalue as m_value public property let charttype(charttype) m_charttype = charttype end property public property get charttype() charttype = m_charttype end property public property let chartname(chartname) m_chartname = chartname end property public property get chartname() chartname = m_chartname end property public property let filename(fname) m_filename = fname end property public property get filename() filename = m_filename end property
public sub addvalue(label, value) icount = icount + 1 redim preserve m_chartdata(icount) tvalue.label = label tvalue.value = value m_chartdata(icount) = tvalue end sub public sub savechart() on error resume next dim isheet dim i set xl = new excel.application xl.application.workbooks.add xl.workbooks(1).worksheets("sheet1").activate if err.number <> 0 then founderr = true errmsg = err.description err.clear else xl.workbooks(1).worksheets("sheet1").cells("2,1").value = m_chartname for i = 1 to icount xl.worksheets("sheet1").cells(1, i + 1).value = m_chartdata(i).label xl.worksheets("sheet1").cells(2, i + 1).value = m_chartdata(i).value next xl.charts.add xl.activechart.charttype = m_charttype xl.activechart.setsourcedata xl.sheets("sheet1").range("a1:" & chr((icount mod 26) + asc("a")) & "2"), 1 xl.activechart.location 2, "sheet1" with xl.activechart .hastitle = true .charttitle.characters.text = m_chartname end with xl.activechart.applydatalabels 2, false, _ true, false with xl.selection.border .weight = 2 .linestyle = 0 end with
xl.activechart.plotarea.select with xl.selection.border .weight = xlhairline .linestyle = xlnone end with xl.selection.interior.colorindex = xlnone
xl.activewindow.visible = false
xl.displayalerts = false
xl.activechart.export m_filename, filtername:="gif" xl.workbooks.close if err.number <> 0 then founderr = true errmsg = errmsg err.clear end if end if set xl = nothing end sub private sub class_initialize() icount = 0 founderr = false errmsg = "" m_charttype = -4102 'xl3dpie '54 '柱状图 end sub '------------------------------------------------------------------------------- 5. 如果实现柱状图? 实际上前面的代码已经实现了柱状图的功能,只是缺省是饼图功能。调用代码改成如下: dim obj set obj = createobject("chinaaspchart.pie") obj.addvalue "男", 150 obj.addvalue "女", 45 obj.addvalue "不知道", 15 obj.chartname = "性别比例图" obj.filename = "d:\123.gif" obj.charttype=54 obj.savechart 6. 在asp里面调用该组件画图并显示它需要注意的地方。 (1)图片必须生成在web目录下。 (2)asp程序运行在多用户环境下,必须加锁处理 可以通过application实现。其逻辑如下: if application("标志")=0 then 显示图片 else application.lock 生成图片 显示图片 application("标志")=0 application.unlock end if 当然何时需要生成图片置标志位,就需要您自己根据程序的要求来确定了。 总结: com里面调用office组件是一个十分有用的技巧,它的优点是开发相对简单,使用方便,适合企业级低访问量,高业务要求的应用,缺点是占用系统资源高。 程序在windows 2000 server + office 2000 + vb6.0 上测试通过。
下面是用EXCEL的CHART,在窗体上绘制抛物线的图象的例子:Private Sub Command1_Click() Image1.Stretch = True '需先引用Excel 9.0对象库 Dim xlsapp As New Excel.Application Dim xlsbook As Excel.Workbook Dim xlssheet As Excel.Worksheet Set xlsbook = xlsapp.Workbooks.Add Set xlssheet = xlsbook.Worksheets("sheet1") 'xlsapp.Visible = True '坐标值 For x = -5 To 5 xlssheet.Cells(x + 6, 1) = x Y = x ^ 2 '改为y=x^2为抛物线 xlssheet.Cells(x + 6, 2) = Y Next '插入图表 xlssheet.Range("A1:B11").Select xlsapp.Charts.Add xlsapp.ActiveChart.ChartType = xlXYScatterSmooth xlsapp.ActiveChart.SetSourceData Source:=xlssheet.Range("A1:B11"), PlotBy _ :=xlColumns xlsapp.ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1" xlsapp.ActiveChart.HasLegend = False
With xlsapp.ActiveChart .HasTitle = False .Axes(xlCategory, xlPrimary).HasTitle = False .Axes(xlValue, xlPrimary).HasTitle = False .ChartArea.Select .ChartArea.Copy End With xlsapp.DisplayAlerts = False xlsbook.Close xlsapp.Quit Set xlssheet = Nothing Set xlsapp = Nothing
关键字:
在vb组件内调用excel2000实现GIF饼图 贴文时间
2001-5-26 14:32:09 文章类型:
转贴 给贴子投票
gnuljf 转贴 出处: http://www.tongyi.net/content.asp?id=982&ppath=undefined
在vb组件内调用excel2000实现gif饼图
--------------------------------------------------------------------------------
http://www.tongyi.net 点击:405
在vb组件内调用excel2000实现gif饼图
当我第一次使用excel的时候,就为excel的图表功能所倾倒,实在强大,并且那些图也挺漂亮了。后来我尝试着在vb里面调用excel所支持的vba功能,发现功能的确强大,就是十分繁琐。后来就考虑用vb在excel外面包一层,写成对象,去掉我们不需要的特性。这样掉用起来就方便多了,所谓一劳永逸 :p。
在这里,我将像大家介绍一个用vb编写的饼图组件,你只需要给它几个简单的参数,就可以生成一副gif格式的图片给你。调用例子如下:
dim obj
set obj = createobject("chinaaspchart.pie")
obj.addvalue "男", 150
obj.addvalue "女", 45
obj.addvalue "不知道", 15
obj.chartname = "性别比例图"
obj.filename = "d:\123.gif"
obj.savechart
除了在vb里面可以调用,这段代码同样也可以在asp里面调用。
下面请follow me 编写我们的组件。
1.new project , 请选择activex dll,在project explorer面板上选择project1,然后在属性面板上修改其name为chinaaspchart。同样把里面的class modules修改为pie 2.保存该project,将project存为chinaaspchart.vbp,将class1.cls存为pie.cls。 3.菜单project,选择菜单项references,然后请把microsoft active server pages ojbect library、microsoft excel 9.0 object library、com+ services type library选上。
注意:在nt4/win98上没有com+ service type library这个东东,应该选microsoft transaction server type library 4.编辑pie.cls,代码如下:
'-------------------------------------------------------------------------------
dim xl
dim m_chartname
dim m_chartdata()
dim m_charttype
dim m_filename
public errmsg
public founderr
dim icount
type m_value
label as string
value as double
end type
dim tvalue as m_value
public property let charttype(charttype)
m_charttype = charttype
end property
public property get charttype()
charttype = m_charttype
end property public property let chartname(chartname)
m_chartname = chartname
end property
public property get chartname()
chartname = m_chartname
end property
public property let filename(fname)
m_filename = fname
end property
public property get filename()
filename = m_filename
end property
public sub addvalue(label, value)
icount = icount + 1
redim preserve m_chartdata(icount)
tvalue.label = label
tvalue.value = value
m_chartdata(icount) = tvalue
end sub
public sub savechart()
on error resume next
dim isheet
dim i
set xl = new excel.application
xl.application.workbooks.add
xl.workbooks(1).worksheets("sheet1").activate
if err.number <> 0 then
founderr = true
errmsg = err.description
err.clear
else
xl.workbooks(1).worksheets("sheet1").cells("2,1").value = m_chartname
for i = 1 to icount
xl.worksheets("sheet1").cells(1, i + 1).value = m_chartdata(i).label
xl.worksheets("sheet1").cells(2, i + 1).value = m_chartdata(i).value
next
xl.charts.add
xl.activechart.charttype = m_charttype
xl.activechart.setsourcedata xl.sheets("sheet1").range("a1:" & chr((icount mod 26) + asc("a")) & "2"), 1
xl.activechart.location 2, "sheet1"
with xl.activechart
.hastitle = true
.charttitle.characters.text = m_chartname
end with
xl.activechart.applydatalabels 2, false, _
true, false
with xl.selection.border
.weight = 2
.linestyle = 0
end with
xl.activechart.plotarea.select
with xl.selection.border
.weight = xlhairline
.linestyle = xlnone
end with
xl.selection.interior.colorindex = xlnone
xl.activewindow.visible = false
xl.displayalerts = false
xl.activechart.export m_filename, filtername:="gif"
xl.workbooks.close
if err.number <> 0 then
founderr = true
errmsg = errmsg
err.clear
end if
end if
set xl = nothing
end sub
private sub class_initialize()
icount = 0
founderr = false
errmsg = ""
m_charttype = -4102 'xl3dpie
'54 '柱状图
end sub
'------------------------------------------------------------------------------- 5. 如果实现柱状图?
实际上前面的代码已经实现了柱状图的功能,只是缺省是饼图功能。调用代码改成如下: dim obj
set obj = createobject("chinaaspchart.pie")
obj.addvalue "男", 150
obj.addvalue "女", 45
obj.addvalue "不知道", 15
obj.chartname = "性别比例图"
obj.filename = "d:\123.gif"
obj.charttype=54
obj.savechart 6. 在asp里面调用该组件画图并显示它需要注意的地方。
(1)图片必须生成在web目录下。
(2)asp程序运行在多用户环境下,必须加锁处理
可以通过application实现。其逻辑如下: if application("标志")=0 then
显示图片
else
application.lock
生成图片
显示图片
application("标志")=0
application.unlock
end if
当然何时需要生成图片置标志位,就需要您自己根据程序的要求来确定了。
总结:
com里面调用office组件是一个十分有用的技巧,它的优点是开发相对简单,使用方便,适合企业级低访问量,高业务要求的应用,缺点是占用系统资源高。
程序在windows 2000 server + office 2000 + vb6.0 上测试通过。
功能特点:
1、多种样式统计图表制作,包括柱状图,折线图,饼图,柏拉图,点状图等;
2、输出多种格式文件,包括BMP,JPG,GIF等格式,输出质量自定义;
3、可在任意位置添加线条、文字、图片;
4、可将统计结果直接输出到浏览器,包括BMP,JPG和GIF格式;
5、柱状图表支持序列数据组比较;
6、图表填充方式支持图片填充;
7、数十个属性灵活控制图表布局和外观(支持3D功能);
8、支持鼠标移上显示数据功能;
9、组件用于WEB开发时只需要服务器端注册即可,无须客户端安装。
10、灵活的接口设计,适用于VB,VC,DELPHI等开发工具。
11、性能稳定,消耗系统资极少,无内存泄露。
Image1.Stretch = True
'需先引用Excel 9.0对象库
Dim xlsapp As New Excel.Application
Dim xlsbook As Excel.Workbook
Dim xlssheet As Excel.Worksheet
Set xlsbook = xlsapp.Workbooks.Add
Set xlssheet = xlsbook.Worksheets("sheet1")
'xlsapp.Visible = True
'坐标值
For x = -5 To 5
xlssheet.Cells(x + 6, 1) = x
Y = x ^ 2
'改为y=x^2为抛物线
xlssheet.Cells(x + 6, 2) = Y
Next
'插入图表
xlssheet.Range("A1:B11").Select
xlsapp.Charts.Add
xlsapp.ActiveChart.ChartType = xlXYScatterSmooth
xlsapp.ActiveChart.SetSourceData Source:=xlssheet.Range("A1:B11"), PlotBy _
:=xlColumns
xlsapp.ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
xlsapp.ActiveChart.HasLegend = False
With xlsapp.ActiveChart
.HasTitle = False
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
.ChartArea.Select
.ChartArea.Copy End With
xlsapp.DisplayAlerts = False
xlsbook.Close
xlsapp.Quit
Set xlssheet = Nothing
Set xlsapp = Nothing
'粘贴图表
Image1.Picture = Clipboard.GetData
End Sub