33chartcreate-bar.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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('Q1', 12, 15, 21),
  42. array('Q2', 56, 73, 86),
  43. array('Q3', 52, 61, 69),
  44. array('Q4', 30, 32, 0),
  45. )
  46. );
  47. // Set the Labels for each data series we want to plot
  48. // Datatype
  49. // Cell reference for data
  50. // Format Code
  51. // Number of datapoints in series
  52. // Data values
  53. // Data Marker
  54. $dataSeriesLabels = array(
  55. new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', NULL, 1), // 2010
  56. new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', NULL, 1), // 2011
  57. new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$1', NULL, 1), // 2012
  58. );
  59. // Set the X-Axis Labels
  60. // Datatype
  61. // Cell reference for data
  62. // Format Code
  63. // Number of datapoints in series
  64. // Data values
  65. // Data Marker
  66. $xAxisTickValues = array(
  67. new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$5', NULL, 4), // Q1 to Q4
  68. );
  69. // Set the Data values for each data series we want to plot
  70. // Datatype
  71. // Cell reference for data
  72. // Format Code
  73. // Number of datapoints in series
  74. // Data values
  75. // Data Marker
  76. $dataSeriesValues = array(
  77. new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$5', NULL, 4),
  78. new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$5', NULL, 4),
  79. new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$5', NULL, 4),
  80. );
  81. // Build the dataseries
  82. $series = new PHPExcel_Chart_DataSeries(
  83. PHPExcel_Chart_DataSeries::TYPE_BARCHART, // plotType
  84. PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED, // plotGrouping
  85. range(0, count($dataSeriesValues)-1), // plotOrder
  86. $dataSeriesLabels, // plotLabel
  87. $xAxisTickValues, // plotCategory
  88. $dataSeriesValues // plotValues
  89. );
  90. // Set additional dataseries parameters
  91. // Make it a horizontal bar rather than a vertical column graph
  92. $series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_BAR);
  93. // Set the series in the plot area
  94. $plotArea = new PHPExcel_Chart_PlotArea(NULL, array($series));
  95. // Set the chart legend
  96. $legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);
  97. $title = new PHPExcel_Chart_Title('Test Bar Chart');
  98. $yAxisLabel = new PHPExcel_Chart_Title('Value ($k)');
  99. // Create the chart
  100. $chart = new PHPExcel_Chart(
  101. 'chart1', // name
  102. $title, // title
  103. $legend, // legend
  104. $plotArea, // plotArea
  105. true, // plotVisibleOnly
  106. 0, // displayBlanksAs
  107. NULL, // xAxisLabel
  108. $yAxisLabel // yAxisLabel
  109. );
  110. // Set the position where the chart should appear in the worksheet
  111. $chart->setTopLeftPosition('A7');
  112. $chart->setBottomRightPosition('H20');
  113. // Add the chart to the worksheet
  114. $objWorksheet->addChart($chart);
  115. // Save Excel 2007 file
  116. echo date('H:i:s') , " Write to Excel2007 format" , EOL;
  117. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  118. $objWriter->setIncludeCharts(TRUE);
  119. $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
  120. echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
  121. // Echo memory peak usage
  122. echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;
  123. // Echo done
  124. echo date('H:i:s') , " Done writing file" , EOL;
  125. echo 'File has been created in ' , getcwd() , EOL;