Basic Bars

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.

sample image sample image

View Model

namespace ViewModelsSamples.Bars.Basic;

public class ViewModel
{
    public double[] MaryValues { get; set; } =
        [2, 5, 4];

    public double[] AnaValues { get; set; } =
        [3, 1, 6];

    public string[] Labels { get; set; } =
        ["Category 1", "Category 2", "Category 3"];
}

XAML

<UserControl
    x:Class="AvaloniaSample.Bars.Basic.View"
    xmlns="https://github.com/avaloniaui"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:lvc="clr-namespace:LiveChartsCore.SkiaSharpView.Avalonia;assembly=LiveChartsCore.SkiaSharpView.Avalonia"
    xmlns:vms="clr-namespace:ViewModelsSamples.Bars.Basic;assembly=ViewModelsSamples"
    x:DataType="vms:ViewModel">

    <UserControl.DataContext>
        <vms:ViewModel/>
    </UserControl.DataContext>

    <lvc:CartesianChart>

        <lvc:CartesianChart.Series>
            <lvc:XamlColumnSeries SeriesName="Mary" Values="{Binding MaryValues}"/>
            <lvc:XamlColumnSeries SeriesName="Ana" Values="{Binding AnaValues}"/>
        </lvc:CartesianChart.Series>

        <lvc:CartesianChart.XAxes>
            <lvc:XamlAxis
                Labels="{Binding Labels}"
                SeparatorsPaint="{lvc:SolidColorPaint Color='#c8c8c8'}"
                SeparatorsAtCenter="False"
                TicksPaint="{lvc:SolidColorPaint Color='#232323'}"
                TicksAtCenter="True"
                MinStep="1"
                ForceStepToMin="True"/>
        </lvc:CartesianChart.XAxes>

    </lvc:CartesianChart>
</UserControl>

Articles you might also find useful: