Hello,
I am trying to add series and series items at runtime to an empty chart in a report.
However, albeit I can see the changes I'm making at runtime happening they do not reflect in the Chart.
Here's my NeedDataSource Event code:
If I execute this code, the chart will be EMPTY...and showing the message: There is no or empty Series. And the Legend has 1 Item with a label text of Series xx.
IF I eventually change my NeedDataSource event to:
The chart will show the values correctly. However I can't change each indivual item values or appearance at runtime! Firstly, I get an exception if I try to use the following line after chart1.Datasource = produtos:
I get an out of bound exception.
If I put that line of code on the ItemDataBound Event it works fine:
As a side note, I'm loading the report on a reportviewer and I initialize it like so:
Even though I'm calling the RefreshReport Method, whenever I execute the application I'm forced to manually press the Refresh Button on the report viewer just to watch my ItemDataBound code be actually rendered...what the heck?!
Back to my ItemDataBound event...I can actually access the serie's Items, but any change I make to them (be it YValues or Appearance stuff) it WONT reflect on my reportviewer no matter how much I smash the refresh button!
Here's my complete code:
ReportTest.cs
(chart1 was just dropped onto the detail section in Design time. No other changes were made!)
Form1.cs
Please help... I already lost a whole day of work trying to figure this out.
P.S.: I followed EVERY single tutorial regarding adding series at runtime and NONE OF THEM WORK on my end.
I am trying to add series and series items at runtime to an empty chart in a report.
However, albeit I can see the changes I'm making at runtime happening they do not reflect in the Chart.
Here's my NeedDataSource Event code:
private void chart1_NeedDataSource(object sender, EventArgs e) |
{ |
List<Product> produtos = new List<Product>(); |
produtos.Add(new Product() { qty = 10, name = "Produto 1" }); |
produtos.Add(new Product() { qty = 35, name = "Produto 2" }); |
produtos.Add(new Product() { qty = 17, name = "Produto 3" }); |
produtos.Add(new Product() { qty = 65, name = "Produto 4" }); |
ChartSeries serie = new ChartSeries(); |
foreach (Product prod in produtos) |
{ |
ChartSeriesItem item = new ChartSeriesItem(); |
item.YValue = prod.qty; |
item.Name = prod.name; |
item.Label.TextBlock.Text = prod.name; |
serie.Items.Add(item); |
} |
chart1.Series.Add(serie); |
} |
If I execute this code, the chart will be EMPTY...and showing the message: There is no or empty Series. And the Legend has 1 Item with a label text of Series xx.
IF I eventually change my NeedDataSource event to:
private void chart1_NeedDataSource(object sender, EventArgs e) |
{ |
List<Product> produtos = new List<Product>(); |
produtos.Add(new Product() { qty = 10, name = "Produto 1" }); |
produtos.Add(new Product() { qty = 35, name = "Produto 2" }); |
produtos.Add(new Product() { qty = 17, name = "Produto 3" }); |
produtos.Add(new Product() { qty = 65, name = "Produto 4" }); |
chart1.DataSource = produtos; |
} |
The chart will show the values correctly. However I can't change each indivual item values or appearance at runtime! Firstly, I get an exception if I try to use the following line after chart1.Datasource = produtos:
ChartSeries
series = chart1.Series[0];
If I put that line of code on the ItemDataBound Event it works fine:
private void chart1_ItemDataBound(object sender, EventArgs e) |
{ |
ChartSeries series = chart1.Series[0]; |
series.Appearance.LegendDisplayMode = ChartSeriesLegendDisplayMode.ItemLabels; |
series.Type = ChartSeriesType.Pie; |
} |
As a side note, I'm loading the report on a reportviewer and I initialize it like so:
private void Form1_Load(object sender, EventArgs e) |
{ |
reportViewer1.Report = new ReportTest(); |
reportViewer1.RefreshReport(); |
} |
Even though I'm calling the RefreshReport Method, whenever I execute the application I'm forced to manually press the Refresh Button on the report viewer just to watch my ItemDataBound code be actually rendered...what the heck?!
Back to my ItemDataBound event...I can actually access the serie's Items, but any change I make to them (be it YValues or Appearance stuff) it WONT reflect on my reportviewer no matter how much I smash the refresh button!
Here's my complete code:
ReportTest.cs
(chart1 was just dropped onto the detail section in Design time. No other changes were made!)
using System; |
using System.ComponentModel; |
using System.Drawing; |
using System.Windows.Forms; |
using Telerik.Reporting; |
using Telerik.Reporting.Drawing; |
using System.Collections.Generic; |
using Telerik.Reporting.Charting; |
public partial class ReportTest : Telerik.Reporting.Report |
{ |
public ReportTest() |
{ |
InitializeComponent(); |
} |
private void chart1_NeedDataSource(object sender, EventArgs e) |
{ |
List<Product> produtos = new List<Product>(); |
produtos.Add(new Product() { qty = 10, name = "Produto 1" }); |
produtos.Add(new Product() { qty = 35, name = "Produto 2" }); |
produtos.Add(new Product() { qty = 17, name = "Produto 3" }); |
produtos.Add(new Product() { qty = 65, name = "Produto 4" }); |
chart1.DataSource = produtos; |
ChartSeries series = chart1.Series[0]; |
} |
private void chart1_ItemDataBound(object sender, EventArgs e) |
{ |
ChartSeries series = chart1.Series[0]; |
series.Appearance.LegendDisplayMode = ChartSeriesLegendDisplayMode.ItemLabels; |
series.Type = ChartSeriesType.Pie; |
foreach (ChartSeriesItem item in series.Items) |
{ |
double x = item.YValue; |
item.Label.TextBlock.Text = "BLAH"; |
item.Appearance.Exploded = true; |
} |
series.SetValues(new double[] {10,15,30,47 }); |
chart1.PlotArea.SetDirty(); |
} |
} |
Form1.cs
public partial class Form1 : Form |
{ |
public Form1() |
{ |
InitializeComponent(); |
} |
private void Form1_Load(object sender, EventArgs e) |
{ |
reportViewer1.Report = new ReportTest(); |
reportViewer1.RefreshReport(); |
} |
} |
Please help... I already lost a whole day of work trying to figure this out.
P.S.: I followed EVERY single tutorial regarding adding series at runtime and NONE OF THEM WORK on my end.