-
Notifications
You must be signed in to change notification settings - Fork 5
/
GoogleCharts.php
148 lines (123 loc) · 3.5 KB
/
GoogleCharts.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
namespace fruppel\googlecharts;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\web\View;
/**
* Google Charts widget class
*
* @package fruppel\googlecharts
*/
class GoogleCharts extends Widget
{
/**
* @var array $data DataTable object
*/
public $data = [];
/**
* @var array $dataArray Data, will be transformed to DataTable
* Example:
* [
* ['Year', 'Sales', 'Expenses'],
* ['2013', 1000, 400],
* ['2014', 1170, 460],
* ['2015', 660, 1120],
* ['2016', 1030, 540]
* ]
*/
public $dataArray = [];
/**
* @var array $options Additional configuration options
*/
public $options = [];
/**
* @var string $packages A list of Google chart libraries to load. The default 'corechart' library defines the
* most basic charts
*/
public $packages = 'corechart';
/**
* @var bool $responsive If set to boolean true the widget adapts to the parent container on resize
*/
public $responsive = false;
/**
* @var string $version Which version of the visualization to load. 1.0 is always the current production
* version.
*/
public $version = '1.0';
/**
* @var string $visualization The google.visualization library. This library defines all the core utility
* classes and functions.
*/
public $visualization = '';
/**
* @var bool $asPng Set it to true and chart will be rendered as PNG. Please note that this is currently only
* possible for core charts and geocharts.
*/
public $asPng = false;
/**
* @throws InvalidConfigException
*/
public function init()
{
parent::init();
if (!$this->visualization) {
throw new InvalidConfigException("'visualization' needs to be set, e.g. 'PieChart' or 'BarChart'");
}
if (empty($this->data) && empty($this->dataArray)) {
throw new InvalidConfigException("Either 'data' or 'dataArray' needs to be set");
}
if ($this->responsive) {
$this->options['width'] = '100%';
}
$this->data = json_encode($this->data);
$this->options = json_encode($this->options);
}
/**
* @inheritdoc
*/
public function run()
{
$this->registerAssets();
echo '<div id="googlechart_' . $this->getId() . '"></div>';
}
protected function registerAssets()
{
$view = $this->getView();
$view->registerJsFile('https://www.gstatic.com/charts/loader.js', ['position' => View::POS_HEAD]);
$functionName = 'drawChart_' . str_replace('-', '', $this->getId());
$js = "
google.load('visualization', '" . $this->version . "', {'packages':['" . $this->packages . "']});
google.setOnLoadCallback(" . $functionName . ");
function " . $functionName . "() {";
if (!empty($this->dataArray))
{
$js .= "
var data = google.visualization.arrayToDataTable(" . json_encode($this->dataArray) . ");
";
}
else
{
$js .= "
var data = new google.visualization.DataTable(" . json_encode($this->data) . ");";
}
$js .= "
var chart_div = document.getElementById('googlechart_". $this->getId() . "');
var options = " . $this->options . ";
var chart = new google.visualization." . $this->visualization . "(chart_div);";
if ($this->asPng)
{
$js .= "
google.visualization.events.addListener(chart, 'ready', function () {
chart_div.innerHTML = '<img src=\"' + chart.getImageURI() + '\">';
});";
}
$js .= "
chart.draw(data, options);
}";
$view->registerJs($js, View::POS_HEAD);
if ($this->responsive) {
$view->registerJs("$(window).resize(" . $functionName . ")", View::POS_END);
}
}
}