Advertisement
kura2yamato

contoh excel graph laravel

Jul 26th, 2021
1,108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.73 KB | None | 0 0
  1. <?php  
  2.     function contoh002()
  3.     {
  4.         $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
  5.         $worksheet = $spreadsheet->getActiveSheet();
  6.         $worksheet->fromArray(
  7.             [
  8.                 ['', 2010, 2011, 2012],
  9.                 ['Q1', 12, 15, 21],
  10.                 ['Q2', 56, 73, 86],
  11.                 ['Q3', 52, 61, 69],
  12.                 ['Q4', 30, 32, 0],
  13.             ]
  14.         );
  15.        
  16.         // Set the Labels for each data series we want to plot
  17.     //     Datatype
  18.     //     Cell reference for data
  19.     //     Format Code
  20.     //     Number of datapoints in series
  21.     //     Data values
  22.     //     Data Marker
  23.         $dataSeriesLabels = [
  24.             new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // 2010
  25.             new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011
  26.             new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // 2012
  27.         ];
  28.     // Set the X-Axis Labels
  29.     //     Datatype
  30.     //     Cell reference for data
  31.     //     Format Code
  32.     //     Number of datapoints in series
  33.     //     Data values
  34.     //     Data Marker
  35.         $xAxisTickValues = [
  36.             new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4
  37.         ];
  38.     // Set the Data values for each data series we want to plot
  39.     //     Datatype
  40.     //     Cell reference for data
  41.     //     Format Code
  42.     //     Number of datapoints in series
  43.     //     Data values
  44.     //     Data Marker
  45.         $dataSeriesValues = [
  46.             new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4),
  47.             new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
  48.             new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4),
  49.         ];
  50.  
  51.     // Build the dataseries
  52.         $series = new DataSeries(
  53.             DataSeries::TYPE_BARCHART, // plotType
  54.             DataSeries::GROUPING_CLUSTERED, // plotGrouping
  55.             range(0, count($dataSeriesValues) - 1), // plotOrder
  56.             $dataSeriesLabels, // plotLabel
  57.             $xAxisTickValues, // plotCategory
  58.             $dataSeriesValues        // plotValues
  59.         );
  60.     // Set additional dataseries parameters
  61.     //     Make it a horizontal bar rather than a vertical column graph
  62.         $series->setPlotDirection(DataSeries::DIRECTION_BAR);
  63.  
  64.     // Set the series in the plot area
  65.         $plotArea = new PlotArea(null, [$series]);
  66.     // Set the chart legend
  67.         $legend = new Legend(Legend::POSITION_RIGHT, null, false);
  68.  
  69.         $title = new Title('Test Bar Chart');
  70.         $yAxisLabel = new Title('Value ($k)');
  71.  
  72.     // Create the chart
  73.         $chart = new Chart(
  74.             'chart1', // name
  75.             $title, // title
  76.             $legend, // legend
  77.             $plotArea, // plotArea
  78.             true, // plotVisibleOnly
  79.             DataSeries::EMPTY_AS_GAP, // displayBlanksAs
  80.             null, // xAxisLabel
  81.             $yAxisLabel  // yAxisLabel
  82.         );
  83.  
  84.     // Set the position where the chart should appear in the worksheet
  85.         $chart->setTopLeftPosition('A7');
  86.         $chart->setBottomRightPosition('H20');
  87.  
  88.     // Add the chart to the worksheet
  89.         $worksheet->addChart($chart);
  90.        
  91.         header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  92.         header('Content-Disposition: attachment;filename="01simple.xlsx"');
  93.         header('Cache-Control: max-age=0');
  94.         // If you're serving to IE 9, then the following may be needed
  95.         header('Cache-Control: max-age=1');
  96.  
  97.         // If you're serving to IE over SSL, then the following may be needed
  98.         header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  99.         header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  100.         header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  101.         header('Pragma: public'); // HTTP/1.0
  102.         $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
  103.  
  104.         $writer->setIncludeCharts(true);
  105.         $writer->save('php://output');
  106.  
  107.     }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement