3

I'm creating a PieChart using OxyPlot here in my Xamarin.Forms (Portable) Application. I created a ViewModel named PieViewModel where in I declare the content of the Pie Chart. In my SalesPage.XAML.cs, I call the ViewModel and access the salesmodel in it. In my XAML code, I bind the salesmodel in my code.

However, I wasn't able to display the PieChart. Here are the codes I used:

PieViewModel.cs

using OxyPlot;
using OxyPlot.Series;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

    namespace XamarinFormsDemo.ViewModels
    {
        public class PieViewModel : INotifyPropertyChanged
        {
            private PlotModel modelP1;
            public PieViewModel()
            {
                modelP1 = new PlotModel { Title = "Pie Sample1" };

                dynamic seriesP1 = new PieSeries { StrokeThickness = 2.0, InsideLabelPosition = 0.8, AngleSpan = 360, StartAngle = 0 };

                seriesP1.Slices.Add(new PieSlice("Africa", 1030) { IsExploded = false, Fill = OxyColors.PaleVioletRed });
                seriesP1.Slices.Add(new PieSlice("Americas", 929) { IsExploded = true });
                seriesP1.Slices.Add(new PieSlice("Asia", 4157) { IsExploded = true });
                seriesP1.Slices.Add(new PieSlice("Europe", 739) { IsExploded = true });
                seriesP1.Slices.Add(new PieSlice("Oceania", 35) { IsExploded = true });

                modelP1.Series.Add(seriesP1);

            }

            public PlotModel Model1
            {
                get { return modelP1; }
                set { modelP1 = value; }
            }


            public event PropertyChangedEventHandler PropertyChanged;

            protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }

        }
    }

.

SalesPage.XAML.cs

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



using Xamarin.Forms;

    namespace XamarinFormsDemo.Views
    {
        public partial class SalesPage : ContentPage
        {

            public 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"
         xmlns:oxy="clr-namespace:OxyPlot.Xamarin.Forms;assembly=OxyPlot.Xamarin.Forms"
         xmlns:ViewModels="clr-namespace:XamarinFormsDemo.ViewModels;assembly=XamarinFormsDemo"
         x:Class="XamarinFormsDemo.Views.SalesPage"
         BackgroundImage="bg3.jpg"
         Title="Sales Page">



    <oxy:PlotView Model="{Binding Model1}" VerticalOptions="Center" HorizontalOptions="Center"/>

</ContentPage>

. 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;
using Xamarin.Forms.Platform.Android;

namespace XamarinFormsDemo.Droid
{
    [Activity(Label = "XamarinFormsDemo", Icon = "@drawable/recordsicon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : AndroidActivity
    {
        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();
        }
    }
}

Please help me with this. I'm really getting confused on how the things are going. Thanks a lot.

20
  • 1
    For one thing, your pvm and your salesmodel are declared as local variables when they should be public properties for the binding to work. Commented Jul 23, 2016 at 12:59
  • @jstreet should I make something like this? > public string salesmodel { get; set; } ? What datatype should I use? Commented Jul 25, 2016 at 2:32
  • Based on your code, salesmodel should be a PlotModel. Commented Jul 25, 2016 at 2:36
  • @jstreet I put this in my PieViewModel: public PlotModel salesmodel {get;set;} And this in my SalesPage.xaml.cs: pvm.salesModel = pvm.Model1; Am I correct doing this? Commented Jul 25, 2016 at 3:06
  • Well.... you changed things a little bit: salesmodel was not a PieViewModel property.... and now, according to your change it is.... If that is the case, you don't need both salesmodel and Model1, only one of them would be enough. Please don't post code in the comments section. Instead, edit the code in your original question. Commented Jul 25, 2016 at 3:14

1 Answer 1

4

Here's some sample code:

App: (Portable)

    public class App : Application
    {
        public App()
        {
            MainPage = new Page3();
        }
    }

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"
             xmlns:oxy="clr-namespace:OxyPlot.Xamarin.Forms;assembly=OxyPlot.Xamarin.Forms"
             x:Class="App26.Page3">
  <ContentPage.Content>
    <oxy:PlotView Model="{Binding MyModel}"></oxy:PlotView>
  </ContentPage.Content>
</ContentPage>

CS:

public partial class Page3 : ContentPage
{
    public MyViewModel vm { get; set; }

    public Page3()
    {
        InitializeComponent();

        vm = new MyViewModel();

        BindingContext = vm;
    }
}

ViewModel:

public class MyViewModel
{
    public PlotModel MyModel { get; set; }

    public MyViewModel()
    {
        PieSeries pieSeries = new PieSeries();
        pieSeries.Slices.Add(new PieSlice("Africa", 1030) { IsExploded = false, Fill = OxyColors.PaleVioletRed });
        pieSeries.Slices.Add(new PieSlice("Americas", 929) { IsExploded = true });
        pieSeries.Slices.Add(new PieSlice("Asia", 4157) { IsExploded = true });
        pieSeries.Slices.Add(new PieSlice("Europe", 739) { IsExploded = true });
        pieSeries.Slices.Add(new PieSlice("Oceania", 350) { IsExploded = true });

        MyModel = new PlotModel();
        MyModel.Series.Add(pieSeries);
    }
}

MainActivity: (Droid)

 [Activity(Label = "App26", Icon = "@drawable/icon", 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());
    }
}

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Good Day Sir. I was able to figure it out yesterday. I just simply add this : <ContentPage.BindingContext> <ViewModels:PieViewModel/> </ContentPage.BindingContext> to my code and the Chart appear. Thank you so much Sir for enlightening me. More powers :)
Yes Sir. Thank you for your effort in helping me. 'Til next time :)

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.