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

Custom ItemLabelFormat issue

1 Answer 19 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Akhil
Top achievements
Rank 1
Akhil asked on 27 Aug 2012, 05:03 PM
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!

1 Answer, 1 is accepted

Sort by
0
Evgenia
Telerik team
answered on 31 Aug 2012, 04:50 AM
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.

Tags
Chart
Asked by
Akhil
Top achievements
Rank 1
Answers by
Evgenia
Telerik team
Share this question
or