33_Chart_create_radar.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\Layout;
  6. use PhpOffice\PhpSpreadsheet\Chart\Legend;
  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. ['Jan', 47, 45, 71],
  18. ['Feb', 56, 73, 86],
  19. ['Mar', 52, 61, 69],
  20. ['Apr', 40, 52, 60],
  21. ['May', 42, 55, 71],
  22. ['Jun', 58, 63, 76],
  23. ['Jul', 53, 61, 89],
  24. ['Aug', 46, 69, 85],
  25. ['Sep', 62, 75, 81],
  26. ['Oct', 51, 70, 96],
  27. ['Nov', 55, 66, 89],
  28. ['Dec', 68, 62, 0],
  29. ]
  30. );
  31. // Set the Labels for each data series we want to plot
  32. // Datatype
  33. // Cell reference for data
  34. // Format Code
  35. // Number of datapoints in series
  36. // Data values
  37. // Data Marker
  38. $dataSeriesLabels = [
  39. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // 2011
  40. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$D$1', null, 1), // 2012
  41. ];
  42. // Set the X-Axis Labels
  43. // Datatype
  44. // Cell reference for data
  45. // Format Code
  46. // Number of datapoints in series
  47. // Data values
  48. // Data Marker
  49. $xAxisTickValues = [
  50. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$13', null, 12), // Jan to Dec
  51. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$2:$A$13', null, 12), // Jan to Dec
  52. ];
  53. // Set the Data values for each data series we want to plot
  54. // Datatype
  55. // Cell reference for data
  56. // Format Code
  57. // Number of datapoints in series
  58. // Data values
  59. // Data Marker
  60. $dataSeriesValues = [
  61. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$13', null, 12),
  62. new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$D$2:$D$13', null, 12),
  63. ];
  64. // Build the dataseries
  65. $series = new DataSeries(
  66. DataSeries::TYPE_RADARCHART, // plotType
  67. null, // plotGrouping (Radar charts don't have any grouping)
  68. range(0, count($dataSeriesValues) - 1), // plotOrder
  69. $dataSeriesLabels, // plotLabel
  70. $xAxisTickValues, // plotCategory
  71. $dataSeriesValues, // plotValues
  72. null, // plotDirection
  73. null, // smooth line
  74. DataSeries::STYLE_MARKER // plotStyle
  75. );
  76. // Set up a layout object for the Pie chart
  77. $layout = new Layout();
  78. // Set the series in the plot area
  79. $plotArea = new PlotArea($layout, [$series]);
  80. // Set the chart legend
  81. $legend = new Legend(Legend::POSITION_RIGHT, null, false);
  82. $title = new Title('Test Radar Chart');
  83. // Create the chart
  84. $chart = new Chart(
  85. 'chart1', // name
  86. $title, // title
  87. $legend, // legend
  88. $plotArea, // plotArea
  89. true, // plotVisibleOnly
  90. 0, // displayBlanksAs
  91. null, // xAxisLabel
  92. null // yAxisLabel - Radar charts don't have a Y-Axis
  93. );
  94. // Set the position where the chart should appear in the worksheet
  95. $chart->setTopLeftPosition('F2');
  96. $chart->setBottomRightPosition('M15');
  97. // Add the chart to the worksheet
  98. $worksheet->addChart($chart);
  99. // Save Excel 2007 file
  100. $filename = $helper->getFilename(__FILE__);
  101. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  102. $writer->setIncludeCharts(true);
  103. $callStartTime = microtime(true);
  104. $writer->save($filename);
  105. $helper->logWrite($writer, $filename, $callStartTime);