插入图表
利用ChartBuilder,我们可以很轻松地在专业报表中插入图表:
示例
Dim
doc
As New PrintDoc
Dim rt As
New prt.RenderTable
Dim cmd As
New SQLCommand '定义一个SQL命令
Dim dt As
DataTable
'定义一个数据表变量
Dim Count As
Integer =
0
'生成临时表
cmd.CommandText = "Select 产品,Sum(数量) As
数量,Sum(数量 * 单价 * (1 - 折扣)) As 金额 From {订单} Group By 产品"
dt = cmd.ExecuteReader() '生成一个临时表
'生成图表
Dim Chart As
New ChartBuilder '定义一个图表变量
Dim Series As
WinForm.ChartSeries '定义一个图系变量
Chart.VisualEffect = True
'加上这一行,让你的图表更漂亮
Chart.ChartType = ChartTypeEnum.Bar
'图表1类型该为Bar(条形)
Chart.ChartType2 = ChartTypeEnum.XYPlot
'图表2类型该为线形
Chart.DataTable = dt
'设置绑定表
Chart.SeriesList.Clear() '清除图表原来的图系
Series = Chart.SeriesList.Add() '增加一个图系
Series.Text = "数量"
Series.X.DataField = "产品"
'X轴绑定到月份列
Series.Y.DataField = "数量"
'Y轴绑定到数量列
Series = Chart.SeriesList2.Add()
'给图表2增加一个图系
Series.Text = "金额"
Series.X.DataField = "产品"
'X轴绑定到月份列
Series.Y.DataField = "金额"
'Y轴绑定到数量列
Chart.LegendVisible = True
'显示图列
Chart.LegendCompass= CompassEnum.South
'图列显示在南方(底端)
Chart.AxisY.Text = "数量"
Chart.AxisY2.Text = "金额"
Chart.PrintWidth = 150
'图表宽度为150毫米
Chart.PrintHeight = 100
'图标高度为100毫米
'打印表
For Each
Col As
DataCol
In Dt.DataCols
rt.Cells(0,Count).Text
= Col.Name
For r
As
integer =
0
To dt.DataRows.Count -
1
rt.Cells(r +1,Count).Text
= dt.DataRows(r)(Col.Name)
Next
Count = Count + 1
Next
rt.Style.Gridlines.All = New prt.Linedef(Color.Gray)
'灰色网格线
rt.CellStyle.Spacing.All = 1
'内容距离网格线1毫米
rt.Rows(0).Style.TextAlignHorz
= prt.AlignHorzEnum.Center '第一行内容水平居中
doc.Body.Children.Add(rt)
'打印图表
Dim rm As
New prt.RenderImage
rm.Width = 150
'设置尺寸,这个尺寸最好和图表尺寸一致,以免打印出来的图表变形。
rm.Height = 100
rm.Image = Chart.Image()
rm.Style.ImageAlign.StretchHorz = False
'禁止拉伸
rm.Style.ImageAlign.StretchVert = False
rm.Style.Spacing.Top = 3
doc.Body.Children.Add(rm) '将图片对象加入报表
doc.Preview()
执行结果:
本页地址:http://www.foxtable.com/webhelp/topics/1245.htm