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
|
|
|
* Fixes responsive width bug when highchart is rendered inside bootstrap modal |
94
|
|
|
*/ |
95
|
|
|
public function modalResponsiveFix() { |
96
|
|
|
//specify the container highchart is rendered To |
97
|
|
|
$renderObj = "$(\"#" . $this->options['chart']['renderTo']."\")"; |
98
|
|
|
|
99
|
|
|
$jsfix = "{$renderObj}.parents().each(function( index ) { |
100
|
|
|
if(($(this).data('bs.modal') || {}).isShown) { |
101
|
|
|
$(this).removeClass( 'fade' ); |
102
|
|
|
$(this).on('show.bs.modal', function() { |
103
|
|
|
{$renderObj}.css('visibility', 'hidden'); |
104
|
|
|
}); |
105
|
|
|
$(this).on('shown.bs.modal', function() { |
106
|
|
|
var chart = {$renderObj}.highcharts(); |
107
|
|
|
{$renderObj}.css('visibility', 'initial'); |
108
|
|
|
chart.reflow(); |
109
|
|
|
}); |
110
|
|
|
} |
111
|
|
|
});"; |
112
|
|
|
|
113
|
|
|
return $jsfix; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Registers required assets and the executing code block with the view |
118
|
|
|
*/ |
119
|
|
|
protected function registerAssets() |
120
|
|
|
{ |
121
|
|
|
// register the necessary script files |
122
|
|
|
HighchartsAsset::register($this->view)->withScripts($this->scripts); |
123
|
|
|
|
124
|
|
|
// prepare and register JavaScript code block |
125
|
|
|
|
126
|
|
|
$jsOptions = Json::encode($this->options); |
127
|
|
|
$setupOptions = Json::encode($this->setupOptions); |
128
|
|
|
$js = "Highcharts.setOptions($setupOptions); new Highcharts.{$this->constr}($jsOptions);"; |
129
|
|
|
$js.=$this->modalResponsiveFix(); |
130
|
|
|
|
131
|
|
|
$key = __CLASS__ . '#' . $this->id; |
132
|
|
|
if (is_string($this->callback)) { |
133
|
|
|
$js = "function {$this->callback}(data) {{$js}}"; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$this->view->registerJs($js, View::POS_READY, $key); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
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..