杰克工作室 发表于 2024-2-10 16:34

PHPExcel生成柱状图

<br />
<img alt="" id="last" rev="0" src="data/attachment/forum/202402/10/5dd8e9d50b136a9671480da88b21b7e7.jpg"   aid="769" style="height:413px; width:483px" /><br />
&nbsp;<br />
error_reporting(E_ALL);<br />
ini_set(&#39;display_errors&#39;, TRUE);<br />
ini_set(&#39;display_startup_errors&#39;, TRUE);<br />
&nbsp;<br />
if (PHP_SAPI == &#39;cli&#39;) die(&#39;This example should only be run from a Web Browser&#39;);<br />
&nbsp;<br />
import(&#39;ORG.phpexcel.PHPExcel&#39;, &#39;&#39;, &#39;.php&#39;);<br />
import(&#39;ORG.phpexcel.PHPExcel.IOFactory&#39;, &#39;&#39;, &#39;.php&#39;);<br />
import(&#39;ORG.phpexcel.PHPExcel.Chart.Title&#39;, &#39;&#39;, &#39;.php&#39;);<br />
&nbsp;<br />
&nbsp;<br />
//////////////////////////////////////<br />
$objPHPExcel = new PHPExcel();<br />
$objWorksheet = $objPHPExcel-&gt;getActiveSheet();<br />
$objWorksheet-&gt;fromArray(<br />
array(<br />
array(&#39;标题标题标题标题&#39;, &nbsp; 121000),<br />
array(&#39;选项1&#39;, &nbsp; 121),<br />
array(&#39;选项2&#39;, &nbsp; 566),<br />
array(&#39;选项3&#39;, &nbsp; 529),<br />
array(&#39;选项4&#39;, &nbsp; 30),<br />
)<br />
);<br />
$objWorksheet-&gt;setTitle(&quot;Q2&quot;); //给当前sheet设置名称<br />
&nbsp;<br />
&nbsp;<br />
&nbsp;<br />
$dataseriesLabels = array(<br />
new PHPExcel_Chart_DataSeriesValues(&#39;String&#39;, &#39;Worksheet!$B$1&#39;, NULL, 1),//2010<br />
//new PHPExcel_Chart_DataSeriesValues(&#39;String&#39;, &#39;Worksheet!$C$1&#39;, NULL, 1),//2011<br />
//new PHPExcel_Chart_DataSeriesValues(&#39;String&#39;, &#39;Worksheet!$D$1&#39;, NULL, 1),//2012<br />
);<br />
&nbsp;<br />
$xAxisTickValues = array(<br />
new PHPExcel_Chart_DataSeriesValues(&#39;String&#39;, &#39;Worksheet!$A$2:$A$5&#39;, NULL, 4),//Q1 to Q4<br />
);<br />
&nbsp;<br />
$dataSeriesValues = array(<br />
new PHPExcel_Chart_DataSeriesValues(&#39;Number&#39;, &#39;Worksheet!$B$2:$B$5&#39;, NULL, 4),<br />
//new PHPExcel_Chart_DataSeriesValues(&#39;Number&#39;, &#39;Worksheet!$C$2:$C$5&#39;, NULL, 4),<br />
//new PHPExcel_Chart_DataSeriesValues(&#39;Number&#39;, &#39;Worksheet!$D$2:$D$5&#39;, NULL, 4),<br />
);<br />
&nbsp;<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-&gt;setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_BAR);<br />
&nbsp;<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 &nbsp;= NULL;<br />
$title &nbsp; = new PHPExcel_Chart_Title(&#39;测试题1&#39;);<br />
$yAxisLabel = new PHPExcel_Chart_Title(&#39;参与人数&#39;);<br />
&nbsp;<br />
&nbsp;<br />
//Create the chart<br />
$chart = new PHPExcel_Chart(<br />
&#39;chart1&#39;,// name<br />
$title,// title<br />
$legend,// legend<br />
$plotarea,// plotArea<br />
true,// plotVisibleOnly<br />
0,// displayBlanksAs<br />
NULL,// xAxisLabel<br />
$yAxisLabel// yAxisLabel<br />
);<br />
&nbsp;<br />
//Set the position where the chart should appear in the worksheet<br />
$chart-&gt;setTopLeftPosition(&#39;A7&#39;);<br />
$chart-&gt;setBottomRightPosition(&#39;H20&#39;);<br />
&nbsp;<br />
//Add the chart to the worksheet<br />
$objWorksheet-&gt;addChart($chart);<br />
//////////////////////////////////////////////////////////////////////////////////////<br />
&nbsp;<br />
// Set active sheet index to the first sheet, so Excel opens this as the first sheet<br />
$objPHPExcel-&gt;setActiveSheetIndex(0);<br />
&nbsp;<br />
&nbsp;<br />
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, &#39;Excel2007&#39;);<br />
$objWriter-&gt;setIncludeCharts(TRUE);<br />
&nbsp;<br />
&nbsp;<br />
$filename = &#39;明细表_&#39;.date(&quot;Y_m_d&quot;).&quot;.xlsx&quot;;<br />
&nbsp;<br />
// Redirect output to a client&rsquo;s web browser (Excel2007)<br />
//header(&#39;Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet&#39;);<br />
//<br />
//////////////////////////////////////////<br />
//处理中文文件名乱码问题<br />
$ua = $_SERVER[&quot;HTTP_USER_AGENT&quot;]; &nbsp;<br />
$encoded_filename = urlencode($filename);<br />
$encoded_filename = str_replace(&quot;+&quot;, &quot;%20&quot;,$encoded_filename);<br />
header(&#39;Content-Type: application/octet-stream&#39;);<br />
if (preg_match(&quot;/MSIE/&quot;, $ua)) {&nbsp;<br />
&nbsp; &nbsp; $filename = $encoded_filename;<br />
&nbsp; &nbsp; header(&#39;Content-Disposition: attachment;filename=&quot;&#39; .$filename . &#39;&quot;&#39;);<br />
}else if (preg_match(&quot;/Firefox/&quot;, $ua)){&nbsp;<br />
&nbsp; &nbsp;header(&#39;Content-Disposition: attachment; filename*=&quot;utf8\&#39;\&#39;&#39; . $filename . &#39;&quot;&#39;);<br />
}else {&nbsp;<br />
&nbsp; header(&#39;Content-Disposition: attachment; filename=&quot;&#39; . $filename . &#39;&quot;&#39;);<br />
}<br />
////////////////////////////////////////<br />
&nbsp;<br />
header(&#39;Cache-Control: max-age=0&#39;);<br />
// If you&#39;re serving to IE 9, then the following may be needed<br />
header(&#39;Cache-Control: max-age=1&#39;);<br />
&nbsp;<br />
// If you&#39;re serving to IE over SSL, then the following may be needed<br />
header (&#39;Expires: Mon, 26 Jul 1997 05:00:00 GMT&#39;); // Date in the past<br />
header (&#39;Last-Modified: &#39;.gmdate(&#39;D, d M Y H:i:s&#39;).&#39; GMT&#39;); // always modified<br />
header (&#39;Cache-Control: cache, must-revalidate&#39;); // HTTP/1.1<br />
header (&#39;Pragma: public&#39;); // HTTP/1.0<br />
&nbsp;<br />
$objWriter-&gt;save(&#39;php://output&#39;);<br />
exit;<br />
&nbsp;<br />
红色的数据部分,多加几列就变成多柱状图,并且后面红色部分也要相应的变更。
<p>&nbsp;</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&amp;utm_medium=referral</p>

<p>http://blog.csdn.net/ouyangyiqin/article/details/8901038</p>

<p>http://www.tuicool.com/articles/vA7VJj</p>
页: [1]
查看完整版本: PHPExcel生成柱状图