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

  • WhChart
  • WhResponsive
  1 <?php
  2 /**
  3  * WhChart class
  4  * Extends WhGridView to provide chart display (on switch)
  5  *
  6  * @author Antonio Ramirez <amigo.cobos@gmail.com>
  7  * @copyright Copyright &copy; Antonio Ramirez 2013-
  8  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  9  * @package yiiwheels.widgets.grid.behaviors
 10  */
 11 
 12 class WhChart extends CBehavior
 13 {
 14 
 15     public function getGrid()
 16     {
 17         return $this->getOwner();
 18     }
 19 
 20     /**
 21      * Renders grid/chart control buttons to switch between both components
 22      */
 23     public function renderChartControlButtons()
 24     {
 25         echo '<div class="row-fluid">';
 26         echo TbHtml::buttonGroup(array(
 27             array(
 28                 'label' => Yii::t('zii', 'Display Grid'),
 29                 'url' => '#',
 30                 'htmlOptions' => array('class' => 'active ' . $this->grid->getId() . '-grid-control grid')
 31             ),
 32             array(
 33                 'label' => Yii::t('zii', 'Display Chart'),
 34                 'url' => '#',
 35                 'htmlOptions' => array('class' => $this->grid->getId() . '-grid-control chart')
 36             ),
 37         ), array('toggle' => TbHtml::BUTTON_TOGGLE_RADIO, 'style' => 'margin-bottom:5px', 'class' => 'pull-right'));
 38         echo '</div>';
 39 
 40     }
 41 
 42     /**
 43      * Registers grid/chart control button script
 44      * @returns string the chart id
 45      */
 46     public function registerChartControlButtonsScript()
 47     {
 48         // cleaning out most possible characters invalid as javascript variable identifiers.
 49         $chartId = preg_replace('[-\\ ?]', '_', 'xyzChart' . $this->grid->getId());
 50 
 51         $this->grid->componentsReadyScripts[] = '$(document).on("click",".' . $this->grid->getId() . '-grid-control", function(){
 52             if ($(this).hasClass("grid") && $("#' . $this->grid->getId() . ' #' . $chartId . '").is(":visible"))
 53             {
 54                 $("#' . $this->grid->getId() . ' #' . $chartId . '").hide();
 55                 $("#' . $this->grid->getId() . ' table.items").show();
 56             }
 57             if ($(this).hasClass("chart") && $("#' . $this->grid->getId() . ' table.items").is(":visible"))
 58             {
 59                 $("#' . $this->grid->getId() . ' table.items").hide();
 60                 $("#' . $this->grid->getId() . ' #' . $chartId . '").show();
 61             }
 62             $(this).addClass("active").siblings().removeClass("active");
 63 
 64             return false;
 65         });';
 66 
 67         return $chartId;
 68     }
 69 
 70     /**
 71      * Renders a chart based on the data series specified
 72      * @throws CException
 73      */
 74     public function renderChart()
 75     {
 76         $displayChart = (!empty($this->grid->chartOptions) && @$this->grid->chartOptions['data'] && $this->grid->dataProvider->getItemCount());
 77 
 78         if (!$displayChart || $this->grid->dataProvider->getItemCount() <= 0) {
 79 
 80             return null;
 81         }
 82 
 83         if (!isset($this->grid->chartOptions['data']['series'])) {
 84             throw new CException(Yii::t(
 85                 'zii',
 86                 'You need to set the "series" attribute in order to render a chart'
 87             ));
 88         }
 89 
 90         $configSeries = $this->grid->chartOptions['data']['series'];
 91         if (!is_array($configSeries)) {
 92             throw new CException(Yii::t('zii', '"chartOptions.series" is expected to be an array.'));
 93         }
 94 
 95         if (!isset($this->grid->chartOptions['config'])) {
 96             $this->grid->chartOptions['config'] = array();
 97         }
 98 
 99         $this->renderChartControlButtons();
100         $chartId = $this->grid->registerChartControlButtonsScript();
101 
102         // render Chart
103         // chart options
104         $data = $this->grid->dataProvider->getData();
105         $count = count($data);
106         $seriesData = array();
107         $cnt = 0;
108         foreach ($configSeries as $set) {
109             $seriesData[$cnt] = array('name' => isset($set['name']) ? $set['name'] : null, 'data' => array());
110 
111             for ($row = 0; $row < $count; ++$row) {
112                 $column = $this->grid->getColumnByName($set['attribute']);
113                 if (!is_null($column) && $column->value !== null) {
114                     $seriesData[$cnt]['data'][] = $this->evaluateExpression(
115                         $column->value,
116                         array('data' => $data[$row], 'row' => $row)
117                     );
118                 } else {
119                     $value = CHtml::value($data[$row], $set['attribute']);
120                     $seriesData[$cnt]['data'][] = is_numeric($value) ? (float)$value : $value;
121                 }
122 
123             }
124             ++$cnt;
125         }
126 
127         $options = CMap::mergeArray($this->grid->chartOptions['config'], array('series' => $seriesData));
128 
129         $this->grid->chartOptions['htmlOptions'] = isset($this->grid->chartOptions['htmlOptions'])
130             ? $this->chartOptions['htmlOptions']
131             : array();
132 
133         // sorry but use a class to provide styles, we need this
134         $this->grid->chartOptions['htmlOptions']['style'] = 'display:none';
135 
136         // build unique ID
137         // important!
138         echo '<div class="row-fluid">';
139         if ($this->grid->ajaxUpdate !== false) {
140             if (isset($options['chart']) && is_array($options['chart'])) {
141                 $options['chart']['renderTo'] = $chartId;
142             } else {
143                 $options['chart'] = array('renderTo' => $chartId);
144             }
145             $jsOptions = CJSON::encode($options);
146 
147             if (isset($this->grid->chartOptions['htmlOptions']['data-config'])) {
148                 unset($this->grid->chartOptions['htmlOptions']['data-config']);
149             }
150 
151             echo "<div id='{$chartId}' " . CHtml::renderAttributes(
152                     $this->grid->chartOptions['htmlOptions']
153                 ) . " data-config='{$jsOptions}'></div>";
154 
155             $this->grid->componentsAfterAjaxUpdate[] = "highchart{$chartId} = new Highcharts.Chart($('#{$chartId}').data('config'));";
156         }
157         $configChart = array(
158             'class' => 'yiiwheels.widgets.highcharts.WhHighCharts',
159             'id' => $chartId,
160             'pluginOptions' => $options,
161             'htmlOptions' => $this->grid->chartOptions['htmlOptions']
162         );
163         $chart = Yii::createComponent($configChart);
164         $chart->init();
165         $chart->run();
166         echo '</div>';
167     }
168 }
YiiWheels API documentation generated by ApiGen 2.8.0