PHPExcel生成柱状图
<br /><img alt="" id="last" rev="0" src="data/attachment/forum/202402/10/5dd8e9d50b136a9671480da88b21b7e7.jpg" aid="769" style="height:413px; width:483px" /><br />
<br />
error_reporting(E_ALL);<br />
ini_set('display_errors', TRUE);<br />
ini_set('display_startup_errors', TRUE);<br />
<br />
if (PHP_SAPI == 'cli') die('This example should only be run from a Web Browser');<br />
<br />
import('ORG.phpexcel.PHPExcel', '', '.php');<br />
import('ORG.phpexcel.PHPExcel.IOFactory', '', '.php');<br />
import('ORG.phpexcel.PHPExcel.Chart.Title', '', '.php');<br />
<br />
<br />
//////////////////////////////////////<br />
$objPHPExcel = new PHPExcel();<br />
$objWorksheet = $objPHPExcel->getActiveSheet();<br />
$objWorksheet->fromArray(<br />
array(<br />
array('标题标题标题标题', 121000),<br />
array('选项1', 121),<br />
array('选项2', 566),<br />
array('选项3', 529),<br />
array('选项4', 30),<br />
)<br />
);<br />
$objWorksheet->setTitle("Q2"); //给当前sheet设置名称<br />
<br />
<br />
<br />
$dataseriesLabels = array(<br />
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', NULL, 1),//2010<br />
//new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', NULL, 1),//2011<br />
//new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$1', NULL, 1),//2012<br />
);<br />
<br />
$xAxisTickValues = array(<br />
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$5', NULL, 4),//Q1 to Q4<br />
);<br />
<br />
$dataSeriesValues = array(<br />
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$5', NULL, 4),<br />
//new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$5', NULL, 4),<br />
//new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$5', NULL, 4),<br />
);<br />
<br />
//Build the dataseries<br />
$series = new PHPExcel_Chart_DataSeries(<br />
PHPExcel_Chart_DataSeries::TYPE_BARCHART,// plotType<br />
PHPExcel_Chart_DataSeries::GROUPING_CLUSTERED,// plotGrouping<br />
range(0, count($dataSeriesValues)-1),// plotOrder<br />
$dataseriesLabels,// plotLabel<br />
$xAxisTickValues,// plotCategory<br />
$dataSeriesValues// plotValues<br />
);<br />
//Set additional dataseries parameters<br />
//Make it a horizontal bar rather than a vertical column graph<br />
$series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_BAR);<br />
<br />
//Set the series in the plot area<br />
$plotarea = new PHPExcel_Chart_PlotArea(NULL, array($series));<br />
//Set the chart legend<br />
//$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);<br />
$legend = NULL;<br />
$title = new PHPExcel_Chart_Title('测试题1');<br />
$yAxisLabel = new PHPExcel_Chart_Title('参与人数');<br />
<br />
<br />
//Create the chart<br />
$chart = new PHPExcel_Chart(<br />
'chart1',// name<br />
$title,// title<br />
$legend,// legend<br />
$plotarea,// plotArea<br />
true,// plotVisibleOnly<br />
0,// displayBlanksAs<br />
NULL,// xAxisLabel<br />
$yAxisLabel// yAxisLabel<br />
);<br />
<br />
//Set the position where the chart should appear in the worksheet<br />
$chart->setTopLeftPosition('A7');<br />
$chart->setBottomRightPosition('H20');<br />
<br />
//Add the chart to the worksheet<br />
$objWorksheet->addChart($chart);<br />
//////////////////////////////////////////////////////////////////////////////////////<br />
<br />
// Set active sheet index to the first sheet, so Excel opens this as the first sheet<br />
$objPHPExcel->setActiveSheetIndex(0);<br />
<br />
<br />
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');<br />
$objWriter->setIncludeCharts(TRUE);<br />
<br />
<br />
$filename = '明细表_'.date("Y_m_d").".xlsx";<br />
<br />
// Redirect output to a client’s web browser (Excel2007)<br />
//header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');<br />
//<br />
//////////////////////////////////////////<br />
//处理中文文件名乱码问题<br />
$ua = $_SERVER["HTTP_USER_AGENT"]; <br />
$encoded_filename = urlencode($filename);<br />
$encoded_filename = str_replace("+", "%20",$encoded_filename);<br />
header('Content-Type: application/octet-stream');<br />
if (preg_match("/MSIE/", $ua)) { <br />
$filename = $encoded_filename;<br />
header('Content-Disposition: attachment;filename="' .$filename . '"');<br />
}else if (preg_match("/Firefox/", $ua)){ <br />
header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"');<br />
}else { <br />
header('Content-Disposition: attachment; filename="' . $filename . '"');<br />
}<br />
////////////////////////////////////////<br />
<br />
header('Cache-Control: max-age=0');<br />
// If you're serving to IE 9, then the following may be needed<br />
header('Cache-Control: max-age=1');<br />
<br />
// If you're serving to IE over SSL, then the following may be needed<br />
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past<br />
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified<br />
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1<br />
header ('Pragma: public'); // HTTP/1.0<br />
<br />
$objWriter->save('php://output');<br />
exit;<br />
<br />
红色的数据部分,多加几列就变成多柱状图,并且后面红色部分也要相应的变更。
<p> </p>
<p>其它相关链接:</p>
<p>http://blog.csdn.net/qq_20480611/article/details/47982501</p>
<p>http://blog.csdn.net/qq_20480611/article/details/48000007</p>
<p>http://blog.csdn.net/xcl168/article/details/25001205</p>
<p>http://www.cnblogs.com/phpgcs/p/phpexcel_chart_2.html?utm_source=tuicool&utm_medium=referral</p>
<p>http://blog.csdn.net/ouyangyiqin/article/details/8901038</p>
<p>http://www.tuicool.com/articles/vA7VJj</p>
页:
[1]