Group X Axis for Month/Year only

5 posts, 1 answers
  1. Paul
    Paul avatar
    38 posts
    Member since:
    Oct 2008

    Posted 23 Dec 2011 Link to this post

    We have a group of data with a simple object structure like so:

    Private _value as integer
    Private _monthYear as date

    We would like to display this in a chart with the dates ordered correctly and the x-axis displaying a single label for each month and year. So the x-axis would only display a single label for Jan/2011, Feb/2011, Mar/2011, etc, and will not display any days. MonthYear is a a chart group descriptor so our data is correctly grouped, however if we set the item mapping for the x-axis as

    ItemMapping("MonthYear", DataPointMember.XCategory)

     

    the dates do not order correctly, even if we order the collection before-hand. If we use XValue then we see mutliple labels for the same months. Is there any way to rectify this?

     

  2. Petar Marchev
    Admin
    Petar Marchev avatar
    968 posts

    Posted 27 Dec 2011 Link to this post

    Hello,

    I suggest you examine this example and use our help pages to understand how data needs to be passed to the chart in order for grouping to work correctly.

    Note that the fieldName ("MonthYear") passed to the constructor of the ItemMapping needs to be a property of your underlying business object.

    Please examine the links provided above and take a look at this help topic on X Axis as it demonstrates how to control the axis labels - the frequency of the labels and ticks.

    All the best,
    Petar Marchev
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
  4. Paul
    Paul avatar
    38 posts
    Member since:
    Oct 2008

    Posted 18 Jan 2012 Link to this post

    I've reviewed the documentation, we now have our chart setup as AutoRange = True and IsDateTime = True. We also group by month year using ChartMonthGroupDescriptor and item mapping as MonthYear XValue. The issue now is we have multiple labels for months. Setting the ticks and step labels doesn't seem like a fix as we would set it to the total days for a month but as the total days will change on a monthly basis. Is it possible to use autorange for a datetime x axis that will display one lable for each month without using item mapping with XCategory. As before, XCategory has the potential to have months out of order when using multiple series mappings.
  5. Answer
    Petar Marchev
    Admin
    Petar Marchev avatar
    968 posts

    Posted 21 Jan 2012 Link to this post

    Hello,

    1. Is it possible to use autorange for a datetime x axis that will display one label for each month
    If you want to be able to show values per day  - no, the above is not possible, because it would need an unevenly distributed axis scale.

    I appologize if I did not understand you correctly.

    2. Setting the ticks and step labels doesn't seem like a fix.
    May be this approach simply doesn't fit your scenario. If you show less than 15 months may be this would work for you - specify a start date for the axis (say the 15th day). With a step of 30 the next tick should get the 14th day of the next month, the next tick should get the 13th day of the month after that and so forth.

    Another possible work-around would be to have a step of 1. This way you will have many labels. Then you will need to iterate through these labels and change their visibility - Visible if the label represents the 1st day of the month and Collapsed otherwise. This is a hacky way to do things and you should consider this as the least preferred way to accomplish your goals.

    The above can be achieve with the code below:
    public MainPage()
    {
     InitializeComponent();
     
     chartArea = this.radChart1.DefaultView.ChartArea;
     chartArea.AxisX.RangeChanged += AxisX_RangeChanged;
    }
     
    Telerik.Windows.Controls.Charting.ChartArea chartArea;
     
    void AxisX_RangeChanged(object sender, EventArgs e)
    {
     chartArea.LayoutUpdated += chartArea_LayoutUpdated;
    }
     
    void chartArea_LayoutUpdated(object sender, EventArgs e)
    {
     var axisPanel = Telerik.Windows.Controls.ChildrenOfTypeExtensions.ChildrenOfType<Telerik.Windows.Controls.Charting.HorizontalAxisLabelsPanel>(chartArea).FirstOrDefault();
     
     if (axisPanel == null)
       return;
     
     chartArea.LayoutUpdated -= this.chartArea_LayoutUpdated;
     var textBoxes = Telerik.Windows.Controls.ChildrenOfTypeExtensions.ChildrenOfType<TextBlock>(chartArea);
     
     foreach (var textBox in textBoxes)
     {
       // if 1st of the month - Visibility.Visible, if not - collapsed
       textBox.Visibility = System.Windows.Visibility.Collapsed;
     }
    }

    3. XCategory has the potential to have months out of order when using multiple series mappings.
    Yes, and this can easily be corrected by including all possible categories in the first series, with null values for the categories that do not belong in this first series.

    Feel free to contact us again if needed.

    Regards,
    Petar Marchev
    the Telerik team
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
  6. Paul
    Paul avatar
    38 posts
    Member since:
    Oct 2008

    Posted 21 Jan 2012 Link to this post

    Solution 3, empty data series with ordered month range, is probably the quickest and simplest solution for us at this time. Thanks for you help.
Back to Top
DevCraft banner