Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTimePoint problem when MinLimit and MaxLimit is null #579

Closed
j-stuemp opened this issue Aug 12, 2022 · 2 comments
Closed

DateTimePoint problem when MinLimit and MaxLimit is null #579

j-stuemp opened this issue Aug 12, 2022 · 2 comments
Labels
bug Something isn't working requires more research

Comments

@j-stuemp
Copy link

j-stuemp commented Aug 12, 2022

Describe the bug
Infinite loop when there is only one DateTimePoint in the Series and MinLimit / MaxLimit is null

To Reproduce
Steps to reproduce the behavior:

Replace samples\ViewModelsSamples\Lines\AutoUpdate\ViewModel.cs with this code and then press "Add" in Lines/Autoupdate sample :


using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using LiveChartsCore;
using LiveChartsCore.Defaults;
using LiveChartsCore.SkiaSharpView;

namespace ViewModelsSamples.Lines.AutoUpdate;

[ObservableObject]
public partial class ViewModel
{
    private readonly Random _random = new();
    private readonly ObservableCollection<DateTimePoint> _observableValues;

    public ViewModel()
    {
        // Use ObservableCollections to let the chart listen for changes (or any INotifyCollectionChanged). // mark
        _observableValues = new ObservableCollection<DateTimePoint>
        {

        };

        Series = new ObservableCollection<ISeries>
        {
            new LineSeries<DateTimePoint>
            {
                Values = _observableValues,
                Fill = null
            }
        };

        // in the following sample notice that the type int does not implement INotifyPropertyChanged
        // and our Series.Values property is of type List<T>
        // List<T> does not implement INotifyCollectionChanged
        // this means the following series is not listening for changes.
        // Series.Add(new ColumnSeries<int> { Values = new List<int> { 2, 4, 6, 1, 7, -2 } }); // mark
    }

    public ObservableCollection<ISeries> Series { get; set; }

    [ICommand]
    public void AddItem()
    {
        var randomValue = _random.Next(1, 10);
        _observableValues.Add(new DateTimePoint(DateTime.Now,randomValue));
    }

    [ICommand]
    public void RemoveItem()
    {
        if (_observableValues.Count == 0) return;
        _observableValues.RemoveAt(0);
    }

    [ICommand]
    public void UpdateItem()
    {
        var randomValue = _random.Next(1, 10);

        // we grab the last instance in our collection
        var lastInstance = _observableValues[_observableValues.Count - 1];

        // finally modify the value property and the chart is updated!
        lastInstance.Value = randomValue;
    }

    [ICommand]
    public void ReplaceItem()
    {
        var randomValue = _random.Next(1, 10);
        var randomIndex = _random.Next(0, _observableValues.Count - 1);
        _observableValues[randomIndex] = new DateTimePoint(DateTime.Now, randomValue);
    }

    [ICommand]
    public void AddSeries()
    {
        //  for this sample only 5 series are supported.
        if (Series.Count == 5) return;

        Series.Add(
            new LineSeries<int>
            {
                Values = new List<int>
                {
                    _random.Next(0, 10),
                    _random.Next(0, 10),
                    _random.Next(0, 10)
                }
            });
    }

    [ICommand]
    public void RemoveSeries()
    {
        if (Series.Count == 1) return;

        Series.RemoveAt(Series.Count - 1);
    }
}

Expected behavior
No infinite loop

Additional context
It seems that there is a problem with double precision in src\LiveChartsCore\Axis.cs function GetPossibleSize

for (var i = start; i <= max; i += s)

when i increments (s = 1) then it has the same value as before

Immediate window:

(long)i
637958911297489152

s
1

(long)((double)i + (double)s)
637958911297489152

(long)i + (long)s
637958911297489153

@beto-rodriguez beto-rodriguez added the bug Something isn't working label Aug 20, 2022
@beto-rodriguez
Copy link
Owner

thanks for the report!

The referenced commit fixes the throw, but there are a couple of issues I would like to investigate before closing this issue.

This fix will be included since version beta.360, but I will keep this open to keep investigating.

@beto-rodriguez
Copy link
Owner

At this point, it does not throw, but we can get strange labels depending on the Axis.UnitWidth and the Series.GeometrySize properties.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working requires more research
Projects
None yet
Development

No branches or pull requests

2 participants