Custom S V G Scatter Points
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.
View model
using System;
using LiveChartsCore.Defaults;
using LiveChartsCore.Drawing;
namespace ViewModelsSamples.Scatter.Custom;
public class ViewModel
{
private static readonly Random s_r = new();
public ObservablePoint[] Values1 { get; set; } = Fetch();
public ObservablePoint[] Values2 { get; set; } = Fetch();
public ObservablePoint[] Values3 { get; set; } = Fetch();
public string PinPath { get; } = SVGPoints.Pin;
private static ObservablePoint[] Fetch()
{
var length = 10;
var values = new ObservablePoint[length];
for (var i = 0; i < length; i++)
{
var x = s_r.Next(0, 20);
var y = s_r.Next(0, 20);
values[i] = new ObservablePoint(x, y);
}
return values;
}
}
MyGeometry.cs
using LiveChartsCore.Drawing;
using LiveChartsCore.SkiaSharpView.Drawing;
namespace ViewModelsSamples.Scatter.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 += 10;
}
}
}
XAML
<UserControl
x:Class="WPFSample.Scatter.Custom.View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:lvc="clr-namespace:LiveChartsCore.SkiaSharpView.WPF;assembly=LiveChartsCore.SkiaSharpView.WPF"
xmlns:vms="clr-namespace:ViewModelsSamples.Scatter.Custom;assembly=ViewModelsSamples"
xmlns:svg="clr-namespace:LiveChartsCore.Drawing;assembly=LiveChartsCore"
xmlns:draw="clr-namespace:LiveChartsCore.SkiaSharpView.Drawing.Geometries;assembly=LiveChartsCore.SkiaSharpView"
xmlns:d="clr-namespace:LiveChartsCore.Defaults;assembly=LiveChartsCore"
xmlns:local="clr-namespace:WPFSample.Scatter.Custom">
<UserControl.DataContext>
<vms:ViewModel/>
</UserControl.DataContext>
<lvc:CartesianChart>
<lvc:CartesianChart.Series>
<lvc:SeriesCollection>
<!--
The HeartScatterSeries is a scatter series that draws a heart shape.
-->
<local:HeartScatterSeries
Values="{Binding Values1}"
Stroke="{x:Null}"
GeometrySize="40" />
<!--
You can also use SVG paths to draw the geometry
the VariableSVGPathGeometry can change the drawn path at runtime
-->
<local:SvgScatterSeries
Values="{Binding Values2}"
GeometrySvg="{Binding PinPath}"
GeometrySize="40"/>
<!--
Finally you can also use SkiaSharp to draw your own geometry
-->
<local:MyGeometryScatterSeries
Values="{Binding Values3}"
GeometrySize="40"/>
</lvc:SeriesCollection>
</lvc:CartesianChart.Series>
</lvc:CartesianChart>
</UserControl>
CustomSeries.cs
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Drawing.Geometries;
using LiveChartsCore.Defaults;
using ViewModelsSamples.Scatter.Custom;
using LiveChartsCore.SkiaSharpView.WPF;
namespace WPFSample.Scatter.Custom;
// WPF xaml parser does not support generic types
// we instead create non-generic classes that inherit from the generic one.
public class HeartScatterSeries : XamlScatterSeries<ObservablePoint, HeartGeometry> { }
public class SvgScatterSeries : XamlScatterSeries<ObservablePoint, VariableSVGPathGeometry> { }
public class MyGeometryScatterSeries : XamlScatterSeries<ObservablePoint, MyGeometry> { }