PHP code example of clacy-builders / svg

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

    

clacy-builders / svg example snippets



ClacyBuilders\Svg\Svg;

$dbRows = array(
        ['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->append('style')->appendText(".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([0, 0], 400, 400)->setClass('bgr');
$group->text('Clicks 1/2015', [200, 45])->setClass('title');
$solid = $group->polygon()->setClass('solid');
$solid->setPoints([[350, 330], [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->addPoint([$x, $y]);
    $graph->addPoint([$x, $y]);
    $labels1->tspan($row['clicks'], [$x - 10, $y - 15]);
    $labels2->tspan(
            strftime('%b', (new DateTime($row['month']))->getTimestamp()),
            [$x - 10, 350]);
}
print $svg->getMarkup();