PHP code example of lib16 / svg

1. Go to this page and download the library: Download lib16/svg 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/ */

    

lib16 / svg example snippets



Lib16\SVG\Svg;
use Lib16\Graphics\Geometry\Point;

$dbRows = [
    ['month' => '2015-01', 'clicks' => '501'],
    ['month' => '2015-02', 'clicks' => '560'],
    ['month' => '2015-03', 'clicks' => '543'],
    ['month' => '2015-04', 'clicks' => '607'],
    ['month' => '2015-05', 'clicks' => '646'],
    ['month' => '2015-06', 'clicks' => '645']
];

$svg = Svg::createSvg(400, 400);
$svg->defs()->style('.bgr {
    fill: #593;
    fill-opacity: 0.25;
}
.graph {
    fill: none;
    stroke-width: 3px;
    stroke: #593;
}
.solid {
    fill: #593;
    fill-opacity: 0.5;
}
.labels, .title {
    font-family: open sans, tahoma, verdana, sans-serif;
    fill: 333;
    stroke: none;
}
.labels {
    font-size: 16px;
}
.title {
    font-size: 24px;
    text-anchor: middle;
}');
$group = $svg->g();
$group->rect(new Point(0, 0), 400, 400)->setClass('bgr');
$group->text('Clicks 1/2015', new Point(200, 45))->setClass('title');
$solid = $group->polygon()->setClass('solid');
$solid->setPoints(new Point(350, 330), new Point(50, 330));
$graph = $group->polyline()->setClass('graph');
$labels1 = $group->text()->setClass('labels');
$labels2 = $group->text()->setClass('labels');
foreach ($dbRows as $i => $row) {
    $x = 50 + $i * 60;
    $y = (1000 - $row['clicks']) / 2;
    $solid->setPoints(new Point($x, $y));
    $graph->setPoints(new Point($x, $y));
    $labels1->tspan($row['clicks'], new Point($x - 10, $y - 15));
    $labels2->tspan(
        strftime('%b', (new DateTime($row['month']))->getTimestamp()),
        new Point($x - 10, 350)
    );
}
print $svg->headerfields('stats');
print $svg->getMarkup();