Advertisement
kura2yamato

contoh excel graph laravel

Jul 17th, 2023
888
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.45 KB | Help | 0 0
  1. <?php
  2. //lebih baik use dibawah digunakan semua di header controllernya
  3. use PhpOffice\PhpSpreadsheet\Chart\Chart;
  4. use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
  5. use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
  6. use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend;
  7. use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
  8. use PhpOffice\PhpSpreadsheet\Chart\Properties;
  9. use PhpOffice\PhpSpreadsheet\Chart\Title;
  10. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  11. use PhpOffice\PhpSpreadsheet\Settings;
  12.  
  13. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  14. /**INFO
  15. membuat Chart Sederhana (Clean)
  16. khusus laravel
  17. **/
  18.  
  19. //anggap saja langsung method nya
  20. function exportXls(){
  21.  
  22.     $spreadsheet = new Spreadsheet();
  23.     $worksheet = $spreadsheet->getActiveSheet();
  24. //silahkan ditambahkan
  25.     $dataGraph =[
  26.         ['', 2010, 2011, 2012, 2013],
  27.         ['Q1', 12, 15, 21,25],
  28.         ['Q2', 56, 73, 86, 80],
  29.         ['Q3', 52, 61, 69,71],
  30.         ['Q4', 30, 32, 0, 0],
  31.         ['Q5', 40, 82, 60, 60]
  32.     ]
  33.     ;
  34.  
  35.     $totalData = count($dataGraph);
  36.  
  37.     //Asumsikan kamu tahu posisinya
  38.     $worksheet->fromArray(
  39.          $dataGraph  
  40.     );
  41.  
  42.     // Set the Labels for each data series we want to plot
  43.     //     Datatype
  44.     //     Cell reference for data
  45.     //     Format Code
  46.     //     Number of datapoints in series
  47.     //     Data values
  48.     //     Data Marker
  49.     $dataSeriesLabels=[];
  50. //B:66
  51.     for($i=1;$i<count($dataGraph[0]); $i++){
  52.         $pos=$i+65;
  53.         $posTitle = chr( $pos );
  54.         $dataSeriesLabels[]= new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING,
  55.         'Worksheet!$'. $posTitle.'$1', null, 1);
  56.     }
  57.  
  58.     $dataSeriesLabels[0]->setFillColor('FF0000');
  59.     // Set the X-Axis Labels
  60.     //     Datatype
  61.     //     Cell reference for data
  62.     //     Format Code
  63.     //     Number of datapoints in series
  64.     //     Data values
  65.     //     Data Marker
  66.     $xAxisTickValues = [
  67.         new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING,
  68.         'Worksheet!$A$2:$A$'.$totalData, null, 4), // Q1 to Q(last)
  69.     ];
  70.     // Set the Data values for each data series we want to plot
  71.     //     Datatype
  72.     //     Cell reference for data
  73.     //     Format Code
  74.     //     Number of datapoints in series
  75.     //     Data values
  76.     //     Data Marker
  77.     $dataSeriesValues=[];
  78.     for($i=1;$i<count($dataGraph[0]); $i++){
  79.         $pos=$i+65;
  80.         $posTitle = chr( $pos );
  81.         $dataSeriesValues[]= new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER,
  82.         'Worksheet!$'.$posTitle.'$2:$'.$posTitle.'$'.$totalData, null, 4);
  83.     }
  84.    
  85.     $dataSeriesValues[2]->setLineWidth(60000 / Properties::POINTS_WIDTH_MULTIPLIER);
  86.  
  87.     // Build the dataseries
  88.     $series = new DataSeries(
  89.         DataSeries::TYPE_LINECHART, // plotType
  90.         null, // plotGrouping, was DataSeries::GROUPING_STACKED, not a usual choice for line chart
  91.         range(0, count($dataSeriesValues) - 1), // plotOrder
  92.         $dataSeriesLabels, // plotLabel
  93.         $xAxisTickValues, // plotCategory
  94.         $dataSeriesValues        // plotValues
  95.     );
  96.  
  97.     // Set the series in the plot area
  98.     $plotArea = new PlotArea(null, [$series]);
  99.     // Set the chart legend
  100.     $legend = new ChartLegend(ChartLegend::POSITION_TOPRIGHT, null, false);
  101.  
  102.     $title = new Title('Test Line Chart');
  103.     $yAxisLabel = new Title('Value ($k)');
  104.  
  105.     // Create the chart
  106.     $chart = new Chart(
  107.         'chart1', // name
  108.         $title, // title
  109.         $legend, // legend
  110.         $plotArea, // plotArea
  111.         true, // plotVisibleOnly
  112.         'gap',  // displayBlanksAs
  113.         null, // xAxisLabel
  114.         $yAxisLabel  // yAxisLabel
  115.     );
  116.  
  117.     // Set the position where the chart should appear in the worksheet
  118.     $chart->setTopLeftPosition('A'.($totalData + 3) );
  119.     $chart->setBottomRightPosition('M'.($totalData+15) );
  120.     // Add the chart to the worksheet
  121.     $worksheet->addChart($chart);
  122.  
  123.  
  124.     $writer = new Xlsx($spreadsheet);
  125.     $writer->setIncludeCharts(true);
  126.     $writer->save(__DIR__.'/example.xlsx');
  127. //download
  128.     header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  129.     header('Content-Disposition: attachment;filename="contoh'.date("Ymdhis").'.xlsx"');
  130.     header('Cache-Control: max-age=0');
  131.     // If you're serving to IE 9, then the following may be needed
  132.     header('Cache-Control: max-age=1');
  133.  
  134.     // If you're serving to IE over SSL, then the following may be needed
  135.     header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  136.     header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  137.     header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  138.     header('Pragma: public'); // HTTP/1.0
  139.    
  140.     $txt = file_get_contents( __DIR__.'/example.xlsx' );
  141.     unlink( __DIR__.'/example.xlsx' );
  142.     echo $txt;
  143.    
  144. }
  145.  
  146. exportXls();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement