33_Chart_create_column.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_BARCHART, // plotType
  59. DataSeries::GROUPING_STANDARD, // plotGrouping
  60. range(0, count($dataSeriesValues) - 1), // plotOrder
  61. $dataSeriesLabels, // plotLabel
  62. $xAxisTickValues, // plotCategory
  63. $dataSeriesValues // plotValues
  64. );
  65. // Set additional dataseries parameters
  66. // Make it a vertical column rather than a horizontal bar graph
  67. $series->setPlotDirection(DataSeries::DIRECTION_COL);
  68. // Set the series in the plot area
  69. $plotArea = new PlotArea(null, [$series]);
  70. // Set the chart legend
  71. $legend = new Legend(Legend::POSITION_RIGHT, null, false);
  72. $title = new Title('Test Column Chart');
  73. $yAxisLabel = new Title('Value ($k)');
  74. // Create the chart
  75. $chart = new Chart(
  76. 'chart1', // name
  77. $title, // title
  78. $legend, // legend
  79. $plotArea, // plotArea
  80. true, // plotVisibleOnly
  81. 0, // displayBlanksAs
  82. null, // xAxisLabel
  83. $yAxisLabel // yAxisLabel
  84. );
  85. // Set the position where the chart should appear in the worksheet
  86. $chart->setTopLeftPosition('A7');
  87. $chart->setBottomRightPosition('H20');
  88. // Add the chart to the worksheet
  89. $worksheet->addChart($chart);
  90. // Save Excel 2007 file
  91. $filename = $helper->getFilename(__FILE__);
  92. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  93. $writer->setIncludeCharts(true);
  94. $callStartTime = microtime(true);
  95. $writer->save($filename);
  96. $helper->logWrite($writer, $filename, $callStartTime);