Manually add categories to categorical axis?

2 posts, 0 answers
  1. Steve
    Steve avatar
    43 posts
    Member since:
    May 2014

    Posted 26 Oct Link to this post

    I'm programmatically adding multiple series to a chart where the categories represent yearly values. For each series the yearly values will be sequential, but not all series will contain all of the years.

    e.g.

    series 1: 2015, 2016, 2017

    series 2: 2014, 2015, 2016

     

    so when I add them to the chart, I will end up with 2015, 2016, 2017, 2014.

     

    I know the overall year range in advance, and I would like to be able to manually add the "year" categories to the axis. I found a categories property on the categorical axis but does not appear to be any way to manipulate it.

  2. Lance | Tech Support Engineer, Sr.
    Admin
    Lance | Tech Support Engineer, Sr. avatar
    138 posts

    Posted 26 Oct Link to this post

    Hi Steve,

    The CategoricalAxis uses the series's bound data source to determine the categories to use on the axis (using the property you've set as the series CategoryBinding). It also displays those categories in the order that they are in the bound collection.

    If you wanted to have the categories appear in a particular order, you can reorder the bound collection's items and the axis will update accordingly.

    Important Note: if your series ItemsSource is bound to a type with item observability, like ObservableCollection<T>, the chart will update as the items change (as seen in my demo). However, if you're using something without observability, like List<T>, you'll need to reset the ItemsSource after sorting.


    Alternative

    There is another option you can use, the DateTimeCategoricalAxis. With this axis type, it understands the order of the dates and will arrange them in order no matter what order they appear in the bound collection.

    To demonstrate this, I've attached a demo app (run the upgrade wizard to restore the Telerik DLLs or update the references manually).

    The Chart on the left, ChartOne, is using a model where the category binding is plain string with the "YYYY" as the category:

    <telerik:CategoricalAxis />


    The Chart on the right, ChartTwo, is using a DateTime as the category, but I set the LabelFormat to only show the YYYY as the label:

    <telerik:DateTimeCategoricalAxis LabelFormat="yyyy"/>


    Underneath the charts you'll see a button, AddItemButton. Clicking it will add a new item to the chart's data sources. You'll see the charts update in real time and demonstrate how the different Axis types handle the data.

    Here's a before and after adding items screenshot:

    BEFORE



    AFTER




    Summary

    The takeaway is that you can do one of two things to accomplish what you want:

    1 - Sort the data in the bound collection so that it is added to the series in the order you want
    2 - Use a DateTimeCategoricalAxis and let the Axis handle the sorting automatically

    Please let us know if you have any further questions. Thank you for contacting Support and for choosing Telerik by Progress.

    Regards,
    Lance | Tech Support Engineer, Sr.
    Telerik by Progress
    Do you need help with upgrading your WPF project? Try the Telerik API Analyzer and share your thoughts!
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top