Advertisement
kura2yamato

Excel010 Chart sederhana

May 31st, 2021
1,138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.43 KB | None | 0 0
  1. <?php  
  2.  
  3. use PhpOffice\PhpSpreadsheet\IOFactory;
  4.  
  5. use PhpOffice\PhpSpreadsheet\Chart\Chart;
  6. use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
  7. use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
  8. use PhpOffice\PhpSpreadsheet\Chart\Legend;
  9. use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
  10. use PhpOffice\PhpSpreadsheet\Chart\Title;
  11. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  12.  
  13. /*
  14. Bagian ini silahkan anggap tidak ada..
  15. karena dasarnya ini include composer (autoload.php)
  16. */
  17. {
  18.     $paths = [
  19.         __DIR__ . '/../vendor/autoload.php', // In case PhpSpreadsheet is cloned directly
  20.         __DIR__ . '/../../../autoload.php', // In case PhpSpreadsheet is a composer dependency.
  21.         __DIR__ . '/../../../web/example/vendor/autoload.php' //memakai ini
  22.     ];
  23.  
  24.     foreach ($paths as $path) {
  25.         if (file_exists($path)) {
  26.             require_once $path;
  27.             //echo "run:$path";
  28.             ;
  29.         }
  30.     }
  31.  
  32.  
  33.    
  34. }
  35.  
  36.  
  37. $callStartTime = microtime(true);
  38.  
  39.  
  40. $spreadsheet = new Spreadsheet();
  41. $worksheet = $spreadsheet->getActiveSheet();
  42. $worksheet->fromArray(
  43.     [
  44.         ['', 2010, 2011, 2012],
  45.         ['Q1', 12, 15, 21],
  46.         ['Q2', 56, 73, 86],
  47.         ['Q3', 52, 61, 69],
  48.         ['Q4', 30, 32, 0],
  49.     ]
  50. );
  51.  
  52. // Set the Labels for each data series we want to plot
  53. //     Datatype
  54. //     Cell reference for data
  55. //     Format Code
  56. //     Number of datapoints in series
  57. //     Data values
  58. //     Data Marker
  59. $dataSeriesLabels = [
  60.     new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // 2010
  61.     new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011
  62.     new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // 2012
  63. ];
  64. // Set the X-Axis Labels
  65. //     Datatype
  66. //     Cell reference for data
  67. //     Format Code
  68. //     Number of datapoints in series
  69. //     Data values
  70. //     Data Marker
  71. $xAxisTickValues = [
  72.     new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4
  73. ];
  74. // Set the Data values for each data series we want to plot
  75. //     Datatype
  76. //     Cell reference for data
  77. //     Format Code
  78. //     Number of datapoints in series
  79. //     Data values
  80. //     Data Marker
  81. $dataSeriesValues = [
  82.     new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4),
  83.     new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
  84.     new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4),
  85. ];
  86.  
  87. // Build the dataseries
  88. $series = new DataSeries(
  89.     DataSeries::TYPE_BARCHART, // plotType
  90.     DataSeries::GROUPING_CLUSTERED, // plotGrouping
  91.     range(0, count($dataSeriesValues) - 1), // plotOrder
  92.     $dataSeriesLabels, // plotLabel
  93.     $xAxisTickValues, // plotCategory
  94.     $dataSeriesValues        // plotValues
  95. );
  96. // Set additional dataseries parameters
  97. //     Make it a horizontal bar rather than a vertical column graph
  98. $series->setPlotDirection(DataSeries::DIRECTION_BAR);
  99.  
  100. // Set the series in the plot area
  101. $plotArea = new PlotArea(null, [$series]);
  102. // Set the chart legend
  103. $legend = new Legend(Legend::POSITION_RIGHT, null, false);
  104.  
  105. $title = new Title('Ini adalah contoh 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.     DataSeries::EMPTY_AS_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('A7');
  122. $chart->setBottomRightPosition('H20');
  123.  
  124. // Add the chart to the worksheet
  125. $worksheet->addChart($chart);
  126.  
  127. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  128.  
  129. $writer->setIncludeCharts(true);
  130.  
  131. /**
  132. ##INFO##
  133. {"title":"excel Chart ","detail":"Menghasilkan excel Chart. Ini adalah bonus coding yang bisa dipelajari"}
  134. ##INFO##
  135. **/
  136.  
  137.  
  138.  
  139. $file='chart010.xlsx';
  140.  
  141. $writer->save($file);
  142.  
  143. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  144. header('Content-Disposition: attachment;filename="'.$file.'"');
  145. header('Content-Length: ' . filesize($file));
  146.  
  147. header('Content-Transfer-Encoding: binary');
  148. header('Cache-Control: must-revalidate');
  149. header('Pragma: public');
  150.  
  151. ob_clean();
  152. flush();
  153.  
  154. $str=file_get_contents($file);
  155.  
  156. die($str);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement