chartSpreadsheet.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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\Spreadsheet;
  9. $spreadsheet = new Spreadsheet();
  10. $worksheet = $spreadsheet->getActiveSheet();
  11. $worksheet->fromArray(
  12. [
  13. ['', 2010, 2011, 2012],
  14. ['Q1', 12, 15, 21],
  15. ['Q2', 56, 73, 86],
  16. ['Q3', 52, 61, 69],
  17. ['Q4', 30, 32, 0],
  18. ]
  19. );
  20. // Set the Labels for each data series we want to plot
  21. // Datatype
  22. // Cell reference for data
  23. // Format Code
  24. // Number of datapoints in series
  25. // Data values
  26. // Data Marker
  27. $dataSeriesLabels = [
  28. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // 2010
  29. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011
  30. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // 2012
  31. ];
  32. // Set the X-Axis Labels
  33. // Datatype
  34. // Cell reference for data
  35. // Format Code
  36. // Number of datapoints in series
  37. // Data values
  38. // Data Marker
  39. $xAxisTickValues = [
  40. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4
  41. ];
  42. // Set the Data values 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. $dataSeriesValues = [
  50. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4),
  51. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
  52. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4),
  53. ];
  54. // Build the dataseries
  55. $series = new DataSeries(
  56. DataSeries::TYPE_BARCHART, // plotType
  57. DataSeries::GROUPING_CLUSTERED, // plotGrouping
  58. range(0, count($dataSeriesValues) - 1), // plotOrder
  59. $dataSeriesLabels, // plotLabel
  60. $xAxisTickValues, // plotCategory
  61. $dataSeriesValues // plotValues
  62. );
  63. // Set additional dataseries parameters
  64. // Make it a horizontal bar rather than a vertical column graph
  65. $series->setPlotDirection(DataSeries::DIRECTION_BAR);
  66. // Set the series in the plot area
  67. $plotArea = new PlotArea(null, [$series]);
  68. // Set the chart legend
  69. $legend = new Legend(Legend::POSITION_RIGHT, null, false);
  70. $title = new Title('Test Bar Chart');
  71. $yAxisLabel = new Title('Value ($k)');
  72. // Create the chart
  73. $chart = new Chart(
  74. 'chart1', // name
  75. $title, // title
  76. $legend, // legend
  77. $plotArea, // plotArea
  78. true, // plotVisibleOnly
  79. 0, // displayBlanksAs
  80. null, // xAxisLabel
  81. $yAxisLabel // yAxisLabel
  82. );
  83. // Set the position where the chart should appear in the worksheet
  84. $chart->setTopLeftPosition('A7');
  85. $chart->setBottomRightPosition('H20');
  86. // Add the chart to the worksheet
  87. $worksheet->addChart($chart);
  88. return $spreadsheet;