Stacked Bars

sample image sample image

Hover over the image to see the chart animation

This sample uses C# 12 features, it also uses features from the CommunityToolkit.Mvvm package, you can learn more about it here.

View Model

using LiveChartsCore;
using LiveChartsCore.Measure;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using SkiaSharp;

namespace ViewModelsSamples.StackedBars.Basic;

public class ViewModel
{
    public ISeries[] Series { get; set; } =
    [
        new StackedColumnSeries<int>
        {
            Values = [3, 5, -3, 2, 5, -4, -2],
            Stroke = null,
            DataLabelsPaint = new SolidColorPaint(new SKColor(45, 45, 45)),
            DataLabelsSize = 14,
            DataLabelsPosition = DataLabelsPosition.Middle,
            YToolTipLabelFormatter =
                p => $"{p.Coordinate.PrimaryValue:N} ({p.StackedValue!.Share:P})"
        },
        new StackedColumnSeries<int>
        {
            Values = [4, 2, -3, 2, 3, 4, -2],
            Stroke = null,
            DataLabelsPaint = new SolidColorPaint(new SKColor(45, 45, 45)),
            DataLabelsSize = 14,
            DataLabelsPosition = DataLabelsPosition.Middle,
            YToolTipLabelFormatter =
                p => $"{p.Coordinate.PrimaryValue:N} ({p.StackedValue!.Share:P})"
        },
        new StackedColumnSeries<int>
        {
            Values = [-2, 6, 6, 5, 4, 3, -2],
            Stroke = null,
            DataLabelsPaint = new SolidColorPaint(new SKColor(45, 45, 45)),
            DataLabelsSize = 14,
            DataLabelsPosition = DataLabelsPosition.Middle,
            YToolTipLabelFormatter =
                p => $"{p.Coordinate.PrimaryValue:N} ({p.StackedValue!.Share:P})"
        }
    ];
}

XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="MauiSample.StackedBars.Basic.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.StackedBars.Basic;assembly=ViewModelsSamples"
             >
    <ContentPage.BindingContext>
        <vms:ViewModel/>
    </ContentPage.BindingContext>
	<ContentPage.Content>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <lvc:CartesianChart
                Series="{Binding Series}">
            </lvc:CartesianChart>
        </Grid>
    </ContentPage.Content>
</ContentPage>

Articles you might also find useful: