Angular Gauge
The [ObservableObject]
, [ObservableProperty]
and [RelayCommand]
attributes come from the
CommunityToolkit.Mvvm package, you can read more about it
here.
View model
using System.Collections.Generic;
using LiveChartsCore;
using CommunityToolkit.Mvvm.ComponentModel;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Extensions;
using LiveChartsCore.VisualElements;
using LiveChartsCore.SkiaSharpView.Drawing;
using LiveChartsCore.SkiaSharpView.VisualElements;
using LiveChartsCore.Defaults;
using CommunityToolkit.Mvvm.Input;
using System;
namespace ViewModelsSamples.Pies.AngularGauge;
public partial class ViewModel : ObservableObject
{
private readonly Random _random = new();
public ViewModel()
{
var sectionsOuter = 130;
var sectionsWidth = 20;
Needle = new NeedleVisual
{
Value = 45
};
Series = GaugeGenerator.BuildAngularGaugeSections(
new GaugeItem(60, s => SetStyle(sectionsOuter, sectionsWidth, s)),
new GaugeItem(30, s => SetStyle(sectionsOuter, sectionsWidth, s)),
new GaugeItem(10, s => SetStyle(sectionsOuter, sectionsWidth, s)));
VisualElements = new VisualElement<SkiaSharpDrawingContext>[]
{
new AngularTicksVisual
{
LabelsSize = 16,
LabelsOuterOffset = 15,
OuterOffset = 65,
TicksLength = 20
},
Needle
};
}
public IEnumerable<ISeries> Series { get; set; }
public IEnumerable<VisualElement<SkiaSharpDrawingContext>> VisualElements { get; set; }
public NeedleVisual Needle { get; set; }
[RelayCommand]
public void DoRandomChange()
{
// modifying the Value property updates and animates the chart automatically
Needle.Value = _random.Next(0, 100);
}
private static void SetStyle(
double sectionsOuter, double sectionsWidth, PieSeries<ObservableValue> series)
{
series.OuterRadiusOffset = sectionsOuter;
series.MaxRadialColumnWidth = sectionsWidth;
}
}
XAML
<UserControl x:Class="UnoWinUISample.Pies.AngularGauge.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.AngularGauge"
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}"
VisualElements="{Binding VisualElements}"
InitialRotation="-225"
MaxAngle="270"
MinValue="0"
MaxValue="100">
</lvc:PieChart>
</Grid>
</UserControl>