Custom S V G Scatter Points
This web site builds the control from code behind but you could also grab it from the toolbox, this sample also uses a ViewModel to populate the properties of the control(s) in this sample.
View model
using System;
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using LiveChartsCore;
using LiveChartsCore.Defaults;
using LiveChartsCore.Drawing;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Drawing.Geometries;
namespace ViewModelsSamples.Scatter.Custom;
public partial class ViewModel : ObservableObject
{
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 = new ISeries[]
{
// use the second type parameter to specify the geometry to draw for every point
// there are already many predefined geometries in the
// LiveChartsCore.SkiaSharpView.Drawing.Geometries namespace
new ScatterSeries<ObservablePoint, RoundedRectangleGeometry>
{
Values = values1,
Stroke = null,
GeometrySize = 40,
},
// You can also use SVG paths to draw the geometry
// LiveCharts already provides some predefined paths in the SVGPoints class.
new ScatterSeries<ObservablePoint, SVGPathGeometry>
{
Values = values2,
GeometrySvg = SVGPoints.Heart
},
// you can declare your own gemetry and use the SkiaSharp api to draw it
new ScatterSeries<ObservablePoint, MyGeometry>
{
Values = values3,
GeometrySize = 40,
}
};
}
public ISeries[] Series { get; set; }
}
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;
}
}
}
Form code behind
using System.Windows.Forms;
using LiveChartsCore.SkiaSharpView.WinForms;
using ViewModelsSamples.Scatter.Custom;
namespace WinFormsSample.Scatter.Custom;
public partial class View : UserControl
{
private readonly CartesianChart cartesianChart;
public View()
{
InitializeComponent();
Size = new System.Drawing.Size(50, 50);
var viewModel = new ViewModel();
cartesianChart = new CartesianChart
{
Series = viewModel.Series,
// out of livecharts properties...
Location = new System.Drawing.Point(0, 0),
Size = new System.Drawing.Size(50, 50),
Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom
};
Controls.Add(cartesianChart);
}
}