✏️ 正在编辑: GChart.php
路径:
/srv/systems_dir/koruspay/library/Core/GChart.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php /** * Exemplo de chamada * * public function clientesPorSegmento($fields = array()) { $chart = new Core_GChart('drawPieChart'); $chart->setWidth(500); $chart->setHeight(300); $chart->setIdentifier('chart_div'); $chart->setTitle('Clientes por segmento (%)'); $columns = array( array( 'type' => 'string', 'value' => 'Segmento', 'color' => 'FF0000' ), array( 'type' => 'number', 'value' => 'Clientes', 'color' => 'FF0000' ) ); $rows = array( ); $receita = new Model_Cliente(); $values = $receita->getClientesPorSegmento(); foreach ( $values as $value ) { array_push($rows, array( 'legend' => $value['segmento_descricao'], 'values' => array( $value['qtd_clientes'] ) )); } $chart->setColumns($columns); $chart->setRows($rows); $chart->drawPieChart(); return $chart->getOutput(); } * * No controller * $grafico = new Model_Grafico(); $jsGraficos = $grafico->clientesPorSegmento(); * $this->view->output_js_gchart = $jsGraficos; $this->view->inicialize_js = Core_GChart::initializeScript(); * * Na view * <?php echo $this->inicialize_js; echo $this->output_js_gchart; ?> <div id="view-content-graph"> <div id="chart_div"></div> <div class="clear"></div> </div> */ class Core_GChart { const API_URI = 'https://www.google.com/jsapi'; private $width; private $height; private $title; private $identifier; private $columns; private $rows; private $output; private $typeChart; function __construct($type = null) { if( $type != null && method_exists($this, $type) ){ $this->typeChart = $type; } else { throw new Exception('Undefined type chart'); } } public function getWidth() { return $this->width; } public function setWidth($width) { $this->width = $width; } public function getHeight() { return $this->height; } public function setHeight($height) { $this->height = $height; } public function getTitle() { return $this->title; } public function setTitle($title) { $this->title = $title; } public function getIdentifier() { return $this->identifier; } public function setIdentifier($identifier) { $this->identifier = $identifier; } /** * * @return array */ public function getColumns() { return $this->columns; } /** * * array( * 0 => array( * 'type' => 'string', * 'value' => 'Anything', * 'color' => '#FF0000' * ) * ); * @param array $columns */ public function setColumns($columns) { $this->columns = $columns; } public function getRows() { return $this->rows; } /** * array( * array( * 'legend' => '', * 'values' => array('') * ) ) * @param array $rows */ public function setRows($rows) { $this->rows = $rows; } public function getOutput() { $output = $this->header(); $output .= $this->output; $output .= $this->footer(); return $output; } public function setOutput($output) { $this->output = $output; } public function getTypeChart() { return $this->typeChart; } public function setTypeChart($typeChart) { $this->typeChart = $typeChart; } public static function initializeScript() { return '<script type="text/javascript" src="' . Core_GChart::API_URI . '"></script>'; } protected function footer() { return '</script>'; } protected function header() { $return = '<script type="text/javascript">' . 'google.load("visualization", "1", {packages:["corechart"], "language": "pt-BR"});' . 'google.setOnLoadCallback(' . $this->getTypeChart() . ');'; return $return; } public function drawAreaChart() { if( !is_array($this->getColumns()) || !is_array($this->getRows()) ) return null; $output = 'function drawAreaChart(){ '; $output .= 'var data = new google.visualization.DataTable();'; foreach ( $this->getColumns() as $column ) { $output .= "data.addColumn('" . $column['type'] . "', '" . $column['value'] . "');"; } $output .= 'data.addRows(['; foreach ( $this->getRows() as $row ) { $output .= "['" . $row['legend'] . "', " . implode(', ', $row['values']) . "],"; } $output = substr($output, 0, (strlen($output) - 1)); $output .= ']);'; $output .= "var chart = new google.visualization.AreaChart(document.getElementById('" . $this->getIdentifier() . "'));"; $output .= "chart.draw(data, {width: " . $this->getWidth() . ", height: " . $this->getHeight() . ", title: '" . $this->getTitle() . "',"; $output .= "backgroundColor: 'transparent',"; $columns = $this->getColumns(); $output .= "hAxis: {title: '" . $columns[0]['value'] . "', titleTextStyle: {color: '" . $columns[0]['color'] . "'}}"; $output .= "});"; //finalize the function drawChart $output .= '}'; $this->setOutput($output); } public function drawColumnChart($numberFormat = true) { if( !is_array($this->getColumns()) || !is_array($this->getRows()) ) return null; $output = 'function drawColumnChart(){ ' . PHP_EOL; $output .= 'var data = new google.visualization.DataTable();' . PHP_EOL; foreach ( $this->getColumns() as $column ) { $output .= "data.addColumn('" . $column['type'] . "', '" . $column['value'] . "');" . PHP_EOL; } $output .= 'data.addRows(' . count($this->getRows()) . ');' . PHP_EOL; if( $numberFormat ){ $output .= "var formatter = new google.visualization.NumberFormat({prefix: 'R$', negativeColor: 'red', negativeParens: true});" . PHP_EOL; } foreach ( $this->getRows() as $key => $row ) { $output .= "data.setValue(" . $key . ", 0, '" . $row['legend'] . "');" . PHP_EOL; if( count($row['values']) > 0 && is_array($row['values']) ){ foreach ( $row['values'] as $keyRow => $rowValue ) { if( $rowValue == '' ){ $rowValue = 0; } $output .= "data.setValue(" . $key . ", " . ($keyRow + 1) . ", " . $rowValue . ");" . PHP_EOL; if( $numberFormat ){ $output .= "formatter.format(data, " . ($keyRow + 1) . ");" . PHP_EOL; } } } } $output .= "var chart = new google.visualization.ColumnChart(document.getElementById('" . $this->getIdentifier() . "'));" . PHP_EOL; $output .= "chart.draw(data, {width: " . $this->getWidth() . ", height: " . $this->getHeight() . ", title: '" . $this->getTitle() . "'," . PHP_EOL; $output .= "backgroundColor: 'transparent'," . PHP_EOL; $columns = $this->getColumns(); $output .= "hAxis: {title: '" . $columns[0]['value'] . "', titleTextStyle: {color: '" . $columns[0]['color'] . "'}}" . PHP_EOL; $output .= "});" . PHP_EOL; //finalize the function drawChart $output .= '}' . PHP_EOL; $this->setOutput($output); } public function drawPieChart() { if( !is_array($this->getColumns()) || !is_array($this->getRows()) ) return null; $output = 'function drawPieChart(){ '; $output .= 'var data = new google.visualization.DataTable();'; foreach ( $this->getColumns() as $column ) { $output .= "data.addColumn('" . $column['type'] . "', '" . $column['value'] . "');"; } $output .= 'data.addRows(' . count($this->getRows()) . ');'; $output .= "var formatter = new google.visualization.NumberFormat({prefix: '', negativeColor: 'red', negativeParens: true});"; foreach ( $this->getRows() as $key => $row ) { $output .= "data.setValue(" . $key . ", 0, '" . $row['legend'] . "');"; foreach ( $row['values'] as $keyRow => $rowValue ) { if( $rowValue == '' ){ $rowValue = 0; } $output .= "data.setValue(" . $key . ", " . ($keyRow + 1) . ", " . $rowValue . ");"; $output .= "formatter.format(data, " . ($keyRow + 1) . ");"; } } $output .= "var chart = new google.visualization.PieChart(document.getElementById('" . $this->getIdentifier() . "'));"; $output .= "chart.draw(data, {width: " . $this->getWidth() . ", height: " . $this->getHeight() . ", title: '" . $this->getTitle() . "',"; $output .= "backgroundColor: 'transparent',"; $columns = $this->getColumns(); $output .= "hAxis: {title: '" . $columns[0]['value'] . "', titleTextStyle: {color: '" . $columns[0]['color'] . "'}}"; $output .= "});"; //finalize the function drawChart $output .= '}'; $this->setOutput($output); } public function drawLineChart() { if( !is_array($this->getColumns()) || !is_array($this->getRows()) ) return null; $output = 'function drawLineChart(){ '; $output .= 'var data = new google.visualization.DataTable();'; foreach ( $this->getColumns() as $column ) { $output .= "data.addColumn('" . $column['type'] . "', '" . $column['value'] . "');"; } $output .= 'data.addRows(' . count($this->getRows()) . ');'; $output .= "var formatter = new google.visualization.NumberFormat({prefix: 'R$', negativeColor: 'red', negativeParens: true});"; foreach ( $this->getRows() as $key => $row ) { $output .= "data.setValue(" . $key . ", 0, '" . $row['legend'] . "');"; foreach ( $row['values'] as $keyRow => $rowValue ) { if( $rowValue == '' ){ $rowValue = 0; } $output .= "data.setValue(" . $key . ", " . ($keyRow + 1) . ", " . $rowValue . ");"; $output .= "formatter.format(data, " . ($keyRow + 1) . ");"; } } $output .= "var chart = new google.visualization.LineChart(document.getElementById('" . $this->getIdentifier() . "'));"; // $output .= "table.draw(data, {allowHtml: true, showRowNumber: true});"; $output .= "chart.draw(data, {width: " . $this->getWidth() . ", height: " . $this->getHeight() . ", title: '" . $this->getTitle() . "',"; $output .= "backgroundColor: 'transparent',"; $output .= "pointSize: 5, visibleInLegend: false,"; $columns = $this->getColumns(); $output .= "hAxis: {title: '" . $columns[0]['value'] . "', titleTextStyle: {color: '" . $columns[0]['color'] . "'}}"; $output .= "});"; //finalize the function drawChart $output .= '}'; $this->setOutput($output); } }
💾 保存文件
← 返回文件管理器