33_Chart_create_bubble.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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\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. ['Number of Products', 'Sales in USD', 'Market share'],
  16. [14, 12200, 15],
  17. [20, 60000, 33],
  18. [18, 24400, 10],
  19. [22, 32000, 42],
  20. [],
  21. [12, 8200, 18],
  22. [15, 50000, 30],
  23. [19, 22400, 15],
  24. [25, 25000, 50],
  25. ]
  26. );
  27. // Set the Labels for each data series we want to plot
  28. // Datatype
  29. // Cell reference for data
  30. // Format Code
  31. // Number of datapoints in series
  32. // Data values
  33. // Data Marker
  34. $dataSeriesLabels = [
  35. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, null, null, 1, ['2013']), // 2013
  36. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, null, null, 1, ['2014']), // 2014
  37. ];
  38. // Set the X-Axis values
  39. // Datatype
  40. // Cell reference for data
  41. // Format Code
  42. // Number of datapoints in series
  43. // Data values
  44. // Data Marker
  45. $dataSeriesCategories = [
  46. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$A$2:$A$5', null, 4),
  47. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$A$7:$A$10', null, 4),
  48. ];
  49. // Set the Y-Axis values
  50. // Datatype
  51. // Cell reference for data
  52. // Format Code
  53. // Number of datapoints in series
  54. // Data values
  55. // Data Marker
  56. $dataSeriesValues = [
  57. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4),
  58. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$7:$B$10', null, 4),
  59. ];
  60. // Set the Z-Axis values (bubble size)
  61. // Datatype
  62. // Cell reference for data
  63. // Format Code
  64. // Number of datapoints in series
  65. // Data values
  66. // Data Marker
  67. $dataSeriesBubbles = [
  68. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
  69. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$7:$C$10', null, 4),
  70. ];
  71. // Build the dataseries
  72. $series = new DataSeries(
  73. DataSeries::TYPE_BUBBLECHART, // plotType
  74. null, // plotGrouping
  75. range(0, count($dataSeriesValues) - 1), // plotOrder
  76. $dataSeriesLabels, // plotLabel
  77. $dataSeriesCategories, // plotCategory
  78. $dataSeriesValues // plotValues
  79. );
  80. $series->setPlotBubbleSizes($dataSeriesBubbles);
  81. // Set the series in the plot area
  82. $plotArea = new PlotArea();
  83. $plotArea->setPlotSeries([$series]);
  84. // Set the chart legend
  85. $legend = new ChartLegend(ChartLegend::POSITION_RIGHT, null, false);
  86. // Create the chart
  87. $chart = new Chart(
  88. 'chart1', // name
  89. null, // title
  90. $legend, // legend
  91. $plotArea, // plotArea
  92. true, // plotVisibleOnly
  93. DataSeries::EMPTY_AS_GAP, // displayBlanksAs
  94. null, // xAxisLabel
  95. null // yAxisLabel
  96. );
  97. // Set the position where the chart should appear in the worksheet
  98. $chart->setTopLeftPosition('E1');
  99. $chart->setBottomRightPosition('M15');
  100. // Add the chart to the worksheet
  101. $worksheet->addChart($chart);
  102. $worksheet->getColumnDimension('A')->setAutoSize(true);
  103. $worksheet->getColumnDimension('B')->setAutoSize(true);
  104. $worksheet->getColumnDimension('C')->setAutoSize(true);
  105. // Save Excel 2007 file
  106. $filename = $helper->getFilename(__FILE__);
  107. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  108. $writer->setIncludeCharts(true);
  109. $callStartTime = microtime(true);
  110. $writer->save($filename);
  111. $helper->logWrite($writer, $filename, $callStartTime);