33_Chart_create_stock.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
  11. require __DIR__ . '/../Header.php';
  12. $spreadsheet = new Spreadsheet();
  13. $worksheet = $spreadsheet->getActiveSheet();
  14. $worksheet->fromArray(
  15. [
  16. ['Counts', 'Max', 'Min', 'Min Threshold', 'Max Threshold'],
  17. [10, 10, 5, 0, 50],
  18. [30, 20, 10, 0, 50],
  19. [20, 30, 15, 0, 50],
  20. [40, 10, 0, 0, 50],
  21. [100, 40, 5, 0, 50],
  22. ],
  23. null,
  24. 'A1',
  25. true
  26. );
  27. $worksheet->getStyle('B2:E6')->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_NUMBER_00);
  28. // Set the Labels for each data series we want to plot
  29. // Datatype
  30. // Cell reference for data
  31. // Format Code
  32. // Number of datapoints in series
  33. // Data values
  34. // Data Marker
  35. $dataSeriesLabels = [
  36. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$B$1', null, 1), //Max / Open
  37. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), //Min / Close
  38. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), //Min Threshold / Min
  39. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$E$1', null, 1), //Max Threshold / Max
  40. ];
  41. // Set the X-Axis Labels
  42. // Datatype
  43. // Cell reference for data
  44. // Format Code
  45. // Number of datapoints in series
  46. // Data values
  47. // Data Marker
  48. $xAxisTickValues = [
  49. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$6', null, 5), // Counts
  50. ];
  51. // Set the Data values for each data series we want to plot
  52. // Datatype
  53. // Cell reference for data
  54. // Format Code
  55. // Number of datapoints in series
  56. // Data values
  57. // Data Marker
  58. $dataSeriesValues = [
  59. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$B$2:$B$6', null, 5),
  60. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$6', null, 5),
  61. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$6', null, 5),
  62. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$E$2:$E$6', null, 5),
  63. ];
  64. // Build the dataseries
  65. $series = new DataSeries(
  66. DataSeries::TYPE_STOCKCHART, // plotType
  67. null, // plotGrouping - if we set this to not null, then xlsx throws error
  68. range(0, count($dataSeriesValues) - 1), // plotOrder
  69. $dataSeriesLabels, // plotLabel
  70. $xAxisTickValues, // plotCategory
  71. $dataSeriesValues // plotValues
  72. );
  73. // Set the series in the plot area
  74. $plotArea = new PlotArea(null, [$series]);
  75. // Set the chart legend
  76. $legend = new Legend(Legend::POSITION_RIGHT, null, false);
  77. $title = new Title('Test Stock Chart');
  78. $xAxisLabel = new Title('Counts');
  79. $yAxisLabel = new Title('Values');
  80. // Create the chart
  81. $chart = new Chart(
  82. 'stock-chart', // name
  83. $title, // title
  84. $legend, // legend
  85. $plotArea, // plotArea
  86. true, // plotVisibleOnly
  87. 0, // displayBlanksAs
  88. $xAxisLabel, // xAxisLabel
  89. $yAxisLabel // yAxisLabel
  90. );
  91. // Set the position where the chart should appear in the worksheet
  92. $chart->setTopLeftPosition('A7');
  93. $chart->setBottomRightPosition('H20');
  94. // Add the chart to the worksheet
  95. $worksheet->addChart($chart);
  96. // Save Excel 2007 file
  97. $filename = $helper->getFilename(__FILE__);
  98. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  99. $writer->setIncludeCharts(true);
  100. $callStartTime = microtime(true);
  101. $writer->save($filename);
  102. $helper->logWrite($writer, $filename, $callStartTime);