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
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="MauiSample.Pies.Gauge5.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.Pies.Gauge5;assembly=ViewModelsSamples"
x:DataType="vms:ViewModel">
<ContentPage.BindingContext>
<vms:ViewModel/>
</ContentPage.BindingContext>
<Grid RowDefinitions="Auto, *">
<Button
Grid.Row="0"
Command="{Binding DoRandomChangeCommand}"
Text="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>
</ContentPage>