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

configure XAxis.LabelStep at runtime

3 Answers 88 Views
Chart (Obsolete)
This is a migrated thread and some comments may be shown as answers.
Chris Ward
Top achievements
Rank 1
Chris Ward asked on 16 Feb 2011, 03:33 PM

Hello,

I have a chart that display a series of data across a date range.  There are 24 data points per day.  Sometimes we are displaying 30 days, sometimes we are displaying many more days. Obviously this is to many labels to display.  I would liket there to always be about 30 labels no matter how many days worth of data I am sidplaying.  I understand the only way to do this is by using the LabelStep property. I would like to set the label step property at runtime based on the number of data points that are in the chart.

I add the labels through the PlotArea.XAxis.AddRange method.  The series is bound to the SQL datasource.  In the chart_ItemDataBound event I get the number of datapoints and try to set the LabelStep property as shown below.  However, no matter what I try the labelstep property that I set at runtime is not used and rather is uses the value set as designtime.

 

private void chart_ItemDataBound(object sender, EventArgs e)
{
    //get handles 
    Telerik.Reporting.Processing.Chart ProcessingChart = (Telerik.Reporting.Processing.Chart)sender;
    Telerik.Reporting.Chart ChartDefinition = (Telerik.Reporting.Chart)ProcessingChart.ItemDefinition;
  
    //get the number of datapoints
    int iDataPoints = ChartDefinition.Series[0].Items.Count;
  
    if (iDataPoints > 0)
                      
        //setting the labelstep on chart defination object from the processing chart
        ChartDefinition.PlotArea.XAxis.LabelStep = iDataPoints / 30;
                  
        //also have tried setting setting the labelstep on actual chart object
        chart.PlotArea.XAxis.LabelStep = iDataPoints / 30;
      
}

Why is the chart not using the labelstep property that I set at runtime?

Thanks,

Chris

3 Answers, 1 is accepted

Sort by
0
Giuseppe
Telerik team
answered on 21 Feb 2011, 05:16 PM
Hello Chris,

Generally it is not possible to use the ItemDataBound event (executed once for every item) to achieve the desired customization. We would suggest you to use the NeedDataSource event instead to apply this axis setting if this is applicable to your scenario:
private void chart1_NeedDataSource(object sender, EventArgs e)
{
    Random rand = new Random(123456);
    ChartSeries series = new ChartSeries();
    for (int i = 0; i < 100; i++)
    {
        series.AddItem(rand.Next(10, 100));
    }
 
    chart1.Series.Add(series);
 
    chart1.PlotArea.XAxis.LabelStep = series.Items.Count / 30;
}

Hope this helps.


Best wishes,
Giuseppe
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Chris Ward
Top achievements
Rank 1
answered on 22 Feb 2011, 11:01 PM
Hello Giuseppe,

I am currenty using the NeedDataSource event to bind to my SQL datasource.  So after I bind the datasource, I then check the items count inside the first series as you suggested. But the series items count is 0.  It's almost like the databinding has not created the series yet.  So that does not work. The seies count is filled once I reach the ItemDataBound event, which is why I was trying there. 

Any other suggestions?

Thanks,
 Chris
0
Giuseppe
Telerik team
answered on 25 Feb 2011, 04:15 PM
Hi Chris Ward,

Indeed your observation is correct -- the actual databinding occurs after the NeedDataSource event so no chart series will be generated here.

Generally there are only two possible places where you can set the XAxis.LabelStep property effectively -- the ItemDataBinding and the NeedDataSource events -- but both of them occur early in the lifecycle and the chart data is not retrieved yet for you to use it in the calculation of the label step.

There are two possible options -- you can build the chart series manually in the NeedDataSource (without SqlDataSource control), or you can keep the SqlDataSource implementation but in this case you would need to perform a second SELECT command to your database (in the ItemDataBinding / NeedDataSource event handlers) in order to retrieve the data item count like this:

private void chart1_ItemDataBinding_1(object sender, EventArgs e)
{
    System.Web.UI.WebControls.SqlDataSource sql = new System.Web.UI.WebControls.SqlDataSource();
    sql.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
    sql.SelectCommand = "SELECT COUNT(UnitsInStock) FROM Products";
 
    var result = sql.Select(new System.Web.UI.DataSourceSelectArguments());
    var itemCount = (result as System.Data.DataView).Table.Rows[0][0];
 
    chart1.PlotArea.XAxis.LabelStep = (int)itemCount / 30;
}

Note that you need to use the stock System.Web.UI.WebControls.SqlDataSource control (and not Telerik.Reporting.SqlDataSource) as you need to call the Select(...) method manually.
 

Kind regards,
Giuseppe
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
Chart (Obsolete)
Asked by
Chris Ward
Top achievements
Rank 1
Answers by
Giuseppe
Telerik team
Chris Ward
Top achievements
Rank 1
Share this question
or