YiiWheels
  • Package
  • Class
  • Tree

Packages

  • None
  • yiiwheels
    • behaviors
    • widgets
    • widgets
      • ace
      • box
      • datepicker
      • daterangepicker
      • datetimepicker
      • detail
      • fileupload
      • fileuploader
      • gallery
      • google
      • grid
        • behaviors
        • operations
      • highcharts
      • maskInput
      • maskmoney
      • modal
      • multiselect
      • rangeslider
      • redactor
      • select2
      • sparklines
      • switch
      • timeago
      • timepicker
      • toggle
      • typeahead

Classes

  • WhSparkLines
 1 <?php
 2 /**
 3  * WhSparkLines class
 4  *
 5  * @author Antonio Ramirez <amigo.cobos@gmail.com>
 6  * @copyright Copyright &copy; 2amigos.us 2013-
 7  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 8  * @package YiiWheels.widgets.sparklines
 9  * @uses YiiStrap.helpers.TbArray
10  */
11 Yii::import('bootstrap.helpers.TbArray');
12 
13 class WhSparkLines extends CWidget
14 {
15     /**
16      * @var string the tag name to render the sparkline to
17      * NOTE: span type of tag may have issues.
18      */
19     public $tagName = 'div';
20 
21     /**
22      * @var array the data to show on the chart
23      * @see http://omnipotent.net/jquery.sparkline/#s-about
24      */
25     public $data = array();
26 
27     /**
28      * @var array additional HTML attributes to the tag
29      */
30     public $htmlOptions = array();
31 
32     /**
33      * @var array plugin options
34      */
35     public $pluginOptions = array();
36 
37     /**
38      * Debug mode
39      * Used to publish full js file instead of min version
40      */
41     public $debugMode = false;
42 
43     /**
44      * Widget's initialization method
45      */
46     public function init()
47     {
48         if (empty($this->data)) {
49             throw new CException(Yii::t('zii', '"data" attribute cannot be blank'));
50         }
51 
52         $this->attachBehavior('ywplugin', array('class' => 'yiiwheels.behaviors.WhPlugin'));
53 
54         $this->htmlOptions['id'] = TbArray::getValue('id', $this->htmlOptions, $this->getId());
55     }
56 
57     /**
58      * Widget's run method
59      */
60     public function run()
61     {
62         echo CHtml::openTag($this->tagName, $this->htmlOptions);
63         echo CHtml::closeTag($this->tagName);
64         $this->registerClientScript();
65     }
66 
67     /**
68      * Registers required client script for sparklines
69      */
70     public function registerClientScript()
71     {
72         /* publish assets dir */
73         $path      = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
74         $assetsUrl = $this->getAssetsUrl($path);
75 
76         /* @var $cs CClientScript */
77         $cs = Yii::app()->getClientScript();
78 
79         $script = $this->debugMode
80             ? 'jquery.sparkline.js'
81             : 'jquery.sparkline.min.js';
82 
83         $cs->registerScriptFile($assetsUrl . '/js/' . $script);
84         $cs->registerCssFile($assetsUrl . '/css/jquery.sparkline.css');
85 
86         /* initialize plugin */
87         $selector = '#' . TbArray::getValue('id', $this->htmlOptions, $this->getId());
88 
89         $data    = CJavaScript::encode($this->data);
90         $options = CJavaScript::encode($this->pluginOptions);
91 
92         $cs->registerScript(__CLASS__ . '#' . $selector, "jQuery('{$selector}').sparkline({$data}, {$options});");
93     }
94 }
YiiWheels API documentation generated by ApiGen 2.8.0