33_Chart_create_line.php 3.5 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 as ChartLegend;
  6. use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
  7. use PhpOffice\PhpSpreadsheet\Chart\Properties;
  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. $dataSeriesLabels = [
  31. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), // 2010
  32. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011
  33. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // 2012
  34. ];
  35. $dataSeriesLabels[0]->setFillColor('FF0000');
  36. // Set the X-Axis Labels
  37. // Datatype
  38. // Cell reference for data
  39. // Format Code
  40. // Number of datapoints in series
  41. // Data values
  42. // Data Marker
  43. $xAxisTickValues = [
  44. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4
  45. ];
  46. // Set the Data values for each data series we want to plot
  47. // Datatype
  48. // Cell reference for data
  49. // Format Code
  50. // Number of datapoints in series
  51. // Data values
  52. // Data Marker
  53. $dataSeriesValues = [
  54. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4),
  55. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
  56. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4),
  57. ];
  58. $dataSeriesValues[2]->setLineWidth(60000 / Properties::POINTS_WIDTH_MULTIPLIER);
  59. // Build the dataseries
  60. $series = new DataSeries(
  61. DataSeries::TYPE_LINECHART, // plotType
  62. DataSeries::GROUPING_STACKED, // plotGrouping
  63. range(0, count($dataSeriesValues) - 1), // plotOrder
  64. $dataSeriesLabels, // plotLabel
  65. $xAxisTickValues, // plotCategory
  66. $dataSeriesValues // plotValues
  67. );
  68. // Set the series in the plot area
  69. $plotArea = new PlotArea(null, [$series]);
  70. // Set the chart legend
  71. $legend = new ChartLegend(ChartLegend::POSITION_TOPRIGHT, null, false);
  72. $title = new Title('Test Stacked Line 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. DataSeries::EMPTY_AS_GAP, // 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);