Skip to content

[Question] Usage Help. Updating chart does not remove previous chart unless resizing the screen #363

@PickleBurg

Description

@PickleBurg

I have encountered an interesting issue while attempting to create a line chart that I expect to update every second with a new value.

this does work and renders on the screen. the issue I have is when I notify a property change the new chart shows but the old ones does not disappear unless I resize the window of the application.

using CommunityToolkit.Mvvm.ComponentModel;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Maui.Dispatching;
using Microcharts;

namespace ImageSource_Memory_Leak
{
    public partial class MainPageViewModel : ObservableObject
    {
        private readonly Random _random;

        public MainPageViewModel()
        {
            _random = new Random();
            MyChart = CreateInitialChart();
            PollValuesAsync();
        }

        private LineChart CreateInitialChart()
        {
            return new LineChart
            {
                Entries = GenerateRandomBpmData(),
                LineMode = LineMode.Straight,
                PointMode = PointMode.Circle,
                LabelTextSize = 20,
                LineSize = 4,
                BackgroundColor = SKColors.Transparent
            };
        }

        private List<ChartEntry> GenerateRandomBpmData()
        {
            float bpm = GenerateRandomBpm();
            string label = DateTime.Now.ToString(@"mm\:ss");

            return new List<ChartEntry>
            {
                new ChartEntry(bpm)
                {
                    Color = SKColor.Parse("#FF1493"),
                    Label = label,
                    ValueLabel = bpm.ToString()
                }
            };
        }

        private float GenerateRandomBpm() => _random.Next(60, 120);

        [ObservableProperty]
        private LineChart myChart;

        private async Task PollValuesAsync()
        {
            var entries = MyChart.Entries.ToList();

            while (true)
            {
                await Task.Delay(1000);

                float bpm = GenerateRandomBpm();
                string label = DateTime.Now.ToString(@"mm\:ss");

                entries.Add(new ChartEntry(bpm)
                {
                    Color = SKColor.Parse("#FF1493"),
                    Label = label,
                    ValueLabel = bpm.ToString()
                });

                MyChart = new LineChart
                {
                    Entries = entries,
                    LineMode = LineMode.Straight,
                    PointMode = PointMode.Circle,
                    LabelTextSize = 20,
                    LineSize = 4,
                    BackgroundColor = SKColors.Transparent,
                    IsAnimated = false,
                };

            }
        }
    }
}

https://github.com/PickleBurg/ImageSource-Memory-Leak/tree/chart-stuff

code repo for anyone interested.

image
It starts to increase in the amout of charts present

image
Them i resize andd it shows less.

<charts:ChartView x:Name="ChartView"
                  Chart="{Binding MyChart}"
                  VerticalOptions="FillAndExpand"
                  HorizontalOptions="FillAndExpand" />

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions