The ChartView component can visualize data in a Cartesian coordinate system as areas, bars, points, bubbles, lines, and other forms. This example allows you to get started with the ChartView component and explore its basic functionality. It demonstrates how to add data series to a chart, configure axes, specify the chart's elements (legend, series labels, and hints), and customize series appearance.
You can also refer to the following YouTube video for instructions on how to get started with the Chart Controls: Adding Charts to Your .NET MAUI App.
The following step-by-step tutorial details how to reproduce this application.
In Visual Studio 2022, create a new .NET MAUI project. Name it ChartViewExample.
> If the wizard does not suggest a template for .NET MAUI projects, you can call the following command in a CLI to create a new .NET MAUI project:
> ```
> dotnet new maui -n ChartViewExample
> ```
In the MauiProgram.cs file, call the UseDevExpress method to register handlers for the ChartView and other DevExpress controls:
using Microsoft.Maui; using Microsoft.Maui.Hosting; using Microsoft.Maui.Controls.Hosting; using DevExpress.Maui.Charts; namespace ScatterChartGetStarted { public static class MauiProgram { public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder(); builder .UseMauiApp<App>() .UseDevExpress() .ConfigureFonts(fonts => { fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); }); return builder.Build(); } } }
In the MainPage.xaml file, use the dxc prefix to declare the DevExpress.Maui.Charts namespace and add a ChartView instance to the ContentPage:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ChartViewExample.MainPage" xmlns:dxc="clr-namespace:DevExpress.Maui.Charts;assembly=DevExpress.Maui.Charts"> <dxc:ChartView/> </ContentPage>
In this example, the chart contains three line series that show annual GDP for three countries. Create a ViewModel.cs file with the following classes:
public class ViewModel { public CountryGdp GdpValueForUSA { get; } public CountryGdp GdpValueForChina { get; } public CountryGdp GdpValueForJapan { get; } public ViewModel() { GdpValueForUSA = new CountryGdp( "USA", new GdpValue(new DateTime(2020, 1, 1), 20.93), new GdpValue(new DateTime(2019, 1, 1), 21.43), new GdpValue(new DateTime(2018, 1, 1), 20.58), new GdpValue(new DateTime(2017, 1, 1), 19.391), new GdpValue(new DateTime(2016, 1, 1), 18.624), new GdpValue(new DateTime(2015, 1, 1), 18.121), new GdpValue(new DateTime(2014, 1, 1), 17.428), new GdpValue(new DateTime(2013, 1, 1), 16.692), new GdpValue(new DateTime(2012, 1, 1), 16.155), new GdpValue(new DateTime(2011, 1, 1), 15.518), new GdpValue(new DateTime(2010, 1, 1), 14.964) ); GdpValueForChina = new CountryGdp( "China", new GdpValue(new DateTime(2020, 1, 1), 14.72), new GdpValue(new DateTime(2019, 1, 1), 14.34), new GdpValue(new DateTime(2018, 1, 1), 13.89), new GdpValue(new DateTime(2017, 1, 1), 12.238), new GdpValue(new DateTime(2016, 1, 1), 11.191), new GdpValue(new DateTime(2015, 1, 1), 11.065), new GdpValue(new DateTime(2014, 1, 1), 10.482), new GdpValue(new DateTime(2013, 1, 1), 9.607), new GdpValue(new DateTime(2012, 1, 1), 8.561), new GdpValue(new DateTime(2011, 1, 1), 7.573), new GdpValue(new DateTime(2010, 1, 1), 6.101) ); GdpValueForJapan = new CountryGdp( "Japan", new GdpValue(new DateTime(2020, 1, 1), 4.888), new GdpValue(new DateTime(2019, 1, 1), 5.082), new GdpValue(new DateTime(2018, 1, 1), 4.955), new GdpValue(new DateTime(2017, 1, 1), 4.872), new GdpValue(new DateTime(2016, 1, 1), 4.949), new GdpValue(new DateTime(2015, 1, 1), 4.395), new GdpValue(new DateTime(2014, 1, 1), 4.850), new GdpValue(new DateTime(2013, 1, 1), 5.156), new GdpValue(new DateTime(2012, 1, 1), 6.203), new GdpValue(new DateTime(2011, 1, 1), 6.156), new GdpValue(new DateTime(2010, 1, 1), 5.700) ); } } public class CountryGdp { public string CountryName { get; } public IList<GdpValue> Values { get; } public CountryGdp(string country, params GdpValue[] values) { this.CountryName = country; this.Values = new List<GdpValue>(values); } } public class GdpValue { public DateTime Year { get; } public double Value { get; } public GdpValue(DateTime year, double value) { this.Year = year; this.Value = value; } }
In the MainPage.xaml file, add three LineSeries objects to the ChartView.Series collection. To bind series to data, set the Data property of each LineSeries object to a SeriesDataAdapter object. Use the adapter’s properties to specify the data source and its fields that contain arguments and values for each series:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ChartViewExample.MainPage" xmlns:local="clr-namespace:ChartViewExample" xmlns:dxc="clr-namespace:DevExpress.Maui.Charts;assembly=DevExpress.Maui.Charts"> <ContentPage.BindingContext> <local:ViewModel/> </ContentPage.BindingContext> <dxc:ChartView> <dxc:ChartView.Series> <dxc:LineSeries DisplayName="{Binding GdpValueForUSA.CountryName}"> <dxc:LineSeries.Data> <dxc:SeriesDataAdapter DataSource="{Binding GdpValueForUSA.Values}" ArgumentDataMember="Year"> <dxc:ValueDataMember Type="Value" Member="Value"/> </dxc:SeriesDataAdapter> </dxc:LineSeries.Data> </dxc:LineSeries> <dxc:LineSeries DisplayName="{Binding GdpValueForChina.CountryName}"> <dxc:LineSeries.Data> <dxc:SeriesDataAdapter DataSource="{Binding GdpValueForChina.Values}" ArgumentDataMember="Year"> <dxc:ValueDataMember Type="Value" Member="Value"/> </dxc:SeriesDataAdapter> </dxc:LineSeries.Data> </dxc:LineSeries> <dxc:LineSeries DisplayName="{Binding GdpValueForJapan.CountryName}"> <dxc:LineSeries.Data> <dxc:SeriesDataAdapter DataSource="{Binding GdpValueForJapan.Values}" ArgumentDataMember="Year"> <dxc:ValueDataMember Type="Value" Member="Value"/> </dxc:SeriesDataAdapter> </dxc:LineSeries.Data> </dxc:LineSeries> </dxc:ChartView.Series> </dxc:ChartView> </ContentPage>
Assign a DateTimeAxisX object with the specified settings to the ChartView.AxisX property to configure the X-axis so that it shows labels for years:
<dxc:ChartView> <dxc:ChartView.AxisX> <dxc:DateTimeAxisX MeasureUnit="Year" GridAlignment="Year" GridSpacing="1"/> </dxc:ChartView.AxisX> </dxc:ChartView>
Set the ChartView.AxisY property to a NumericAxisY object, and specify this object's Title and Label properties to configure the title and labels of the Y-axis:
<dxc:ChartView> <!-- The X-axis config is here. --> <dxc:ChartView.AxisY> <dxc:NumericAxisY> <dxc:NumericAxisY.Title> <dxc:AxisTitle Text="Trillions of US$"> <dxc:AxisTitle.Style> <dxc:TitleStyle> <dxc:TitleStyle.TextStyle> <dxc:TextStyle Size="16"/> </dxc:TitleStyle.TextStyle> </dxc:TitleStyle> </dxc:AxisTitle.Style> </dxc:AxisTitle> </dxc:NumericAxisY.Title> <dxc:NumericAxisY.Label> <dxc:AxisLabel TextFormat="#.#" Position="Inside"/> </dxc:NumericAxisY.Label> </dxc:NumericAxisY> </dxc:ChartView.AxisY> </dxc:ChartView>
Set the ChartView.Legend property to a Legend object, and use this object's properties to configure the legend position and orientation:
<dxc:ChartView> <dxc:ChartView.Legend> <dxc:Legend VerticalPosition="BottomOutside" HorizontalPosition="Center" Orientation="LeftToRight"/> </dxc:ChartView.Legend> </dxc:ChartView>
Set up the chart so that it shows a series point hint as a crosshair cursor, and specify the hint’s content, data format, and visibility options.
- Set the ChartView.Hint property to a Hint object, and assign a CrosshairHintBehavior object to Hint.Behavior.
- Set the LineSeries.HintOptions property to a SeriesCrosshairOptions object with the specified settings.
<ContentPage.Resources> <dxc:SeriesCrosshairOptions x:Key="lineSeriesHintOptions" PointTextPattern="{}{S}: {V}M" ShowInLabel="True" AxisLabelVisible="True" AxisLineVisible="True"/> </ContentPage.Resources> <dxc:ChartView> <dxc:ChartView.Hint> <dxc:Hint> <dxc:Hint.Behavior> <dxc:CrosshairHintBehavior GroupHeaderTextPattern="{}{A$YYYY}" MaxSeriesCount="3"/> </dxc:Hint.Behavior> </dxc:Hint> </dxc:ChartView.Hint> <dxc:ChartView.Series> <dxc:LineSeries HintOptions="{StaticResource lineSeriesHintOptions}"> <!--Series Data--> </dxc:LineSeries> <dxc:LineSeries HintOptions="{StaticResource lineSeriesHintOptions}"> <!--Series Data--> </dxc:LineSeries> <dxc:LineSeries HintOptions="{StaticResource lineSeriesHintOptions}"> <!--Series Data--> </dxc:LineSeries> </dxc:ChartView.Series> </dxc:ChartView>
Set the LineSeries.MarkersVisible property to True to display point markers. To change the line series appearance, set the LineSeries.Style property to a LineSeriesStyle object. This object’s properties allow you to configure the appearance of the series line (Stroke, StrokeThickness) and point markers (MarkerSize, MarkerStyle).
<dxc:LineSeries MarkersVisible="True"> <!--Series Data--> <dxc:LineSeries.Style> <dxc:LineSeriesStyle Stroke="#7145a7" StrokeThickness="2" MarkerSize="8"> <dxc:LineSeriesStyle.MarkerStyle> <dxc:MarkerStyle Fill="#7145a7"/> </dxc:LineSeriesStyle.MarkerStyle> </dxc:LineSeriesStyle> </dxc:LineSeries.Style> </dxc:LineSeries>
