Auto Updates On Gauges
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.Collections.Generic;
using LiveChartsCore;
using LiveChartsCore.Defaults;
using LiveChartsCore.Measure;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using LiveChartsCore.SkiaSharpView.Extensions;
namespace ViewModelsSamples.Pies.Gauge5;
public partial class ViewModel : ObservableObject
{
private readonly Random _random = new();
public ViewModel()
{
ObservableValue1 = new ObservableValue { Value = 50 };
ObservableValue2 = new ObservableValue { Value = 80 };
Series = GaugeGenerator.BuildSolidGauge(
new GaugeItem(ObservableValue1, series =>
{
series.Name = "North";
series.DataLabelsPosition = PolarLabelsPosition.Start;
}),
new GaugeItem(ObservableValue2, series =>
{
series.Name = "South";
series.DataLabelsPosition = PolarLabelsPosition.Start;
}));
}
public ObservableValue ObservableValue1 { get; set; }
public ObservableValue ObservableValue2 { get; set; }
public IEnumerable<ISeries> Series { get; set; }
[RelayCommand]
public void DoRandomChange()
{
// modifying the Value property updates and animates the chart automatically
ObservableValue1.Value = _random.Next(0, 100);
ObservableValue2.Value = _random.Next(0, 100);
}
}
XAML
<UserControl x:Class="UnoWinUISample.Pies.Gauge5.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.Pies.Gauge5"
mc:Ignorable="d">
<UserControl.DataContext>
<vms:ViewModel/>
</UserControl.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Command="{Binding DoRandomChangeCommand}">Change Value</Button>
<lvc:PieChart
Grid.Row="1"
Series="{Binding Series}"
InitialRotation="-90"
MinValue="0"
MaxValue="100"
MaxAngle="270"
LegendPosition="Bottom">
</lvc:PieChart>
</Grid>
</UserControl>