HighchartsWidget   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 75
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 25 4
B registerAssets() 0 31 5
1
<?php
2
3
/**
4
 * HighchartsWidget class file.
5
 *
6
 * @author Milo Schuman <[email protected]>
7
 * @link https://github.com/miloschuman/yii-highcharts/
8
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
9
 */
10
11
/**
12
 * HighchartsWidget encapsulates the {@link http://www.highcharts.com/ Highcharts}
13
 * charting library's Chart object.
14
 *
15
 * To use this widget, you may insert the following code in a view:
16
 * <pre>
17
 * $this->Widget('ext.highcharts.HighchartsWidget', array(
18
 *    'options'=>array(
19
 *       'title' => array('text' => 'Fruit Consumption'),
20
 *       'xAxis' => array(
21
 *          'categories' => array('Apples', 'Bananas', 'Oranges')
22
 *       ),
23
 *       'yAxis' => array(
24
 *          'title' => array('text' => 'Fruit eaten')
25
 *       ),
26
 *       'series' => array(
27
 *          array('name' => 'Jane', 'data' => array(1, 0, 4)),
28
 *          array('name' => 'John', 'data' => array(5, 7, 3))
29
 *       )
30
 *    )
31
 * ));
32
 * </pre>
33
 *
34
 * By configuring the {@link $options} property, you may specify the options
35
 * that need to be passed to the Highcharts JavaScript object. Please refer to
36
 * the demo gallery and documentation on the {@link http://www.highcharts.com/
37
 * Highcharts website} for possible options.
38
 *
39
 * Alternatively, you can use a valid JSON string in place of an associative
40
 * array to specify options:
41
 *
42
 * <pre>
43
 * $this->Widget('ext.highcharts.HighchartsWidget', array(
44
 *    'options'=>'{
45
 *       "title": { "text": "Fruit Consumption" },
46
 *       "xAxis": {
47
 *          "categories": ["Apples", "Bananas", "Oranges"]
48
 *       },
49
 *       "yAxis": {
50
 *          "title": { "text": "Fruit eaten" }
51
 *       },
52
 *       "series": [
53
 *          { "name": "Jane", "data": [1, 0, 4] },
54
 *          { "name": "John", "data": [5, 7,3] }
55
 *       ]
56
 *    }'
57
 * ));
58
 * </pre>
59
 *
60
 * Note: You must provide a valid JSON string (e.g. double quotes) when using
61
 * the second option. You can quickly validate your JSON string online using
62
 * {@link http://jsonlint.com/ JSONLint}.
63
 *
64
 * Note: You do not need to specify the <code>chart->renderTo</code> option as
65
 * is shown in many of the examples on the Highcharts website. This value is
66
 * automatically populated with the id of the widget's container element. If you
67
 * wish to use a different container, feel free to specify a custom value.
68
 */
69
class HighchartsWidget extends CWidget
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
70
{
71
    protected $_constr = 'Chart';
72
    protected $_baseScript = 'highcharts';
73
    public $options = array();
74
    public $htmlOptions = array();
75
    public $setupOptions = array();
76
    public $scripts = array();
77
    public $callback = false;
78
    public $scriptPosition = null;
79
80
    /**
81
     * Renders the widget.
82
     */
83
    public function run()
84
    {
85
        if (isset($this->htmlOptions['id'])) {
86
            $this->id = $this->htmlOptions['id'];
87
        } else {
88
            $this->htmlOptions['id'] = $this->getId();
89
        }
90
91
        echo CHtml::openTag('div', $this->htmlOptions);
92
        echo CHtml::closeTag('div');
93
94
        // check if options parameter is a json string
95
        if (is_string($this->options)) {
96
            if (!$this->options = CJSON::decode($this->options)) {
97
                throw new CException('The options parameter is not valid JSON.');
98
            }
99
        }
100
101
        // merge options with default values
102
        $defaultOptions = array('chart' => array('renderTo' => $this->id));
103
        $this->options = CMap::mergeArray($defaultOptions, $this->options);
104
        array_unshift($this->scripts, $this->_baseScript);
105
106
        $this->registerAssets();
107
    }
108
109
    /**
110
     * Publishes and registers the necessary script files.
111
     */
112
    protected function registerAssets()
113
    {
114
        $basePath = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR;
115
        $baseUrl = Yii::app()->getAssetManager()->publish($basePath, false, 1, YII_DEBUG);
116
117
        $cs = Yii::app()->clientScript;
118
        $cs->registerCoreScript('jquery');
119
120
        // register additional scripts
121
        $extension = YII_DEBUG ? '.src.js' : '.js';
122
        foreach ($this->scripts as $script) {
123
            $cs->registerScriptFile("{$baseUrl}/{$script}{$extension}", $this->scriptPosition);
124
        }
125
126
        // highcharts and highstock can't live on the same page
127
        if ($this->_baseScript === 'highstock') {
128
            $cs->scriptMap["highcharts{$extension}"] = "{$baseUrl}/highstock{$extension}";
129
        }
130
131
        // prepare and register JavaScript code block
132
        $jsOptions = CJavaScript::encode($this->options);
133
        $setupOptions = CJavaScript::encode($this->setupOptions);
134
        $js = "Highcharts.setOptions($setupOptions); var chart = new Highcharts.{$this->_constr}($jsOptions);";
135
        $key = __CLASS__ . '#' . $this->id;
136
        if (is_string($this->callback)) {
137
            $callbackScript = "function {$this->callback}(data) {{$js}}";
138
            $cs->registerScript($key, $callbackScript, CClientScript::POS_END);
139
        } else {
140
            $cs->registerScript($key, $js, CClientScript::POS_LOAD);
141
        }
142
    }
143
}
144