mauilivecharts

How to create Axis in XAML in LiveCharts2, MAUI


I would like to create Axis under XAML in MAUI. But this code:

<lvc:CartesianChart>
    <lvc:CartesianChart.YAxes>
        <lvcs:Axis Name="Y axis">
        </lvcs:Axis>
    </lvc:CartesianChart.YAxes>
</lvc:CartesianChart>

gives me an error:

No property, BindableProperty, or event found for "YAxes", or mismatching type between value and property.

Namespaces added:

xmlns:lvc="clr-namespace:LiveChartsCore.SkiaSharpView.Maui;assembly=LiveChartsCore.SkiaSharpView.Maui"
xmlns:lvcs="clr-namespace:LiveChartsCore.SkiaSharpView;assembly=LiveChartsCore.SkiaSharpView"

What am I missing? Is it somehow possible? I havent found any examples of creating axis via XAML.


Solution

  • I can reproduce you problem.

    No property, BindableProperty, or event found for "YAxes", or mismatching type between value and property.

    This error is caused by mismatching type between value and property.

    The official document about Axes the said:

    The cartesian chart control has the XAxes and YAxes properties, both of type IEnumerable by default when you do not set these properties, they will be an array containing only one element of the Axis class (new Axis[] { new Axis() }).

    But the value you gave was type of Axis class. So you can create axis in the xaml by the following code:

    <ContentPage.Resources>
        <ResourceDictionary>
            <x:Array x:Key="values" Type="{x:Type lcs:Axis}">
                <lcs:Axis Name="Y Axis"/>
            </x:Array>
        </ResourceDictionary>
    </ContentPage.Resources>
    .....
    <lct:CartesianChart YAxes="{StaticResource Key=values}"/>