An open API service indexing awesome lists of open source software.

https://github.com/lib16/svg-builder-php

lib16 SVG Builder is a PHP 7 library for creating SVG documents.
https://github.com/lib16/svg-builder-php

charts composer php php-library php7 svg svg-builder svg-writer

Last synced: about 1 month ago
JSON representation

lib16 SVG Builder is a PHP 7 library for creating SVG documents.

Awesome Lists containing this project

README

          

# Lib16 SVG Builder for PHP 7
A library for creating SVG documents written in PHP 7.

[![Build Status](https://travis-ci.org/lib16/svg-builder-php.svg?branch=master)](https://travis-ci.org/lib16/svg-builder-php)

## Installation with Composer
This package is available on [packagist](https://packagist.org/packages/lib16/svg), so you can use [Composer](https://getcomposer.org) to install it.
Run the following command in your shell:

```
composer require lib16/svg
```

## Basic Usage

```php
'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();
```

The generated markup:

```svg



<![CDATA[
.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;
}
]]>




Clicks 1/2015



501
560
543
607
646
645


Jan
Feb
Mar
Apr
May
Jun

```