3
votes

Following this simple example I'm just playing about embedding OxyPlot in a Xamarin Forms Sample app. I've created a brand new Xamarin Forms App, added the Nuget packages and in each project added the required Init code for OxyPlot. Then, in my MainPage.xaml I have:

<?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:local="clr-namespace:OxyPlotTestApp"
             x:Class="OxyPlotTestApp.MainPage">

    <AbsoluteLayout>
        <oxy:PlotView Model="{Binding PieModel}" AbsoluteLayout.LayoutBounds="20,0,.9,.9" 
                      AbsoluteLayout.LayoutFlags="WidthProportional,HeightProportional" />
    </AbsoluteLayout>

</ContentPage>

And in the codebehind:

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

namespace OxyPlotTestApp
{
    public class PieViewModel
    {
        public PlotModel PieModel { get; set; }

        public PieViewModel()
        {
            PieModel = CreatePieChart();

        }

        private PlotModel CreatePieChart()
        {
            var model = new PlotModel { Title = "World Population By Content" };
            var ps = new PieSeries
            {
                StrokeThickness = .25,
                InsideLabelPosition = 0.25,
                AngleSpan = 360,
                StartAngle = 0
            };
            ps.Slices.Add(new PieSlice("Africa", 1030) { IsExploded = false });
            ps.Slices.Add(new PieSlice("Americas", 929) { IsExploded = false });
            ps.Slices.Add(new PieSlice("Asia", 4157));
            ps.Slices.Add(new PieSlice("Europe", 739) { IsExploded = false });
            ps.Slices.Add(new PieSlice("Oceania", 35) { IsExploded = false });
            model.Series.Add(ps);
            return model;
        }

    }
    public partial class MainPage : ContentPage
    {
        public PieViewModel vm { get; set; }
        public MainPage()
        {
            InitializeComponent();
            vm = new PieViewModel();
            this.BindingContext = vm;
        }

    }
}

I'm not sure what, if anything, I've missed out, but I've tried both the UWP project and the Android project (the latter on a physical device) and its fine on Android but the UWP app just renders a blank page.

My UWP App.xaml.cs (relevant part):

if (rootFrame == null)
            {
                // Create a Frame to act as the navigation context and navigate to the first page
                rootFrame = new Frame();

                rootFrame.NavigationFailed += OnNavigationFailed;

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

                Xamarin.Forms.Forms.Init(e);

Thanks...

Edit:

I've just created a new test project and get the exact same results, so its not project or machine specific for me...

1

1 Answers

1
votes

I had the same problem as you mentioned, a PieChart working on Android but not on UWP with Xamarin.Forms (3.1.0.697729). I finally made it work on UWP by installing the latest version of OxyPlot.Xamarin.Forms from here instead of the 1.0.0. It works fine for a PieChart, I did not test other plots.

In addition, the Oxyplot documentation here says that "the VerticalOptions and HorizontalOptions properties must be set" but you also need to set the HeightRequest and WidthRequest. I read that in this post, but contrary to this post, it works fine in a StackLayout for me. The question is old but I hope it will help !