如何使用 Active Reports 在运行时绘制图表?
How to draw a chart in runtime using Active Reports?
我需要在运行时绘制图表并使用 Active Reports.
将其保存为 jpg 文件
我已经设法创建了一个 SectionReport
并向其中添加了一个 ChartControl
:
Dim chartControl As New ChartControl()
Dim series As New GrapeCity.ActiveReports.Chart.Series
series.Type = Chart.ChartType.Bar3D
Dim objs As New List(Of Teste)()
objs.Add(New Teste With {.Nome = "TESTE1", .Valor = 15})
objs.Add(New Teste With {.Nome = "TESTE2", .Valor = 22})
objs.Add(New Teste With {.Nome = "TESTE3", .Valor = 10})
objs.Add(New Teste With {.Nome = "TESTE4", .Valor = 36})
Dim chartArea As New ChartArea()
chartControl.ChartAreas.Add(chartArea)
chartControl.DataSource = ConvertToDataTable(objs)
chartControl.Series.Add(series)
chartControl.Series(0).ValueMembersY = "Nome"
chartControl.Series(0).ValueMemberX = "Valor"
Dim rpt As New SectionReport()
rpt.DataSource = ConvertToDataTable(objs)
rpt.Sections.InsertPageHF()
rpt.Sections(0).BackColor = Color.LightGray
rpt.Sections.Insert(1, New Detail())
rpt.Sections(1).BackColor = Color.PeachPuff
rpt.Sections(1).Height = 1.5
rpt.Sections(0).Controls.Add(chartControl)
rpt.Run()
Export(rpt.Document, "C:\Testes\")
Export
方法有效并生成了jpg,问题是图表没有出现在图像中;在它的位置,有一条消息说 "Failed to draw the chart."。我已经使用在 Active Reports 设计器上创建的图表测试了 Export 方法,它运行良好。
这是生成的jpg文件:
我最终得到以下代码。我真的不知道为什么,但它奏效了。
Dim objs As New List(Of Teste)()
objs.Add(New Teste With {.Nome = "TESTE1", .Valor = 15})
objs.Add(New Teste With {.Nome = "TESTE2", .Valor = 22})
objs.Add(New Teste With {.Nome = "TESTE3", .Valor = 10})
objs.Add(New Teste With {.Nome = "TESTE4", .Valor = 36})
objs.Add(New Teste With {.Nome = "TESTE5", .Valor = 36})
objs.Add(New Teste With {.Nome = "TESTE6", .Valor = 36})
objs.Add(New Teste With {.Nome = "TESTE7", .Valor = 36})
objs.Add(New Teste With {.Nome = "TESTE8", .Valor = 36})
objs.Add(New Teste With {.Nome = "TESTE9", .Valor = 36})
Dim chartArea As New ChartArea(True)
Dim series As New Series()
For Each obj As Teste In objs
Dim pt As New DataPoint()
pt.XValue = obj.Nome
pt.YValues = New DoubleArray(New Double() {obj.Valor})
series.Points.Add(pt)
Next
series.ChartArea = chartArea
series.Type = ChartType.Bar2D
series.ColorPalette = ColorPalette.Iceberg
Dim chartControl As New ChartControl()
chartControl.DataSource = ConvertToDataTable(objs)
chartControl.Series.Add(series)
chartControl.ChartAreas.Add(chartArea)
Dim rpt As New SectionReport()
rpt.Sections.InsertPageHF()
rpt.Sections.Insert(1, New Detail())
rpt.PageSettings.Orientation = PageOrientation.Landscape
chartControl.Width = CSng(rpt.PageSettings.PaperWidth / 1.35)
chartControl.Height = CSng(rpt.PageSettings.PaperHeight / 5)
rpt.Sections(1).Controls.Add(chartControl)
rpt.Run()
Export(rpt.Document, "C:\Testes\")
我需要在运行时绘制图表并使用 Active Reports.
将其保存为 jpg 文件我已经设法创建了一个 SectionReport
并向其中添加了一个 ChartControl
:
Dim chartControl As New ChartControl()
Dim series As New GrapeCity.ActiveReports.Chart.Series
series.Type = Chart.ChartType.Bar3D
Dim objs As New List(Of Teste)()
objs.Add(New Teste With {.Nome = "TESTE1", .Valor = 15})
objs.Add(New Teste With {.Nome = "TESTE2", .Valor = 22})
objs.Add(New Teste With {.Nome = "TESTE3", .Valor = 10})
objs.Add(New Teste With {.Nome = "TESTE4", .Valor = 36})
Dim chartArea As New ChartArea()
chartControl.ChartAreas.Add(chartArea)
chartControl.DataSource = ConvertToDataTable(objs)
chartControl.Series.Add(series)
chartControl.Series(0).ValueMembersY = "Nome"
chartControl.Series(0).ValueMemberX = "Valor"
Dim rpt As New SectionReport()
rpt.DataSource = ConvertToDataTable(objs)
rpt.Sections.InsertPageHF()
rpt.Sections(0).BackColor = Color.LightGray
rpt.Sections.Insert(1, New Detail())
rpt.Sections(1).BackColor = Color.PeachPuff
rpt.Sections(1).Height = 1.5
rpt.Sections(0).Controls.Add(chartControl)
rpt.Run()
Export(rpt.Document, "C:\Testes\")
Export
方法有效并生成了jpg,问题是图表没有出现在图像中;在它的位置,有一条消息说 "Failed to draw the chart."。我已经使用在 Active Reports 设计器上创建的图表测试了 Export 方法,它运行良好。
这是生成的jpg文件:
我最终得到以下代码。我真的不知道为什么,但它奏效了。
Dim objs As New List(Of Teste)()
objs.Add(New Teste With {.Nome = "TESTE1", .Valor = 15})
objs.Add(New Teste With {.Nome = "TESTE2", .Valor = 22})
objs.Add(New Teste With {.Nome = "TESTE3", .Valor = 10})
objs.Add(New Teste With {.Nome = "TESTE4", .Valor = 36})
objs.Add(New Teste With {.Nome = "TESTE5", .Valor = 36})
objs.Add(New Teste With {.Nome = "TESTE6", .Valor = 36})
objs.Add(New Teste With {.Nome = "TESTE7", .Valor = 36})
objs.Add(New Teste With {.Nome = "TESTE8", .Valor = 36})
objs.Add(New Teste With {.Nome = "TESTE9", .Valor = 36})
Dim chartArea As New ChartArea(True)
Dim series As New Series()
For Each obj As Teste In objs
Dim pt As New DataPoint()
pt.XValue = obj.Nome
pt.YValues = New DoubleArray(New Double() {obj.Valor})
series.Points.Add(pt)
Next
series.ChartArea = chartArea
series.Type = ChartType.Bar2D
series.ColorPalette = ColorPalette.Iceberg
Dim chartControl As New ChartControl()
chartControl.DataSource = ConvertToDataTable(objs)
chartControl.Series.Add(series)
chartControl.ChartAreas.Add(chartArea)
Dim rpt As New SectionReport()
rpt.Sections.InsertPageHF()
rpt.Sections.Insert(1, New Detail())
rpt.PageSettings.Orientation = PageOrientation.Landscape
chartControl.Width = CSng(rpt.PageSettings.PaperWidth / 1.35)
chartControl.Height = CSng(rpt.PageSettings.PaperHeight / 5)
rpt.Sections(1).Controls.Add(chartControl)
rpt.Run()
Export(rpt.Document, "C:\Testes\")