OxyPlot Example Line Plot Not Displaying

I followed the getting started guide as well as used the example code form the GIT repo to try to create a line graph. I put all the initializer calls in the corresponding projects. I cannot get a graph to display specifically on an android device. My XAML and code behind:

<?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:local="clr-namespace:OxyPlotExamples"
             xmlns:oxy="clr-namespace:OxyPlot.Xamarin.Forms;assembly=OxyPlot.Xamarin.Forms"
             x:Class="OxyPlotExamples.MainPage">

    <Grid>

        <Grid.RowDefinitions>

            <RowDefinition />

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition />

        </Grid.ColumnDefinitions>

        <oxy:PlotView Grid.Row="0" Grid.Column="0" 
                      Model="{Binding Model}" 
                      VerticalOptions="Center" 
                      HorizontalOptions="Center" 
                      WidthRequest="1000"
                      HeightRequest="500"/>

    </Grid>

</ContentPage>
using OxyPlot;
using OxyPlot.Series;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace OxyPlotExamples
{
    public partial class MainPage : ContentPage
    {
        public PlotModel Model { get; set; }

        public MainPage()
        {
            InitializeComponent();

            Model = new PlotModel
            {
                Title = "Test",
                LegendSymbolLength = 24
            };

            LineSeries s1 = new LineSeries
            {
                Title = "Series 1",
                Color = OxyColors.SkyBlue,
                MarkerType = MarkerType.Circle,
                MarkerSize = 6,
                MarkerStroke = OxyColors.White,
                MarkerFill = OxyColors.SkyBlue,
                MarkerStrokeThickness = 1.5
            };

            s1.Points.Add(new DataPoint(0, 10));
            s1.Points.Add(new DataPoint(10, 40));
            s1.Points.Add(new DataPoint(40, 20));
            s1.Points.Add(new DataPoint(60, 30));

            Model.Series.Add(s1);
        }
    }
}

Best Answers

  • JoshSchweigertJoshSchweigert US ✭✭
    Accepted Answer

    Well it's a good thing I'm not implementing it in an active app yet then yeah? This was to learn how to use the graphs not so much how to do everything else. This is not a complex app, I'm simply adding a plot. Some people have time to devote to learning, others do not and could be in my situation where you have to learn and develop at the same time. I dont consider the CONCEPT of data binding to be exclusive to MVVM. Speaking in general terms of whats happening that is. Something is "subscribed" to something else. When that something else changes that something reflects. Not really something exclusive.

    Anyway I only answered because you're not technically wrong but your approach just seems ill effective, in this case. I have shown you an example of a project, that I compiled and built, which also will not display. And I have also mentioned that the direct code method does display. So I am not satisfied it is directly related to may lack of deeper understanding of a few concepts.

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    ~~Just guessing...
    Maybe the Oxyplot Model doesn't raise a change event for adding to the series.
    Your XAML is binded to Model and that property is not changing... a property in the model is changing.
    You could try raising an INotifyPropetyChanged event when you are done adding the series - to trick the UI into thinking the model its binded to has changed.~~

    Oh... Wait... I don't see as you ever set the BindingContext of the page to be itself. So the UI doesn't know what its binding to.

  • JoshSchweigertJoshSchweigert USMember ✭✭

    @ClintStLaurent I thought the binding context was already inherited as itself, at least that's what I read somewhere. I set the binding context of the page under InitializeComponent() but still do not see a graph.

    So what you are saying by the property not changing is that I need to create another PlotView with all of those properties changed, then set the Model property to that new PlotView to trigger the property changed event on the Model property?

  • JoshSchweigertJoshSchweigert USMember ✭✭

    I do not feel like I'm completely new to the concept of binding but perhaps new to MVVM and the like. Even still, this should not be difficult, especially following the examples, to implement with little knowledge in those.

    What you described was essentially an event that is raised whenever the property changes. Whatever is binded to that property is notified of that change. So if I set the Model property to a new PlotView that is changing the property, not properties within, which should trigger that event.

    Either way, I ran the hello world example from the GIT repo which does not use any xaml, or binding, and it displays.

    I also tried following https://techierathore.com/2017/01/using-oxyplot-graphs-with-xamarin-forms/ But this also did not display. And it would appear the developer that created that has more experience in the areas you mentioned.

    If I'm coming off as acting like I know more than I do, that's not my intent. But it's just frustrating when you're telling me I need to learn, yet projects created by those that do have the same result as what I'm doing.

    Thank you for the link, I also have a subscription to University I'll do more research before continuing.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Let's be honest... "Hello World" has very little resemblance to how you do things in the real world, in real apps. It sounds like you should have at lease enough experience to agree with that.

    I do not feel like I'm completely new to the concept of binding but perhaps new to MVVM

    Not sure how you can be one without the other... but whatever.

    MVVM is THE central concept of modern software from WPF and later. Use that knowledge as you see fit.

    So if I set the Model property to a new PlotView

    Eh... That's really wrong. The model is of type PlotModel - PlotVIew is a UI element, not a data object. You really don't want UI views in your ViewModels.

    One last comment then I will bow out

    But it's just frustrating when you're telling me I need to learn,

    Well yah... You learn before you do. You wouldn't decide to learn carpentry by buying a truck load of wood and saying "I'm going to build a 2 story home, and learn all the little skills along the way." You start with a bird house, then a dog house {....} and work your way up to designing your home. Now it doesn't affect me one way or the other. If you want to try to jump in and learn the basics while you also try to make a new app from scratch then go for it. But 10+ years of mentoring tells me that never goes well for anyone just starting out.

  • JoshSchweigertJoshSchweigert USMember ✭✭
    Accepted Answer

    Well it's a good thing I'm not implementing it in an active app yet then yeah? This was to learn how to use the graphs not so much how to do everything else. This is not a complex app, I'm simply adding a plot. Some people have time to devote to learning, others do not and could be in my situation where you have to learn and develop at the same time. I dont consider the CONCEPT of data binding to be exclusive to MVVM. Speaking in general terms of whats happening that is. Something is "subscribed" to something else. When that something else changes that something reflects. Not really something exclusive.

    Anyway I only answered because you're not technically wrong but your approach just seems ill effective, in this case. I have shown you an example of a project, that I compiled and built, which also will not display. And I have also mentioned that the direct code method does display. So I am not satisfied it is directly related to may lack of deeper understanding of a few concepts.

Sign In or Register to comment.