This is a migrated thread and some comments may be shown as answers.

Report Chart Unable to change series items in runtime

5 Answers 137 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
HelpdeskLisboa
Top achievements
Rank 1
HelpdeskLisboa asked on 20 May 2010, 12:11 PM
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:

        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];

 

 

I get an out of bound exception.

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.

5 Answers, 1 is accepted

Sort by
0
Accepted
Steve
Telerik team
answered on 21 May 2010, 04:48 PM
Hi HelpdeskLisboa,

We've noticed from your download history that you're using Q1 version, which had similar problem. Please download and upgrade to Q1 SP1, where this problem has been addressed.

Please excuse us for the temporary inconvenience.

Kind regards,
Steve
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Mike
Top achievements
Rank 1
answered on 30 Dec 2011, 06:57 AM
This is exactly the behavior I am seeing with my pie chart in a Report.
I am attempting to change the Exploded property of the series items by looping through them in the ItemDataBound event handler for the chart.  When I run the debugger, the event fires and the code runs but the chart appearance in the ReportViewer doesn't show any effect from the changes made in the code.

I am using your 2011 Q3 release of Reporting.  Any ideas why this isn't working? 

Thanks.

Mike

0
Steve
Telerik team
answered on 03 Jan 2012, 03:34 PM
Hi Mike,

You should use the NeedDataSource event handler of the chart instead. You can see an example of pie chart with exploded slices using this event in the following help article: Data Binding Chart to a Generic List of Objects.

Greetings,
Steve
the Telerik team

Q3’11 of Telerik Reporting is available for download. Register for the What's New in Data Tools webinar to see what's new and get a chance to WIN A FREE LICENSE!

0
Tahir Ahmad
Top achievements
Rank 1
answered on 25 Feb 2013, 11:07 AM
I am experiencing exact same problem when using 2012 Q3 codebase. If I hook chart's NeedDataSource, it never fires. If I databind chart in parent report's NeedDataSource nothing shows up. I tried chart.DataSource, manually assigned series objects.
0
Stef
Telerik team
answered on 28 Feb 2013, 11:39 AM
Hello Tahir,

Can you please verify that the DataSource for the Chart item is not set in order the NeedDataSource event to be fired. For more details about Chart binding, take a look at:

If you have further questions, let us know more details as what data and how it is passed to the chart item.

Kind regards,
Stef
the Telerik team

See what's new in Telerik Reporting Q1 2013. Register for the March 4 webinar to witness the impressive new visualizations in Telerik Reporting. Just for fun, 10 webinar attendees will be randomly selected to win a Telerik T-shirt and a $50 Gift Certificate to ThinkGeek. Register now! Seats are limited!

Tags
General Discussions
Asked by
HelpdeskLisboa
Top achievements
Rank 1
Answers by
Steve
Telerik team
Mike
Top achievements
Rank 1
Tahir Ahmad
Top achievements
Rank 1
Stef
Telerik team
Share this question
or