33_Chart_create_pie.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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\Layout;
  6. use PhpOffice\PhpSpreadsheet\Chart\Legend;
  7. use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
  8. use PhpOffice\PhpSpreadsheet\Chart\Title;
  9. use PhpOffice\PhpSpreadsheet\IOFactory;
  10. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  11. require __DIR__ . '/../Header.php';
  12. $spreadsheet = new Spreadsheet();
  13. $worksheet = $spreadsheet->getActiveSheet();
  14. $worksheet->fromArray(
  15. [
  16. ['', 2010, 2011, 2012],
  17. ['Q1', 12, 15, 21],
  18. ['Q2', 56, 73, 86],
  19. ['Q3', 52, 61, 69],
  20. ['Q4', 30, 32, 0],
  21. ]
  22. );
  23. // Set the Labels for each data series we want to plot
  24. // Datatype
  25. // Cell reference for data
  26. // Format Code
  27. // Number of datapoints in series
  28. // Data values
  29. // Data Marker
  30. $dataSeriesLabels1 = [
  31. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011
  32. ];
  33. // Set the X-Axis Labels
  34. // Datatype
  35. // Cell reference for data
  36. // Format Code
  37. // Number of datapoints in series
  38. // Data values
  39. // Data Marker
  40. $xAxisTickValues1 = [
  41. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4
  42. ];
  43. // Set the Data values for each data series we want to plot
  44. // Datatype
  45. // Cell reference for data
  46. // Format Code
  47. // Number of datapoints in series
  48. // Data values
  49. // Data Marker
  50. $dataSeriesValues1 = [
  51. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
  52. ];
  53. // Build the dataseries
  54. $series1 = new DataSeries(
  55. DataSeries::TYPE_PIECHART, // plotType
  56. null, // plotGrouping (Pie charts don't have any grouping)
  57. range(0, count($dataSeriesValues1) - 1), // plotOrder
  58. $dataSeriesLabels1, // plotLabel
  59. $xAxisTickValues1, // plotCategory
  60. $dataSeriesValues1 // plotValues
  61. );
  62. // Set up a layout object for the Pie chart
  63. $layout1 = new Layout();
  64. $layout1->setShowVal(true);
  65. $layout1->setShowPercent(true);
  66. // Set the series in the plot area
  67. $plotArea1 = new PlotArea($layout1, [$series1]);
  68. // Set the chart legend
  69. $legend1 = new Legend(Legend::POSITION_RIGHT, null, false);
  70. $title1 = new Title('Test Pie Chart');
  71. // Create the chart
  72. $chart1 = new Chart(
  73. 'chart1', // name
  74. $title1, // title
  75. $legend1, // legend
  76. $plotArea1, // plotArea
  77. true, // plotVisibleOnly
  78. 0, // displayBlanksAs
  79. null, // xAxisLabel
  80. null // yAxisLabel - Pie charts don't have a Y-Axis
  81. );
  82. // Set the position where the chart should appear in the worksheet
  83. $chart1->setTopLeftPosition('A7');
  84. $chart1->setBottomRightPosition('H20');
  85. // Add the chart to the worksheet
  86. $worksheet->addChart($chart1);
  87. // Set the Labels for each data series we want to plot
  88. // Datatype
  89. // Cell reference for data
  90. // Format Code
  91. // Number of datapoints in series
  92. // Data values
  93. // Data Marker
  94. $dataSeriesLabels2 = [
  95. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011
  96. ];
  97. // Set the X-Axis Labels
  98. // Datatype
  99. // Cell reference for data
  100. // Format Code
  101. // Number of datapoints in series
  102. // Data values
  103. // Data Marker
  104. $xAxisTickValues2 = [
  105. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4
  106. ];
  107. // Set the Data values for each data series we want to plot
  108. // Datatype
  109. // Cell reference for data
  110. // Format Code
  111. // Number of datapoints in series
  112. // Data values
  113. // Data Marker
  114. $dataSeriesValues2 = [
  115. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
  116. ];
  117. // Build the dataseries
  118. $series2 = new DataSeries(
  119. DataSeries::TYPE_DONUTCHART, // plotType
  120. null, // plotGrouping (Donut charts don't have any grouping)
  121. range(0, count($dataSeriesValues2) - 1), // plotOrder
  122. $dataSeriesLabels2, // plotLabel
  123. $xAxisTickValues2, // plotCategory
  124. $dataSeriesValues2 // plotValues
  125. );
  126. // Set up a layout object for the Pie chart
  127. $layout2 = new Layout();
  128. $layout2->setShowVal(true);
  129. $layout2->setShowCatName(true);
  130. // Set the series in the plot area
  131. $plotArea2 = new PlotArea($layout2, [$series2]);
  132. $title2 = new Title('Test Donut Chart');
  133. // Create the chart
  134. $chart2 = new Chart(
  135. 'chart2', // name
  136. $title2, // title
  137. null, // legend
  138. $plotArea2, // plotArea
  139. true, // plotVisibleOnly
  140. 0, // displayBlanksAs
  141. null, // xAxisLabel
  142. null // yAxisLabel - Like Pie charts, Donut charts don't have a Y-Axis
  143. );
  144. // Set the position where the chart should appear in the worksheet
  145. $chart2->setTopLeftPosition('I7');
  146. $chart2->setBottomRightPosition('P20');
  147. // Add the chart to the worksheet
  148. $worksheet->addChart($chart2);
  149. // Save Excel 2007 file
  150. $filename = $helper->getFilename(__FILE__);
  151. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  152. $writer->setIncludeCharts(true);
  153. $callStartTime = microtime(true);
  154. $writer->save($filename);
  155. $helper->logWrite($writer, $filename, $callStartTime);