|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* C3JsWidget class file. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Frank Gehann <[email protected]> |
|
7
|
|
|
* @link https://github.com/code-works/yii-c3js/ |
|
8
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
|
9
|
|
|
* @version 1.0.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* C3JsWidget encapsulates the {@link http://c3js.org/ C3.js} D3-based reusable chart library. |
|
14
|
|
|
* |
|
15
|
|
|
* To use this widget, you may insert the following code in a view: |
|
16
|
|
|
* <pre> |
|
17
|
|
|
* $this->Widget('ext.c3js.C3JsWidget',array( |
|
18
|
|
|
* 'options' => " |
|
19
|
|
|
* data: { |
|
20
|
|
|
* columns: [ |
|
21
|
|
|
* ['data1', 30, 200, 100, 400, 150, 250], |
|
22
|
|
|
* ['data2', 50, 20, 10, 40, 15, 25] |
|
23
|
|
|
* ], |
|
24
|
|
|
* axes: { |
|
25
|
|
|
* data2: 'y2' |
|
26
|
|
|
* } |
|
27
|
|
|
* }, |
|
28
|
|
|
* axis: { |
|
29
|
|
|
* y: { |
|
30
|
|
|
* label: { // ADD |
|
31
|
|
|
* text: 'Y Label', |
|
32
|
|
|
* position: 'outer-middle' |
|
33
|
|
|
* } |
|
34
|
|
|
* }, |
|
35
|
|
|
* y2: { |
|
36
|
|
|
* show: true, |
|
37
|
|
|
* label: { // ADD |
|
38
|
|
|
* text: 'Y2 Label', |
|
39
|
|
|
* position: 'outer-middle' |
|
40
|
|
|
* } |
|
41
|
|
|
* } |
|
42
|
|
|
* } |
|
43
|
|
|
* " |
|
44
|
|
|
* )); |
|
45
|
|
|
* </pre> |
|
46
|
|
|
* |
|
47
|
|
|
* By configuring the {@link $options} property, you may specify the options |
|
48
|
|
|
* that need to be passed to the C3.js JavaScript object. Please refer to |
|
49
|
|
|
* the demo gallery and documentation on the {@link http://c3js.org/examples.html C3.js examples Website} |
|
50
|
|
|
* for possible options. |
|
51
|
|
|
*/ |
|
52
|
|
|
class C3JsWidget extends CWidget |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
public $options = array(); |
|
55
|
|
|
public $htmlOptions = array(); |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Renders the widget. |
|
59
|
|
|
*/ |
|
60
|
|
|
public function run() |
|
61
|
|
|
{ |
|
62
|
|
|
$defaultHtmlOptions = array('class' => "c3"); |
|
63
|
|
|
|
|
64
|
|
|
if (isset($this->htmlOptions['id'])) { |
|
65
|
|
|
$id = $this->htmlOptions['id']; |
|
66
|
|
|
} else { |
|
67
|
|
|
$id = $this->htmlOptions['id'] = $this->getId(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$mergedHtmlOptions = CMap::mergeArray($defaultHtmlOptions, $this->htmlOptions); |
|
71
|
|
|
|
|
72
|
|
|
echo CHtml::openTag('div', $mergedHtmlOptions); |
|
73
|
|
|
echo CHtml::closeTag('div'); |
|
74
|
|
|
|
|
75
|
|
|
// check if options parameter is a json string |
|
76
|
|
|
if (is_string($this->options)) { |
|
77
|
|
|
if (!$this->options = CJSON::decode($this->options)) { |
|
78
|
|
|
throw new CException('The options parameter is not valid JSON.'); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// merge options with default values |
|
83
|
|
|
$defaultOptions = array('bindto' => '#'.$id); |
|
84
|
|
|
$this->options = CMap::mergeArray($defaultOptions, $this->options); |
|
85
|
|
|
|
|
86
|
|
|
$jsOptions = CJavaScript::encode($this->options); |
|
87
|
|
|
$this->registerScripts(__CLASS__ . '#' . $id, "var c3js_chart_{$id} = c3.generate($jsOptions);"); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Publishes and registers the necessary script files. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string the id of the script to be inserted into the page |
|
94
|
|
|
* @param string the embedded script to be inserted into the page |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function registerScripts($id, $embeddedScript) |
|
97
|
|
|
{ |
|
98
|
|
|
$basePath = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR; |
|
99
|
|
|
$baseUrl = Yii::app()->getAssetManager()->publish($basePath, false, 1, YII_DEBUG); |
|
100
|
|
|
|
|
101
|
|
|
$cs = Yii::app()->clientScript; |
|
102
|
|
|
|
|
103
|
|
|
// register additional scripts |
|
104
|
|
|
$extension = YII_DEBUG ? '.min.js' : '.js'; |
|
105
|
|
|
$extension_css = YII_DEBUG ? '.min.css' : '.css'; |
|
106
|
|
|
$cs->registerScriptFile("{$baseUrl}/d3{$extension}"); |
|
107
|
|
|
$cs->registerScriptFile("{$baseUrl}/c3{$extension}"); |
|
108
|
|
|
$cs->registerCssFile("{$baseUrl}/c3{$extension_css}"); |
|
109
|
|
|
|
|
110
|
|
|
// register embedded script |
|
111
|
|
|
$cs->registerScript($id, $embeddedScript, CClientScript::POS_LOAD); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.