This is a migrated thread and some comments may be shown as answers.

How to use a special label format for axis labels ?

5 Answers 657 Views
Chart
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Alex
Top achievements
Rank 1
Alex asked on 26 Mar 2012, 02:40 PM
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.

5 Answers, 1 is accepted

Sort by
0
Kiril Stanoev
Telerik team
answered on 28 Mar 2012, 01:49 PM
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 >>
0
Alex
Top achievements
Rank 1
answered on 29 Mar 2012, 01:41 PM
Thank you, it works :)
0
Ricardo
Top achievements
Rank 1
answered on 11 Jul 2014, 04:02 PM
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.
0
Rosy Topchiyska
Telerik team
answered on 16 Jul 2014, 09:04 AM
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.

 
0
Ricardo
Top achievements
Rank 1
answered on 16 Jul 2014, 10:01 PM
Hello Rosy Topchiyska,

Thanks you for the answer.

Very good.
Tags
Chart
Asked by
Alex
Top achievements
Rank 1
Answers by
Kiril Stanoev
Telerik team
Alex
Top achievements
Rank 1
Ricardo
Top achievements
Rank 1
Rosy Topchiyska
Telerik team
Share this question
or