1 <?php
2 3 4 5 6 7 8 9 10 11 12 13
14 Yii::import('yiiwheels.widgets.grid.operations.WhPercentOfTypeOperation');
15
16 class WhPercentOfTypeGooglePieOperation extends WhPercentOfTypeOperation
17 {
18 19 20
21 public $chartCssClass = 'bootstrap-operation-google-pie-chart';
22
23 24 25 26 27
28 public $chartOptions = array(
29 'title' => 'Google Pie Chart'
30 );
31
32 33 34
35 protected $data = array();
36
37 38 39 40
41 public function displaySummary()
42 {
43 $this->data[] = array('Label', 'Percent');
44
45 foreach ($this->types as $type) {
46 if (!isset($type['value'])) {
47 $type['value'] = 0;
48 }
49
50 $this->data[] = $this->getTotal() ? array(
51 $type['label'],
52 (float)number_format(($type['value'] / $this->getTotal()) * 100, 1)
53 ) : 0;
54 }
55 $data = CJavaScript::jsonEncode($this->data);
56 $options = CJavaScript::jsonEncode($this->chartOptions);
57 echo "<div id='{$this->id}' class='{$this->chartCssClass}' data-data='{$data}' data-options='{$options}'></div>";
58
59 $this->registerClientScript();
60 }
61
62 63 64 65
66 public function registerClientScript()
67 {
68
69 $chart = Yii::createComponent(
70 array(
71 'class' => 'yiiwheels.widgets.google.WhVisualizationChart',
72 'visualization' => 'PieChart',
73 'containerId' => $this->getId(),
74 'data' => $this->data,
75 'options' => $this->chartOptions
76 )
77 );
78 $chart->init();
79 $chart->run();
80
81
82 $this->column->grid->componentsAfterAjaxUpdate[__CLASS__] =
83 'var $el = $("#' . $this->getId() . '");var data = $el.data("data");var opts = $el.data("options");
84 data = google.visualization.arrayToDataTable(data);
85 ' . $chart->getId() . '=new google.visualization.PieChart(document.getElementById("' . $this->getId() . '"));
86 ' . $chart->getId() . '.draw(data,opts);';
87 }
88
89 }
90