Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //lebih baik use dibawah digunakan semua di header controllernya
- use PhpOffice\PhpSpreadsheet\Chart\Chart;
- use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
- use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
- use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend;
- use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
- use PhpOffice\PhpSpreadsheet\Chart\Properties;
- use PhpOffice\PhpSpreadsheet\Chart\Title;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use PhpOffice\PhpSpreadsheet\Settings;
- use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
- /**INFO
- membuat Chart Sederhana (Clean)
- khusus laravel
- **/
- //anggap saja langsung method nya
- function exportXls(){
- $spreadsheet = new Spreadsheet();
- $worksheet = $spreadsheet->getActiveSheet();
- //silahkan ditambahkan
- $dataGraph =[
- ['', 2010, 2011, 2012, 2013],
- ['Q1', 12, 15, 21,25],
- ['Q2', 56, 73, 86, 80],
- ['Q3', 52, 61, 69,71],
- ['Q4', 30, 32, 0, 0],
- ['Q5', 40, 82, 60, 60]
- ]
- ;
- $totalData = count($dataGraph);
- //Asumsikan kamu tahu posisinya
- $worksheet->fromArray(
- $dataGraph
- );
- // Set the Labels for each data series we want to plot
- // Datatype
- // Cell reference for data
- // Format Code
- // Number of datapoints in series
- // Data values
- // Data Marker
- $dataSeriesLabels=[];
- //B:66
- for($i=1;$i<count($dataGraph[0]); $i++){
- $pos=$i+65;
- $posTitle = chr( $pos );
- $dataSeriesLabels[]= new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING,
- 'Worksheet!$'. $posTitle.'$1', null, 1);
- }
- $dataSeriesLabels[0]->setFillColor('FF0000');
- // Set the X-Axis Labels
- // Datatype
- // Cell reference for data
- // Format Code
- // Number of datapoints in series
- // Data values
- // Data Marker
- $xAxisTickValues = [
- new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING,
- 'Worksheet!$A$2:$A$'.$totalData, null, 4), // Q1 to Q(last)
- ];
- // Set the Data values for each data series we want to plot
- // Datatype
- // Cell reference for data
- // Format Code
- // Number of datapoints in series
- // Data values
- // Data Marker
- $dataSeriesValues=[];
- for($i=1;$i<count($dataGraph[0]); $i++){
- $pos=$i+65;
- $posTitle = chr( $pos );
- $dataSeriesValues[]= new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER,
- 'Worksheet!$'.$posTitle.'$2:$'.$posTitle.'$'.$totalData, null, 4);
- }
- $dataSeriesValues[2]->setLineWidth(60000 / Properties::POINTS_WIDTH_MULTIPLIER);
- // Build the dataseries
- $series = new DataSeries(
- DataSeries::TYPE_LINECHART, // plotType
- null, // plotGrouping, was DataSeries::GROUPING_STACKED, not a usual choice for line chart
- range(0, count($dataSeriesValues) - 1), // plotOrder
- $dataSeriesLabels, // plotLabel
- $xAxisTickValues, // plotCategory
- $dataSeriesValues // plotValues
- );
- // Set the series in the plot area
- $plotArea = new PlotArea(null, [$series]);
- // Set the chart legend
- $legend = new ChartLegend(ChartLegend::POSITION_TOPRIGHT, null, false);
- $title = new Title('Test Line Chart');
- $yAxisLabel = new Title('Value ($k)');
- // Create the chart
- $chart = new Chart(
- 'chart1', // name
- $title, // title
- $legend, // legend
- $plotArea, // plotArea
- true, // plotVisibleOnly
- 'gap', // displayBlanksAs
- null, // xAxisLabel
- $yAxisLabel // yAxisLabel
- );
- // Set the position where the chart should appear in the worksheet
- $chart->setTopLeftPosition('A'.($totalData + 3) );
- $chart->setBottomRightPosition('M'.($totalData+15) );
- // Add the chart to the worksheet
- $worksheet->addChart($chart);
- $writer = new Xlsx($spreadsheet);
- $writer->setIncludeCharts(true);
- $writer->save(__DIR__.'/example.xlsx');
- //download
- header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
- header('Content-Disposition: attachment;filename="contoh'.date("Ymdhis").'.xlsx"');
- header('Cache-Control: max-age=0');
- // If you're serving to IE 9, then the following may be needed
- header('Cache-Control: max-age=1');
- // If you're serving to IE over SSL, then the following may be needed
- header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
- header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
- header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
- header('Pragma: public'); // HTTP/1.0
- $txt = file_get_contents( __DIR__.'/example.xlsx' );
- unlink( __DIR__.'/example.xlsx' );
- echo $txt;
- }
- exportXls();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement