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

Binding DataSource to Chart?

11 Answers 670 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Dan
Top achievements
Rank 2
Dan asked on 06 Jul 2009, 03:14 PM
I am probably having two problems:
  1. How do you set the chart x axis and y axis to the dataset columns?
  2. Am not seeing any data in my chart from the datasource code below.

When I preview this chart, I am not getting data in the chart.  It shows 5 empty charts.

Code:

 

namespace

 

CPR_Reports

 

 

 {

 

using System;

 

 

 

using System.ComponentModel;

 

 

 

using System.Data;

 

 

 

using System.Data.SqlClient;

 

 

 

using System.Drawing;

 

 

 

using System.Windows.Forms;

 

 

 

using Telerik.Reporting;

 

 

 

using Telerik.Reporting.Drawing;

 

 

 

using Telerik.Reporting.Processing;

 

 

 

 

public partial class ActivePatientsChart : Telerik.Reporting.Report

 

 

 

 

{

 

 

 

 

public ActivePatientsChart()

 

 

 {

 

 

 

 

 InitializeComponent();

 

 

 

 

 

try

 

 

{

 

 

 

this.weB_PORTAL_TESTDataSetTableAdapter1.Fill(this.wEB_PORTAL_TESTDataSet.WEB_PORTAL_TESTDataSetTable);

 

 

}

 

catch (System.Exception ex)

 

 

 {

 

System.Diagnostics.Debug.WriteLine(ex.Message);

 

 

 }

}

 

 

 

private void chart1_NeedDataSource(object sender, System.EventArgs e)

{

 

string commandText = "select sum(I_Val) as ivalue,Date_ as day from stats where DATE_ between'6/26/2009' and '7/2/2009' and stat_no =4 group by Date_";

 

 

 

 

Telerik.Reporting.Processing.Chart chart = (Telerik.Reporting.Processing.Chart)sender;

 

 

 

SqlDataAdapter adapter = new SqlDataAdapter(commandText, Properties.Settings.Default.WEB_PORTAL_TEST);

 

 

 

DataSet ds = new DataSet();

 

 

adapter.Fill(ds);

 

DataView view = ds.Tables[0].DefaultView;

 

 

chart.DataSource = view;

 

}

 

}

}

 

11 Answers, 1 is accepted

Sort by
0
Steve
Telerik team
answered on 08 Jul 2009, 09:17 AM
Hi Dan,

The code you've pasted is correct, but note that the chart would only show data if there is a numeric column to bind to. Additionally if you want to specify where the XAxis and YAxis get their values from, you should create chart series and set their DataYColumn and DataXColumn. Here is a sample code:

  private void chart1_NeedDataSource(object sender, EventArgs e) 
        { 
            string sql = 
   @"SELECT TOP 20 Production.Product.ListPrice, Production.Product.Name, Production.Product.ProductID FROM Production.Product"
            string connectionString = 
              "Data Source=(local)\\SQLEXPRESS;Initial Catalog=AdventureWorks;Integrated Security=True"
            SqlDataAdapter adapter = new SqlDataAdapter(sql, connectionString); 
            DataSet dataSet = new DataSet(); 
            adapter.Fill(dataSet); 
            ChartSeries series = new ChartSeries(); 
            series.DataYColumn = "ProductID"
            series.DataLabelsColumn = "Name"
            chart1.Series.Add(series); 
            (sender as Telerik.Reporting.Processing.Chart).DataSource = dataSet.Tables[0].DefaultView; 
        } 


All the best,
Steve
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Dan
Top achievements
Rank 2
answered on 08 Jul 2009, 01:30 PM
Steve,

I'm really at a loss with this.  I added your code to the NeedDataSource routine as you suggested.  

Telerik.Reporting.Charting.

ChartSeries series = new Telerik.Reporting.Charting.ChartSeries();

 

series.DataYColumn =

"ivalue";

 

series.DataLabelsColumn =

"day";

 

chart1.Series.Add(series);

ivalue is numeric, and day is a short date field.  When I preview the report, I am seeing seven empty charts which display 'There is no or empty series'.  Why seven?  Because there are seven data rows in the DataSource being retrieved.  The seven data rows should be binding to produce a single bar chart with seven columns, yes?  But I am getting seven empty charts instead.

I have looked at the Telerik Reporting Examples project, at the ProductLineSales chart report.  My chart properties and code-behind are the same as this example, so it should work, but it does not.  There are no errors to help me deduce what the problem is.

Any idea why I'm getting multiple empty charts?

Thanks.
Dan

0
Steve
Telerik team
answered on 08 Jul 2009, 02:05 PM
Hello Dan,

The only logical reason is that your report is bound to a datasource with seven rows (or the very same datasource as the chart). In such case, the detail section would be repeated 7 times and this is expected. What is not expected is why your chart is not populated, which seems like an issue on your end as our demo reports and tests work fine. Anyway I've attached my sample report for your convenience, hopefully you'll be able to spot what causes the issue on your end.

Regards,
Steve
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Dan
Top achievements
Rank 2
answered on 08 Jul 2009, 06:01 PM
Steve,

Your Report1 works fine, of course.  I copied your chart1_NeedDataSource code into my report code, and cross-checked all of the property settings on the chart.  I did delete references to a DataSet that was originally build with my report, and this got rid of the 7 empty charts on preview.  But, my chart report, on preview, still shows 'no or empty series' even though I have your exact NeedDataSource code.

Would it be possible for me to send you a zip file of my code, and have you take a look at it.  I am baffled....

Dan
0
Dan
Top achievements
Rank 2
answered on 08 Jul 2009, 07:58 PM
Steve,

Found the problem.  This line of code:

this

 

.chart1.NeedDataSource += new System.EventHandler(this.chart1_NeedDataSource);

was missing from my ActivePatientsChart.Designer.cs so chart1_NeedDataSource was not being called, therefore the chart had no data.  I don't understand why it was missing, but that is the culprit.

Any idea why this line of code was gone?  Something I did?

Sign me....frustrated.
Dan

 

0
tica
Top achievements
Rank 1
answered on 09 Jul 2009, 09:46 AM
hi Dan
are you sure that that piece of code is in yourreport.designer.cs?
i've put that on mine and give me an error but on myreport.cs was accepted.
0
Dan
Top achievements
Rank 2
answered on 09 Jul 2009, 01:34 PM
Steve,

Yes, it's in the report.designer.cs code.

See line 39 of Report1.Designer.cs that you sent to me.

Dan
0
tica
Top achievements
Rank 1
answered on 09 Jul 2009, 01:49 PM
sorry you are corrected it was my mistache
on my chart that line was generated.
0
Steve
Telerik team
answered on 10 Jul 2009, 04:10 PM
Hi Dan,

I have no idea how it got lost if you have wired it up through the property/events grid in the report designer. I've assumed that you have debugged to verify that data is being fed to the chart and that is why did not make a suggestion to check whether or not the event itself fires.

All the best,
Steve
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Sameers
Top achievements
Rank 1
answered on 23 Jun 2011, 10:22 AM
Hi,

   I just wonder, Why I had to manually add a "series" from the code (using NeedDataSource method). I had a series defined at design time, but that was NOT appearing. Only when I added a series from the code (in NeedDataSource event), it worked. Something like the reply in the same post from Telerik admin (Posted on Jul 8, 2009).

    Couple of other things, If I can get answered. Is it possible to set the maximum value for the series? For example, see attached file with this post, the horizontal has values 0 5 10 15 25. But I really want that to be max of 24 (that is hours of the day). And similarly, the Y (vertical) has values like 0.5 1 1.5 2 2.5 etc. I would like to remove decimal values and just show 1 2 3 4 5 etc instead. Is that possible? Since my data will never have decimal places.

   Thanks,
   Sameers
0
Steve
Telerik team
answered on 24 Jun 2011, 02:09 PM
Hello Sameers,

It is possible to control the XAxis scaling by setting the following properties of the PlotArea --> XAxis:
  • AutoScale=false
  • MinValue - desired min value
  • MaxValue - desired max value
  • Step=3 (if you want the items to go by three)
The Y axis is not data bound and thus cannot be bound to any kind of column or data. It either auto scales depending on the Y values of the series items or you can specify a fixed range through the MinValue and the MaxValue properties.
All the best,
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
Tags
General Discussions
Asked by
Dan
Top achievements
Rank 2
Answers by
Steve
Telerik team
Dan
Top achievements
Rank 2
tica
Top achievements
Rank 1
Sameers
Top achievements
Rank 1
Share this question
or