Advertisement
kura2yamato

excel dgn graph

Oct 23rd, 2019
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.30 KB | None | 0 0
  1. <?php
  2. include('inc_excel.php');
  3.  
  4. $objPHPExcel = new PHPExcel();
  5. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  6.                              ->setLastModifiedBy("Maarten Balliauw")
  7.                              ->setTitle("PHPExcel Test Document")
  8.                              ->setSubject("PHPExcel Test Document")
  9.                              ->setDescription("Test document for PHPExcel, generated using PHP classes.")
  10.                              ->setKeywords("office PHPExcel php")
  11.                              ->setCategory("Test result file");
  12.  
  13. //Tentukan target halamannya     
  14. $objWorksheet = $objPHPExcel->getActiveSheet();
  15.  
  16. //buat list datanya... yg pertama adalah judul dan sianya adalah datanya..
  17. $objWorksheet->fromArray(
  18.     array(
  19.         array('',   2010,   2011,   2012),
  20.         array('Q1',   12,   15,     21),
  21.         array('Q2',   56,   73,     86),
  22.         array('Q3',   52,   61,     69),
  23.         array('Q4',   30,   32,     0),
  24.     )
  25. );
  26.  
  27. //tentukan heder datanya, dalam hal ini tahunnya
  28. $dataSeriesLabels = array(
  29.     new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', NULL, 1),   //  2010
  30.     new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', NULL, 1),   //  2011
  31.     new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$1', NULL, 1),   //  2012
  32. );
  33. //tentukan rentang datanya.. lihat dari data ini maka yang di ambil adalah q1-q4
  34. $xAxisTickValues = array(
  35.     new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$5', NULL, 4),  //  Q1 to Q4
  36. );
  37. //masukkan nilainya
  38. $dataSeriesValues = array(
  39.     new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$5', NULL, 4),
  40.     new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$5', NULL, 4),
  41.     new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$5', NULL, 4),
  42. );
  43.  
  44. //bangun grafiknya
  45. //  Build the dataseries
  46. $series = new PHPExcel_Chart_DataSeries(
  47.     PHPExcel_Chart_DataSeries::TYPE_BARCHART,       // plotType
  48.     PHPExcel_Chart_DataSeries::GROUPING_STANDARD,   // plotGrouping
  49.     range(0, count($dataSeriesValues)-1),           // plotOrder
  50.     $dataSeriesLabels,                              // plotLabel
  51.     $xAxisTickValues,                               // plotCategory
  52.     $dataSeriesValues                               // plotValues
  53. );
  54. //salah satu option
  55. $series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL);
  56.  
  57. //  Set the series in the plot area
  58. $plotArea = new PHPExcel_Chart_PlotArea(NULL, array($series));
  59. //  Set the chart legend
  60. $legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);
  61.  
  62. //judul
  63. $title = new PHPExcel_Chart_Title('Test Column Chart');
  64. $yAxisLabel = new PHPExcel_Chart_Title('Value ($k)');
  65.  
  66. //mulai membangun
  67. //  Create the chart
  68. $chart = new PHPExcel_Chart(
  69.     'chart1',       // name
  70.     $title,         // title
  71.     $legend,        // legend
  72.     $plotArea,      // plotArea
  73.     true,           // plotVisibleOnly
  74.     0,              // displayBlanksAs
  75.     NULL,           // xAxisLabel
  76.     $yAxisLabel     // yAxisLabel
  77. );
  78.  
  79. //meletakkan pada tempatnya dalam hal ini dari H1-P20
  80. //  Set the position where the chart should appear in the worksheet
  81. $chart->setTopLeftPosition('H1');
  82. $chart->setBottomRightPosition('P20');
  83.  
  84. //memasang
  85. //  Add the chart to the worksheet
  86. $objWorksheet->addChart($chart);
  87.  
  88. save_chart( $objPHPExcel, 'excel_graph.xlsx');
  89.  
  90.  
  91. echo "done <a href='excel_graph.xlsx'>download</a><hr/>";
  92.  
  93. echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;
  94.  
  95. // Echo done
  96. echo date('H:i:s') , " Done writing file" , EOL;
  97. echo 'File has been created in ' , getcwd() , EOL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement