PHP code example of ondrej-vrto / php-linechart

1. Go to this page and download the library: Download ondrej-vrto/php-linechart 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/ */

    

ondrej-vrto / php-linechart example snippets


$data = [0, 2, 1, 3, 3, 2, 1, 5, 4];

$svg = LineChart::new($data)->make();

$data = [0, 1, 2, 3];          // integers
$data = [0.12, 1.5555, 5.4];   // decimal numbers
$data = [true, false, true];   // booleans
$data = ["0", "002", "4.05"];  // numbers in string
$data = collect([0, 1, 2, 3]); // Illuminate\Support\Collection from Laravel
$data = [5];                   // one value => prepend zero value
$data = [];                    // empty array => set two zero value
$data = [null];                // null => set two zero value

$svg = LineChart::new(0, 1, 2, 3, 4, 5)->make();

$collection = WebVisits::query()
    ->select(['day_visit_count'])
    ->whereMorphedTo('visitable', $model)
    ->orderByDesc('date')
    ->limit(365)
    ->get()
    ->pluck('day_visit_count');

$svg = LineChart::new($collection)->make();

$svg = LineChart::new($data)
    ->withStrokeWidth(5)
    ->withOrderReversed()
    ->withMaxItemAmount(50)
    ->withLockYAxisRange(200)
    ->withDimensions(500, 100)
    ->withColorGradient('Green', 'Orange', 'Red')
    ->make();

$svg = LineChart::new($data)
    ->withColorGradient('rgb(48, 231, 237)', 'rgb(0, 166, 215)', 'rgb(0, 88, 179)', 'rgb(0, 27, 135)')
    ->make();
bash
composer 
xml
<svg viewBox="0 0 200 30" width="200" preserveAspectRatio="none" xmlns="http://www.w3.org/2000/svg">
    <defs>
        <linearGradient x2="0" y1="1" id="color-44dac552-0a1b-4b31-9c35-8bda05443b6f">
            <stop stop-color="#fbd808" offset="0"></stop>
            <stop stop-color="#ff9005" offset="0.34"></stop>
            <stop stop-color="#f9530b" offset="0.67"></stop>
            <stop stop-color="#ff0000" offset="1"></stop>
        </linearGradient>
        <mask id="linechart-44dac552-0a1b-4b31-9c35-8bda05443b6f">
            <polyline
                stroke="#fff"
                stroke-width="2"
                fill="transparent"
                stroke-linecap="round"
                stroke-linejoin="round"
                vector-effect="non-scaling-stroke"
                transform="scale(24.75 -5.6) translate(0.0404 -5.1786)"
                points="0 0 1 2 2 1 3 3 4 3 5 2 6 1 7 5 8 4">
            </polyline>
        </mask>
    </defs>
    <g>
        <rect
            width="200"
            height="30"
            fill="url(#color-44dac552-0a1b-4b31-9c35-8bda05443b6f)"
            mask="url(#linechart-44dac552-0a1b-4b31-9c35-8bda05443b6f)"
        />
    </g>
</svg>