brendt/php-sparkline

The SVG render a straight line only, no matter how data is

phuclh opened this issue · 2 comments

I used the example of the docs:

$sparkLine = SparkLine::new(collect([
    new SparkLineDay(
        count: 1,
        day: new DateTimeImmutable('2022-01-01')
    ),
    new SparkLineDay(
        count: 2,
        day: new DateTimeImmutable('2022-01-02')
    ),
    new SparkLineDay(
        count: 10,
        day: new DateTimeImmutable('2022-01-03')
    ),
    new SparkLineDay(
        count: 4,
        day: new DateTimeImmutable('2022-01-04')
    ),
]));

return $sparkLink->make();

But it only renders a straight line

CleanShot 2023-06-10 at 09 17 20@2x

This is how the SVG is rendered

CleanShot 2023-06-10 at 09 23 03@2x

I am using PHP 8.2 and Laravel 10.

I have also encountered the issue and I believe I have found the cause.

The SparkLine will only show SparkLineDay objects whose date does not differ from the current date by more than X days. Where X is the number of SparkLineDay objects passed into the SparkLine object (maxItemAmount).

That is because inside the resolveCoordinates method on line 169 a collection of dates is created that is relative to the current date. Then on line 173 those dates are used to find entries in the collection of SparkLineDay objects whose dates are not guaranteed to be relative to the current date.

This should be fixed in v2