Xamarin Microcharts and when should I create the Chart instance?

Xamarin Microcharts and when should I create the Chart instance?



I have been trying to integrate Microcharts within my Xamarin project. As I wanted them to exist within a listview, I added them to a Model-View-Class and instantiated the BarChart class right there:


public class ChangeOverview


Chart _SuccessChart = null;
public Chart SuccessChart get;
List<Entry> _entries = new List<Entry>()

new Entry(200)

Label = "January",
ValueLabel = "200",
Color = SKColor.Parse("#FF0033")
,
new Entry(300)

Label = "February",
ValueLabel = "300",
Color = SKColor.Parse("#FF0000")
,
new Entry(400)

Label = "March",
ValueLabel = "400",
Color = SKColor.Parse("#AA0000")
,
;
public List<Entry> ChartEntries

get

return _entries;


public ChangeOverview(Change c)

_SuccessChart = new BarChart()

Entries = _entries
;




So in my ContentPage.xaml.cs, I did the following:


public ChangeList ()

InitializeComponent ();
Debug.WriteLine("### Changes: " + User.LoggedUser.AllMyChanges.Count);


foreach (Change c in User.LoggedUser.AllMyChanges)

list.Add(new ChangeOverview(c));


TestChart.Chart = list[0].SuccessChart;



This should have been pretty straight forward, however this did not show me a chart. As I searched around, I found out that I should put the code within my OnAppearing method:


protected override void OnAppearing()

base.OnAppearing();
TestChart.Chart = list[0].SuccessChart;
TestChart.Chart = new BarChart() Entries = list[0].ChartEntries ;




Just assigning the chart doesn't help me, I really have to instantiate the BarChart() within this method.



Is there another way? Can I just somehow refresh the chart? Or do I really have to instantiate the Chart here in the OnAppearing method?



Thanks in advance!






Does anybody have an idea on why it behaves like this?

– Wolffi82
Sep 19 '18 at 18:08




0



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy