CategoricalAxis LabelFormat not working

6 posts, 0 answers
  1. Daní
    Daní avatar
    303 posts
    Member since:
    Feb 2008

    Posted 04 Feb Link to this post

    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í

  2. Martin
    Admin
    Martin avatar
    1099 posts

    Posted 08 Feb Link to this post

    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
  3. DevCraft banner
  4. Daní
    Daní avatar
    303 posts
    Member since:
    Feb 2008

    Posted 08 Feb in reply to Martin Link to this post

    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}"...

     

     

     
  5. Martin
    Admin
    Martin avatar
    1099 posts

    Posted 09 Feb Link to this post

    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
  6. Daní
    Daní avatar
    303 posts
    Member since:
    Feb 2008

    Posted 09 Feb in reply to Martin Link to this post

    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
                    };

  7. Ivan
    Admin
    Ivan avatar
    44 posts

    Posted 12 Feb Link to this post

    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
Back to Top
DevCraft banner