-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMorrisChartWidget.php
92 lines (80 loc) · 3.07 KB
/
MorrisChartWidget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/**
* Class MorrisChartWidget
*
* Simple wrapper for Morris.js - http://www.oesmith.co.uk/morris.js/
*
* Usage:
$this->widget('application.extensions.morris.MorrisChartWidget', array(
'id' => 'myChartElement',
'options' => array(
'chartType' => MorrisChartWidget::CHART_AREA,
'data' => array(
array('y' => 2006, 'a' => 100, 'b' => 90),
array('y' => 2007, 'a' => 40, 'b' => 60),
array('y' => 2008, 'a' => 50, 'b' => 10),
array('y' => 2009, 'a' => 60, 'b' => 50),
array('y' => 2010, 'a' => 60, 'b' => 40),
),
'xkey' => 'y',
'ykeys' => array('a', 'b'),
'labels' => array('Series A', 'Series B'),
),
));
*
* @license BSD / MIT.
* @author <eirikhm@warmsys.com> Eirik Hoem
*/
class MorrisChartWidget extends CWidget
{
public $options = array();
public $htmlOptions = array();
public $jsArrayName = 'MorrisObjects';
const CHART_AREA = 'Area';
const CHART_LINE = 'Line';
const CHART_BAR = 'Bar';
const CHART_DONUT = 'Donut';
public function run()
{
$id = $this->getId();
$this->htmlOptions['id'] = $id;
echo CHtml::openTag('div', $this->htmlOptions);
echo CHtml::closeTag('div');
$defaultOptions = array();
$this->options = CMap::mergeArray($defaultOptions, $this->options);
$this->options['element'] = $id;
$jsOptions = CJavaScript::encode($this->options);
$chartType = $this->options['chartType'];
$this->registerScripts(__CLASS__ . '#' . $id, $this->declareArray()."Morris.{$chartType}($jsOptions);");
}
protected function declareArray()
{
if(empty($this->jsArrayName))
{
return '';
}
$cs = Yii::app()->clientScript;
$cs->registerScript($this->jsArrayName.'MorrisArrayDeclaration', 'window.'.$this->jsArrayName.' = {};', CClientScript::POS_HEAD); //the ID makes sure it can't be registered twice
return 'window.'.$this->jsArrayName.'["'.$this->getId().'"] = ';
}
/**
* Publishes and registers the necessary script files.
*
* @param string the id of the script to be inserted into the page
* @param string the embedded script to be inserted into the page
*/
protected function registerScripts($id, $embeddedScript)
{
$basePath = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR;
$baseUrl = Yii::app()->getAssetManager()->publish($basePath, false, 1, YII_DEBUG);
$scriptFile = YII_DEBUG ? '/morris.js' : '/morris.min.js';
$cs = Yii::app()->clientScript;
$cs->registerCoreScript('jquery');
$cs->registerScriptFile($baseUrl . $scriptFile);
$scriptFile = 'raphael-min.js';
$cs->registerScriptFile("$baseUrl/$scriptFile");
$stylefile = 'morris.css';
$cs->registerCssFile("$baseUrl/$stylefile");
$cs->registerScript($id, $embeddedScript, CClientScript::POS_LOAD);
}
}