How to Use Oxyplot Chart in Xamarin iOS

How To Use Oxyplot Chart In Xamarin iOS
 

Introduction

 
Showing data in a chart/graph is much easier to understand. In Xamarin iOS no default option to show the chart. We must use a third-party plugin to show charts. A lot of third-party plugins are paid like Syncfusion, Telerik. I recommend using Oxyplot charts because it's full of open-source and easy to use this in your project. In this article, we are going to learn how to use and understand the Oxyplot chart in Xamarin.iOS Project.
 
Let's start.
 
Create a Xamarin iOS Single View Application in Visual Studio for Mac.
 
Before start designing, we must install an Oxyplot chart plugin from NuGet Manager in your project.
 
How To Use Oxyplot Chart In Xamarin iOS
 
Now, open Main.Storyboard in Visual Studio designer, and drag the View from Toolbox and place as per your requirement and apply class as PlotView in the properties window and set identity Name [Eg - plotview]
 
How To Use Oxyplot Chart In Xamarin iOS
 
Next, write your code in ViewController.cs file. Here, there are many different types of chart API available for use like Bar, Stack, Function, Pie Chart and etc, Create PlotModel and set required properties as per your requirement and create the X, Y-axis, position, labels, zoom, angle, Max, Min, and other properties. The Zoom property is for the zoom level of the chart. The Max, Min properties are for avoiding more scrolling in the non-data representation in the chart. You set min level is a zero, it will avoid minus (-) side-scrolling, because, we don't have data representation on minus side. Position properties are used to set the position of chart in the top, bottom, left or right side.
 
Afterward, create Axis and Data Column Series. In the Column series also have different properties like color, width and etc. Apply this X, Y-Axis and Column series to this PlotModel as we created first. Finally set this plotModel to PlotView as we designed in the Storyboard scene. Here is the full source code. 
  1. using System;  
  2. using System.Collections.ObjectModel;  
  3. using CoreGraphics;  
  4. using OxyPlot;  
  5. using OxyPlot.Axes;  
  6. using OxyPlot.Series;  
  7. using UIKit;  
  8.   
  9. namespace chartsDemo  
  10. {  
  11.     public partial class ViewController : UIViewController  
  12.     {  
  13.         public ViewController(IntPtr handle) : base(handle)  
  14.         {  
  15.         }  
  16.   
  17.         public override void ViewDidLoad()  
  18.         {  
  19.             base.ViewDidLoad();  
  20.             // Perform any additional setup after loading the view, typically from a nib.  
  21.   
  22.             var model = new PlotModel()  
  23.             {  
  24.                 Title = "Column",  
  25.                 PlotType = PlotType.XY,  
  26.                 LegendSymbolLength = 5,  
  27.                 LegendPlacement = LegendPlacement.Outside,  
  28.                 LegendOrientation = LegendOrientation.Vertical,  
  29.             };  
  30.   
  31.             CategoryAxis xaxis = new CategoryAxis();  
  32.             xaxis.Position = AxisPosition.Bottom;  
  33.             xaxis.Labels.Add("Mon, 4/24");  
  34.             xaxis.Labels.Add("Tue, 4/25");  
  35.             xaxis.Labels.Add("Wed, 4/26");  
  36.             xaxis.Labels.Add("Thu, 4/27");  
  37.             xaxis.Labels.Add("Mon, 4/24");  
  38.             xaxis.Labels.Add("Tue, 4/25");  
  39.             xaxis.AbsoluteMinimum = -.5;  
  40.             xaxis.AbsoluteMaximum = 6;  
  41.             xaxis.Zoom(0, 4.5);  
  42.             xaxis.Angle = 45;  
  43.   
  44.   
  45.             LinearAxis yaxis = new LinearAxis();  
  46.             yaxis.Position = AxisPosition.Left;  
  47.                   yaxis.AbsoluteMinimum = 0;  
  48.             yaxis.MinimumPadding = 0;  
  49.             yaxis.AbsoluteMaximum = 100;  
  50.   
  51.             ColumnSeries s1 = new ColumnSeries();  
  52.             s1.IsStacked = true;  
  53.             s1.Items.Add(new ColumnItem(20));  
  54.             s1.Items.Add(new ColumnItem(60));  
  55.             s1.Items.Add(new ColumnItem(40));  
  56.             s1.Items.Add(new ColumnItem(50));  
  57.             s1.Items.Add(new ColumnItem(20));  
  58.             s1.Items.Add(new ColumnItem(60));  
  59.             s1.ColumnWidth = 20;  
  60.             s1.FillColor = OxyColor.FromRgb(255, 0, 0);  
  61.   
  62.             ColumnSeries s2 = new ColumnSeries();  
  63.             s2.IsStacked = true;  
  64.             s2.Items.Add(new ColumnItem(50));  
  65.             s2.Items.Add(new ColumnItem(30));  
  66.             s2.Items.Add(new ColumnItem(10));  
  67.             s2.Items.Add(new ColumnItem(20));  
  68.             s2.ColumnWidth = 20;  
  69.   
  70.             model.Axes.Add(xaxis);  
  71.             model.Axes.Add(xaxis1);  
  72.             model.Series.Add(s1);  
  73.             model.Series.Add(s2);  
  74.   
  75.             this.plotview.Model = model;  
  76.             plotview.Frame = new CGRect(0, 0, this.View.Frame.Width + 20, this.View.Frame.Height);  
  77.   
  78.         }  
  79.   
  80.         public override void DidReceiveMemoryWarning()  
  81.         {  
  82.             base.DidReceiveMemoryWarning();  
  83.             // Release any cached data, images, etc that aren't in use.  
  84.         }  
  85.     }  
  86. }  
