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

Demo error and question

2 Answers 50 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
John McLean
Top achievements
Rank 1
John McLean asked on 01 Dec 2009, 04:04 AM
Hi

I am binding a chart at runtime to a generic list and having some difficulty with "what" gets charted.  I have an object structure like this:

    sealed public class MisPricedRegions : List<MisPricedRegion> 
    {  
    }  
 
    sealed public class MisPricedRegion  
    {  
        public string Region { get; set; }  
        public string Quarter { get; set; }  
        public int NumberConnectionPoints { get; set; }  
        public double AverageAmount { get; set; }  
    } 

I want to use this for two different charts... one where the Y axis is for NumberConnectionPoints and the other where the Y axis is for AverageAmount.

The sample code on your site here gave me an idea of what I need to do (set the Series[0].DataYColumn), but the sample code throws an error (IndexOutOfRange) as there is no series... even though the datasource has been set?

Thanks,

John

2 Answers, 1 is accepted

Sort by
0
Accepted
Steve
Telerik team
answered on 01 Dec 2009, 04:12 PM
Hi John McLean,

Setting the DataSource property of the chart creates series automatically from the provided datasource.
But referencing series like shown in the example defChart.Series[0]... means that there is an existing serie in the report definition i.e. created through in the report designer. Since this is not your case, we suggest to loop through your data, manually create the series and populate the series items. The item.Name property is shown in the Legend and the item.Label.TextBlock.Text is shown as the label for the corresponding pie:

private void chart1_NeedDataSource(object sender, EventArgs e)
        {
            List<Product> products = new List<Product>();
            products.Add(new Product("Parka L", 120));
            products.Add(new Product("Parka M", 100));
            products.Add(new Product("Parka S", 132));
            products.Add(new Product("Wool Cap", 45));
            products.Add(new Product("Mittens", 67));
            Telerik.Reporting.Processing.Chart procChart = (Telerik.Reporting.Processing.Chart)sender;
            Telerik.Reporting.Chart defChart = (Telerik.Reporting.Chart)procChart.ItemDefinition;
            defChart.IntelligentLabelsEnabled = false;
            ChartSeries serie = new ChartSeries();
            serie.Clear();
            serie.Appearance.LegendDisplayMode = Telerik.Reporting.Charting.ChartSeriesLegendDisplayMode.ItemLabels;

            foreach (Product lst in products)
            {
                ChartSeriesItem item = new ChartSeriesItem();
                item.YValue = (double)lst.QuantityInStock;
                item.Name = (string)lst.Name;              
                serie.Items.Add(item);
            }

            defChart.Series.Add(serie);
        }

Note that you should not set the DataSource property in this case!

Greetings,
Steve
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
John McLean
Top achievements
Rank 1
answered on 01 Dec 2009, 10:38 PM
Thanks Steve

I found a different solution... I set up the series when I create the chart in runtime and it works a treat.  This is a better solution for me as the charts require grouping and this is a piece of cake with the DataGroupColumn property.

J
Tags
General Discussions
Asked by
John McLean
Top achievements
Rank 1
Answers by
Steve
Telerik team
John McLean
Top achievements
Rank 1
Share this question
or