Grouped And Stacked Bars
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 LiveChartsCore;
using LiveChartsCore.Kernel.Sketches;
using LiveChartsCore.SkiaSharpView;
namespace ViewModelsSamples.StackedBars.Groups;
public class ViewModel
{
public ISeries[] Series { get; set; } =
[
new StackedColumnSeries<int>
{
Values = [3, 5, 3],
Stroke = null,
StackGroup = 0 // mark
},
new StackedColumnSeries<int>
{
Values = [4, 2, 3],
Stroke = null,
StackGroup = 0 // mark
},
new StackedColumnSeries<int>
{
Values = [4, 6, 6],
Stroke = null,
StackGroup = 1 // mark
},
new StackedColumnSeries<int>
{
Values = [2, 5, 4],
Stroke = null,
StackGroup = 1 // mark
}
];
public ICartesianAxis[] XAxis { get; set; } = [
new Axis
{
LabelsRotation = -15,
Labels = ["Category 1", "Category 2", "Category 3"]
}
];
}
Form code behind
using System.Windows.Forms;
using LiveChartsCore.SkiaSharpView.WinForms;
using ViewModelsSamples.StackedBars.Groups;
namespace WinFormsSample.StackedBars.Groups;
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,
XAxes = viewModel.XAxis,
// 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);
}
}