Line Geometries

This sample uses C# 12 features, 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;
using LiveChartsCore.Drawing;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Drawing.Geometries;

namespace ViewModelsSamples.Lines.Custom;

public class ViewModel
{
    public ISeries[] Series { get; set; } =
    [
        new LineSeries<double>
        {
            Values = [2, 1, 4, 2, 2, -5, -2],
            Fill = null,
            GeometrySize = 20
        },

        // use the second generic parameter to define the geometry to draw
        // there are many predefined geometries in the LiveChartsCore.Drawing namespace
        // for example, the StarGeometry, CrossGeometry, RectangleGeometry and DiamondGeometry
        new LineSeries<double, StarGeometry>
        {
            Values = [3, 3, -3, -2, -4, -3, -1],
            Fill = null,
            GeometrySize = 20
        },

        // You can also use SVG paths to draw the geometry
        // the VariableSVGPathGeometry can change the drawn path at runtime
        new LineSeries<double, VariableSVGPathGeometry>
        {
            Values = [-2, 2, 1, 3, -1, 4, 3],
            Fill = null,
            GeometrySvg = SVGPoints.Pin,
            GeometrySize = 20
        },

        // finally you can also use SkiaSharp to draw your own geometry
        new LineSeries<double, MyGeometry>
        {
            Values = [4, 5, 2, 4, 3, 2, 1],
            Fill = null,
            GeometrySize = 20
        },
    ];
}

MyGeometry.cs

using LiveChartsCore.SkiaSharpView.Drawing;
using LiveChartsCore.SkiaSharpView.Drawing.Geometries;
using SkiaSharp;

namespace ViewModelsSamples.Lines.Custom;

public class MyGeometry : SizedGeometry
{
    public override void OnDraw(SkiaSharpDrawingContext context, SKPaint paint)
    {
        var canvas = context.Canvas;

        canvas.DrawRect(X, Y, Width, Height, paint);
        canvas.DrawLine(X, Y, X + Width, Y + Height, paint);
        canvas.DrawLine(X + Width, Y, X, Y + Height, paint);
    }
}

XAML

<UserControl x:Class="UnoWinUISample.Lines.Custom.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.Lines.Custom"
            mc:Ignorable="d">
    <UserControl.DataContext>
        <vms:ViewModel/>
    </UserControl.DataContext>
    <lvc:CartesianChart Series="{Binding Series}"/>
</UserControl>

Articles you might also find useful:

Cartesian chart control
Line series properties