Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Commit

Permalink
Updating dep; adding new features
Browse files Browse the repository at this point in the history
Introducing background color, role & preferViewbox switch
  • Loading branch information
S1SYPHOS committed Aug 12, 2020
1 parent 5a51a30 commit 1a448e1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.
36 changes: 21 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,37 +65,43 @@ Both methods return a file object of the newly created SVG chart for further use
You may want to ..
- change the thickness of the chart
- the spacing between the segments
- adjust spacing between the segments
- add classes to the output SVG element
- use width & height instead of `viewBox`
- make it a pie chart
- add `role`
- change background color

.. here's how:

```php
$page->doDonut($data, $thickness, $spacing, $classes, $isPieChart);
```

| Parameter | Type | Default | Description |
| ------------- | ------ | --------- | ---------------------------- |
| `$data` | array | - | data to be visualized |
| `$thickness` | float | see below | thickness of the chart |
| `$spacing` | float | see below | spacing between the segments |
| `$classes` | string | `''` | classes applied to chart |
| `$isPieChart` | bool | `false` | make it a pie chart |
| Parameter | Type | Default | Description |
| ------------------ | ------ | --------- | ---------------------------- |
| `$data` | array | - | data to be visualized |
| `$thickness` | float | see below | thickness of the chart |
| `$spacing` | float | see below | spacing between the segments |
| `$backgroundColor` | string | `''` | classes applied to chart |
| `$classes` | string | `''` | classes applied to chart |
| `$isPieChart` | bool | `false` | make it a pie chart |

Attention: The field methods omits the `$data` argument (which is gathered from the structure field).


#### Configuration
You may also change certain options from your `config.php` globally (`'fundevogel.donuts.optionName'`):

| Option | Type | Default | Description |
| ------------- | ------ | --------- | -------------------------- |
| `'thickness'` | float | `3` | global default thickness |
| `'spacing'` | float | `0.005` | global default spacing |
| `'size'` | int | `100` | `viewBox` width & height |
| `'inline'` | bool | `false` | output inline SVG directly |
| `'template'` | string | `'donut'` | chart file template |
| Option | Type | Default | Description |
| ------------- | ------ | --------- | --------------------------------- |
| `'thickness'` | float | `3` | global default thickness |
| `'spacing'` | float | `0.005` | global default spacing |
| `'size'` | int | `100` | width & height dimensions |
| `'viewbox'` | bool | `true` | viewBox instead of width & height |
| `'role'` | string | `'img'` | role attribute |
| `'inline'` | bool | `false` | output inline SVG directly |
| `'template'` | string | `'donut'` | chart file template |


### Example
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Kirby v3 wrapper for tiny-phpeanuts",
"type": "kirby-plugin",
"license": "MIT",
"version": "1.0.0",
"version": "1.1.0",
"keywords": ["kirby3", "svg", "charts"],
"homepage": "https://github.com/Fundevogel/kirby3-donuts#readme",
"authors": [
Expand All @@ -20,7 +20,7 @@
"issues": "https://github.com/Fundevogel/kirby3-donuts/issues"
},
"require": {
"fundevogel/tiny-phpeanuts": "0.3.0",
"fundevogel/tiny-phpeanuts": "^1.0.0",
"getkirby/composer-installer": "^1.1"
},
"require-dev": {
Expand Down
26 changes: 22 additions & 4 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,25 @@ function render(Kirby\Cms\Page $page, array $data)
$donut->setSize(option('fundevogel.donuts.size'));
}

$donut->setPreferViewbox(option('fundevogel.donuts.viewbox'));

if ($data['backgroundColor'] !== 'transparent') {
$donut->setBackgroundColor($data['backgroundColor']);
}

if ($data['classes'] !== '') {
$donut->setClasses($data['classes']);
}

if (option('fundevogel.donuts.role') !== 'img') {
$donut->setRole(option('fundevogel.donuts.role'));
}

if ($data['isPieChart'] === true) {
$donut->setPieChart(true);
}

$content = $donut->getSVGElement();
$content = $donut->render();

$file = new File([
'parent' => $page,
Expand Down Expand Up @@ -67,6 +77,8 @@ function render(Kirby\Cms\Page $page, array $data)
'thickness' => 3,
'spacing' => 0.005,
'size' => 100,
'viewbox' => true,
'role' => 'img',
'inline' => false,
'template' => 'donut',
],
Expand All @@ -81,14 +93,16 @@ function render(Kirby\Cms\Page $page, array $data)
* @param float $spacing
* @param string classes
* @param bool $isPieChart
* @param string $backgroundColor
* @return Kirby\Cms\File|string
*/
'toDonut' => function (
array $entries,
float $thickness = null,
float $spacing = null,
string $classes = '',
bool $isPieChart = false
bool $isPieChart = false,
string $backgroundColor = 'transparent'
) {
try {
$file = render($this, [
Expand All @@ -97,6 +111,7 @@ function render(Kirby\Cms\Page $page, array $data)
'spacing' => $spacing,
'classes' => $classes,
'isPieChart' => $isPieChart,
'backgroundColor' => $backgroundColor,
]);
} catch (Exception $e) {
throw $e;
Expand All @@ -112,15 +127,17 @@ function render(Kirby\Cms\Page $page, array $data)
* @param float $spacing
* @param string classes
* @param bool $isPieChart
* @param string $backgroundColor
* @return Kirby\Cms\File|string
*/
'toDonut' => function (
Kirby\Cms\Field $field,
float $thickness = null,
float $spacing = null,
string $classes = '',
bool $isPieChart = false
) {
bool $isPieChart = false,
string $backgroundColor = 'transparent'
) {
$page = $field->model();
$entries = $field->toStructure()->toArray();

Expand All @@ -131,6 +148,7 @@ function render(Kirby\Cms\Page $page, array $data)
'spacing' => $spacing,
'classes' => $classes,
'isPieChart' => $isPieChart,
'backgroundColor' => $backgroundColor,
]);
} catch (Exception $e) {
throw $e;
Expand Down

0 comments on commit 1a448e1

Please sign in to comment.