33_Chart_create_line.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. $dataSeriesValues[2]->setLineWidth(60000);
  57. // Build the dataseries
  58. $series = new DataSeries(
  59. DataSeries::TYPE_LINECHART, // plotType
  60. DataSeries::GROUPING_STACKED, // plotGrouping
  61. range(0, count($dataSeriesValues) - 1), // plotOrder
  62. $dataSeriesLabels, // plotLabel
  63. $xAxisTickValues, // plotCategory
  64. $dataSeriesValues // plotValues
  65. );
  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_TOPRIGHT, null, false);
  70. $title = new Title('Test Stacked Line 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. // Save Excel 2007 file
  89. $filename = $helper->getFilename(__FILE__);
  90. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  91. $writer->setIncludeCharts(true);
  92. $callStartTime = microtime(true);
  93. $writer->save($filename);
  94. $helper->logWrite($writer, $filename, $callStartTime);