Now, you can execute the project and the output like below.
 
How To Use Oxyplot Chart In Xamarin iOS
 
Next, create another type of chart. Create a new Model named item.cs and write the below code.
  1. namespace chartsDemo  
  2. {  
  3.     internal class Item  
  4.     {  
  5.         public string Label { getset; }  
  6.         public int Value1 { getset; }  
  7.         public int Value2 { getset; }  
  8.         public int Value3 { getset; }  
  9.     }  
  10. }  
As usual use bar Charts and apply item model values to this plot chart. The code is given below. 
  1. using System;  
  2. using System.Collections.ObjectModel;  
  3. using CoreGraphics;  
  4. using OxyPlot;  
  5. using OxyPlot.Axes;  
  6. using OxyPlot.Series;  
  7. using UIKit;  
  8.   
  9. namespace chartsDemo  
  10. {  
  11.     public partial class ViewController : UIViewController  
  12.     {  
  13.         public ViewController(IntPtr handle) : base(handle)  
  14.         {  
  15.         }  
  16.   
  17.         public override void ViewDidLoad()  
  18.         {  
  19.             base.ViewDidLoad();  
  20.             // Perform any additional setup after loading the view, typically from a nib.  
  21.   
  22.             var model = new PlotModel()  
  23.             {  
  24.                 Title = "Column",  
  25.                 PlotType = PlotType.XY,  
  26.                 LegendSymbolLength = 5,  
  27.                 LegendPlacement = LegendPlacement.Outside,  
  28.                 LegendOrientation = LegendOrientation.Vertical,  
  29.             };  
  30.   
  31.             CategoryAxis xaxis = new CategoryAxis();  
  32.             xaxis.Position = AxisPosition.Bottom;  
  33.             xaxis.Labels.Add("Mon, 4/24");  
  34.             xaxis.Labels.Add("Tue, 4/25");  
  35.             xaxis.Labels.Add("Wed, 4/26");  
  36.             xaxis.Labels.Add("Thu, 4/27");  
  37.             xaxis.Labels.Add("Mon, 4/24");  
  38.             xaxis.Labels.Add("Tue, 4/25");  
  39.             xaxis.AbsoluteMinimum = -.5;  
  40.             xaxis.AbsoluteMaximum = 6;  
  41.             xaxis.Zoom(0, 4.5);  
  42.             xaxis.Angle = 45;  
  43.   
  44.             LinearAxis yaxis = new LinearAxis();  
  45.             yaxis.Position = AxisPosition.Left;  
  46.             yaxis.AbsoluteMinimum = 0;  
  47.             yaxis.MinimumPadding = 0;  
  48.             yaxis.AbsoluteMaximum = 100;  
  49.   
  50.             ColumnSeries s1 = new ColumnSeries();  
  51.             s1.IsStacked = true;  
  52.             s1.Items.Add(new ColumnItem(20));  
  53.             s1.Items.Add(new ColumnItem(60));  
  54.             s1.Items.Add(new ColumnItem(40));  
  55.             s1.Items.Add(new ColumnItem(50));  
  56.             s1.Items.Add(new ColumnItem(20));  
  57.             s1.Items.Add(new ColumnItem(60));  
  58.               
  59.             s1.ColumnWidth = 20;  
  60.             s1.FillColor = OxyColor.FromRgb(255, 0, 0);  
  61.   
  62.             ColumnSeries s2 = new ColumnSeries();  
  63.             s2.IsStacked = true;  
  64.             s2.Items.Add(new ColumnItem(50));  
  65.             s2.Items.Add(new ColumnItem(30));  
  66.             s2.Items.Add(new ColumnItem(10));  
  67.             s2.Items.Add(new ColumnItem(20));  
  68.             s2.ColumnWidth = 20;  
  69.   
  70.             var Items = new Collection<Item>  
  71.             {  
  72.                 new Item {Label = "Apples", Value1 = 37, Value2 = 12},  
  73.                 new Item {Label = "Pears", Value1 = 7, Value2 = 21},  
  74.                 new Item {Label = "Bananas", Value1 = 23, Value2 = 2}  
  75.             };  
  76.   
  77.             model.Axes.Add(new CategoryAxis { ItemsSource = Items, LabelField = "Label", AbsoluteMinimum = -0.5 });  
  78.             model.Axes.Add(new LinearAxis { Position = AxisPosition.Left, MinimumPadding = 0, AbsoluteMinimum = 0 });  
  79.             model.Series.Add(new ColumnSeries { Title = "2009", ItemsSource = Items, ValueField = "Value1", ColumnWidth = 20 });  
  80.             model.Series.Add(new ColumnSeries { Title = "2010", ItemsSource = Items, ValueField = "Value2", ColumnWidth = 20 });  
  81.             model.Series.Add(new ColumnSeries { Title = "2011", ItemsSource = Items, ValueField = "Value3" });  
  82.   
  83.             this.plotview.Model = model;  
  84.             plotview.Frame = new CGRect(0, 0, this.View.Frame.Width + 20, this.View.Frame.Height);  
  85.   
  86.         }  
  87.   
  88.         public override void DidReceiveMemoryWarning()  
  89.         {  
  90.             base.DidReceiveMemoryWarning();  
  91.             // Release any cached data, images, etc that aren't in use.  
  92.         }  
  93.     }  
  94. }  
Run your project by pressing F5, and you will get output like shown below.
 
How To Use Oxyplot Chart In Xamarin iOS
 
The full source code can be found in Github.
 

Summary

  1. Install Xamarin.iOS.Oxtplot Plugin
  2. Place View and apply PlotView class
  3. Create PlotModel, Column Series, X and Y-Axis
  4. Apply to PlotView
Thanks for reading.