Sample_05_Chart_with_PHPExcel.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. include_once 'Sample_Header.php';
  3. use PhpOffice\PhpPresentation\PhpPresentation;
  4. use PhpOffice\PhpPresentation\Shape\Chart\Type\Bar3D;
  5. use PhpOffice\PhpPresentation\Shape\Chart\Type\Pie3D;
  6. use PhpOffice\PhpPresentation\Shape\Chart\Series;
  7. use PhpOffice\PhpPresentation\Style\Fill;
  8. use PhpOffice\PhpPresentation\Style\Color;
  9. use PhpOffice\PhpPresentation\Style\Border;
  10. if (!class_exists('PHPExcel')) {
  11. echo('<strong>PHPExcel has not been loaded. Include PHPExcel.php in your script, e.g. require_once \'PHPExcel.php\'.</strong>');
  12. } else {
  13. // Create new PHPPresentation object
  14. echo date('H:i:s') . ' Create new PHPPresentation object'.EOL;
  15. $objPHPPresentation = new PhpPresentation();
  16. // Set properties
  17. echo date('H:i:s') . ' Set properties'.EOL;
  18. $objPHPPresentation->getDocumentProperties()->setCreator('PHPOffice')
  19. ->setLastModifiedBy('PHPPresentation Team')
  20. ->setTitle('Sample 08 Title')
  21. ->setSubject('Sample 08 Subject')
  22. ->setDescription('Sample 08 Description')
  23. ->setKeywords('office 2007 openxml libreoffice odt php')
  24. ->setCategory('Sample Category');
  25. // Remove first slide
  26. echo date('H:i:s') . ' Remove first slide'.EOL;
  27. $objPHPPresentation->removeSlideByIndex(0);
  28. // Create templated slide
  29. echo date('H:i:s') . ' Create templated slide'.EOL;
  30. $currentSlide = createTemplatedSlide($objPHPPresentation); // local function
  31. // Generate sample data for first chart
  32. echo date('H:i:s') . ' Generate sample data for first chart'.EOL;
  33. $series1Data = array('Jan' => 133, 'Feb' => 99, 'Mar' => 191, 'Apr' => 205, 'May' => 167, 'Jun' => 201, 'Jul' => 240, 'Aug' => 226, 'Sep' => 255, 'Oct' => 264, 'Nov' => 283, 'Dec' => 293);
  34. $series2Data = array('Jan' => 266, 'Feb' => 198, 'Mar' => 271, 'Apr' => 305, 'May' => 267, 'Jun' => 301, 'Jul' => 340, 'Aug' => 326, 'Sep' => 344, 'Oct' => 364, 'Nov' => 383, 'Dec' => 379);
  35. // Create a bar chart (that should be inserted in a shape)
  36. echo date('H:i:s') . ' Create a bar chart (that should be inserted in a chart shape)'.EOL;
  37. $bar3DChart = new Bar3D();
  38. $bar3DChart->addSeries( new Series('2009', $series1Data) );
  39. $bar3DChart->addSeries( new Series('2010', $series2Data) );
  40. // Create a shape (chart)
  41. echo date('H:i:s') . ' Create a shape (chart)'.EOL;
  42. $shape = $currentSlide->createChartShape();
  43. $shape->setName('PHPPresentation Monthly Downloads')
  44. ->setResizeProportional(false)
  45. ->setHeight(550)
  46. ->setWidth(700)
  47. ->setOffsetX(120)
  48. ->setOffsetY(80)
  49. ->setIncludeSpreadsheet(true);
  50. $shape->getShadow()->setVisible(true)
  51. ->setDirection(45)
  52. ->setDistance(10);
  53. $shape->getFill()->setFillType(Fill::FILL_GRADIENT_LINEAR)
  54. ->setStartColor(new Color('FFCCCCCC'))
  55. ->setEndColor(new Color('FFFFFFFF'))
  56. ->setRotation(270);
  57. $shape->getBorder()->setLineStyle(Border::LINE_SINGLE);
  58. $shape->getTitle()->setText('PHPPresentation Monthly Downloads');
  59. $shape->getTitle()->getFont()->setItalic(true);
  60. $shape->getPlotArea()->getAxisX()->setTitle('Month');
  61. $shape->getPlotArea()->getAxisY()->setTitle('Downloads');
  62. $shape->getPlotArea()->setType($bar3DChart);
  63. $shape->getView3D()->setRightAngleAxes(true);
  64. $shape->getView3D()->setRotationX(20);
  65. $shape->getView3D()->setRotationY(20);
  66. $shape->getLegend()->getBorder()->setLineStyle(Border::LINE_SINGLE);
  67. $shape->getLegend()->getFont()->setItalic(true);
  68. // Create templated slide
  69. echo date('H:i:s') . ' Create templated slide'.EOL;
  70. $currentSlide = createTemplatedSlide($objPHPPresentation); // local function
  71. // Generate sample data for second chart
  72. echo date('H:i:s') . ' Generate sample data for second chart'.EOL;
  73. $seriesData = array('Monday' => 12, 'Tuesday' => 15, 'Wednesday' => 13, 'Thursday' => 17, 'Friday' => 14, 'Saturday' => 9, 'Sunday' => 7);
  74. // Create a pie chart (that should be inserted in a shape)
  75. echo date('H:i:s') . ' Create a pie chart (that should be inserted in a chart shape)'.EOL;
  76. $pie3DChart = new Pie3D();
  77. $pie3DChart->addSeries( new Series('Downloads', $seriesData) );
  78. // Create a shape (chart)
  79. echo date('H:i:s') . ' Create a shape (chart)'.EOL;
  80. $shape = $currentSlide->createChartShape();
  81. $shape->setName('PHPPresentation Daily Downloads')
  82. ->setResizeProportional(false)
  83. ->setHeight(550)
  84. ->setWidth(700)
  85. ->setOffsetX(120)
  86. ->setOffsetY(80)
  87. ->setIncludeSpreadsheet(true);
  88. $shape->getShadow()->setVisible(true)
  89. ->setDirection(45)
  90. ->setDistance(10);
  91. $shape->getFill()->setFillType(Fill::FILL_GRADIENT_LINEAR)
  92. ->setStartColor(new Color('FFCCCCCC'))
  93. ->setEndColor(new Color('FFFFFFFF'))
  94. ->setRotation(270);
  95. $shape->getBorder()->setLineStyle(Border::LINE_SINGLE);
  96. $shape->getTitle()->setText('PHPPresentation Daily Downloads');
  97. $shape->getTitle()->getFont()->setItalic(true);
  98. $shape->getPlotArea()->setType($pie3DChart);
  99. $shape->getView3D()->setRotationX(30);
  100. $shape->getView3D()->setPerspective(30);
  101. $shape->getLegend()->getBorder()->setLineStyle(Border::LINE_SINGLE);
  102. $shape->getLegend()->getFont()->setItalic(true);
  103. // Save file
  104. echo write($objPHPPresentation, basename(__FILE__, '.php'), $writers);
  105. }
  106. if (!CLI) {
  107. include_once 'Sample_Footer.php';
  108. }