TbExtendedGridView::renderChart PHP Метод

renderChart() публичный Метод

Renders chart
public renderChart ( )
    public function renderChart()
    {
        if (!$this->displayChart || $this->dataProvider->getItemCount() <= 0) {
            return;
        }
        if (!isset($this->chartOptions['data']['series'])) {
            throw new CException(Yii::t('zii', 'You need to set the "series" attribute in order to render a chart'));
        }
        $configSeries = $this->chartOptions['data']['series'];
        if (!is_array($configSeries)) {
            throw new CException(Yii::t('zii', '"chartOptions.series" is expected to be an array.'));
        }
        if (!isset($this->chartOptions['config'])) {
            $this->chartOptions['config'] = array();
        }
        // ****************************************
        // render switch buttons
        $buttons = Yii::createComponent(array('class' => 'bootstrap.widgets.TbButtonGroup', 'toggle' => 'radio', 'buttons' => array(array('label' => Yii::t('zii', 'Grid'), 'url' => '#', 'htmlOptions' => array('class' => 'active ' . $this->getId() . '-grid-control grid')), array('label' => Yii::t('zii', 'Chart'), 'url' => '#', 'htmlOptions' => array('class' => $this->getId() . '-grid-control chart'))), 'htmlOptions' => array('style' => 'margin-bottom:5px')));
        echo '<div class="row">';
        $buttons->init();
        $buttons->run();
        echo '</div>';
        $chartId = preg_replace('[-\\ ?]', '_', 'exgvwChart' . $this->getId());
        // cleaning out most possible characters invalid as javascript variable identifiers.
        $this->componentsReadyScripts[] = '$(document).on("click",".' . $this->getId() . '-grid-control", function(){
			if ($(this).hasClass("grid") && $("#' . $this->getId() . ' #' . $chartId . '").is(":visible"))
			{
				$("#' . $this->getId() . ' #' . $chartId . '").hide();
				$("#' . $this->getId() . ' table.items").show();
			}
			if ($(this).hasClass("chart") && $("#' . $this->getId() . ' table.items").is(":visible"))
			{
				$("#' . $this->getId() . ' table.items").hide();
				$("#' . $this->getId() . ' #' . $chartId . '").show();
			}
			return false;
		});';
        // end switch buttons
        // ****************************************
        // render Chart
        // chart options
        $data = $this->dataProvider->getData();
        $count = count($data);
        $seriesData = array();
        $cnt = 0;
        foreach ($configSeries as $set) {
            $seriesData[$cnt] = array('name' => isset($set['name']) ? $set['name'] : null, 'data' => array());
            for ($row = 0; $row < $count; ++$row) {
                $column = $this->getColumnByName($set['attribute']);
                if (!is_null($column) && $column->value !== null) {
                    $seriesData[$cnt]['data'][] = $this->evaluateExpression($column->value, array('data' => $data[$row], 'row' => $row));
                } else {
                    $value = CHtml::value($data[$row], $set['attribute']);
                    $seriesData[$cnt]['data'][] = is_numeric($value) ? (double) $value : $value;
                }
            }
            ++$cnt;
        }
        // ****************************************
        // render chart
        $options = CMap::mergeArray($this->chartOptions['config'], array('series' => $seriesData));
        $this->chartOptions['htmlOptions'] = isset($this->chartOptions['htmlOptions']) ? $this->chartOptions['htmlOptions'] : array();
        $this->chartOptions['htmlOptions']['style'] = 'display:none';
        // sorry but use a class to provide styles, we need this
        // build unique ID
        // important!
        echo '<div class="row">';
        if ($this->ajaxUpdate !== false) {
            if (isset($options['chart']) && is_array($options['chart'])) {
                $options['chart']['renderTo'] = $chartId;
            } else {
                $options['chart'] = array('renderTo' => $chartId);
            }
            $jsOptions = CJSON::encode($options);
            if (isset($this->chartOptions['htmlOptions']['data-config'])) {
                unset($this->chartOptions['htmlOptions']['data-config']);
            }
            echo "<div id='{$chartId}' " . CHtml::renderAttributes($this->chartOptions['htmlOptions']) . " data-config='{$jsOptions}'></div>";
            $this->componentsAfterAjaxUpdate[] = "highchart{$chartId} = new Highcharts.Chart(\$('#{$chartId}').data('config'));";
        }
        $configChart = array('class' => 'bootstrap.widgets.TbHighCharts', 'id' => $chartId, 'options' => $options, 'htmlOptions' => $this->chartOptions['htmlOptions']);
        $chart = Yii::createComponent($configChart);
        $chart->init();
        $chart->run();
        echo '</div>';
        // end chart display
        // ****************************************
    }