Auto Updates On Gauges
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.
View model
using System;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.ComponentModel;
namespace ViewModelsSamples.Pies.Gauge5;
public partial class ViewModel : ObservableObject
{
private readonly Random _random = new();
[ObservableProperty]
public partial double Value1 { get; set; } = 50;
[ObservableProperty]
public partial double Value2 { get; set; } = 80;
[RelayCommand]
public void DoRandomChange()
{
Value1 = _random.Next(0, 100);
Value2 = _random.Next(0, 100);
}
}
XAML
<UserControl
x:Class="WPFSample.Pies.Gauge5.View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:lvc="clr-namespace:LiveChartsCore.SkiaSharpView.WPF;assembly=LiveChartsCore.SkiaSharpView.WPF"
xmlns:vms="clr-namespace:ViewModelsSamples.Pies.Gauge5;assembly=ViewModelsSamples"
MaxHeight="400"
MaxWidth="400">
<UserControl.DataContext>
<vms:ViewModel/>
</UserControl.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button
Grid.Row="0"
Command="{Binding DoRandomChangeCommand}"
Content="Change Value"/>
<lvc:PieChart
Grid.Row="1"
InitialRotation="-90"
MinValue="0"
MaxValue="100"
MaxAngle="270"
LegendPosition="Bottom">
<lvc:PieChart.Series>
<lvc:SeriesCollection>
<lvc:XamlGaugeSeries
SeriesName="North"
GaugeValue="{Binding Value1}"
DataLabelsPosition="Start"/>
<lvc:XamlGaugeSeries
SeriesName="South"
GaugeValue="{Binding Value2}"
DataLabelsPosition="Start"/>
</lvc:SeriesCollection>
</lvc:PieChart.Series>
</lvc:PieChart>
</Grid>
</UserControl>