33_Chart_create_area.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Chart\Chart;
  3. use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
  4. use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
  5. use PhpOffice\PhpSpreadsheet\Chart\Legend;
  6. use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
  7. use PhpOffice\PhpSpreadsheet\Chart\Title;
  8. use PhpOffice\PhpSpreadsheet\IOFactory;
  9. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  10. require __DIR__ . '/../Header.php';
  11. $spreadsheet = new Spreadsheet();
  12. $worksheet = $spreadsheet->getActiveSheet();
  13. $worksheet->fromArray(
  14. [
  15. ['', 2010, 2011, 2012],
  16. ['Q1', 12, 15, 21],
  17. ['Q2', 56, 73, 86],
  18. ['Q3', 52, 61, 69],
  19. ['Q4', 30, 32, 0],
  20. ]
  21. );
  22. // Set the Labels for each data series we want to plot
  23. // Datatype
  24. // Cell reference for data
  25. // Format Code
  26. // Number of datapoints in series
  27. // Data values
  28. // Data Marker
  29. $dataSeriesLabels = [
  30. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // 2010
  31. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011
  32. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // 2012
  33. ];
  34. // Set the X-Axis Labels
  35. // Datatype
  36. // Cell reference for data
  37. // Format Code
  38. // Number of datapoints in series
  39. // Data values
  40. // Data Marker
  41. $xAxisTickValues = [
  42. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4
  43. ];
  44. // Set the Data values for each data series we want to plot
  45. // Datatype
  46. // Cell reference for data
  47. // Format Code
  48. // Number of datapoints in series
  49. // Data values
  50. // Data Marker
  51. $dataSeriesValues = [
  52. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4),
  53. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
  54. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4),
  55. ];
  56. // Build the dataseries
  57. $series = new DataSeries(
  58. DataSeries::TYPE_AREACHART, // plotType
  59. DataSeries::GROUPING_PERCENT_STACKED, // plotGrouping
  60. range(0, count($dataSeriesValues) - 1), // plotOrder
  61. $dataSeriesLabels, // plotLabel
  62. $xAxisTickValues, // plotCategory
  63. $dataSeriesValues // plotValues
  64. );
  65. // Set the series in the plot area
  66. $plotArea = new PlotArea(null, [$series]);
  67. // Set the chart legend
  68. $legend = new Legend(Legend::POSITION_TOPRIGHT, null, false);
  69. $title = new Title('Test %age-Stacked Area Chart');
  70. $yAxisLabel = new Title('Value ($k)');
  71. // Create the chart
  72. $chart = new Chart(
  73. 'chart1', // name
  74. $title, // title
  75. $legend, // legend
  76. $plotArea, // plotArea
  77. true, // plotVisibleOnly
  78. 0, // displayBlanksAs
  79. null, // xAxisLabel
  80. $yAxisLabel // yAxisLabel
  81. );
  82. // Set the position where the chart should appear in the worksheet
  83. $chart->setTopLeftPosition('A7');
  84. $chart->setBottomRightPosition('H20');
  85. // Add the chart to the worksheet
  86. $worksheet->addChart($chart);
  87. // Save Excel 2007 file
  88. $filename = $helper->getFilename(__FILE__);
  89. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  90. $writer->setIncludeCharts(true);
  91. $callStartTime = microtime(true);
  92. $writer->save($filename);
  93. $helper->logWrite($writer, $filename, $callStartTime);