33_Chart_create_scatter.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. $xAxisTickValues = [
  36. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4
  37. ];
  38. // Set the Data values for each data series we want to plot
  39. // Datatype
  40. // Cell reference for data
  41. // Format Code
  42. // Number of datapoints in series
  43. // Data values
  44. // Data Marker
  45. $dataSeriesValues = [
  46. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4),
  47. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
  48. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4),
  49. ];
  50. // Build the dataseries
  51. $series = new DataSeries(
  52. DataSeries::TYPE_SCATTERCHART, // plotType
  53. null, // plotGrouping (Scatter charts don't have any grouping)
  54. range(0, count($dataSeriesValues) - 1), // plotOrder
  55. $dataSeriesLabels, // plotLabel
  56. $xAxisTickValues, // plotCategory
  57. $dataSeriesValues, // plotValues
  58. null, // plotDirection
  59. null, // smooth line
  60. DataSeries::STYLE_LINEMARKER // plotStyle
  61. );
  62. // Set the series in the plot area
  63. $plotArea = new PlotArea(null, [$series]);
  64. // Set the chart legend
  65. $legend = new Legend(Legend::POSITION_TOPRIGHT, null, false);
  66. $title = new Title('Test Scatter Chart');
  67. $yAxisLabel = new Title('Value ($k)');
  68. // Create the chart
  69. $chart = new Chart(
  70. 'chart1', // name
  71. $title, // title
  72. $legend, // legend
  73. $plotArea, // plotArea
  74. true, // plotVisibleOnly
  75. 0, // displayBlanksAs
  76. null, // xAxisLabel
  77. $yAxisLabel // yAxisLabel
  78. );
  79. // Set the position where the chart should appear in the worksheet
  80. $chart->setTopLeftPosition('A7');
  81. $chart->setBottomRightPosition('H20');
  82. // Add the chart to the worksheet
  83. $worksheet->addChart($chart);
  84. // Save Excel 2007 file
  85. $filename = $helper->getFilename(__FILE__);
  86. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  87. $writer->setIncludeCharts(true);
  88. $callStartTime = microtime(true);
  89. $writer->save($filename);
  90. $helper->logWrite($writer, $filename, $callStartTime);