Search code examples
.netvb.netactivereports

How to draw a chart in runtime using Active Reports?


I need to draw a chart in runtime and save it as a jpg file using Active Reports.

I already managed to create a SectionReport and add a ChartControl to it:

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\")

The Export method works and generates the jpg, the problem is that the chart doesn't appear in the image; in it's place, there's a message that says "Failed to draw the chart.". I've tested the Export method with a chart created on the Active Reports designer, and it worked perfectly.

That's the generated jpg file:

enter image description here


Solution

  • I end up with the following code. I really don't know why, but it worked.

    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\")