Time Span Scale

This sample uses C# 12 features, 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;
using LiveChartsCore.Defaults;
using LiveChartsCore.Kernel.Sketches;
using LiveChartsCore.SkiaSharpView;

namespace ViewModelsSamples.Axes.TimeSpanScaled;

public class ViewModel
{
    public ISeries[] Series { get; set; } = [
        new ColumnSeries<TimeSpanPoint>
        {
            Values = [
                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 }
            ],
        }
    ];

    // You can use the TimeSpanAxis class to define a time span based axis // mark

    // The first parameter is the time between each point, in this case 1 day // mark
    // you can also use 1 year, 1 month, 1 hour, 1 minute, 1 second, 1 millisecond, etc // mark

    // The second parameter is a function that receives the value and returns the label // mark
    public ICartesianAxis[] XAxes { get; set; } = [
        new TimeSpanAxis(TimeSpan.FromMilliseconds(1), date => date.ToString("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">
    <ContentPage.BindingContext>
        <vms:ViewModel/>
    </ContentPage.BindingContext>
    <lvc:CartesianChart
        Series="{Binding Series}"
        YAxes="{Binding YAxes}">
    </lvc:CartesianChart>
</ContentPage>

Articles you might also find useful: