Using OxyPlot With Xamarin.Forms

Those who are working with Xamarin.Forms, know that there is no out-of-the-box support for charts/graphs in Xamarin Forms. If you want to show any data report in the form of charts/graph like Pie, Bar etc., you will have to use third party graph controls like Syncfusion(Some Xamarin pre-built examples use these), Infragistics, or Telerik controls and you have to pay license fees for them.

There is a little known open source plotting library for Xamarin.Forms called OxyPlot. This library is not only free/open source but it’s easy to use too. However, you may have to play around with code samples to understand the working of these graph controls. In this article, we will learn how to use a few of graph options in OxyPlot.

As usual, create a new Xamarin.Forms XAML Project.

Add the NuGet Package named ‘OxyPlot.Xamarin.Forms’ to all your projects, as shown in the below image.

Just like Xamarin.Forms, Oxyplot also needs to be initialized in the platform specific projects so that it can work.

Add the following code in MainActivity.cs file of your Android project, just after global::Xamarin.Forms.Forms.Init(this, bundle);line in onCreate method.

OxyPlot.Xamarin.Forms.Platform.Android.PlotViewRenderer.Init();

Similarly, Add the following code in AppDelegate.cs file of your iOS project, just after global::Xamarin.Forms.Forms.Init(); line in FinishedLaunching method.

OxyPlot.Xamarin.Forms.Platform.iOS.PlotViewRenderer.Init();

And in the same way, add the following code in App.xaml.cs file of your UWP project, just after Xamarin.Forms.Forms.Init(e);line in FinishedLaunching method.

OxyPlot.Xamarin.Forms.Platform.UWP.PlotViewRenderer.Init();

Once the Oxyplot component is initialized in all the platform projects of the application, we can use the component in the same way as we use any user control in Xamarin.Forms. For example, the following code is of the file “PieEx.xaml” where we are declaring the Oxyplot component with local namespace and the creating the control of same.

As you can see from the code that the PlotView control requires Model to show the data, that’s why we have bound that property with PieModel which is object of type PlotModel available in OxyPlot Library.

The ContentPage shown in previous step is bound with the object of following class, which has three properties named PieModel, AreaModel, BarModel, StackedBarModel all of type PlotModel as this same object is used to bind rest of 3 pages used in the example to show different types of graphs like Area, Stacked Bar, and Normal Comparison Bar. These charts are created using hard coded values in the example, however you can use similar code to show any data you get from Web service or database.

publicclass OxyExData

{

public PlotModel PieModel { get; set; }

public PlotModel AreaModel { get; set; }

public PlotModel BarModel { get; set; }

public PlotModel StackedBarModel { get; set; }

public OxyExData()

{

PieModel = CreatePieChart();

AreaModel = CreateAreaChart();

StackedBarModel = CreateBarChart(true,"Stacked Bar");

BarModel = CreateBarChart(false, "Un-Stacked Bar");

}

private PlotModel CreatePieChart()

{

var model = new PlotModel { Title = "World population by continent" };

As you can see from the above example, we can use same PlotModel type of object to show different types of data by adding different types of series object to it.

The sample code given in this article is published on Github and it has 3 more screens showing Area Graph, Stacked Bar Graph, and Comparison Bar Graph.

This is how the sample application looks, when executed in the iOS simulator.

This article has just scratched the surface of using Oxyplot controls to create different types of charts and graphs. You can experiment more with it by trying out these examples. Let me know if you need any particular graph example.