33_Chart_create_scatter4.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Chart\Chart;
  3. use PhpOffice\PhpSpreadsheet\Chart\ChartColor;
  4. use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
  5. use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
  6. use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend;
  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. $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. // Set the X-Axis Labels
  36. $xAxisTickValues = [
  37. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$5', null, 4), // Q1 to Q4
  38. ];
  39. // Set the Data values for each data series we want to plot
  40. // Datatype
  41. // Cell reference for data
  42. // Format Code
  43. // Number of datapoints in series
  44. // Data values
  45. // Data Marker
  46. $dataSeriesValues = [
  47. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$5', null, 4),
  48. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$5', null, 4),
  49. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$5', null, 4),
  50. ];
  51. // Build the dataseries
  52. $series = new DataSeries(
  53. DataSeries::TYPE_SCATTERCHART, // plotType
  54. null, // plotGrouping (Scatter charts don't have any grouping)
  55. range(0, count($dataSeriesValues) - 1), // plotOrder
  56. $dataSeriesLabels, // plotLabel
  57. $xAxisTickValues, // plotCategory
  58. $dataSeriesValues, // plotValues
  59. null, // plotDirection
  60. null, // smooth line
  61. DataSeries::STYLE_LINEMARKER // plotStyle
  62. );
  63. // Set the series in the plot area
  64. $plotArea = new PlotArea(null, [$series]);
  65. $pos1 = 0; // pos = 0% (extreme low side or lower left corner)
  66. $brightness1 = 0; // 0%
  67. $gsColor1 = new ChartColor();
  68. $gsColor1->setColorProperties('FF0000', 75, 'srgbClr', $brightness1); // red
  69. $gradientStop1 = [$pos1, $gsColor1];
  70. $pos2 = 0.5; // pos = 50% (middle)
  71. $brightness2 = 0.5; // 50%
  72. $gsColor2 = new ChartColor();
  73. $gsColor2->setColorProperties('FFFF00', 50, 'srgbClr', $brightness2); // yellow
  74. $gradientStop2 = [$pos2, $gsColor2];
  75. $pos3 = 1.0; // pos = 100% (extreme high side or upper right corner)
  76. $brightness3 = 0.5; // 50%
  77. $gsColor3 = new ChartColor();
  78. $gsColor3->setColorProperties('00B050', 50, 'srgbClr', $brightness3); // green
  79. $gradientStop3 = [$pos3, $gsColor3];
  80. $gradientFillStops = [
  81. $gradientStop1,
  82. $gradientStop2,
  83. $gradientStop3,
  84. ];
  85. $gradientFillAngle = 315.0; // 45deg above horiz
  86. $plotArea->setGradientFillProperties($gradientFillStops, $gradientFillAngle);
  87. // Set the chart legend
  88. $legend = new ChartLegend(ChartLegend::POSITION_TOPRIGHT, null, false);
  89. $title = new Title('Test Scatter Chart');
  90. $yAxisLabel = new Title('Value ($k)');
  91. // Create the chart
  92. $chart = new Chart(
  93. 'chart1', // name
  94. $title, // title
  95. $legend, // legend
  96. $plotArea, // plotArea
  97. true, // plotVisibleOnly
  98. DataSeries::EMPTY_AS_GAP, // displayBlanksAs
  99. null, // xAxisLabel
  100. $yAxisLabel // yAxisLabel
  101. );
  102. // Set the position where the chart should appear in the worksheet
  103. $chart->setTopLeftPosition('A7');
  104. $chart->setBottomRightPosition('H20');
  105. // Add the chart to the worksheet
  106. $worksheet->addChart($chart);
  107. // Save Excel 2007 file
  108. $filename = $helper->getFilename(__FILE__);
  109. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  110. $writer->setIncludeCharts(true);
  111. $callStartTime = microtime(true);
  112. $writer->save($filename);
  113. $helper->logWrite($writer, $filename, $callStartTime);