33chartcreate-radar.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /** Error reporting */
  3. error_reporting(E_ALL);
  4. ini_set('display_errors', TRUE);
  5. ini_set('display_startup_errors', TRUE);
  6. date_default_timezone_set('Europe/London');
  7. define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
  8. date_default_timezone_set('Europe/London');
  9. /**
  10. * PHPExcel
  11. *
  12. * Copyright (c) 2006 - 2015 PHPExcel
  13. *
  14. * This library is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU Lesser General Public
  16. * License as published by the Free Software Foundation; either
  17. * version 2.1 of the License, or (at your option) any later version.
  18. *
  19. * This library is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * Lesser General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Lesser General Public
  25. * License along with this library; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  27. *
  28. * @category PHPExcel
  29. * @package PHPExcel
  30. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  31. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  32. * @version ##VERSION##, ##DATE##
  33. */
  34. /** PHPExcel */
  35. require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
  36. $objPHPExcel = new PHPExcel();
  37. $objWorksheet = $objPHPExcel->getActiveSheet();
  38. $objWorksheet->fromArray(
  39. array(
  40. array('', 2010, 2011, 2012),
  41. array('Jan', 47, 45, 71),
  42. array('Feb', 56, 73, 86),
  43. array('Mar', 52, 61, 69),
  44. array('Apr', 40, 52, 60),
  45. array('May', 42, 55, 71),
  46. array('Jun', 58, 63, 76),
  47. array('Jul', 53, 61, 89),
  48. array('Aug', 46, 69, 85),
  49. array('Sep', 62, 75, 81),
  50. array('Oct', 51, 70, 96),
  51. array('Nov', 55, 66, 89),
  52. array('Dec', 68, 62, 0),
  53. )
  54. );
  55. // Set the Labels for each data series we want to plot
  56. // Datatype
  57. // Cell reference for data
  58. // Format Code
  59. // Number of datapoints in series
  60. // Data values
  61. // Data Marker
  62. $dataSeriesLabels = array(
  63. new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', NULL, 1), // 2011
  64. new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$1', NULL, 1), // 2012
  65. );
  66. // Set the X-Axis Labels
  67. // Datatype
  68. // Cell reference for data
  69. // Format Code
  70. // Number of datapoints in series
  71. // Data values
  72. // Data Marker
  73. $xAxisTickValues = array(
  74. new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$13', NULL, 12), // Jan to Dec
  75. new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$13', NULL, 12), // Jan to Dec
  76. );
  77. // Set the Data values for each data series we want to plot
  78. // Datatype
  79. // Cell reference for data
  80. // Format Code
  81. // Number of datapoints in series
  82. // Data values
  83. // Data Marker
  84. $dataSeriesValues = array(
  85. new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$13', NULL, 12),
  86. new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$13', NULL, 12),
  87. );
  88. // Build the dataseries
  89. $series = new PHPExcel_Chart_DataSeries(
  90. PHPExcel_Chart_DataSeries::TYPE_RADARCHART, // plotType
  91. NULL, // plotGrouping (Radar charts don't have any grouping)
  92. range(0, count($dataSeriesValues)-1), // plotOrder
  93. $dataSeriesLabels, // plotLabel
  94. $xAxisTickValues, // plotCategory
  95. $dataSeriesValues, // plotValues
  96. NULL, // plotDirection
  97. NULL, // smooth line
  98. PHPExcel_Chart_DataSeries::STYLE_MARKER // plotStyle
  99. );
  100. // Set up a layout object for the Pie chart
  101. $layout = new PHPExcel_Chart_Layout();
  102. // Set the series in the plot area
  103. $plotArea = new PHPExcel_Chart_PlotArea($layout, array($series));
  104. // Set the chart legend
  105. $legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);
  106. $title = new PHPExcel_Chart_Title('Test Radar Chart');
  107. // Create the chart
  108. $chart = new PHPExcel_Chart(
  109. 'chart1', // name
  110. $title, // title
  111. $legend, // legend
  112. $plotArea, // plotArea
  113. true, // plotVisibleOnly
  114. 0, // displayBlanksAs
  115. NULL, // xAxisLabel
  116. NULL // yAxisLabel - Radar charts don't have a Y-Axis
  117. );
  118. // Set the position where the chart should appear in the worksheet
  119. $chart->setTopLeftPosition('F2');
  120. $chart->setBottomRightPosition('M15');
  121. // Add the chart to the worksheet
  122. $objWorksheet->addChart($chart);
  123. // Save Excel 2007 file
  124. echo date('H:i:s') , " Write to Excel2007 format" , EOL;
  125. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  126. $objWriter->setIncludeCharts(TRUE);
  127. $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
  128. echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
  129. // Echo memory peak usage
  130. echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;
  131. // Echo done
  132. echo date('H:i:s') , " Done writing file" , EOL;
  133. echo 'File has been created in ' , getcwd() , EOL;