Custom S V G Scatter Points
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 ContentPage
instance, but LiveCharts controls can be used inside any container.
View model
using System;
using System.Collections.ObjectModel;
using LiveChartsCore;
using LiveChartsCore.Defaults;
using LiveChartsCore.Drawing;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Drawing.Geometries;
namespace ViewModelsSamples.Scatter.Custom;
public class ViewModel
{
public ISeries[] Series { get; set; }
public ViewModel()
{
var r = new Random();
var values1 = new ObservableCollection<ObservablePoint>();
var values2 = new ObservableCollection<ObservablePoint>();
var values3 = new ObservableCollection<ObservablePoint>();
for (var i = 0; i < 20; i++)
{
values1.Add(new ObservablePoint(r.Next(0, 20), r.Next(0, 20)));
values2.Add(new ObservablePoint(r.Next(0, 20), r.Next(0, 20)));
values3.Add(new ObservablePoint(r.Next(0, 20), r.Next(0, 20)));
}
Series = [
// 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 ScatterSeries<ObservablePoint, HeartGeometry>
{
Values = values1,
Stroke = null,
GeometrySize = 40
},
// You can also use SVG paths to draw the geometry
// the VariableSVGPathGeometry can change the drawn path at runtime
new ScatterSeries<ObservablePoint, VariableSVGPathGeometry>
{
Values = values2,
GeometrySvg = SVGPoints.Pin
},
// finally you can also use SkiaSharp to draw your own geometry
new ScatterSeries<ObservablePoint, MyGeometry>
{
Values = values3,
GeometrySize = 40,
}
];
}
}
MyGeometry.cs
using LiveChartsCore.SkiaSharpView.Drawing;
using LiveChartsCore.SkiaSharpView.Drawing.Geometries;
using SkiaSharp;
namespace ViewModelsSamples.Scatter.Custom;
public class MyGeometry : SizedGeometry
{
public override void OnDraw(SkiaSharpDrawingContext context, SKPaint paint)
{
var canvas = context.Canvas;
var y = Y;
while (y < Y + Height)
{
canvas.DrawLine(X, y, X + Width, y, paint);
y += 10;
}
}
}
XAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="MauiSample.Scatter.Custom.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.Scatter.Custom;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>