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

CategoricalAxis LabelFormat not working

5 Answers 245 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Daní
Top achievements
Rank 1
Daní asked on 04 Feb 2016, 03:27 PM

Hi,

 

I recently upgraded a project that was using a fairly old version of Telerik controls (about 2 years ago). In this project there's a chart with an horizontal axis that contains DateTimes elements. This axis was configured with follpwing snippet

                    
if(chart.HorizontalAxis == null)
            {
                chart.HorizontalAxis = new CategoricalAxis
                {
                    LabelFitMode = AxisLabelFitMode.Rotate,
                    LabelRotationAngle = 45,
                    LabelFormat = "dd/MM/yyyy\nHH:mm:ss",
                    PlotMode = AxisPlotMode.OnTicksPadded,
                    Padding = new System.Windows.Thickness(0.1),
                    GapLength = 0.1,
                    HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center,
                    LabelInterval = 1,
                    MajorTickInterval = 1,
#if SILVERLIGHT
                    LabelTemplateSelector = new HorizontalAxisTemplateSelector(series)
#endif
                };
                chart.HorizontalAxis.FontSize = chart.HorizontalAxis.FontSize - 2;
            }
 

With the LabelFormat I could force xAxis to show labels in long datetimeformat in two lines. Since I've upgraded to latest telerik bits the label format does not longer work, I'm seeing the LabelFormat as the Label for each series value. Is there any way I can format the label as I wish?

 

Regards

DanĂ­

5 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 08 Feb 2016, 12:42 PM
Hi Dani,

In order to apply the desired label format you can use the following syntax:
<telerik:CategoricalAxis LabelFormat="{}{0:dd/MM/yyyy HH:mm:ss}" />
Since you are working with dates you can try the DataTimeCategoricalAxis of the chart. In this case you can work with the old label definition.
<telerik:DateTimeCategoricalAxis LabelFormat="dd/MM/yyyy HH:mm:ss" />

I hope this is useful.

Regards,
Martin
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Daní
Top achievements
Rank 1
answered on 08 Feb 2016, 01:21 PM

Hi Martin

Thanks, it worked with DateTimeCategoricalAxis and setting LabelFormat to "dd/MM/yyyy \nHH:mm:ss". I got a crash with CategoricalAxis and setting LabelFormat to "{}{0:dd/MM/yyyy \nHH:mm:ss}"...

 

 

 
0
Martin Ivanov
Telerik team
answered on 09 Feb 2016, 01:22 PM
Hello Dani,

Can you tell me what kind of crash you get using the CategoricalAxis? I tested the format on my side and it works as expected. Can you also take a look at the attached project and let me know if I am missing something? 

Regards,
Martin
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Daní
Top achievements
Rank 1
answered on 09 Feb 2016, 03:42 PM

Hi Martin,

 First of all, you should notice the "\n" part of label format. I want to format de label in 2 lines in order to reduce the bottom margin required for such a long label.

The behaviour is a bit a weird, in yout demo project, setting Label format to {}{0:dd/MM/yyyy \nHH:mm:ss} , please, note the "\n" part between date and time sections, do not cause any crash, the "\n" part is ignored and the result is something like "09/02/2016 n16:35:00" in a single line.

 

My scenario is a bit more complex and i have to create the charts dynamically by code, therefore, HorizontalAxis i also created by code. The weid thing is that if create the axis with the following code I get an exception (FormatException: Input string was not in a correct format) that makes the application crash. As I said in previous post, everything works as expected if I use a DateTimeCategoricalAxis

chart.HorizontalAxis = new CategoricalAxis
                {
                    LabelFitMode = AxisLabelFitMode.Rotate,
                    LabelRotationAngle = 45,
                    LabelFormat = "{}{0:dd/MM/yyyy \nHH:mm:ss}",
                    PlotMode = AxisPlotMode.OnTicksPadded,
                    Padding = new Thickness(0.1),
                    GapLength = 0.1,
                    HorizontalContentAlignment = HorizontalAlignment.Center,
                    LabelInterval = 1,
                    MajorTickInterval = 1,
#if SILVERLIGHT
                    LabelTemplateSelector = new HorizontalAxisTemplateSelector(series)
#endif
                };

0
Ivan
Telerik team
answered on 12 Feb 2016, 11:44 AM
Hi Dani,

The reason for this exception is the opening and closing curly braces next to each other.
LabelFormat = "{}{0:dd/MM/yyyy \nHH:mm:ss}",

This syntax is used in xaml to escape the opening curly brace, because the opening curly brace is a special symbol in xaml. But in c# code this syntax should not be used.

Try to replace the above code snipped with the following and let us know if it works for you.
LabelFormat = "{0:dd/MM/yyyy \nHH:mm:ss}",


Regards,
Ivan
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
ChartView
Asked by
Daní
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Daní
Top achievements
Rank 1
Ivan
Telerik team
Share this question
or