|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Highcharts class file. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Milo Schuman <[email protected]> |
|
7
|
|
|
* @link https://github.com/miloschuman/yii2-highcharts/ |
|
8
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace miloschuman\highcharts; |
|
12
|
|
|
|
|
13
|
|
|
use yii\base\Widget; |
|
14
|
|
|
use yii\helpers\ArrayHelper; |
|
15
|
|
|
use yii\helpers\Html; |
|
16
|
|
|
use yii\helpers\Json; |
|
17
|
|
|
use yii\web\View; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Highcharts encapsulates the {@link http://www.highcharts.com/ Highcharts} |
|
21
|
|
|
* charting library's Chart object. |
|
22
|
|
|
* |
|
23
|
|
|
* To use this widget, you can insert the following code in a view: |
|
24
|
|
|
* ```php |
|
25
|
|
|
* echo \miloschuman\highcharts\Highcharts::widget([ |
|
26
|
|
|
* 'options' => [ |
|
27
|
|
|
* 'title' => ['text' => 'Fruit Consumption'], |
|
28
|
|
|
* 'xAxis' => [ |
|
29
|
|
|
* 'categories' => ['Apples', 'Bananas', 'Oranges'] |
|
30
|
|
|
* ], |
|
31
|
|
|
* 'yAxis' => [ |
|
32
|
|
|
* 'title' => ['text' => 'Fruit eaten'] |
|
33
|
|
|
* ], |
|
34
|
|
|
* 'series' => [ |
|
35
|
|
|
* ['name' => 'Jane', 'data' => [1, 0, 4]], |
|
36
|
|
|
* ['name' => 'John', 'data' => [5, 7, 3]] |
|
37
|
|
|
* ] |
|
38
|
|
|
* ] |
|
39
|
|
|
* ]); |
|
40
|
|
|
* ``` |
|
41
|
|
|
* |
|
42
|
|
|
* By configuring the {@link $options} property, you may specify the options |
|
43
|
|
|
* that need to be passed to the Highcharts JavaScript object. Please refer to |
|
44
|
|
|
* the demo gallery and documentation on the {@link https://www.highcharts.com/ |
|
45
|
|
|
* Highcharts website} for possible options. |
|
46
|
|
|
* |
|
47
|
|
|
* Note: You do not need to specify the <code>chart->renderTo</code> option as |
|
48
|
|
|
* is shown in many of the examples on the Highcharts website. This value is |
|
49
|
|
|
* automatically populated with the id of the widget's container element. If you |
|
50
|
|
|
* wish to use a different container, feel free to specify a custom value. |
|
51
|
|
|
*/ |
|
52
|
|
|
class Highcharts extends Widget |
|
53
|
|
|
{ |
|
54
|
|
|
|
|
55
|
|
|
protected $constr = 'Chart'; |
|
56
|
|
|
protected $baseScript = 'highcharts'; |
|
57
|
|
|
public $options = []; |
|
58
|
|
|
public $htmlOptions = []; |
|
59
|
|
|
public $setupOptions = []; |
|
60
|
|
|
public $scripts = []; |
|
61
|
|
|
public $callback = false; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Renders the widget. |
|
65
|
|
|
*/ |
|
66
|
|
|
public function run() |
|
67
|
|
|
{ |
|
68
|
|
|
// determine the ID of the container element |
|
69
|
|
|
if (isset($this->htmlOptions['id'])) { |
|
70
|
|
|
$this->id = $this->htmlOptions['id']; |
|
71
|
|
|
} else { |
|
72
|
|
|
$this->id = $this->htmlOptions['id'] = $this->getId(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// render the container element |
|
76
|
|
|
echo Html::tag('div', '', $this->htmlOptions); |
|
77
|
|
|
|
|
78
|
|
|
// check if options parameter is a json string |
|
79
|
|
|
if (is_string($this->options)) { |
|
80
|
|
|
$this->options = Json::decode($this->options); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// merge options with default values |
|
84
|
|
|
$defaultOptions = ['chart' => ['renderTo' => $this->id]]; |
|
85
|
|
|
$this->options = ArrayHelper::merge($defaultOptions, $this->options); |
|
86
|
|
|
array_unshift($this->scripts, $this->baseScript); |
|
87
|
|
|
|
|
88
|
|
|
$this->registerAssets(); |
|
89
|
|
|
|
|
90
|
|
|
parent::run(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Registers required assets and the executing code block with the view |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function registerAssets() |
|
97
|
|
|
{ |
|
98
|
|
|
// register the necessary script files |
|
99
|
|
|
HighchartsAsset::register($this->view)->withScripts($this->scripts); |
|
100
|
|
|
|
|
101
|
|
|
// prepare and register JavaScript code block |
|
102
|
|
|
$jsOptions = Json::encode($this->options); |
|
103
|
|
|
$setupOptions = Json::encode($this->setupOptions); |
|
104
|
|
|
$js = "Highcharts.setOptions($setupOptions); new Highcharts.{$this->constr}($jsOptions);"; |
|
105
|
|
|
$key = __CLASS__ . '#' . $this->id; |
|
106
|
|
|
if (is_string($this->callback)) { |
|
107
|
|
|
$js = "function {$this->callback}(data) {{$js}}"; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$this->view->registerJs($js, View::POS_READY, $key); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..