How to add multiple axes in .NET MAUI Chart (SfCartesianChart)?
This article describes how to add multiple Y-axes in the .NET MAUI Cartesian Charts.
Consider the use case of plotting the graph for two different scenarios with varying unit rates on the same field, as shown in the following image. It has been achieved by using the XAxes and YAxes of the SfCartesianChart.
Step 1: Populate the chart with multiple axes. Add multiple axes to the SfCartesianChart.YAxes property. Then, assign the axis Name property for each axis as follows:
[XAML]
<chart:SfCartesianChart.XAxes>
<chart:DateTimeAxis Name="primaryX">
<chart:DateTimeAxis.Title>
<chart:ChartAxisTitle Text="Months"/>
</chart:DateTimeAxis.Title>
</chart:DateTimeAxis>
</chart:SfCartesianChart.XAxes>
<chart:SfCartesianChart.YAxes>
<chart:NumericalAxis Name="primaryY">
<chart:NumericalAxis.Title>
<chart:ChartAxisTitle Text="Amount"/>
</chart:NumericalAxis.Title>
</chart:NumericalAxis>
<chart:NumericalAxis Name="secondaryY">
<chart:NumericalAxis.Title>
<chart:ChartAxisTitle Text="Percentage"/>
</chart:NumericalAxis.Title>
</chart:NumericalAxis>
</chart:SfCartesianChart.YAxes>
[C#]
DateTimeAxis xAxis = new DateTimeAxis()
{
Name = "primaryX",
Title = new ChartAxisTitle
{
Text = "Months"
}
};
chart.XAxes.Add(xAxis);
NumericalAxis yAxis = new NumericalAxis()
{
Name = "primaryY",
Title = new ChartAxisTitle
{
Text = "Amount"
}
};
chart.YAxes.Add(yAxis);
NumericalAxis yAxis2 = new NumericalAxis()
{
Name = "secondaryY",
Title = new ChartAxisTitle
{
Text = "Percentage"
}
};
chart.YAxes.Add(yAxis2);
Step 2: Assign the respective axis to the series. Assign the respective axis name to the series using the Cartesian series YAxisName property as follows:
[XAML]
<chart:SfCartesianChart.Series> <chart:ColumnSeries YAxisName="primaryY"/> <chart:LineSeries YAxisName="secondaryY"/> </chart:SfCartesianChart.Series>
[C#]
ColumnSeries series1 = new ColumnSeries(); Series1.YAxisName = "primaryY"; chart.Series.Add(series1); LineSeries series2 = new LineSeries(); series2.YAxisName = "secondaryY"; chart.Series.Add(series2);
Step 3: Position the chart axis. An axis can be positioned anywhere in the chart area by using the CrossesAt property. Set the CrossesAt property as the MaxValue to place the Y axis at the opposite of its actual position.
[XAML]
<chart:SfCartesianChart.YAxes> <chart:NumericalAxis Name="primaryY" /> <chart:NumericalAxis CrossesAt="{Static x:Double.MaxValue}" Name="secondaryY"/> </chart:SfCartesianChart.YAxes>
[C#]
NumericalAxis yAxis = new NumericalAxis() { Name = "primaryY", }; chart.YAxes.Add(yAxis); NumericalAxis yAxis2 = new NumericalAxis() { Name = "secondaryY", CrossesAt = double.MaxValue }; chart.YAxes.Add(yAxis2);
By default, the series plots are based on the 0th index axis of the XAxes and YAxes collections.
Download the complete sample on GitHub.
Conclusion
I hope you enjoyed learning how to add multiple axes in .NET MAUI Chart (SfCartesianChart).
You can refer to our .NET MAUI Chart feature tour page to learn about its other groundbreaking feature representations. Explore our .NET MAUI Chart documentation to understand how to present and manipulate data.
For current customers, check out our .NET MAUI from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our .NET MAUI Chart and other .NET MAUI components.
Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!