Advertisement
kura2yamato

contoh excel graph laravel(?)

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