Grouped And Stacked Bars
The [ObservableObject]
, [ObservableProperty]
and [RelayCommand]
attributes come from the
CommunityToolkit.Mvvm package, you can read more about it
here.
This web site wraps every sample using a UserControl
instance, but LiveCharts controls can be used inside any container.
View model
using System.Collections.Generic;
using CommunityToolkit.Mvvm.ComponentModel;
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
namespace ViewModelsSamples.StackedBars.Groups;
public partial class ViewModel : ObservableObject
{
public ISeries[] Series { get; set; } =
{
new StackedColumnSeries<int>
{
Values = new List<int> { 3, 5, 3 },
Stroke = null,
StackGroup = 0 // mark
},
new StackedColumnSeries<int>
{
Values = new List<int> { 4, 2, 3 },
Stroke = null,
StackGroup = 0 // mark
},
new StackedColumnSeries<int>
{
Values = new List<int> { 4, 6, 6 },
Stroke = null,
StackGroup = 1 // mark
},
new StackedColumnSeries<int>
{
Values = new List<int> { 2, 5, 4 },
Stroke = null,
StackGroup = 1 // mark
}
};
public Axis[] XAxis { get; set; } =
{
new Axis
{
LabelsRotation = -15,
Labels = new[] { "Category 1", "Category 2", "Category 3" }
}
};
}
XAML
<UserControl x:Class="UnoWinUISample.StackedBars.Groups.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.StackedBars.Groups"
mc:Ignorable="d">
<UserControl.DataContext>
<vms:ViewModel/>
</UserControl.DataContext>
<lvc:CartesianChart Series="{Binding Series}" XAxes="{Binding XAxis}"/>
</UserControl>