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

Line Graph Binding

3 Answers 142 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Travis
Top achievements
Rank 1
Travis asked on 21 Nov 2011, 09:48 PM
Hello, I am trying out Telerik reporting and am attempting to learn how to bind to a line Graph. Here is my situation:

-I have a table of transactions, each of which has a "category"
-I have a parameter which is a date
-I want my line graph to show the count of each "category" for each of the last 7 days. So I want a series (line) for each "Category", where my x-axis is the day (7 days ago to now)  and the y-axis is the count of transactions for that category.

My problem is that I do not know "what" categories these may be before-hand. I tried using a "pivot" in my sql query: this gives me a result set with each day as columns and counts for each category. I cannot create my line series in the design view because I don't know the categories (could be 2, 15 ect.)

Is there a way, after the chart is bound to this result set, to get that set, iterate through it, construct a "custom" result set with the "categories" as columns,  and programatically add the series to construct the chart?  Thanks so much

3 Answers, 1 is accepted

Sort by
0
Elian
Telerik team
answered on 23 Nov 2011, 03:20 PM
Hello Travis,

In your case it seems that it will be easier to create the chart series programmatically. 
Here is how can you achieve that:

private void FillChart(Telerik.Reporting.Chart chart)
{  
    var seriesData = ...;//get the data for the series
    foreach (var item in seriesData)//iterate through the rows
    {
        //Create new series
        var newSeries = new ChartSeries();
        newSeries.Name = "...";
        //Set some styling here
        newSeries.Type = ChartSeriesType.Line;
        newSeries.Appearance...
 
        foreach(var day in item.Days)//add item for each of the 7 days(your implementation)
        {
            newSeries.Items.Add(Value);
        }   
        //add the new series to the chart's item collection
        chart.Series.Add(newSeries);
    }
}

You will also have to add the labels on XAxis (Mon, Tue...) according to the parameters.
Hope this helps. Greetings,
Elian
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
Travis
Top achievements
Rank 1
answered on 23 Nov 2011, 04:08 PM
Thank you so much for the reply, that is indeed what I did and it works great. I have a couple more simple questions:

1. What property is used to change the X-Axis labels (Mon., Tue., ect)? I tried adding a label in the "AddItem" call ( chartSeries.AddItem(value, "Mon") ) but the labels still show up as (1,2,3...).

2. When populating the chart programmatically, where is the best spot to but this code (when the "Generate" button is clicked). Right now I have a method for the chart's "NeedDataSource" event which creates an sql connection and populates the series, is there another spot or event that is fired when "generate" is clicked?

Thanks again for the help.
0
Elian
Telerik team
answered on 24 Nov 2011, 06:33 PM
Hi Travis, 
  1. In order to set custom Axis labels you have to set the AutoScale to False.
    chart1.PlotArea.XAxis.AutoScale = false;
    chart1.PlotArea.XAxis.Items.Clear();
    chart1.PlotArea.XAxis.Items.Add(new ChartAxisItem("Label1"));
    chart1.PlotArea.XAxis.Items.Add(new ChartAxisItem("Label2"));
    ...
  2. We suggest to encapsulate this functionality in a method and call it in the constructor after the InitializeComponent();
    public Report1()
           {
               InitializeComponent();
               CreateChart(chart1);
               ...
    Although using the NeedDataSource event is ok, too.

Greetings,
Elian
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!

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