1 <?php
2 /**
3 * WhHighCharts widget class
4 *
5 * WhHighCharts is a layer of the amazing {@link http://www.highcharts.com/ Highcharts}
6 *
7 * To use this widget, you may insert the following code in a view:
8 * <pre>
9 * $this->widget('yiiwheels.widgets.WhHighCharts', array(
10 * 'options'=>array(
11 * 'title' => array('text' => 'Fruit Consumption'),
12 * 'xAxis' => array(
13 * 'categories' => array('Apples', 'Bananas', 'Oranges')
14 * ),
15 * 'yAxis' => array(
16 * 'title' => array('text' => 'Fruit eaten')
17 * ),
18 * 'series' => array(
19 * array('name' => 'Jane', 'data' => array(1, 0, 4)),
20 * array('name' => 'John', 'data' => array(5, 7, 3))
21 * )
22 * )
23 * ));
24 * </pre>
25 *
26 * To find out more about the possible {@link $options} attribute please refer to
27 * {@link http://www.hightcharts.com/ Highcharts site}
28 *
29 * @author Antonio Ramirez <amigo.cobos@gmail.com>
30 * @copyright Copyright © 2amigos.us 2013-
31 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
32 * @package YiiWheels.widgets.highcharts
33 * @uses YiiStrap.helpers.TbArray
34 */
35 Yii::import('bootstrap.helpers.TbArray');
36
37 class WhHighCharts extends CWidget
38 {
39 /**
40 * @var array $options the highcharts js configuration options
41 * @see http://api.highcharts.com/highcharts
42 */
43 public $pluginOptions = array();
44
45 /**
46 * @var array $htmlOptions the HTML tag attributes
47 */
48 public $htmlOptions = array();
49
50 /**
51 * Widget's initialization method
52 */
53 public function init()
54 {
55 $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
56 $this->htmlOptions['id'] = TbArray::getValue('id', $this->htmlOptions, $this->getId());
57 }
58
59 /**
60 * Renders the widget.
61 */
62 public function run()
63 {
64 // if there is no renderTo id, build the layer with current id and initialize renderTo option
65 if (!isset($this->pluginOptions['chart']) || !isset($this->pluginOptions['chart']['renderTo'])) {
66 echo CHtml::openTag('div', $this->htmlOptions);
67 echo CHtml::closeTag('div');
68
69 if (isset($this->pluginOptions['chart']) && is_array($this->pluginOptions['chart'])) {
70 $this->pluginOptions['chart']['renderTo'] = $this->htmlOptions['id'];
71 } else {
72 $this->pluginOptions['chart'] = array('renderTo' => $this->htmlOptions['id']);
73 }
74
75 }
76 $this->registerClientScript();
77 }
78
79 /**
80 * Publishes and registers the necessary script files.
81 */
82 protected function registerClientScript()
83 {
84 /* publish assets dir */
85 $path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
86 $assetsUrl = $this->getAssetsUrl($path);
87
88 /* @var $cs CClientScript */
89 $cs = Yii::app()->getClientScript();
90
91 $cs->registerScriptFile($assetsUrl . '/js/highcharts.js');
92
93 /* register required files */
94 $defaultOptions = array('exporting' => array('enabled' => true));
95
96 $this->pluginOptions = CMap::mergeArray($defaultOptions, $this->pluginOptions);
97
98 if (isset($this->pluginOptions['exporting']) && @$this->pluginOptions['exporting']['enabled']) {
99 $cs->registerScriptFile($assetsUrl . '/js/modules/exporting.js');
100 }
101
102 if ($theme = TbArray::getValue('theme', $this->pluginOptions)) {
103 $cs->registerScriptFile($assetsUrl . '/js/themes/' . $theme . '.js');
104 }
105
106 $options = CJavaScript::encode($this->pluginOptions);
107
108 $cs->registerScript(
109 __CLASS__ . '#' . $this->getId(),
110 "var highchart{$this->getId()} = new Highcharts.Chart({$options});"
111 );
112 }
113 }