Time Span Scale

This sample uses C# 13 preview features such as partial properties, it also uses features from the CommunityToolkit.Mvvm package, you can learn more about it here.

This web site wraps every sample using a ContentPage instance, but LiveCharts controls can be used inside any container.

sample image

View model

using System;
using LiveChartsCore.Defaults;

namespace ViewModelsSamples.Axes.TimeSpanScaled;

public class ViewModel
{
    public TimeSpanPoint[] Values { get; set; } = [
        new () { TimeSpan = TimeSpan.FromMilliseconds(1), Value = 10 },
        new () { TimeSpan = TimeSpan.FromMilliseconds(2), Value = 6 },
        new () { TimeSpan = TimeSpan.FromMilliseconds(3), Value = 3 },
        new () { TimeSpan = TimeSpan.FromMilliseconds(4), Value = 12 },
        new () { TimeSpan = TimeSpan.FromMilliseconds(5), Value = 8 }
    ];

    public Func<TimeSpan, string> Formatter { get; set; } =
        value => $"{value:fff}ms";
}

XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="MauiSample.Axes.TimeSpanScaled.View"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:lvc="clr-namespace:LiveChartsCore.SkiaSharpView.Maui;assembly=LiveChartsCore.SkiaSharpView.Maui"
    xmlns:vms="clr-namespace:ViewModelsSamples.Axes.TimeSpanScaled;assembly=ViewModelsSamples"
    x:DataType="vms:ViewModel">

    <ContentPage.BindingContext>
        <vms:ViewModel/>
    </ContentPage.BindingContext>

    <lvc:CartesianChart>

        <lvc:CartesianChart.Series>
            <lvc:SeriesCollection>
                <lvc:XamlColumnSeries Values="{Binding Values}"/>
            </lvc:SeriesCollection>
        </lvc:CartesianChart.Series>

        <!--
            The Interval property is used to set the interval between each label,
            in this case, the interval is set to 1 millisecond.

            In the DateFormatter property, we define the format of the labels.
        -->

        <lvc:CartesianChart.XAxes>
            <lvc:AxesCollection>
                <lvc:XamlTimeSpanAxis
                    Interval="0:0:0.001"
                    TimeFormatter="{Binding Formatter}"/>
            </lvc:AxesCollection>
        </lvc:CartesianChart.XAxes>
        
    </lvc:CartesianChart>
</ContentPage>

Articles you might also find useful: