Custom Bars
The [ObservableObject]
, [ObservableProperty]
and [RelayCommand]
attributes come from the
CommunityToolkit.Mvvm package, you can read 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 CommunityToolkit.Mvvm.ComponentModel;
using LiveChartsCore;
using LiveChartsCore.Drawing;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Drawing.Geometries;
namespace ViewModelsSamples.Bars.Custom;
public partial class ViewModel : ObservableObject
{
public ISeries[] Series { get; set; } =
{
new ColumnSeries<double>
{
Values = new double[] { 2, 1, 4},
},
// You can also use SVG paths to draw the geometry
// LiveCharts already provides some predefined paths in the SVGPoints class.
new ColumnSeries<double, SVGPathGeometry>
{
Values = new double[] { -2, 2, 1 },
GeometrySvg = SVGPoints.Star
},
// you can declare your own gemetry and use the SkiaSharp api to draw it
new ColumnSeries<double, MyGeometry>
{
Values = new double[] { 4, 5, 2 },
},
};
}
MyGeometry.cs
using LiveChartsCore.SkiaSharpView.Drawing;
using LiveChartsCore.SkiaSharpView.Drawing.Geometries;
using SkiaSharp;
namespace ViewModelsSamples.Bars.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 += 5;
}
}
}
XAML
<UserControl
x:Class="UnoWinUISample.Bars.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.Bars.Custom"
mc:Ignorable="d">
<UserControl.DataContext>
<vms:ViewModel/>
</UserControl.DataContext>
<Grid>
<lvc:CartesianChart Series="{Binding Series}"></lvc:CartesianChart>
</Grid>
</UserControl>