1 <?php
2 /**
3 *
4 * WhPercentOfTypeEasyPieOperation class
5 *
6 * Displays an chart based on jquery.easy.pie plugin
7 *
8 * @author Antonio Ramirez <amigo.cobos@gmail.com>
9 * @copyright Copyright © 2amigos.us 2013-
10 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
11 * @package YiiWheels.widgets.grid.operations
12 * @uses Yiistrap.widgets.WhPercentOfTypeOperation
13 */
14 Yii::import('yiiwheels.widgts.grid.operations.WhPercentOfTypeOperation');
15
16 class WhPercentOfTypeEasyPieOperation extends WhPercentOfTypeOperation
17 {
18 /**
19 * @var string $chartCssClass the class of the layer containing the class
20 */
21 public $chartCssClass = 'bootstrap-operation-easy-pie-chart';
22
23 /**
24 * @var string $template
25 * @see TbOperation
26 */
27 public $template = '<div style="clear:both">{label}: </div>{types}';
28
29 /**
30 * @var string $typeTemplate
31 * @see parent class
32 */
33 public $typeTemplate = '<div style="float:left;text-align:center;margin:2px"><div class="{class}" data-percent="{value}">{value}%</div><div>{label}</div></div>';
34
35 /**
36 * @var array the easy-pie-chart plugin configuration options
37 * @see https://github.com/rendro/easy-pie-chart#configuration-parameter
38 */
39 public $chartOptions = array(
40 'barColor' => '#ef1e25',
41 // The color of the curcular bar. You can pass either a css valid color string like rgb,
42 // rgba hex or string colors. But you can also pass a function that accepts the current
43 // percentage as a value to return a dynamically generated color.
44 'trackColor' => '#f2f2f2',
45 // The color of the track for the bar, false to disable rendering.
46 'scaleColor' => '#dfe0e0',
47 // The color of the scale lines, false to disable rendering.
48 'lineCap' => 'round',
49 // Defines how the ending of the bar line looks like. Possible values are: butt, round and square.
50 'lineWidth' => 5,
51 // Width of the bar line in px.
52 'size' => 80,
53 // Size of the pie chart in px. It will always be a square.
54 'animate' => false,
55 // Time in milliseconds for a eased animation of the bar growing, or false to deactivate.
56 'onStart' => 'js:$.noop',
57 // Callback function that is called at the start of any animation (only if animate is not false).
58 'onStop' => 'js:$.noop'
59 // Callback function that is called at the end of any animation (only if animate is not false).
60 );
61
62 /**
63 * Widget's initialization widget
64 */
65 public function init()
66 {
67 $this->typeTemplate = strtr($this->typeTemplate, array('{class}' => $this->chartCssClass));
68 parent::init();
69 }
70
71
72 /**
73 * @see WhOperation
74 * @return mixed|void
75 */
76 public function displaySummary()
77 {
78 parent::displaySummary();
79 $this->registerClientScripts();
80 }
81
82 /**
83 * Register required scripts
84 */
85 protected function registerClientScripts()
86 {
87 /* publish assets dir */
88 $path = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'assets';
89
90 $assetsUrl = $this->getAssetsUrl($path);
91
92 /* @var $cs CClientScript */
93 $cs = Yii::app()->getClientScript();
94
95 $cs->registerCssFile($assetsUrl . '/css/easy-pie-chart.css');
96 $cs->registerScriptFile($assetsUrl . '/js/jquery.easy.pie.chart.js');
97
98 $options = CJavaScript::encode($this->chartOptions);
99 Yii::app()->getClientScript()->registerScript(
100 __CLASS__ . '#percent-of-type-operation-simple-pie',
101 '$("#' . $this->column->grid->id . ' .' . $this->column->grid->extendedSummaryCssClass . ' .' . $this->chartCssClass . '")
102 .easyPieChart(' . $options . ');');
103
104 $this->column->grid->componentsReadyScripts[__CLASS__] =
105 $this->column->grid->componentsAfterAjaxUpdate[__CLASS__] =
106 '$("#' . $this->column->grid->id . ' .' . $this->column->grid->extendedSummaryCssClass . ' .' . $this->chartCssClass . '")
107 .easyPieChart(' . $options . ');';
108 }
109 }