1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace dominus77\highcharts; |
4
|
|
|
|
5
|
|
|
use yii\base\Widget; |
6
|
|
|
use yii\helpers\Json; |
7
|
|
|
use yii\helpers\Html; |
8
|
|
|
use yii\helpers\ArrayHelper; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class HighChartsWidget |
12
|
|
|
* @package dominus77\highcharts |
13
|
|
|
*/ |
14
|
|
|
class HighChartsWidget extends Widget |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array the HTML attributes for the links container tag. |
18
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
19
|
|
|
*/ |
20
|
|
|
public $options = []; |
21
|
|
|
/** |
22
|
|
|
* @var array the options for the HighCharts plugin. Default options have exporting enabled. |
23
|
|
|
* Please refer to the HighCharts plugin Web page for possible options. |
24
|
|
|
* @see http://api.highcharts.com/highcharts |
25
|
|
|
*/ |
26
|
|
|
public $clientOptions = []; |
27
|
|
|
/** |
28
|
|
|
* @var bool Set to true to enable 3D support. |
29
|
|
|
*/ |
30
|
|
|
public $enable3d = false; |
31
|
|
|
/** |
32
|
|
|
* @var bool Set to true to enable support for more chart types. |
33
|
|
|
*/ |
34
|
|
|
public $enableMore = false; |
35
|
|
|
/** |
36
|
|
|
* @var array the modules to register. The modules need to point to the name of the modules available under |
37
|
|
|
*/ |
38
|
|
|
public $modules = []; |
39
|
|
|
/** |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
private $_renderTo; |
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $_src = ''; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
*/ |
51
|
|
|
public function init() |
52
|
|
|
{ |
53
|
|
|
parent::init(); |
54
|
|
|
$this->_src = YII_DEBUG ? '.src' : ''; |
55
|
|
|
if (!isset($this->options['id'])) { |
56
|
|
|
$this->options['id'] = $this->getId(); |
57
|
|
|
} |
58
|
|
|
$this->clientOptions = ArrayHelper::merge( |
59
|
|
|
[ |
60
|
|
|
'exporting' => [ |
61
|
|
|
'enabled' => true |
62
|
|
|
] |
63
|
|
|
], |
64
|
|
|
$this->clientOptions |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
if (ArrayHelper::getValue($this->clientOptions, 'exporting.enabled')) { |
68
|
|
|
$this->modules[] = 'exporting' . $this->_src . '.js'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->_renderTo = ArrayHelper::getValue($this->clientOptions, 'chart.renderTo'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritdoc |
76
|
|
|
*/ |
77
|
|
|
public function run() |
78
|
|
|
{ |
79
|
|
|
if (empty($this->_renderTo)) { |
80
|
|
|
echo Html::tag('div', '', $this->options); |
81
|
|
|
$this->clientOptions['chart']['renderTo'] = $this->options['id']; |
82
|
|
|
} |
83
|
|
|
$this->registerClientScript(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Registers the script for the plugin |
88
|
|
|
*/ |
89
|
|
|
public function registerClientScript() |
90
|
|
|
{ |
91
|
|
|
$view = $this->getView(); |
92
|
|
|
|
93
|
|
|
$bundle = HighChartsAsset::register($view); |
94
|
|
|
$id = str_replace('-', '_', $this->options['id']); |
95
|
|
|
$options = $this->clientOptions; |
96
|
|
|
|
97
|
|
|
if ($this->enable3d) { |
98
|
|
|
$bundle->js[] = 'highcharts-3d' . $this->_src . '.js'; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($this->enableMore) { |
102
|
|
|
$bundle->js[] = 'highcharts-more' . $this->_src . '.js'; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
foreach ($this->modules as $module) { |
106
|
|
|
$bundle->js[] = "modules/{$module}"; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if ($theme = ArrayHelper::getValue($options, 'theme')) { |
110
|
|
|
$bundle->js[] = "themes/{$theme}" . $this->_src . '.js'; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$options = Json::encode($options); |
114
|
|
|
|
115
|
|
|
$view->registerJs("var highChart_{$id} = new Highcharts.Chart({$options});"); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|