We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Data binding question for SfChart

How to use the TimeSpanAxis for a Series where bound collection x axis property is of type double representing seconds?  Is there a way to use a converter with the XBindingPath? I am assuming if using the TimeSpanAxis for the Primary, that the type expected for the Series XBindingPath needs to be a TimeSpan or a string with a valid time span representation?


1 Reply

MD Moneeshram Dhanabal Syncfusion Team February 10, 2023 10:15 AM UTC

Hi Jack,


We have validated your query and would like to suggest that you can use your double type seconds data in our TimeSpan axis by using below solution. 

You can convert your seconds to TimeSpan by using TimeSpan.FromSeconds methods. Then assign the TimeSpan type model property to our XBindingPath as per the below code snippet. 

public class ViewModel

{

        private Random random;

        public ObservableCollection<ChartModel> Data { get; set; }

        public ViewModel()

        {

            random = new Random();

            Data = new ObservableCollection<ChartModel>();

            for(int i=0; i<10;i++)

            {

                Data.Add(new ChartModel() { TimeSpanValue = GetTimeSpanValue(random.Next(20,120)),

                                           Value1 = random.Next(10,50) });

            }

        }

        private TimeSpan GetTimeSpanValue(double seconds)

        {

            return TimeSpan.FromSeconds(seconds);

        }

}

public class ChartModel

{

        public double Value1 { get; set; }

        public TimeSpan TimeSpanValue { get; set; }

}


<chart:SfChart>

 

            <chart:SfChart.PrimaryAxis>

                <chart:TimeSpanAxis/>

            </chart:SfChart.PrimaryAxis>

 

            <chart:SfChart.SecondaryAxis>

                <chart:NumericalAxis/>

            </chart:SfChart.SecondaryAxis>

 

            <chart:LineSeries ItemsSource="{Binding Data}" Label="First"

                                           XBindingPath="TimeSpanValue" YBindingPath="Value1" />

 

</chart:SfChart>


I have also attached a demo sample based on your requirement, please check and let us know if need any further assistance.


Regards,

Moneeshram


Attachment: F_180453_a2442f0f.zip

Loader.
Live Chat Icon For mobile
Up arrow icon