Articles in this section
Category / Section

How to bind the SQLite Database to the .NET MAUI Chart?

5 mins read

This article demonstrates the connection establishment with the SQLite database and binds the retrieving data from the database to the .NET MAUI Charts.

 

Follow these steps to learn how to work with the .NET MAUI Chart using the SQLite database.

 

Step 1: Add the SQLite reference in your project.

 

Step 2: Create the database access class as follows.

public class ChartDatabase
{
    readonly SQLiteConnection _database;
 
    public ChartDatabase(string dbPath)
    {
        _database = new SQLiteConnection(dbPath);
        _database.CreateTable<ChartDataModel>();
    }
 
    //Get the list of ChartDataModel items from the database
    public List<ChartDataModel> GetChartDataModel()
    {
        return _database.Table<ChartDataModel>().ToList();
    }
 
    //Insert an item in the database
    public int SaveChartDataModelAsync(ChartDataModel chartDataModel)
    {
        if (chartDataModel == null)
        {
            throw new Exception("Null");
        }
 
        return _database.Insert(chartDataModel);
    }
 
    //Delete an item in the database 
    public int DeleteChartDataModelAsync(ChartDataModel chartDataModel)
    {
        return _database.Delete(chartDataModel);
    }
}

 

public partial class App : Application
{
    static ChartDatabase database;
    …
 
    public static ChartDatabase Database
    {
        get
        {
            if (database == null)
            {
                database = new ChartDatabase(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "ChartDataBase.db3"));
            }
            return database;
        }
    }
}

 

Step 3: Now, create the following Model for the Chart data.

public class ChartDataModel
{
    [PrimaryKey]
    public string XValue { get; set; }
    public double YValue { get; set; }
}

 

Step 4: Bind the retrieved data from the Database to the Chart.

<ContentPage.BindingContext>
    <local:ViewModel/>
</ContentPage.BindingContext>
 
<chart:SfCartesianChart>
    <chart:SfCartesianChart.XAxes>
        <chart:CategoryAxis/>
    </chart:SfCartesianChart.XAxes>
    <chart:SfCartesianChart.YAxes>
        <chart:NumericalAxis/>
    </chart:SfCartesianChart.YAxes>
    <chart:ColumnSeries ItemsSource="{Binding Data}" XBindingPath="XValue" YBindingPath="YValue"/>
</chart:SfCartesianChart>

 

Step 5: Retrieving the database data of Chart as follows.

public partial class ChartSample : ContentPage
{
 public ChartSample()
 {
          InitializeComponent();
                        (BindingContext as ViewModel).Data = App.Database.GetChartDataModel();
              }
}

 

Output:

 Displaying the database data in a ListView in .NET MAUI Charts.

Initial page to display the SQLite database data


Inserting an data item in the database in .NET MAUI Charts.

Inserting an item into the database

 

Output after inserting a data into the database in .NET MAUI Charts.

After inserting data into the database

 

Chart output based on the database data in .NET MAUI Charts.

Display the chart with generated data


View sample in GitHub

Conclusion

I hope you enjoyed learning about how to bind the SQLite Database to the MAUI Chart (SfCartesianChart).

You can refer to our .NET MAUI Chart’s feature tour page to know about its other groundbreaking feature representations.

For current customers, you can check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our .NET MAUI Charts and other .NET MAUI components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied