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