Graph DateTime axis scaling

5 posts, 1 answers
  1. Martin
    Martin avatar
    20 posts
    Member since:
    Apr 2009

    Posted 07 Oct 2014 Link to this post

    Hi,

    I'm trying to create a graph with a few series on a numerical Y axis and date time X axis.
    The Y axis is very simple with a fixed range and a known number of steps. However the X axis could have anything from a few values over a 5 minute range to a few hundred values over a 5 year range.

    By default the graph appears to be configured to automatically set the scale, unit set to auto, but for a short range of values it just puts a label every minute and for longer periods it complains about having more than 5000 ticks.
    All the documentation seems to suggest I need to set the unit and step values in the designer but this is not an option when I have an unknown range of values.

    What do I need to do to get it to automatically calculate a suitable scale for the X axis.

    I'm using Q2 2014 SP1.

    Thanks,
    Martin
  2. Answer
    Stef
    Admin
    Stef avatar
    3610 posts

    Posted 10 Oct 2014 Link to this post

    Hi Martin,

    Currently the way to set the Scale's Minimum and Maximum, MajorUnit and MajorStep properties would be by programmatically reading and analyzing your data in your application and assigning appropriate values. This does not mean, that you should re-create the whole graph programmatically, you can just set these properties at run-time.

    After reading your data programmatically, you can use the following code to set the Scale properties:

    //at run time
    var report = new Telerik.Reporting.Report();
    var graph = report.Items.Find("graph1", true)[0] as Telerik.Reporting.Graph;
     
    //Assuming you have a single Cartesian coordinate system in your graph.
    var cartesianCoordinateSystem = (Telerik.Reporting.CartesianCoordinateSystem)graph.CoordinateSystems[0];
     
    //Assuming your X axis has a DateTime scale.
    var dateTimeScale = (Telerik.Reporting.DateTimeScale)cartesianCoordinateSystem.XAxis.Scale;
    dateTimeScale.Minimum = DateTime.MinValue;
    dateTimeScale.Maximum = DateTime.MaxValue ;
    dateTimeScale.MajorStep = 100;
    dateTimeScale.MajorUnit = DateTimeScaleUnits.Years;

    reportViewer1.ReportSource = new InstanceReportSource { ReportDocument = report };


    I hope the above information is helpful.

    Regards,
    Stef
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. Martin
    Martin avatar
    20 posts
    Member since:
    Apr 2009

    Posted 13 Oct 2014 in reply to Stef Link to this post

    Hi Stef,

    Ive just got this up and running now and it works great for what im doing. I was really dreading having to create the whole thing programmatically. I expect ill find a few more uses for the ability to tweak the reports before i run them as well.

    Thanks for the help.
    Martin
  4. Nancy
    Nancy avatar
    2 posts
    Member since:
    Mar 2018

    Posted 12 Mar 2018 in reply to Martin Link to this post

    Hi Martin,

    Can you give more details how you accomplished this? I want to change the MajorUnit and MajorStep based on the number of days displayed on the graph but don't know how to read the data programmatically.

    Did you add the code below InitializeComponent() in the constructor or is it in another event (and if so, what event)?

    Thanks!

    Nancy

  5. Nancy
    Nancy avatar
    2 posts
    Member since:
    Mar 2018

    Posted 13 Mar 2018 in reply to Nancy Link to this post

    I worked out how to do this, all code is in the report's ItemDataBinding event. I'm using the date range from the parameters to determine the number of days that will be displayed.

    private void Report_ItemDataBinding(object sender, EventArgs e)
    {
        Telerik.Reporting.Processing.Report processingReport = (Telerik.Reporting.Processing.Report) sender;
     
        //Get the date range from the parameters
        DateTime firstDate = (DateTime) processingReport.Parameters["DateFrom"].Value;
        DateTime lastDate = (DateTime) processingReport.Parameters["DateTo"].Value;
     
        //work out how many days we are displaying
        double daysOnReport  = lastDate.Subtract(firstDate).TotalDays;
     
        //get the dateTime scale for the date X axis
        Telerik.Reporting.Report report = this;
        var graph = report.Items.Find("graph1", true)[0] as Telerik.Reporting.Graph;
        var cartesianCoordinateSystem = (Telerik.Reporting.CartesianCoordinateSystem) graph.CoordinateSystems[0];
        var dateTimeScale = (Telerik.Reporting.DateTimeScale) cartesianCoordinateSystem.XAxis.Scale;
     
        //depending on the number of days we're displaying set the distance between the dates displayed on the axis.
        if (daysOnReport < 14)
        {
            dateTimeScale.MajorUnit = DateTimeScaleUnits.Days;
            dateTimeScale.MajorStep = 1;
        }
        else if (daysOnReport < 30)
        {
            dateTimeScale.MajorUnit = DateTimeScaleUnits.Days;
            dateTimeScale.MajorStep = 3;
        }
        else if (daysOnReport < 90)
        {
            dateTimeScale.MajorUnit = DateTimeScaleUnits.Days;
            dateTimeScale.MajorStep = 7;
        }
        else if (daysOnReport < 180)
        {
            dateTimeScale.MajorUnit = DateTimeScaleUnits.Days;
            dateTimeScale.MajorStep = 14;
        }           
        else if (daysOnReport < 360)
        {
            dateTimeScale.MajorUnit = DateTimeScaleUnits.Months;
            dateTimeScale.MajorStep = 1;
        }        
        else
        {
            dateTimeScale.MajorUnit = DateTimeScaleUnits.Months;
            dateTimeScale.MajorStep = 2;
        }
    }
Back to Top