PHPackages                             tito10047/php-sparkline - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Image &amp; Media](/categories/media)
4. /
5. tito10047/php-sparkline

ActiveLibrary[Image &amp; Media](/categories/media)

tito10047/php-sparkline
=======================

Generate sparkline SVGs in PHP

1.0.0(1y ago)0204↓100%1MITPHPPHP ^8.1

Since May 17Pushed 1y agoCompare

[ Source](https://github.com/tito10047/php-sparkline)[ Packagist](https://packagist.org/packages/tito10047/php-sparkline)[ Docs](https://github.com/brendt/php-sparkline)[ RSS](/packages/tito10047-php-sparkline/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (6)Versions (3)Used By (1)

Generate sparkline SVGs in PHP
==============================

[](#generate-sparkline-svgs-in-php)

[![Latest Version on Packagist](https://camo.githubusercontent.com/a8e65122d62fa4faf6e4911ab97c2dba4272da13ec5c7f4c54ae8c44c8eb3f06/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6272656e64742f7068702d737061726b6c696e652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/brendt/php-sparkline)[![Tests](https://github.com/brendt/php-sparkline/actions/workflows/run-tests.yml/badge.svg?branch=main)](https://github.com/brendt/php-sparkline/actions/workflows/run-tests.yml)[![Total Downloads](https://camo.githubusercontent.com/884ca508105741d3f6fdd6c2f62128a89470ae506fa4dc34a9514888a7fe3419/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6272656e64742f7068702d737061726b6c696e652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/brendt/php-sparkline)

PHP-Sparkline generates GitHub style sparkline graphs. Read this guide to know how to use it.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require tito10047/php-sparkline
```

Usage
-----

[](#usage)

```
$sparkLine = SparkLine::new(collect([
    new SparkLineDay(
        count: 1,
        day: new DateTimeImmutable('2022-01-01')
    ),
    new SparkLineDay(
        count: 2,
        day: new DateTimeImmutable('2022-01-02')
    ),
    // …
]));

$total = $sparkLine->getTotal();
$period = $sparkLine->getPeriod(); // Spatie\Period
$svg = $sparkLine->make();
```

[![](./.github/img/0.png)](./.github/img/0.png)

To construct a sparkline, you'll have to pass in a collection of `Brendt\SparkLineDay` objects. This object takes two parameters: a `count`, and a `DateTimeInterface`. You could for example convert database entries like so:

```
$days = PostVistisPerDay::query()
    ->orderByDesc('day')
    ->limit(20)
    ->get()
    ->map(fn (SparkLineDay $row) => new SparkLineDay(
        count: $row->visits,
        day: Carbon::make($row->day),
    ));
```

In many cases though, you'll want to aggregate data with an SQL query, and convert those aggregations on the fly to `SparkLineDay` objects:

```
$days = DB::query()
    ->from((new Post())->getTable())
    ->selectRaw('`published_at_day`, COUNT(*) as `visits`')
    ->groupBy('published_at_day')
    ->orderByDesc('published_at_day')
    ->whereNotNull('published_at_day')
    ->limit(20)
    ->get()
    ->map(fn (object $row) => new SparkLineDay(
        count: $row->visits,
        day: Carbon::make($row->published_at_day),
    ));
```

### Precision

[](#precision)

Make precision for 5 minute intervals

```
$sparkLine = SparkLine::new(collect([
    new SparkLineDay(
        count: 1,
        day: new DateTimeImmutable('2022-01-01')
    ),
    new SparkLineDay(
        count: 2,
        day: new DateTimeImmutable('2022-01-02')
    ),
    // …
]),Period::MINUTE,5);
$svg = $sparkLine->make();
```

### Customization

[](#customization)

This package offers some methods to customize the sparkline. First off, you can pick any amount of colors and the sparkline will automatically generate a gradient from them:

```
$sparkLine = SparkLine::new($days)->withColors('#4285F4', '#31ACF2', '#2BC9F4');
```

[![](./.github/img/1.png)](./.github/img/1.png)

Next, you can configure a bunch of numbers:

```
$sparkLine = SparkLine::new($days)
    ->withStrokeWidth(4)
    ->withDimensions(500, 100)
    ->withMaxItemAmount(100)
    ->withMaxValue(20);
```

[![](./.github/img/2.png)](./.github/img/2.png)

- **`withStrokeWidth`** will determine the stroke's width
- **`withDimensions`** will determine the width and height of the rendered SVG
- **`withMaxItemAmount`** will determine how many days will be shown. If you originally passed on more days than this max, then the oldest ones will be omitted. If the max amount is set to a number that's *higher* than the current amount of days, then the sparkline will contain empty days. By default, the amount of given days will be used.
- **`withMaxValue`** will set the maximum value of the sparkline. This is useful if you have multiple sparklines that should all have the same scale. By default, the maximum value is determined based on the given days.

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Brent Roose](https://github.com/brendt)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance32

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community20

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

699d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3d6d558226302620ec462f64672e9dc81609227626923a808005641e76e8c38f?d=identicon)[tito10047](/maintainers/tito10047)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (98 commits)")[![mvdnbrk](https://avatars.githubusercontent.com/u/802681?v=4)](https://github.com/mvdnbrk "mvdnbrk (29 commits)")[![brendt](https://avatars.githubusercontent.com/u/6905297?v=4)](https://github.com/brendt "brendt (19 commits)")[![Nielsvanpach](https://avatars.githubusercontent.com/u/10651054?v=4)](https://github.com/Nielsvanpach "Nielsvanpach (13 commits)")[![pforret](https://avatars.githubusercontent.com/u/474312?v=4)](https://github.com/pforret "pforret (9 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (8 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (7 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (7 commits)")[![riasvdv](https://avatars.githubusercontent.com/u/3626559?v=4)](https://github.com/riasvdv "riasvdv (4 commits)")[![gregkos](https://avatars.githubusercontent.com/u/6676236?v=4)](https://github.com/gregkos "gregkos (4 commits)")[![thecaliskan](https://avatars.githubusercontent.com/u/13554944?v=4)](https://github.com/thecaliskan "thecaliskan (3 commits)")[![koossaayy](https://avatars.githubusercontent.com/u/6431084?v=4)](https://github.com/koossaayy "koossaayy (2 commits)")[![erikn69](https://avatars.githubusercontent.com/u/4933954?v=4)](https://github.com/erikn69 "erikn69 (2 commits)")[![irfanm96](https://avatars.githubusercontent.com/u/42065936?v=4)](https://github.com/irfanm96 "irfanm96 (2 commits)")[![jamessessford](https://avatars.githubusercontent.com/u/17096446?v=4)](https://github.com/jamessessford "jamessessford (2 commits)")[![mnaderian](https://avatars.githubusercontent.com/u/30769988?v=4)](https://github.com/mnaderian "mnaderian (1 commits)")[![LarsWiegers](https://avatars.githubusercontent.com/u/20204608?v=4)](https://github.com/LarsWiegers "LarsWiegers (1 commits)")[![patinthehat](https://avatars.githubusercontent.com/u/5508707?v=4)](https://github.com/patinthehat "patinthehat (1 commits)")[![dietercoopman](https://avatars.githubusercontent.com/u/4672752?v=4)](https://github.com/dietercoopman "dietercoopman (1 commits)")[![cmgmyr](https://avatars.githubusercontent.com/u/4693481?v=4)](https://github.com/cmgmyr "cmgmyr (1 commits)")

---

Tags

brendtphp-sparkline

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/tito10047-php-sparkline/health.svg)

```
[![Health](https://phpackages.com/badges/tito10047-php-sparkline/health.svg)](https://phpackages.com/packages/tito10047-php-sparkline)
```

###  Alternatives

[grumpydictator/firefly-iii

Firefly III: a personal finances manager.

22.8k69.3k](/packages/grumpydictator-firefly-iii)[brendt/php-sparkline

Generate sparkline SVGs in PHP

35172.8k](/packages/brendt-php-sparkline)[tightenco/jigsaw

Simple static sites with Laravel's Blade.

2.2k438.5k29](/packages/tightenco-jigsaw)[illuminate/queue

The Illuminate Queue package.

20331.4M1.2k](/packages/illuminate-queue)[laravel-zero/framework

The Laravel Zero Framework.

3371.4M368](/packages/laravel-zero-framework)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

255.2k](/packages/aedart-athenaeum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
