2

Good Day everyone. I'm creating a Xamarin.Forms Portable Application and I want to display there a Chart using a OxyPlot.

But the codes I've found on the Internet doesn't seem to work fine. I just want to ask how am I going to create a Chart, specifically Pie Chart here in OxyPlot. Starting from my XAML to XAML.cs and to other files like model and viewmodel.

Can you please somehow provide me some instructions or code maybe on how am I going to do this?

I want the Chart to be coded here in my SalesPage.

SalesPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="XamarinFormsDemo.Views.SalesPage"
         xmlns:oxy="clr-namespace:OxyPlot.Xamarin.Forms;assembly=OxyPlot.Xamarin.Forms"
         BackgroundImage="bg3.jpg"
         Title="Sales Page">

</ContentPage>

SalesPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OxyPlot;
using Xamarin.Forms;
using OxyPlot.Xamarin.Forms;

namespace XamarinFormsDemo.Views
    {
        public partial class SalesPage : ContentPage
        {       
        public SalesPage()
        {
            InitializeComponent();
        }
    }
}

MainActivity.cs

using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using ImageCircle.Forms.Plugin.Droid;

namespace XamarinFormsDemo.Droid
{
    [Activity(Label = "XamarinFormsDemo", Icon = "@drawable/recordsicon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
        base.OnCreate(bundle);
        global::Xamarin.Forms.Forms.Init(this, bundle);
        OxyPlot.Xamarin.Forms.Platform.Android.PlotViewRenderer.Init();
        LoadApplication(new App());
        ImageCircleRenderer.Init();
        }
    }
}

This is actually my first time to create Charts so please help me. Thanks for your help.

2
  • Add the c# tag to your question to give the code some color. Commented Jul 19, 2016 at 7:44
  • @Kirenenko Okay Sir. Thanks. Commented Jul 19, 2016 at 8:02

1 Answer 1

3

There is a getting started guide for Oxy Plot and Xamarin.Forms here. To answer your question, you can do the following:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="XamarinFormsDemo.Views.SalesPage"
         xmlns:oxy="clr-namespace:OxyPlot.Xamarin.Forms;assembly=OxyPlot.Xamarin.Forms"
         BackgroundImage="bg3.jpg"
         Title="Sales Page">

    <oxy:PieSeries ... />

</ContentPage>

There is more documentation on the PieSeries and some samples of using it on the internet. Example:

    private static PlotModel CreateExample()
    {
        var model = new PlotModel { Title = "World population by continent" };

        var ps = new PieSeries
        {
            StrokeThickness = 2.0,
            InsideLabelPosition = 0.8,
            AngleSpan = 360,
            StartAngle = 0
        };

        // http://www.nationsonline.org/oneworld/world_population.htm
        // http://en.wikipedia.org/wiki/Continent
        ps.Slices.Add(new PieSlice("Africa", 1030) { IsExploded = true });
        ps.Slices.Add(new PieSlice("Americas", 929) { IsExploded = true });
        ps.Slices.Add(new PieSlice("Asia", 4157));
        ps.Slices.Add(new PieSlice("Europe", 739) { IsExploded = true });
        ps.Slices.Add(new PieSlice("Oceania", 35) { IsExploded = true });

        model.Series.Add(ps);
        return model;
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Sir. But the links you provided is in a single file only? The PieSeriesExamples exaple only? Where should I put that? Is that a model? What should I code in my .XAML file?
Sir where should i put this code above? In a model or in the code behind? And what should I put inside my .xaml file? Thanks a lot Sir.
Sir where should i put this code above? In a model or in the code behind? And what should I put inside my .xaml file? Thanks a lot Sir.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.