How to use a special label format for axis labels ?

6 posts, 0 answers
  1. Alex
    Alex avatar
    12 posts
    Member since:
    Mar 2012

    Posted 26 Mar 2012 Link to this post

    Hello everybody,

    I'm trying to represent durations on the y-axis of a RadCartesianChart.

    The data points values are a count of seconds (double number) and I would like the value to be converted into a string : "hh:mm:ss".

    I saw the LabelFormat property for a LinearAxis but I down know if I can use a converter or something like that to achieve what I want to do.

    Thanks for your help,

    Alex.
  2. Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1511 posts

    Posted 28 Mar 2012 Link to this post

    Hello Jean-Alexis,

    Thank you for contacting us. This scenario can be achieved by using a simple converter. Take for example the following chart definition:

    <chart:RadCartesianChart x:Name="chart" Margin="10">
        <chart:RadCartesianChart.Series>
            <chart:LineSeries />
        </chart:RadCartesianChart.Series>
        <chart:RadCartesianChart.HorizontalAxis>
            <chart:LinearAxis Maximum="10" DesiredTickCount="3">
                <chart:LinearAxis.LabelTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding .,Converter={StaticResource TimeConverter}}" />
                    </DataTemplate>
                </chart:LinearAxis.LabelTemplate>
            </chart:LinearAxis>
        </chart:RadCartesianChart.HorizontalAxis>
        <chart:RadCartesianChart.VerticalAxis>
            <chart:LinearAxis Maximum="60" />
        </chart:RadCartesianChart.VerticalAxis>
    </chart:RadCartesianChart>
     
    The chart is populated with the following items source:

    public MainPage()
    {
        InitializeComponent();
        this.chart.Series[0].ItemsSource = new double[] { 0.0, 5.0, 10.0 };
    }

    Finally, this is how the TimeConverter looks like:

    public class TimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double seconds = System.Convert.ToDouble(value);
            TimeSpan timeSpan = TimeSpan.FromSeconds(seconds);
            return timeSpan.ToString();
        }
     
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    The output from the above code is:



    Give it a try and let me know how it works for you. I'd be glad to assist you with any additional qustions.
    All the best,
    Kiril Stanoev
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. DevCraft banner
  4. Alex
    Alex avatar
    12 posts
    Member since:
    Mar 2012

    Posted 29 Mar 2012 Link to this post

    Thank you, it works :)
  5. Ricardo
    Ricardo avatar
    8 posts
    Member since:
    Jun 2013

    Posted 11 Jul 2014 Link to this post

    Hello ,

    I'm trying to represent km on the y-axis of a RadCartesianChart.

    The data points values are a count of seconds (double number) and I would like the value to be converted into a string : "###+###".

    I saw the LabelFormat property for a LinearAxis but I down know if I can use a converter or something like that to achieve what I want to do.

    example:

    decimal value=32590 and out 032+590

    Thanks for your help,

    Ricardo.
  6. Rosy Topchiyska
    Admin
    Rosy Topchiyska avatar
    550 posts

    Posted 16 Jul 2014 Link to this post

    Hello Ricardo,

    Thank you for the question.

    You can use a converter to achieve the desired scenario, here is a sample:
    public class Converter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double seconds = System.Convert.ToDouble(value);
            var a = (int)seconds / 1000;
            var b = (int)seconds - a * 1000;
            return string.Format("{0,3:D3}-{1,3:D3}", a, b);
        }
     
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    Here is an article from MSDN with more information about formatting numbers: How to: Pad a Number with Leading Zeros

    I hope this helps. Please, let us know if you have further questions.

    Regards,
    Rosy Topchiyska
    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.

     
  7. Ricardo
    Ricardo avatar
    8 posts
    Member since:
    Jun 2013

    Posted 16 Jul 2014 in reply to Rosy Topchiyska Link to this post

    Hello Rosy Topchiyska,

    Thanks you for the answer.

    Very good.
Back to Top
DevCraft banner