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

How can i use TimeSpan in SfChart?

Hello Syncfusion,

I want to specify the time in my project on the X axis, but not with DateTime but with TimeSpan.

My first point is at x=Timespan{00:00:00}, but the X axis shows 1900.

How can I use TimeSpan and format it correctly?


My class model:

public class Data

    {

       //Value

        public float Value { get; set; } = 0;

       //Time

        public TimeSpan CurrentTimeSpan { get; set; }

    }


With kind regards

Robert


1 Reply

RR Raja Ramalingam Syncfusion Team January 19, 2023 07:27 AM UTC

Hi Robert,


We have achieved your requirement using Numerical axis by converting the double values into timespan values in the LabelCreated event handler as per the code snippets provided. We hope that this meets your requirements.


<chart:SfChart.PrimaryAxis>

    <chart:NumericalAxis LabelCreated="NumericalAxis_LabelCreated" >

    </chart:NumericalAxis>

</chart:SfChart.PrimaryAxis>


private void NumericalAxis_LabelCreated(object sender, ChartAxisLabelEventArgs e)

{

    var value = e.LabelContent;

    if (double.TryParse(value, out double parsedValue))

    {

        var timeSpan = TimeSpan.FromMilliseconds(parsedValue).ToString();

        e.LabelContent = timeSpan;

    }

}


Output:


Best regards,

Raja.


Loader.
Live Chat Icon For mobile
Up arrow icon