PHP code example of tito10047 / php-sparkline

1. Go to this page and download the library: Download tito10047/php-sparkline library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tito10047 / php-sparkline example snippets


$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();

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

$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),
    ));

$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();

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

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