Conditional Draw

This sample uses C# 12 features, it also uses features from the CommunityToolkit.Mvvm package, you can learn more about it here.

sample image

View model

using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using SkiaSharp;
using LiveChartsCore;
using LiveChartsCore.ConditionalDraw;
using LiveChartsCore.Defaults;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using LiveChartsCore.Kernel.Sketches;

namespace ViewModelsSamples.General.ConditionalDraw;

public class ViewModel
{
    private readonly ObservableCollection<ObservableValue> _values = [];

    public ViewModel()
    {
        var dangerPaint = new SolidColorPaint(SKColors.Red);

        _values = [
            new(2),
            new(8),
            new(4)
        ];

        var series = new ColumnSeries<ObservableValue>
        {
            Name = "Mary",
            Values = _values
        }
        .OnPointMeasured(point =>
        {
            if (point.Visual is null) return;

            var isDanger = point.Model?.Value > 5;

            point.Visual.Fill = isDanger
                ? dangerPaint
                : null; // when null, the series fill is used // mark
        });

        Series = [series];

        Randomize();
    }

    public ISeries[] Series { get; set; }

    public RectangularSection[] Sections { get; set; } = [
        new RectangularSection
        {
            Label = "Danger zone!",
            LabelSize = 15,
            LabelPaint = new SolidColorPaint(SKColors.Red)
            {
                SKTypeface = SKTypeface.FromFamilyName("Arial", SKFontStyle.Bold)
            },
            Yj = 5,
            Fill = new SolidColorPaint(SKColors.Red.WithAlpha(50))
        }
    ];

    public ICartesianAxis[] Y { get; set; } = [
        new Axis { MinLimit = 0 }
    ];

    private async void Randomize()
    {
        var r = new Random();

        while (true)
        {
            await Task.Delay(3000);

            foreach (var item in _values)
            {
                item.Value = r.Next(0, 10);
            }
        }
    }
}

XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="XamarinSample.General.ConditionalDraw.View"
             xmlns:lvc="clr-namespace:LiveChartsCore.SkiaSharpView.Xamarin.Forms;assembly=LiveChartsCore.SkiaSharpView.XamarinForms"
             xmlns:vms="clr-namespace:ViewModelsSamples.General.ConditionalDraw;assembly=ViewModelsSamples">
    <ContentPage.BindingContext>
        <vms:ViewModel/>
    </ContentPage.BindingContext>
    <lvc:CartesianChart
        Series="{Binding Series}"
        Sections="{Binding Sections}"
        YAxes="{Binding Y}">
    </lvc:CartesianChart>
</ContentPage>

Articles you might also find useful: