Racing Bars
The [ObservableObject]
, [ObservableProperty]
and [RelayCommand]
attributes come from the
CommunityToolkit.Mvvm package, you can read more about it
here.
View model
using System;
using System.Linq;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using LiveChartsCore;
using LiveChartsCore.Defaults;
using LiveChartsCore.Drawing;
using LiveChartsCore.Measure;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using SkiaSharp;
namespace ViewModelsSamples.Bars.Race;
public partial class ViewModel : ObservableObject
{
private readonly Random _r = new();
private static readonly (string, double)[] s_initialData =
{
("Tsunoda", 500),
("Sainz", 450),
("Riccardo", 520),
("Bottas", 550),
("Perez", 660),
("Verstapen", 920),
("Hamilton", 1000)
};
public ViewModel()
{
_ = StartRace();
}
public bool IsReading { get; set; } = true;
[ObservableProperty]
private ISeries[] _series =
s_initialData
.Select(x => new RowSeries<ObservableValue>
{
Values = new[] { new ObservableValue(x.Item2) },
Name = x.Item1,
Stroke = null,
MaxBarWidth = 25,
DataLabelsPaint = new SolidColorPaint(new SKColor(245, 245, 245)),
DataLabelsPosition = DataLabelsPosition.End,
DataLabelsTranslate = new LvcPoint(-1, 0),
DataLabelsFormatter = point => $"{point.Context.Series.Name} {point.Coordinate.PrimaryValue}"
})
.OrderByDescending(x => ((ObservableValue[])x.Values!)[0].Value)
.ToArray();
[ObservableProperty]
private Axis[] _xAxes = { new Axis { SeparatorsPaint = new SolidColorPaint(new SKColor(220, 220, 220)) } };
[ObservableProperty]
private Axis[] _yAxes = { new Axis { IsVisible = false } };
public async Task StartRace()
{
await Task.Delay(1000);
// to keep this sample simple, we run the next infinite loop
// in a real application you should stop the loop/task when the view is disposed
while (IsReading)
{
foreach (var item in Series)
{
if (item.Values is null) continue;
var i = ((ObservableValue[])item.Values)[0];
i.Value += _r.Next(0, 100);
}
Series = Series
.OrderByDescending(x => ((ObservableValue[])x.Values!)[0].Value)
.ToArray();
await Task.Delay(100);
}
}
}
XAML
<UserControl
x:Class="WinUISample.Bars.Race.View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:lvc="using:LiveChartsCore.SkiaSharpView.WinUI"
xmlns:vms="using:ViewModelsSamples.Bars.Race"
mc:Ignorable="d">
<UserControl.DataContext>
<vms:ViewModel/>
</UserControl.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<lvc:CartesianChart
Series="{Binding Series}"
XAxes="{Binding XAxes}"
YAxes="{Binding YAxes}"
TooltipPosition="Hidden">
</lvc:CartesianChart>
</Grid>
</UserControl>