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

Bar chart issue

3 Answers 67 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 09 Nov 2011, 02:26 PM
Hello everybody,

I have a single report who displaying tables from different SqlDataSource and everything works fine.

I want to add a bar chart displaying data from an other SqlDataSource ( stored procedure ) in the same scope.

Here is my stored procedure return :

ID        Name

1        Hello
2        Hello
3        Bye
4        Hello
5        Bye    
6        Hi

I want my chart to display data like this :
- One bar by name (distinct)
- The value is the count of the repetition

Example :
bar "Hello" => value = 3
bar "Bye" => value = 2
bar "Hi" => value = 1

I am trying to solve my problem since few days, without the expected result

PS : i cannot modify the stored procedure

Thanks a lot, Kevin.

3 Answers, 1 is accepted

Sort by
0
Elian
Telerik team
answered on 10 Nov 2011, 01:49 PM
Hi Kevin,

Since you cannot modify the procedure, I'd suggest you create a method that takes the data from the stored procedure and converts it into a different format. For example you can create Dictionary<string,int> items and every time you encounter a word you increment the value with +1 and afterwards add the items to a list for example. This way you will get data that can be binded directly to the chart.

Here is a sample code that should accomplish this:


class Answer     
{        
 public string Name{get;set;}         
 public int Count{get;set;}     
}     

class DataSourceClass     
{        
 public static List<Answer> GetAnswers()         
	{             
	List<string> items = ... \\this is the collection you get from the stored procedure.
Dictionary<string, int> dict = new Dictionary<string, int>();              foreach (var item in items)              {                  if (!dict.ContainsKey(item))                  {                      dict.Add(item, 0);                  }                  dict[item]++;              }              List<Answer> result = new List<Answer>();              foreach (var item in dict.Keys)              {                  result.Add(new Answer() { Name = item, Count = dict[item] });              }              return result;          }   

Afterwards, you can create and ObjectDataSource and bind it to the GetAnswers() method of the DataSourceClass.

Regards,
Elian
the Telerik team

Q2’11 SP1 of Telerik Reporting is available for download (see what's new). Get it today.

0
Kevin
Top achievements
Rank 1
answered on 10 Nov 2011, 02:59 PM
Hi Elian,

Thx a lot for your response, and sorry for my english ;)

However, I still have a question,

How can i get a collection from my SqlDataSource object ?
Or should I use a SqlDataAdapter an a DataTable ?

Kévin.

0
Elian
Telerik team
answered on 10 Nov 2011, 06:01 PM
Hi Kevin,
string connectionString = ".....
string selectString = "SELECT .....
SqlDataAdapter sqlAdapter = new SqlDataAdapter(selectString, connectionString); 
DataSet ds = new DataSet(); 
sqlAdapter.Fill(ds); 
ds.Tables[i] gets you the tables that the query returns
This should work for you. Note that preparing and feeding the data for the report is entirely up to you and not part of the reporting product goals.

Kind regards,
Elian
the Telerik team

Q2’11 SP1 of Telerik Reporting is available for download (see what's new). Get it today.

Tags
General Discussions
Asked by
Kevin
Top achievements
Rank 1
Answers by
Elian
Telerik team
Kevin
Top achievements
Rank 1
Share this question
or