Skip to content

generate small chart and sparklines in your laravel app

License

Notifications You must be signed in to change notification settings

abbasudo/laravel-sparkline

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

laravel-sparkline

Generate small chart and sparklines in your laravel app like a breeze.
this package helps you to plot prices of stock ,currencies ,crypto, etc, into an image in your laravel app.
it uses image intervention and PHP gd to generate the image.



Installation

composer require llabbasmkhll/laravel-sparkline


Usage

to use sparkline all you have to is providing the numbers that you want to plot to data function and render the chart like below:

$metrics = [76, 80, 50, 62, 32, 55, 42, 5, 10, 2, 22, 5, 6, 26, 25, 55, 40, 32, 55, 42, 5, 10, 2, 22, 56];

return Sparkline::data($metrics)->render()->response('png');

this will generate an sparkline like this:
sparkline

Note

render() will return an Intervention\Image object. more information in image intervention.
if your too lazy to read intervention docs simply use ->response('png') to return the sparkline to the browser.
or ->save('public/fou.jpg') to save the sparkline.



Customization

Color

line

set the color of the sparkline by color function. by defult its yellow.

$red   = 250;
$green = 100;
$blue  = 100;
$alpha = 1;

Sparkline::data($metrics)->color($red, $green, $blue, $alpha)->render()->response('png');

d8l94xyhh

background

set the background color by backgorund function. by defult its transparent.

$red   = 250;
$green = 70;
$blue  = 70;
$alpha = 0.2;

Sparkline::data($metrics)->backgound($red, $green, $blue, $alpha)->render()->response('png');

KTGrO6VsI

fill

to fill the sparkline use fill function. by defult its transparent.

$red   = 250;
$green = 70;
$blue  = 70;
$alpha = 0.2;

Sparkline::data($metrics)->fill($red, $green, $blue, $alpha)->render()->response('png');

a4OK4h092

Thickness

set line thikness by calling thickness like so :

Sparkline::data($metrics)->thikness(3)->render()->response('png');

z_qh8IEhz

Fade

by defult sparklines made with faded color in the begining of the line. to customize it use fade.

Sparkline::data($metrics)->fade(0.2)->render()->response('png');

FCRHK8Zpq
1.0 to maximum fade and 0.0 to remove the fade

Size

to change the size of the sparkline use size. the defult size is 80px for height and 200px for width.

$width = 500;
$height = 100;

Sparkline::data($metrics)->size($width, $height)->render()->response('png');

9Sbe60Lvx

use width and heigt to change the size seperatly.

Sparkline::data($metrics)->width(400)->render()->response('png');

Sparkline::data($metrics)->height(100)->render()->response('png');

Sparkline::data($metrics)->width(300)->height(80)->render()->response('png');


Example

SparklineController.php

class SparklineController extends Controller
{
    public function index(Currency $currency)
    {
        $metrics   = Coingecko::getMetrics($currency->code);
        $sparkline = Sparkline::data($metrics);

        if ($metrics[0] - end($metrics) > 0) {
            $sparkline->color(250, 100, 100);
        } elseif ($metrics[0] - end($metrics) < 0) {
            $sparkline->color(100, 250, 100);
        }

        return $sparkline->render()->response('png');
    }
}

web.php

Route::get('/currencies/{currency}/sparkline.png', [SparklineController::class, 'index'])->name('currencies.sparkline');


Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.



License

Distributed under the MIT License. See LICENSE for more information.



Contact

Abbas mkhzomi - Telegram@llabbasmkhll - llabbasmkhll@gmail.com

Project Link: https://github.com/llabbasmkhll/laravel-sparkline