Skip to content

Commit

Permalink
Merge pull request #1586 from voltan/develop
Browse files Browse the repository at this point in the history
Add chart helper
  • Loading branch information
voltan authored Mar 31, 2018
2 parents c9a5899 + afc9d5a commit b474519
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions lib/Pi/View/Helper/Chart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Pi Engine (http://piengine.org)
*
* @link http://code.piengine.org for the Pi Engine source repository
* @copyright Copyright (c) Pi Engine http://piengine.org
* @license http://piengine.org/license.txt BSD 3-Clause License
* @package View
*/

namespace Pi\View\Helper;

use Pi;
use Zend\View\Helper\AbstractHelper;

/**
* Helper for draw charts
*
* $this->chart($type, $data, $options, $htmlClass);
*
* @see http://www.chartjs.org
* @author Hossein Azizabadi <[email protected]>
*/
class Chart extends AbstractHelper
{
/**
* Load chart script
*
* @param string $type
* @param array $data
* @param array $options
* @param string $htmlClass
*
* @return $this
*/
public function __invoke($type, $data, $options = [], $htmlClass = 'pi-chart')
{
// Set uniqid
$id = uniqid('piChart');

// Sat data and option
$pattern = '/"([a-zA-Z]+[a-zA-Z0-9_]*)":/';
$replacement = '$1:';
$data = preg_replace($pattern, $replacement, json_encode($data));
$options = empty($options) ? '{}' : preg_replace($pattern, $replacement, json_encode($options));

// Set script
$script
= <<<'EOT'
(function ($) {
$(document).ready(function () {
var ctx = document.getElementById('%s').getContext('2d');
var %s = new Chart(ctx, {
type: '%s',
data: %s,
options: %s
});
});
})(jQuery)
EOT;
$script = sprintf(
$script,
$id,
$id,
$type,
$data,
$options
);

// Load chart
$this->view->jQuery();
$this->view->js(pi::url('static/vendor/chart/Chart.min.js'));
$this->view->footScript()->appendScript($script);

// render html
$htmlTemplate
= <<<'EOT'
<div class="pi-chart-section">
<canvas id="%s" class="%s"></canvas>
</div>
EOT;

$content = sprintf($htmlTemplate, $id, $htmlClass);

return $content;
}
}

0 comments on commit b474519

Please sign in to comment.