custom label format not properly created

3 posts, 1 answers
  1. Parth
    Parth avatar
    2 posts
    Member since:
    Aug 2010

    Posted 30 Apr 2014 Link to this post

    Hi,

    I am trying to create custom label format but I have a problem when there is only 1 record in my dataset. For more than 1 record the label works normally.

    XAML Code:
    <Setter Property="Template">
         <Setter.Value>
    <ControlTemplate TargetType="telerik:AxisLabel2D">
     <?SILVERLIGHT BEGIN?>
     <primitives:LayoutTransformControl x:Name="PART_LayoutTransformControl"  VerticalAlignment="{TemplateBinding VerticalAlignment}"  HorizontalAlignment="{TemplateBinding HorizontalAlignment}">
      <primitives:LayoutTransformControl.Content>
      <TextBlock Style="{TemplateBinding ItemLabelStyle}"  Text="{Binding Converter={StaticResource LabelConverter}}" />
       </primitives:LayoutTransformControl.Content>
       <primitives:LayoutTransformControl.LayoutTransform>
        <RotateTransform x:Name="PART_RotateTransform" />
      </primitives:LayoutTransformControl.LayoutTransform>
      </primitives:LayoutTransformControl>
    <?SILVERLIGHT END?>
    </ControlTemplate>
    </Setter.Value>
    </Setter>

    Convert Override Method:
    public class LabelConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                string returnValue;
                TickPoint tickPoint = value as TickPoint;
                int pkIndex = 0;
     
                switch ((value as TickPoint).LabelFormat)
                {
                    case "CustomTopComponentFormat":
                        pkIndex = (value as TickPoint).Label.IndexOf("Component_CapitolItem_PK_Sort");
                        returnValue = (value as TickPoint).Label.Substring(0, pkIndex);
                        break;
                    case "CustomStdDevFormat":
                        pkIndex = (value as TickPoint).Label.IndexOf("Component_Item_FK");
                        returnValue = (value as TickPoint).Label.Substring(0, pkIndex);
                        break;
                    case "AggregateShortName":
                        if ((value as TickPoint).Label != null)
                        {
                            string[] lines = (value as TickPoint).Label.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
                            if (lines.Count() > 1)
                                returnValue = string.Concat(lines[0], "\n", lines[1]);
                            else
                                returnValue = lines[0];
                            break;
                        }
                        else
                        {
                            returnValue = null;
                            break;
                        }
                    case "SmartMoney":
                        double money;
                        if (double.TryParse((value as TickPoint).Value.ToString(), out money))
                        {
                            string[] dec = money.ToString("C0").Split(new char[] {','});
                            switch (dec.Count())
                            {                           
                                case 2:
                                    returnValue = string.Concat((money / 1000.0).ToString("$###.#"), "k");
                                    break;
                                case 3:
                                    returnValue = string.Concat((money / 1000000.0).ToString("$###.#"), "mil");
                                    break;
                                case 4:
                                    returnValue = string.Concat((money / 1000000000.0).ToString("$###.#"), "bil");
                                    break;
                                case 5:
                                    returnValue = string.Concat((money / 1000000000000.0).ToString("$###.#"), "trl");
                                    break;                         
                                default:
                                    returnValue = money.ToString("C0");
                                    break;
                            }
                        }
                        else
                            returnValue = (value as TickPoint).Value.ToString();
                        break;
                    default:
                        if (string.IsNullOrWhiteSpace((value as TickPoint).LabelFormat))
                            returnValue = (value as TickPoint).Value.ToString();
                        else
                            returnValue = (value as TickPoint).Value.ToString((value as TickPoint).LabelFormat);
                        break;
                }
                return returnValue;
            }
     
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                return "";
            }
        }

    Check the attached chart image to see the issue. The X-axis labels are repeated with the value of the dataset.

    Any help will be appreciated.

    Thanks,
    Parth
  2. Answer
    Petar Marchev
    Admin
    Petar Marchev avatar
    968 posts

    Posted 01 May 2014 Link to this post

    Hi Parth,

    When there is only a single YValue that the axis has to work with, a very small range is calculated. For example if you have one item with y value 10 000 000, the range calculated by the axis is actually (min = 9 999 999, max = 10 000 001, step = 1). This will happen even if you have two items, if both of the items have the same Y value. Later when the labels are shown, the human-readable logic of the chart kicks in and shows 10mil, 10mil, 10mil for the actula values of 9999999, 10000000, 10000001.

    There are a couple of things you can do. One is to set the IsZeroBased property of the axis to true. This will ensure that the zero is always included in the range and the issue will not occur. Another solution is to set the DefaultLabelFormat property of the axis (say "G15"). This will force the chart to show the actual numbers and not the human readable k or mil.

    Another solution would be to set the DefaultLabelFormat only when needed. Perhaps you can attach a handler to the RangeChanged event of the axis and in the event check to see whether the step chosen is too small for the numbers being displayed. In this case you can set the label format to what you decide best, or you can set it to string.Empty to get the human readable formatting back on. I hope this helps.

    On a side note - if you have not yet integrated the old RadChart into your application, I suggest you switch to our new ChartView control. It is much faster, very easy to set up and is really flexible. References:
    qsf demos
    RadChart vs ChartView
    sdk samples (you can install the sdk browser for quick and fun browsing)

    Regards,
    Petar Marchev
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. DevCraft banner
  4. Parth
    Parth avatar
    2 posts
    Member since:
    Aug 2010

    Posted 01 May 2014 in reply to Petar Marchev Link to this post

    Hi Petar,

    Setting the IsZeroBased property to true worked for me. 

    Thanks,
    Parth
Back to Top