Custom 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.

This web site wraps every sample using a UserControl instance, but LiveCharts controls can be used inside any container.

sample image

View model

using LiveChartsCore.Drawing;

namespace ViewModelsSamples.Bars.Custom;

public class ViewModel
{
    public double[] Values1 { get; } = [2, 1, 4];
    public double[] Values2 { get; } = [4, 3, 6];
    public double[] Values3 { get; } = [-2, 2, 1];
    public double[] Values4 { get; } = [1, 2, 3];
    public string StarPath { get; } = SVGPoints.Star;
}

MyGeometry.cs

using LiveChartsCore.Drawing;
using LiveChartsCore.SkiaSharpView.Drawing;

namespace ViewModelsSamples.Bars.Custom;

public class MyGeometry : BoundedDrawnGeometry, IDrawnElement<SkiaSharpDrawingContext>
{
    public void Draw(SkiaSharpDrawingContext context)
    {
        var paint = context.ActiveSkiaPaint;
        var canvas = context.Canvas;
        var y = Y;

        while (y < Y + Height)
        {
            canvas.DrawLine(X, y, X + Width, y, paint);
            y += 5;
        }
    }
}

XAML

<UserControl
    x:Class="AvaloniaSample.Bars.Custom.View"
    xmlns="https://github.com/avaloniaui"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:lvc="using:LiveChartsCore.SkiaSharpView.Avalonia"
    xmlns:vms="using:ViewModelsSamples.Bars.Custom"
    xmlns:draw="using:LiveChartsCore.SkiaSharpView.Drawing.Geometries"
    xmlns:local="using:ViewModelsSamples.Bars.Custom"
    x:DataType="vms:ViewModel">

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

    <lvc:CartesianChart>
        <lvc:CartesianChart.Series>
            <lvc:XamlColumnSeries Values="{Binding Values1}"/>

            <!--
            Use the generic type arguments to set the type of the geometry to draw,
            here are many predefined geometries in the LiveChartsCore.SkiaSharpView.Drawing.Geometries
            namespace, for example, the StarGeometry, CrossGeometry, RectangleGeometry and DiamondGeometry

            The first type argument is the type of data, in this case double (parsed from the values string).
            The second type argument is the type of geometry to draw.
            -->
            <lvc:XamlColumnSeries
                x:TypeArguments="x:Double, draw:DiamondGeometry"
                Values="{Binding Values2}" />

            <!--
            You can also use SVG paths to draw the geometry
            the VariableSVGPathGeometry can change the drawn path at runtime
            -->
            <lvc:XamlColumnSeries
                x:TypeArguments="x:Double, draw:VariableSVGPathGeometry"
                GeometrySvg="{Binding StarPath}"
                Values="{Binding Values3}"/>

            <!--
            Finally you can also use SkiaSharp to draw your own geometry
            -->
            <lvc:XamlColumnSeries
                x:TypeArguments="x:Double, local:MyGeometry"
                Values="{Binding Values4}"/>

        </lvc:CartesianChart.Series>
    </lvc:CartesianChart>
</UserControl>

Articles you might also find useful:

Cartesian chart control
Column series properties