Dynamic Visibility
This sample uses C# 12 features, it also uses features from the CommunityToolkit.Mvvm package, you can learn more about it here.
View model
using CommunityToolkit.Mvvm.Input;
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
namespace ViewModelsSamples.General.Visibility;
public partial class ViewModel
{
public ISeries[] Series { get; set; } = [
new ColumnSeries<double>
{
Values = [2, 5, 4, 3],
IsVisible = true
},
new ColumnSeries<double>
{
Values = [6, 3, 2, 8],
IsVisible = true
},
new ColumnSeries<double>
{
Values = [4, 2, 8, 7],
IsVisible = true
}
];
[RelayCommand]
public void ToggleSeries0() =>
Series[0].IsVisible = !Series[0].IsVisible;
[RelayCommand]
public void ToggleSeries1() =>
Series[1].IsVisible = !Series[1].IsVisible;
[RelayCommand]
public void ToggleSeries2() =>
Series[2].IsVisible = !Series[2].IsVisible;
}
XAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="MauiSample.General.Visibility.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.General.Visibility;assembly=ViewModelsSamples"
>
<ContentPage.BindingContext>
<vms:ViewModel/>
</ContentPage.BindingContext>
<ContentPage.Content>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackLayout Grid.Row="0" Orientation="Horizontal">
<Button Margin="6" Command="{Binding ToggleSeries0Command}" Text="toggle 1" />
<Button Margin="6" Command="{Binding ToggleSeries1Command}" Text="toggle 2"/>
<Button Margin="6" Command="{Binding ToggleSeries2Command}" Text="toggle 3"/>
</StackLayout>
<lvc:CartesianChart Grid.Row="1" Series="{Binding Series}"></lvc:CartesianChart>
</Grid>
</ContentPage.Content>
</ContentPage>