configure XAxis.LabelStep at runtime

Thread is closed for posting
4 posts, 0 answers
  1. Chris Ward
    Chris Ward avatar
    56 posts
    Member since:
    Jan 2010

    Posted 16 Feb 2011 Link to this post

    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
  2. Giuseppe
    Admin
    Giuseppe avatar
    2363 posts

    Posted 21 Feb 2011 Link to this post

    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!
  3. Chris Ward
    Chris Ward avatar
    56 posts
    Member since:
    Jan 2010

    Posted 22 Feb 2011 Link to this post

    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
  4. Giuseppe
    Admin
    Giuseppe avatar
    2363 posts

    Posted 25 Feb 2011 Link to this post

    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!
Back to Top