This repository has been archived by the owner on Feb 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e36aa42
Showing
10 changed files
with
346 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Composer + sensitive files | ||
/composer.lock | ||
/vendor | ||
*.cache | ||
|
||
# Auto-generated | ||
/dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
$finder = PhpCsFixer\Finder::create() | ||
->exclude([ | ||
'dist' | ||
]) | ||
->in(__DIR__) | ||
; | ||
|
||
return PhpCsFixer\Config::create() | ||
->setRules([ | ||
'@PSR2' => true, | ||
'array_syntax' => ['syntax' => 'short'], | ||
]) | ||
->setFinder($finder) | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 Fundevogel Kinder- & Jugendbuchhandlung | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# kirby3-donuts | ||
[](https://github.com/Fundevogel/kirby3-donuts/releases) [](https://github.com/Fundevogel/kirby3-donuts/blob/master/LICENSE) [](https://github.com/Fundevogel/kirby3-donuts/issues) [](https://travis-ci.org/fundevogel/kirby3-donuts) | ||
|
||
A Kirby v3 plugin for creating SVG donut (and pie) charts. | ||
|
||
|
||
## What | ||
This library is a Kirby v3 wrapper for the PHP port [`tiny-phpeanuts`](https://github.com/Fundevogel/tiny-phpeanuts) of the TypeScript library [`tiny-donuts`](https://github.com/Verivox/tiny-donuts). | ||
|
||
|
||
## Roadmap | ||
- [ ] Add tests | ||
- [ ] Add pie charts | ||
- [ ] Add size adjustments | ||
|
||
## Credits | ||
Naturally, a big shoutout goes to [Kim Almasan](https://github.com/Narquadah) & [Lars Krumbier](), who created `tiny-donuts` for [Verivox GmbH](https://github.com/Verivox). Most of the helper functions were taken from [Kirby](https://getkirby.com) by [Bastian Allgeier](https://github.com/bastianallgeier) (who's just awesome, btw). | ||
|
||
|
||
**Happy coding!** | ||
|
||
|
||
:copyright: Fundevogel Kinder- und Jugendbuchhandlung |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
type: structure | ||
fields: | ||
title: | ||
label: | ||
en: Title | ||
de: Titel | ||
type: text | ||
width: 1/2 | ||
|
||
value: | ||
label: | ||
en: Value | ||
de: Wert | ||
type: number | ||
min: 0 | ||
max: 1 | ||
step: .01 | ||
width: 1/4 | ||
|
||
color: | ||
label: | ||
en: Color | ||
de: Farbe | ||
type: text | ||
width: 1/4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
title: Donut | ||
|
||
accept: image/svg+xml | ||
|
||
fields: | ||
title: | ||
label: Title | ||
type: text | ||
|
||
caption: | ||
label: Caption | ||
type: text | ||
|
||
altAttribute: | ||
label: Alternative text | ||
type: text | ||
|
||
titleAttribute: | ||
label: Title text | ||
type: text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"name": "fundevogel/kirby3-donuts", | ||
"description": "Kirby v3 wrapper for tiny-phpeanuts", | ||
"type": "kirby-plugin", | ||
"license": "MIT", | ||
"homepage": "https://github.com/Fundevogel/kirby3-donuts#readme", | ||
"authors": [ | ||
{ | ||
"name": "S1SYPHOS", | ||
"email": "hello@twobrain.io", | ||
"homepage": "https://twobrain.io" | ||
} | ||
], | ||
"support": { | ||
"email": "support@twobrain.io", | ||
"forum": "https://forum.getkirby.com", | ||
"source": "https://github.com/Fundevogel/kirby3-donuts", | ||
"issues": "https://github.com/Fundevogel/kirby3-donuts/issues" | ||
}, | ||
"require": { | ||
"fundevogel/tiny-phpeanuts": "^0.2.0", | ||
"getkirby/composer-installer": "^1.1" | ||
}, | ||
"require-dev": { | ||
"friendsofphp/php-cs-fixer": "^2.14", | ||
"phpunit/phpunit": "^8.1" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Fundevogel\\": "lib/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Fundevogel\\Tests\\": "tests/" | ||
} | ||
}, | ||
"config": { | ||
"optimize-autoloader": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
@include_once __DIR__ . '/vendor/autoload.php'; | ||
|
||
use Fundevogel\Chart; | ||
|
||
Kirby::plugin('fundevogel/donuts', [ | ||
'options' => [ | ||
'thickness' => 3, | ||
'spacing' => 0.005, | ||
'size' => 100, | ||
'inline' => false, | ||
'template' => 'donut', | ||
], | ||
'blueprints' => [ | ||
'fields/donut' => __DIR__ . '/blueprints/field.yml', | ||
'files/donut' => __DIR__ . '/blueprints/file.yml', | ||
], | ||
'pageMethods' => [ | ||
/** | ||
* @param array $entries | ||
* @param float $thickness | ||
* @param float $spacing | ||
* @return Kirby\Cms\File|string | ||
*/ | ||
'toDonut' => function ( | ||
array $entries, | ||
float $thickness = null, | ||
float $spacing = null | ||
) { | ||
try { | ||
$file = (new Chart($this, [ | ||
'entries' => $entries, | ||
'thickness' => $thickness, | ||
'spacing' => $spacing, | ||
]))->render(); | ||
} catch (Exception $e) { | ||
throw $e; | ||
} | ||
|
||
return $file; | ||
} | ||
], | ||
'fieldMethods' => [ | ||
/** | ||
* @param Kirby\Cms\Field $field | ||
* @param float $thickness | ||
* @param float $spacing | ||
* @return Kirby\Cms\File|string | ||
*/ | ||
'toDonut' => function ( | ||
Kirby\Cms\Field $field, | ||
float $thickness = null, | ||
float $spacing = null | ||
) { | ||
$page = $field->model(); | ||
$entries = $field->toStructure()->toArray(); | ||
|
||
try { | ||
$file = (new Chart($page, [ | ||
'entries' => $entries, | ||
'thickness' => $thickness, | ||
'spacing' => $spacing, | ||
]))->render(); | ||
} catch (Exception $e) { | ||
throw $e; | ||
} | ||
|
||
return $file; | ||
} | ||
] | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
namespace Fundevogel; | ||
|
||
use Fundevogel\Donut; | ||
|
||
use \Kirby\Cms\File; | ||
use \Kirby\Cms\Page; | ||
|
||
|
||
/** | ||
* Class Chart | ||
* | ||
* Creates donut charts for Kirby v3 | ||
* | ||
* @package kirby3-donuts | ||
*/ | ||
class Chart | ||
{ | ||
/** | ||
* Current version number of kirby3-donut | ||
*/ | ||
const VERSION = '0.1.0'; | ||
|
||
|
||
/** | ||
* This contains: | ||
* - Data points being visualized, where each entry consists of | ||
* - a color string | ||
* - a value representing the share (between 0 and 1) | ||
* - Thickness of the chart | ||
* - Spacing between chart segments | ||
* - Viewport width & height | ||
* | ||
* @var array | ||
*/ | ||
private $options; | ||
|
||
|
||
public function __construct( | ||
Kirby\Cms\Page $page, | ||
array $options | ||
) { | ||
$this->page = $page; | ||
$this->thickness = $options['thickness']; | ||
$this->entries = $options['entries']; | ||
$this->spacing = $options['spacing']; | ||
} | ||
|
||
function render(Kirby\Cms\Page $page, array $data) | ||
{ | ||
$thickness = $thickness ?? option('fundevogel.donuts.thickness'); | ||
$spacing = $spacing ?? option('fundevogel.donuts.spacing'); | ||
|
||
$donut = new Donut( | ||
$data['entries'], | ||
$data['thickness'], | ||
$data['spacing'] | ||
); | ||
|
||
$content = $donut->getSVGElement(); | ||
|
||
$file = new File([ | ||
'parent' => $page, | ||
'filename' => 'chart-' . hash('md5', $content), | ||
]); | ||
|
||
$file->update([ | ||
'template' => option('fundevogel.donuts.template'), | ||
]); | ||
|
||
if (file_exists($file->root())) { | ||
if (option('fundevogel.donuts.inline') === true) { | ||
return svg($file); | ||
} | ||
|
||
return $file; | ||
} | ||
|
||
if (F::write($file->root(), $content)) { | ||
|
||
if (option('fundevogel.donuts.inline') === true) { | ||
return svg($file); | ||
} | ||
|
||
return $file; | ||
} | ||
|
||
throw new Exception('Couldn\'t create chart!'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd" | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
bootstrap="vendor/autoload.php" | ||
> | ||
<testsuites> | ||
<testsuite name="kirby3-donuts Test Suite"> | ||
<directory>./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory>./</directory> | ||
<exclude> | ||
<directory>./dist</directory> | ||
<directory>./vendor</directory> | ||
</exclude> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |