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.
composer require llabbasmkhll/laravel-sparkline
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:
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.
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');
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');
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');
set line thikness by calling thickness
like so :
Sparkline::data($metrics)->thikness(3)->render()->response('png');
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');
1.0 to maximum fade and 0.0 to remove the fade
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');
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');
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');
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
Distributed under the MIT License. See LICENSE
for more information.
Abbas mkhzomi - Telegram@llabbasmkhll - llabbasmkhll@gmail.com
Project Link: https://github.com/llabbasmkhll/laravel-sparkline