✏️ 正在编辑: GChart.php
路径:
/srv/systems_dir/yuppiecred-lidernegocios/library/Core/GChart.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php 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($tipoHeader = 0) { $output = $this->header($tipoHeader); $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($typeInitialize = 0) { if( $typeInitialize == 0 ){ return '<script type="text/javascript" src="' . Core_GChart::API_URI . '"></script>'; } elseif( $typeInitialize == 2 ){ $return = '<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>' . PHP_EOL; $return .= '<script type="text/javascript">' . PHP_EOL; $return .= "google.charts.load('current', {'packages':['corechart']}); " . PHP_EOL; $return .= "</script>" . PHP_EOL; return $return; } else { $return = '<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>' . PHP_EOL; $return .= '<script type="text/javascript">' . PHP_EOL; $return .= "google.charts.load('current', {'packages':['gauge'], 'language': 'de'}); " . PHP_EOL; $return .= "</script>" . PHP_EOL; return $return; } } protected function footer() { return '</script>'; } protected function header($tipo = 0) { $return = ''; if( $tipo == 0 ){ $return = '<script type="text/javascript">' . 'google.load("visualization", "1", {packages:["corechart"], "language": "pt-BR"});' . 'google.setOnLoadCallback(' . $this->getTypeChart() . ');' . PHP_EOL; } 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() . "',"; $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() { if( !is_array($this->getColumns()) || !is_array($this->getRows()) ) return null; $output = 'function drawColumnChart(){ '; $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()) . ');'; foreach ( $this->getRows() as $key => $row ) { $output .= "data.setValue(" . $key . ", 0, '" . $row['legend'] . "');"; foreach ( $row['values'] as $keyRow => $rowValue ) { $output .= "data.setValue(" . $key . ", " . ($keyRow + 1) . ", " . $rowValue . ");"; } } $output .= "var chart = new google.visualization.ColumnChart(document.getElementById('" . $this->getIdentifier() . "'));"; $output .= "chart.draw(data, {width: " . $this->getWidth() . ", height: " . $this->getHeight() . ", title: '" . $this->getTitle() . "'," . "legend: 'none',"; $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 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()) . ');'; foreach ( $this->getRows() as $key => $row ) { $legend = isset($row['legend']) && !empty($row['legend']) ? $row['legend'] : ""; $output .= "data.setValue(" . $key . ", 0, '" . $legend . "');"; foreach ( $row['values'] as $keyRow => $rowValue ) { $output .= "data.setValue(" . $key . ", " . ($keyRow + 1) . ", " . $rowValue . ");"; } } $output .= "var chart = new google.visualization.PieChart(document.getElementById('" . $this->getIdentifier() . "'));"; $output .= "chart.draw(data, {width: " . $this->getWidth() . ", height: " . $this->getHeight() . ", title: '" . $this->getTitle() . "', pieHole: 0.4, legend: 'none',"; $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()) . ');'; foreach ( $this->getRows() as $key => $row ) { $output .= "data.setValue(" . $key . ", 0, '" . $row['legend'] . "');"; foreach ( $row['values'] as $keyRow => $rowValue ) { $output .= "data.setValue(" . $key . ", " . ($keyRow + 1) . ", " . $rowValue . ");"; } } $output .= "var chart = new google.visualization.LineChart(document.getElementById('" . $this->getIdentifier() . "'));"; $output .= "chart.draw(data, {width: " . $this->getWidth() . ", height: " . $this->getHeight() . ", title: '" . $this->getTitle() . "'," . "legend: 'none',"; $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 drawGaugeChart($options = array(), $generateOtherMaximuns = false) { $redTo = 10; $yellowFrom = 20; $redFrom = 0; $greenTo = 100; if( !is_array($this->getRows()) ) return null; $output = '<script type="text/javascript">' . PHP_EOL; $output .= "google.charts.setOnLoadCallback(drawGaugeChart);" . PHP_EOL; $output .= 'function drawGaugeChart(){ ' . PHP_EOL; $rows = ""; $row = $this->getRows(); if( $generateOtherMaximuns == true ){ $greenTo = ($options['max'] * 90 / 100); $redTo = ($options['max'] * 10 / 100); $yellowFrom = ($options['max'] * 20 / 100); $rows .= "['{$row['legend']}', {$row['value']}]"; if( count($this->getRows()) > $row['cont'] ){ $rows .= ", "; } $rows .= PHP_EOL; } else { foreach ( $this->getRows() as $key => $row ) { $greenTo = ($options['max'] * 90 / 100); $redTo = ($options['max'] * 10 / 100); $yellowFrom = ($options['max'] * 20 / 100); $rows .= "['{$row['legend']}', {$row['value']}]"; if( count($this->getRows()) > $key ){ $rows .= ", "; } $rows .= PHP_EOL; } } $output .= 'var data = google.visualization.arrayToDataTable([' . "['Label', 'Value'], " . PHP_EOL . $rows . ']);' . PHP_EOL; $output .= "var chart = new google.visualization.Gauge(document.getElementById('" . $this->getIdentifier() . "'));" . PHP_EOL; $output .= "var options = { width: " . $this->getWidth() . ", height: " . $this->getHeight() . ", greenFrom:{$greenTo}, greenTo: {$options['max']}, redFrom: {$redFrom}, redTo: {$redTo}, yellowFrom:{$redTo}, yellowTo: {$yellowFrom}, minorTicks: 5, " . (isset($options['max']) ? "max: {$options['max']} " : "" ) . " };" . PHP_EOL; $output .= "chart.draw(data, options); " . PHP_EOL; //finalize the function drawChart $output .= '}' . PHP_EOL; $this->setOutput($output); } }
💾 保存文件
← 返回文件管理器