Custom ItemLabelFormat issue

2 posts, 0 answers
  1. Akhil
    Akhil avatar
    1 posts
    Member since:
    Aug 2012

    Posted 27 Aug 2012 Link to this post

    Hello,

    I am trying to Display Data on a Bar chart with the y-Axis plotting the Time in Minutes to Resolve an issue. It works great, except, i am unable to figure out a way to Format the Time in Minutes to a User Friendly string such as "3d, 2 h, 26 min", etc.

    I have tried using the FormatExpressions for the Series Definition's ItemLabelFormat, but they don't seem to work for the requirement. I would really appreciate if you can point me in the direction of achieving the above mentioned functionality. The screenshot of the chart can be found here: Chart Image

    Thanks in Advance!
  2. Evgenia
    Admin
    Evgenia avatar
    1406 posts

    Posted 30 Aug 2012 Link to this post

    Hi Akhil,

     Using the FormatExpressions for the Series Definition's ItemLabelFormat  won't be helpful to you as you can't have such complex format in it. In fact you'll have to prepare the format for the value at the time you bind the chart. The following sample demonstrates what I mean:

    public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
                List<TradeData> data = new List<TradeData>();
                Random r = new Random();
                for (int i = 0; i < 10; i++)
                {
                    data.Add(new TradeData() { Value = i, Minutes = r.Next(0, 2000) });
                }
                radChart.ItemsSource = data;
                SeriesMapping mappingStartUpTime = new SeriesMapping();
                ItemMapping timeMappingTime = new ItemMapping("SecondsFormatted", DataPointMember.Label);
                ItemMapping timeMapping = new ItemMapping("Minutes", DataPointMember.YValue);
                timeMappingTime.FieldType = typeof(double);
                timeMapping.FieldType = typeof(double);
                mappingStartUpTime.ItemMappings.Add(timeMappingTime);
                mappingStartUpTime.ItemMappings.Add(timeMapping); ;
                radChart.SeriesMappings.Add(mappingStartUpTime);
            }
      
            public class TradeData
            {
                public double Value
                {
                    get;
                    set;
                }
                public double Minutes
                {
                    get;
                    set;
                }
                public string SecondsFormatted
                {
                    get
                    {
                        TimeSpan formattedValue = TimeSpan.FromMinutes(Minutes);
                        return (
                                string.Format("{0:00}:{1:00}:{2:00}", formattedValue.Days, formattedValue.Hours, formattedValue.Minutes)
                              );
                    }
                }
            }
        }

    I used another string property (SecondsFormatted) which returns the formatted minutes of a TimeSpan. Then bound it to the Label DataPointMember of RadChart's ItemMapping (marked in yellow) and voilà.

    Regards,
    Evgenia
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
Back to Top