use PHPExcel;
public function echart(){
$objPHPExcel = new PHPExcel();
$objSheet = $objPHPExcel->getActiveSheet();
$data = array(
// array("起始时间","2020/6/15 0:00:00","终止时间",'2020-06-15 17:21:45'),
// array("源IP",'0.0.0.0','目的IP','0.0.0.0'),
array("目标标识","HTTP审计",'HTTPS审计','即时通讯审计','电子邮件审计','远程登录审计','文件传输审计'),
array("条数",'10','256','3','100','20','46')//加空表示文本 0也显示
);//这里面的数据可以用变量代替
$objSheet->fromArray($data);
$labels = array(new \PHPExcel_Chart_DataSeriesValues('String','Worksheet!$A$3',null,1),);//条数图例
$xLabels = array(new \PHPExcel_Chart_DataSeriesValues('String','Worksheet!$B$1:$G$1',null,6),);//取x轴刻度
$datas = array(new \PHPExcel_Chart_DataSeriesValues('Number','Worksheet!$B$2:$G$2',null,6),);//取数据
$series = array(new \PHPExcel_Chart_DataSeries(\PHPExcel_Chart_DataSeries::TYPE_BARCHART,//选择图表形式,柱状图,还可选择其他 折线图 饼图
\PHPExcel_Chart_DataSeries::GROUPING_STANDARD,
range(0, count($datas)-1),
$labels,
$xLabels,
$datas
)
);
$layout=new \PHPExcel_Chart_Layout();
$layout->setShowVal(true);
$areas = new \PHPExcel_Chart_PlotArea($layout,$series);
$legend = new \PHPExcel_Chart_Legend(\PHPExcel_Chart_Legend::POSITION_RIGHT,$layout,false);
// $title = new \PHPExcel_Chart_Title("目标标识统计aaa");
$title = null;
// $ytitle = new \PHPExcel_Chart_Title("条数bbb");
$ytitle = null;
$chart = new \PHPExcel_Chart('line_chart',$title,$legend,$areas,true,false,null,$ytitle);
$chart->setTopLeftPosition("A10")->setBottomRightPosition("K25"); //图表位置
$objSheet->addChart($chart);
$filename = '统计'.date('YmdHis').'.xlsx';
$excel = 'Excel2007';
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel,$excel);
$objWriter->setIncludeCharts(true); //图表必须加此行
ob_end_clean();//解决乱码
ob_start();
Header("Content-type: application/octet-stream;charset=utf-8");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); //excel2007
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$filePath = dirname(__FILE__) . rand(0, getrandmax()) . rand(0, getrandmax()) . ".xlxs";
$objWriter->save($filePath);
//readfile($filePath);
unlink($filePath);
}
在选择图表形式的时候 可以选择其他需要的图表,然后只要相应的调整参数即可,
效果: