Custom S V G Scatter Points
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;
}
}
}
Razor
@page "/Scatter/Custom"
@using LiveChartsCore.SkiaSharpView.Blazor
@using LiveChartsCore.SkiaSharpView
@using LiveChartsCore
@using LiveChartsCore.Defaults
@using LiveChartsCore.SkiaSharpView.Drawing.Geometries
@using LiveChartsCore.Drawing
@using ViewModelsSamples.Scatter.Custom
<CartesianChart
Series="Series">
</CartesianChart>
@code {
private ISeries[] Series { get; set; }
protected override void OnInitialized()
{
var values1 = Fetch();
var values2 = Fetch();
var values3 = Fetch();
var pinPath = SVGPoints.Pin;
Series = new ISeries[]
{
// the second type argument is the geometry to draw.
new ScatterSeries<ObservablePoint, HeartGeometry>
{
Values = values1,
Stroke = null,
GeometrySize = 40
},
// the variable SVG path geometry can change the path at runtime.
new ScatterSeries<ObservablePoint, VariableSVGPathGeometry>
{
Values = values2,
GeometrySvg = pinPath,
GeometrySize = 40
},
// finally you can use skiasharp to draw your own geometries.
new ScatterSeries<ObservablePoint, MyGeometry>
{
Values = values3,
GeometrySize = 40
}
};
}
private static ObservablePoint[] Fetch()
{
var r = new Random();
var length = 10;
var values = new ObservablePoint[length];
for (var i = 0; i < length; i++)
{
var x = r.Next(0, 20);
var y = r.Next(0, 20);
values[i] = new ObservablePoint(x, y);
}
return values;
}
}
Articles you might also find useful: