I have an interesting situation. I have a report that has a chart on it. The web form (ASP.NET) determines what data is going to the chart itself. In the telerik report I build the chart programmatically based on the data passed in. This means I have to set a public member as a datatable and refresh the report to have the chart recall the need datasource event. The need datasource event fires off the function that builds the chart. Now I recreated this in a windows app and it works perfectly. It just isn't working in the web. I am using 2012 Q1. Here is my code to call below:
In Telerik Report:
Public ChartSource As New DataTable
Private Sub Chart1_NeedDataSource(sender As System.Object, e As System.EventArgs) Handles Chart1.NeedDataSource
GetSeries(ChartSource)
End Sub
In ASP.NET VB:
Dim rpt As New clsQT9Reports.rptCARBarChart()
rpt.ChartSource = DT1 'this is the chart data
rpt.DataSource = dt 'this is a single record datatable that literally just loaded the header on the report
rptVWR.Report = Nothing
rptVWR.Report = rpt
rptVWR.RefreshReport()
In Telerik Report:
Public ChartSource As New DataTable
Private Sub Chart1_NeedDataSource(sender As System.Object, e As System.EventArgs) Handles Chart1.NeedDataSource
GetSeries(ChartSource)
End Sub
In ASP.NET VB:
Dim rpt As New clsQT9Reports.rptCARBarChart()
rpt.ChartSource = DT1 'this is the chart data
rpt.DataSource = dt 'this is a single record datatable that literally just loaded the header on the report
rptVWR.Report = Nothing
rptVWR.Report = rpt
rptVWR.RefreshReport()
3 Answers, 1 is accepted
0
Hi Brant,
You have not posted the code for the GetSeries method, where the actual data source assignment is done. Be aware that the NeedDataSource event of the chart would only fire if the chart has no data source set i.e. its DataSource property is empty. If in the GetSeries method you're setting the data source to the chart definition item, then the observed behavior is expected, because after the first iteration, the chart no longer needs a datasource and there is no reason to fire the NeedDataSource event. To avoid that you should either create the chart manually by creating series and series items or use the processing chart object and its DataSource property i.e.:
Private Sub chart1_NeedDataSource(sender As Object, e As EventArgs)
Dim procChart As Telerik.Reporting.Processing.Chart = DirectCast(sender, Telerik.Reporting.Processing.Chart)
procChart.DataSource = GetSeries(ChartSource)
End Sub
As per your description, it sounds a better idea to have a DataSource for the chart set via the declarative Data Source Components and only modify the data via DataSourceParameter which comes from the web application. This way the chart would always display relevant data.
Regards,
Steve
the Telerik team
You have not posted the code for the GetSeries method, where the actual data source assignment is done. Be aware that the NeedDataSource event of the chart would only fire if the chart has no data source set i.e. its DataSource property is empty. If in the GetSeries method you're setting the data source to the chart definition item, then the observed behavior is expected, because after the first iteration, the chart no longer needs a datasource and there is no reason to fire the NeedDataSource event. To avoid that you should either create the chart manually by creating series and series items or use the processing chart object and its DataSource property i.e.:
Private Sub chart1_NeedDataSource(sender As Object, e As EventArgs)
Dim procChart As Telerik.Reporting.Processing.Chart = DirectCast(sender, Telerik.Reporting.Processing.Chart)
procChart.DataSource = GetSeries(ChartSource)
End Sub
As per your description, it sounds a better idea to have a DataSource for the chart set via the declarative Data Source Components and only modify the data via DataSourceParameter which comes from the web application. This way the chart would always display relevant data.
Regards,
Steve
the Telerik team
HAPPY WITH TELERIK REPORTING? Do you feel that it is fantastic? Or easy to use? Or better than Crystal Reports? Tell the world, and help fellow developers! Write a short review about Telerik Reporting and Telerik Report Designer in Visual Studio Gallery today!
0
Brant
Top achievements
Rank 1
answered on 29 Nov 2012, 02:04 AM
Steve,
Sorry for my delay. I was out of town. I don't think that would work with how my sub GetSeries works. I have attached the code below:
Public Sub GetSeries(_DT As DataTable)
Dim dt As New DataTable, xValue As Double
'Dim CN As New SqlClient.SqlConnection(Me.ReportParameters.Item("ConnectionString").Value)
'Dim CMD As New SqlClient.SqlCommand(Me.ReportParameters.Item("sSQL").Value, CN)
' CN.Open()
'Dim DA As New SqlClient.SqlDataAdapter(CMD)
' DA.Fill(dt)
' CN.Close()
dt = _DT
Chart1.PlotArea.XAxis.AutoScale = False
Chart1.PlotArea.XAxis.AutoShrink = False
Chart1.Style.BackgroundColor = Color.LightGray
Chart1.SeriesOrientation = Charting.ChartSeriesOrientation.Horizontal
'get the column count to determine how many series there are per row
Dim iCOL As Integer, stSeries As String = ""
iCOL = dt.Columns.Count
If dt.Columns.Count > 2 Then
If Me.ReportParameters.Item("stack").Value = False Then 'do multi series bar chart
Dim oSeries As Charting.ChartSeries = New Charting.ChartSeries("Open", Charting.ChartSeriesType.Bar)
Chart1.Series.Add(oSeries)
Dim rSeries As Charting.ChartSeries = New Charting.ChartSeries("Rejected", Charting.ChartSeriesType.Bar)
Chart1.Series.Add(rSeries)
Dim sSeries As Charting.ChartSeries = New Charting.ChartSeries("Submitted", Charting.ChartSeriesType.Bar)
Chart1.Series.Add(sSeries)
Dim cSeries As Charting.ChartSeries = New Charting.ChartSeries("Closed", Charting.ChartSeriesType.Bar)
Chart1.Series.Add(cSeries)
xValue = -1
Dim axisItem As Charting.ChartAxisItem
For i As Integer = 0 To dt.Rows.Count - 1
If stSeries <> dt.Rows(i).Item(0).ToString Then
xValue = xValue + 1
axisItem = New Charting.ChartAxisItem(dt.Rows(i).Item(0).ToString)
axisItem.Value = xValue
Chart1.PlotArea.XAxis.AddItem(axisItem)
stSeries = dt.Rows(i).Item(0).ToString
End If
Select Case dt.Rows(i).Item(1)
Case Is = 1
Dim seriesItem As New Telerik.Reporting.Charting.ChartSeriesItem
seriesItem.YValue = dt.Rows(i).Item(iCOL - 1)
seriesItem.XValue = xValue
seriesItem.Appearance.FillStyle.SecondColor = Color.DarkRed
seriesItem.Appearance.FillStyle.MainColor = Color.Red
seriesItem.Name = dt.Rows(i).Item(0).ToString
oSeries.AddItem(seriesItem)
Case Is = 2
Dim seriesItem As New Telerik.Reporting.Charting.ChartSeriesItem
seriesItem.YValue = dt.Rows(i).Item(iCOL - 1)
seriesItem.XValue = xValue
seriesItem.Appearance.FillStyle.SecondColor = Color.Orange
seriesItem.Appearance.FillStyle.MainColor = Color.DarkOrange
seriesItem.Name = dt.Rows(i).Item(0).ToString
rSeries.AddItem(seriesItem)
Case Is = 3
Dim seriesItem As New Telerik.Reporting.Charting.ChartSeriesItem
seriesItem.YValue = dt.Rows(i).Item(iCOL - 1)
seriesItem.XValue = xValue
seriesItem.Appearance.FillStyle.SecondColor = Color.Blue
seriesItem.Appearance.FillStyle.MainColor = Color.DarkBlue
seriesItem.Name = dt.Rows(i).Item(0).ToString
sSeries.AddItem(seriesItem)
Case Is = 4
Dim seriesItem As New Telerik.Reporting.Charting.ChartSeriesItem
seriesItem.YValue = dt.Rows(i).Item(iCOL - 1)
seriesItem.XValue = xValue
seriesItem.Appearance.FillStyle.SecondColor = Color.DarkGreen
seriesItem.Appearance.FillStyle.MainColor = Color.LightGreen
seriesItem.Name = dt.Rows(i).Item(0).ToString
cSeries.AddItem(seriesItem)
End Select
Next
Else 'do stacked bar chart
Dim oSeries As Charting.ChartSeries = New Charting.ChartSeries("Open", Charting.ChartSeriesType.StackedBar)
Dim rSeries As Charting.ChartSeries = New Charting.ChartSeries("Rejected", Charting.ChartSeriesType.StackedBar)
Dim sSeries As Charting.ChartSeries = New Charting.ChartSeries("Submitted", Charting.ChartSeriesType.StackedBar)
Dim cSeries As Charting.ChartSeries = New Charting.ChartSeries("Closed", Charting.ChartSeriesType.StackedBar)
stSeries = ""
xValue = -1
Dim axisItem As Charting.ChartAxisItem
For i As Integer = 0 To dt.Rows.Count - 1
If stSeries <> dt.Rows(i).Item(0).ToString Then
xValue = xValue + 1
axisItem = New Charting.ChartAxisItem(dt.Rows(i).Item(0).ToString)
axisItem.Value = xValue
Chart1.PlotArea.XAxis.AddItem(axisItem)
stSeries = dt.Rows(i).Item(0).ToString
End If
Select Case dt.Rows(i).Item(1)
Case Is = 1
Dim seriesItem As New Telerik.Reporting.Charting.ChartSeriesItem
seriesItem.YValue = dt.Rows(i).Item(iCOL - 1)
seriesItem.XValue = xValue
seriesItem.Appearance.FillStyle.SecondColor = Color.DarkRed
seriesItem.Appearance.FillStyle.MainColor = Color.Red
seriesItem.Name = dt.Rows(i).Item(0).ToString
oSeries.AddItem(seriesItem)
Case Is = 2
Dim seriesItem As New Telerik.Reporting.Charting.ChartSeriesItem
seriesItem.YValue = dt.Rows(i).Item(iCOL - 1)
seriesItem.XValue = xValue
seriesItem.Appearance.FillStyle.SecondColor = Color.DarkGoldenrod
seriesItem.Appearance.FillStyle.MainColor = Color.Goldenrod
seriesItem.Name = dt.Rows(i).Item(0).ToString
rSeries.AddItem(seriesItem)
Case Is = 3
Dim seriesItem As New Telerik.Reporting.Charting.ChartSeriesItem
seriesItem.YValue = dt.Rows(i).Item(iCOL - 1)
seriesItem.XValue = xValue
seriesItem.Appearance.FillStyle.SecondColor = Color.DarkBlue
seriesItem.Appearance.FillStyle.MainColor = Color.Blue
seriesItem.Name = dt.Rows(i).Item(0).ToString
sSeries.AddItem(seriesItem)
Case Is = 4
Dim seriesItem As New Telerik.Reporting.Charting.ChartSeriesItem
seriesItem.YValue = dt.Rows(i).Item(iCOL - 1)
seriesItem.XValue = xValue
seriesItem.Appearance.FillStyle.SecondColor = Color.DarkGreen
seriesItem.Appearance.FillStyle.MainColor = Color.LightGreen
seriesItem.Name = dt.Rows(i).Item(0).ToString
cSeries.AddItem(seriesItem)
End Select
Next
Chart1.PlotArea.XAxis.AutoScale = False
Chart1.Series.Add(oSeries)
Chart1.Series.Add(rSeries)
Chart1.Series.Add(sSeries)
Chart1.Series.Add(cSeries)
End If
Else
Dim cSeries As New Telerik.Reporting.Charting.ChartSeries
cSeries.Name = "CorrectiveActions"
stSeries = cSeries.Name
cSeries.Type = Charting.ChartSeriesType.Bar
For i As Integer = 0 To dt.Rows.Count - 1
Dim seriesItem As New Telerik.Reporting.Charting.ChartSeriesItem
seriesItem.YValue = dt.Rows(i).Item(1)
seriesItem.Appearance.FillStyle.SecondColor = Color.Red
seriesItem.Appearance.FillStyle.MainColor = Color.DarkRed
seriesItem.Name = dt.Rows(i).Item(0).ToString
cSeries.AddItem(seriesItem)
Chart1.PlotArea.XAxis.AddItem(dt.Rows(i).Item(0).ToString)
Next
cSeries.Appearance.TextAppearance.TextProperties.Color = Color.DarkBlue
Chart1.Series.Add(cSeries)
End If
If dt.Columns.Count > 2 Then
Chart1.Legend.Visible = True
Dim CARColors(3) As Color
CARColors(0) = Color.Red
CARColors(1) = Color.Orange
CARColors(2) = Color.Green
CARColors(3) = Color.Blue
Dim seriesP As Charting.Palette = New Charting.Palette("CAR", CARColors, CARColors)
Chart1.CustomPalettes.Add(seriesP)
Chart1.SeriesPalette = "CAR"
Else
Chart1.Legend.Visible = False
End If
Chart1.PlotArea.XAxis.AxisLabel.TextBlock.Text = Me.ReportParameters.Item("Xaxis").Value
Chart1.PlotArea.XAxis.AxisLabel.Visible = True
Chart1.PlotArea.YAxis.AxisLabel.TextBlock.Text = Me.ReportParameters.Item("Yaxis").Value
Chart1.PlotArea.YAxis.AxisLabel.Visible = True
Chart1.ChartTitle.TextBlock.Text = Me.ReportParameters.Item("header").Value
Chart1.ImageFormat = System.Drawing.Imaging.ImageFormat.Png
End Sub
Sorry for my delay. I was out of town. I don't think that would work with how my sub GetSeries works. I have attached the code below:
Public Sub GetSeries(_DT As DataTable)
Dim dt As New DataTable, xValue As Double
'Dim CN As New SqlClient.SqlConnection(Me.ReportParameters.Item("ConnectionString").Value)
'Dim CMD As New SqlClient.SqlCommand(Me.ReportParameters.Item("sSQL").Value, CN)
' CN.Open()
'Dim DA As New SqlClient.SqlDataAdapter(CMD)
' DA.Fill(dt)
' CN.Close()
dt = _DT
Chart1.PlotArea.XAxis.AutoScale = False
Chart1.PlotArea.XAxis.AutoShrink = False
Chart1.Style.BackgroundColor = Color.LightGray
Chart1.SeriesOrientation = Charting.ChartSeriesOrientation.Horizontal
'get the column count to determine how many series there are per row
Dim iCOL As Integer, stSeries As String = ""
iCOL = dt.Columns.Count
If dt.Columns.Count > 2 Then
If Me.ReportParameters.Item("stack").Value = False Then 'do multi series bar chart
Dim oSeries As Charting.ChartSeries = New Charting.ChartSeries("Open", Charting.ChartSeriesType.Bar)
Chart1.Series.Add(oSeries)
Dim rSeries As Charting.ChartSeries = New Charting.ChartSeries("Rejected", Charting.ChartSeriesType.Bar)
Chart1.Series.Add(rSeries)
Dim sSeries As Charting.ChartSeries = New Charting.ChartSeries("Submitted", Charting.ChartSeriesType.Bar)
Chart1.Series.Add(sSeries)
Dim cSeries As Charting.ChartSeries = New Charting.ChartSeries("Closed", Charting.ChartSeriesType.Bar)
Chart1.Series.Add(cSeries)
xValue = -1
Dim axisItem As Charting.ChartAxisItem
For i As Integer = 0 To dt.Rows.Count - 1
If stSeries <> dt.Rows(i).Item(0).ToString Then
xValue = xValue + 1
axisItem = New Charting.ChartAxisItem(dt.Rows(i).Item(0).ToString)
axisItem.Value = xValue
Chart1.PlotArea.XAxis.AddItem(axisItem)
stSeries = dt.Rows(i).Item(0).ToString
End If
Select Case dt.Rows(i).Item(1)
Case Is = 1
Dim seriesItem As New Telerik.Reporting.Charting.ChartSeriesItem
seriesItem.YValue = dt.Rows(i).Item(iCOL - 1)
seriesItem.XValue = xValue
seriesItem.Appearance.FillStyle.SecondColor = Color.DarkRed
seriesItem.Appearance.FillStyle.MainColor = Color.Red
seriesItem.Name = dt.Rows(i).Item(0).ToString
oSeries.AddItem(seriesItem)
Case Is = 2
Dim seriesItem As New Telerik.Reporting.Charting.ChartSeriesItem
seriesItem.YValue = dt.Rows(i).Item(iCOL - 1)
seriesItem.XValue = xValue
seriesItem.Appearance.FillStyle.SecondColor = Color.Orange
seriesItem.Appearance.FillStyle.MainColor = Color.DarkOrange
seriesItem.Name = dt.Rows(i).Item(0).ToString
rSeries.AddItem(seriesItem)
Case Is = 3
Dim seriesItem As New Telerik.Reporting.Charting.ChartSeriesItem
seriesItem.YValue = dt.Rows(i).Item(iCOL - 1)
seriesItem.XValue = xValue
seriesItem.Appearance.FillStyle.SecondColor = Color.Blue
seriesItem.Appearance.FillStyle.MainColor = Color.DarkBlue
seriesItem.Name = dt.Rows(i).Item(0).ToString
sSeries.AddItem(seriesItem)
Case Is = 4
Dim seriesItem As New Telerik.Reporting.Charting.ChartSeriesItem
seriesItem.YValue = dt.Rows(i).Item(iCOL - 1)
seriesItem.XValue = xValue
seriesItem.Appearance.FillStyle.SecondColor = Color.DarkGreen
seriesItem.Appearance.FillStyle.MainColor = Color.LightGreen
seriesItem.Name = dt.Rows(i).Item(0).ToString
cSeries.AddItem(seriesItem)
End Select
Next
Else 'do stacked bar chart
Dim oSeries As Charting.ChartSeries = New Charting.ChartSeries("Open", Charting.ChartSeriesType.StackedBar)
Dim rSeries As Charting.ChartSeries = New Charting.ChartSeries("Rejected", Charting.ChartSeriesType.StackedBar)
Dim sSeries As Charting.ChartSeries = New Charting.ChartSeries("Submitted", Charting.ChartSeriesType.StackedBar)
Dim cSeries As Charting.ChartSeries = New Charting.ChartSeries("Closed", Charting.ChartSeriesType.StackedBar)
stSeries = ""
xValue = -1
Dim axisItem As Charting.ChartAxisItem
For i As Integer = 0 To dt.Rows.Count - 1
If stSeries <> dt.Rows(i).Item(0).ToString Then
xValue = xValue + 1
axisItem = New Charting.ChartAxisItem(dt.Rows(i).Item(0).ToString)
axisItem.Value = xValue
Chart1.PlotArea.XAxis.AddItem(axisItem)
stSeries = dt.Rows(i).Item(0).ToString
End If
Select Case dt.Rows(i).Item(1)
Case Is = 1
Dim seriesItem As New Telerik.Reporting.Charting.ChartSeriesItem
seriesItem.YValue = dt.Rows(i).Item(iCOL - 1)
seriesItem.XValue = xValue
seriesItem.Appearance.FillStyle.SecondColor = Color.DarkRed
seriesItem.Appearance.FillStyle.MainColor = Color.Red
seriesItem.Name = dt.Rows(i).Item(0).ToString
oSeries.AddItem(seriesItem)
Case Is = 2
Dim seriesItem As New Telerik.Reporting.Charting.ChartSeriesItem
seriesItem.YValue = dt.Rows(i).Item(iCOL - 1)
seriesItem.XValue = xValue
seriesItem.Appearance.FillStyle.SecondColor = Color.DarkGoldenrod
seriesItem.Appearance.FillStyle.MainColor = Color.Goldenrod
seriesItem.Name = dt.Rows(i).Item(0).ToString
rSeries.AddItem(seriesItem)
Case Is = 3
Dim seriesItem As New Telerik.Reporting.Charting.ChartSeriesItem
seriesItem.YValue = dt.Rows(i).Item(iCOL - 1)
seriesItem.XValue = xValue
seriesItem.Appearance.FillStyle.SecondColor = Color.DarkBlue
seriesItem.Appearance.FillStyle.MainColor = Color.Blue
seriesItem.Name = dt.Rows(i).Item(0).ToString
sSeries.AddItem(seriesItem)
Case Is = 4
Dim seriesItem As New Telerik.Reporting.Charting.ChartSeriesItem
seriesItem.YValue = dt.Rows(i).Item(iCOL - 1)
seriesItem.XValue = xValue
seriesItem.Appearance.FillStyle.SecondColor = Color.DarkGreen
seriesItem.Appearance.FillStyle.MainColor = Color.LightGreen
seriesItem.Name = dt.Rows(i).Item(0).ToString
cSeries.AddItem(seriesItem)
End Select
Next
Chart1.PlotArea.XAxis.AutoScale = False
Chart1.Series.Add(oSeries)
Chart1.Series.Add(rSeries)
Chart1.Series.Add(sSeries)
Chart1.Series.Add(cSeries)
End If
Else
Dim cSeries As New Telerik.Reporting.Charting.ChartSeries
cSeries.Name = "CorrectiveActions"
stSeries = cSeries.Name
cSeries.Type = Charting.ChartSeriesType.Bar
For i As Integer = 0 To dt.Rows.Count - 1
Dim seriesItem As New Telerik.Reporting.Charting.ChartSeriesItem
seriesItem.YValue = dt.Rows(i).Item(1)
seriesItem.Appearance.FillStyle.SecondColor = Color.Red
seriesItem.Appearance.FillStyle.MainColor = Color.DarkRed
seriesItem.Name = dt.Rows(i).Item(0).ToString
cSeries.AddItem(seriesItem)
Chart1.PlotArea.XAxis.AddItem(dt.Rows(i).Item(0).ToString)
Next
cSeries.Appearance.TextAppearance.TextProperties.Color = Color.DarkBlue
Chart1.Series.Add(cSeries)
End If
If dt.Columns.Count > 2 Then
Chart1.Legend.Visible = True
Dim CARColors(3) As Color
CARColors(0) = Color.Red
CARColors(1) = Color.Orange
CARColors(2) = Color.Green
CARColors(3) = Color.Blue
Dim seriesP As Charting.Palette = New Charting.Palette("CAR", CARColors, CARColors)
Chart1.CustomPalettes.Add(seriesP)
Chart1.SeriesPalette = "CAR"
Else
Chart1.Legend.Visible = False
End If
Chart1.PlotArea.XAxis.AxisLabel.TextBlock.Text = Me.ReportParameters.Item("Xaxis").Value
Chart1.PlotArea.XAxis.AxisLabel.Visible = True
Chart1.PlotArea.YAxis.AxisLabel.TextBlock.Text = Me.ReportParameters.Item("Yaxis").Value
Chart1.PlotArea.YAxis.AxisLabel.Visible = True
Chart1.ChartTitle.TextBlock.Text = Me.ReportParameters.Item("header").Value
Chart1.ImageFormat = System.Drawing.Imaging.ImageFormat.Png
End Sub
0
Hi Brant,
Thanks for the code, we see now that you customize the chart item, so that you cannot accomplish this with direct binding to a data source component. Also we see that you do not set DataSource to the chart definition or processing counterpart, so that could not be the problem either.
The only thing left that I can think of is if you are using an out-proc session for your web application. In such case, to utilize the events of the chart item (NeedDataSource, ItemDataBinding, ItemDataBound) you have to attach them in the ItemDataBinding event of the Report. For example:
Regards,
Steve
the Telerik team
Thanks for the code, we see now that you customize the chart item, so that you cannot accomplish this with direct binding to a data source component. Also we see that you do not set DataSource to the chart definition or processing counterpart, so that could not be the problem either.
The only thing left that I can think of is if you are using an out-proc session for your web application. In such case, to utilize the events of the chart item (NeedDataSource, ItemDataBinding, ItemDataBound) you have to attach them in the ItemDataBinding event of the Report. For example:
Private
Sub
Report1_ItemDataBinding(sender
As
System.
Object
, e
As
System.EventArgs)
Handles
MyBase
.ItemDataBinding
Dim
processingReport =
DirectCast
(sender, Telerik.Reporting.Processing.Report)
Dim
reportDef =
DirectCast
(processingReport.ItemDefinition, Telerik.Reporting.Report)
Dim
chart
As
Telerik.Reporting.Chart =
DirectCast
(reportDef.Items.Find(
"Chart1"
,
True
)(0), Telerik.Reporting.Chart)
AddHandler
chart.NeedDataSource,
AddressOf
Chart1_NeedDataSource
End
Sub
Private
Sub
Chart1_NeedDataSource(sender
As
System.
Object
, e
As
System.EventArgs)
Dim
processingChart =
DirectCast
(sender, Telerik.Reporting.Processing.Chart)
Dim
chartDef =
DirectCast
(sender, Telerik.Reporting.Chart)
' ...
End
Sub
Regards,
Steve
the Telerik team
HAPPY WITH TELERIK REPORTING? Do you feel that it is fantastic? Or easy to use? Or better than Crystal Reports? Tell the world, and help fellow developers! Write a short review about Telerik Reporting and Telerik Report Designer in Visual Studio Gallery today!