how to change display of AxisLabels on DateTimeContinuousAxis

4 posts, 1 answers
  1. Doug
    Doug avatar
    2 posts
    Member since:
    Oct 2016

    Posted 04 Nov Link to this post

    I have a RadCartesianChart with a DateTimeContinuousAxis on the HorizontalAxis.  The data graphs exactly the way I want it, however I would like to display the way the horizontal AxisLabels display.  I would like them to display the total elapsed seconds from the minimum X value.  Is there some kind of template I could override?
  2. Answer
    Lance | Tech Support Engineer, Sr.
    Admin
    Lance | Tech Support Engineer, Sr. avatar
    135 posts

    Posted 04 Nov Link to this post

    Hi Doug,

    You can indeed set a custom template for the Axis's label template, here's an example:

    <telerik:RadCartesianChart.HorizontalAxis>
                    <telerik:DateTimeContinuousAxis LabelFitMode="MultiLine" LabelFormat="MMM yyyy">
                        <telerik:DateTimeContinuousAxis.LabelTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding}"/>
                            </DataTemplate>
                        </telerik:DateTimeContinuousAxis.LabelTemplate>
                    </telerik:DateTimeContinuousAxis>
    </telerik:RadCartesianChart.HorizontalAxis>


    Here's a screenshot of the chart using the template above.

    One thing to be aware of is that the DataContext of the label template is the display value for that label (of type string). If you want to display something different, you could use a value converter and pass other relevant information in the converter parameter.

    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
  4. Doug
    Doug avatar
    2 posts
    Member since:
    Oct 2016

    Posted 07 Nov in reply to Lance | Tech Support Engineer, Sr. Link to this post

    Lance, That worked perfectly.  I created a Converter from IValueConverter and changed the DateTime data to a Timespan object using a predefined epoch DateTime object.  I then returned the TotalSeconds from the epoch.

    public class TimespanDataConverter : IValueConverter
      {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
         {
           if (value is DateTime)
           {
             TimeSpan xVal = (DateTime)value - MainWindow._epoch;
             return Math.Round(xVal.TotalSeconds);
           }
           return value.ToString();
         }
         public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
         {
           throw new NotImplementedException();
         }
       }
    }

     

  5. Lance | Tech Support Engineer, Sr.
    Admin
    Lance | Tech Support Engineer, Sr. avatar
    135 posts

    Posted 07 Nov Link to this post

    Hi Doug,

    I'm happy to hear that the template approach worked for you and thank you for sharing your solution with the community.

    Please let us know if you have any further questions (note: since you have a current license, you can also use the Support Ticket system).

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