我正在使用PHPExcel庫,以便對4列柱狀圖進行總結。我設法做到了這一點,但現在我想改變列的顏色,我沒有找到任何方法來做到這一點。任何幫助將非常感激。PHPExcel更改柱狀圖的顏色
這是我建立我的excel文件
private function createReport($result = null, $pdf = false) {
if ($result != null) {
$nameFile = "List";
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator('App')->setTitle($nameFile)->setSubject("S")
->setCategory("Test data");
$objWorksheet = $objPHPExcel->getActiveSheet();
$charstSheet = $objPHPExcel->createSheet();
$charstSheet->setTitle("Summary");
$columnArea = "A";
$columnCount = "B";
$indexSheet = 0;
foreach ($result as $result_value_index => $result_value) {
if ($indexSheet > 0) {
$sheet = $objPHPExcel->createSheet($indexSheet);
$dataSheetTitle = 'Worksheet' . $result_value_index;
$sheet->setTitle($dataSheetTitle);
}
$objPHPExcel->setActiveSheetIndex($indexSheet);
$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheet->setSheetState(PHPExcel_Worksheet::SHEETSTATE_HIDDEN);
$row = 0;
$currentArea = null;
for ($j = 0; $j < count($result[$result_value_index]); $j++) {
$currentArea = $result[$result_value_index][$j];
$row = $j + 1;
$objWorksheet->setCellValue($columnArea . $row, $currentArea['Area_name']);
$objWorksheet->setCellValue($columnCount . $row, $currentArea['ToDo_count']);
}
$sheetTitle = $objWorksheet->getTitle();
$dataSeriesLabels = array(
new PHPExcel_Chart_DataSeriesValues('String', $sheetTitle . '!$A$1', NULL, 1)
);
$xAxisTickValues = array(
new PHPExcel_Chart_DataSeriesValues('String', $sheetTitle . '!$A$1:$A$' . $row, NULL, $j), // Q1 to Q4
);
$dataSeriesValues = array(
new PHPExcel_Chart_DataSeriesValues('Number', $sheetTitle . '!$B$1:$B$' . $row, NULL, $j),
);
// Build the dataseries
$series = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_BARCHART, // plotType
PHPExcel_Chart_DataSeries::GROUPING_STANDARD, // plotGrouping
range(0, count($dataSeriesValues) - 1), // plotOrder
$dataSeriesLabels, // plotLabel
$xAxisTickValues, // plotCategory
$dataSeriesValues // plotValues
);
$series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL);
$plotArea = new PHPExcel_Chart_PlotArea(NULL, array($series));
$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);
$title = new PHPExcel_Chart_Title();
if ($currentArea != null) {
$calculationEngine = PHPExcel_Calculation::getInstance($objPHPExcel);
$average = round($calculationEngine->calculateFormula('=AVERAGE(B1:B' . $row . ")"));
$title = new PHPExcel_Chart_Title($currentArea['ParentAreaName'] . " - Promedio ≈ " . $average . " reservas");
}
$yAxisLabel = new PHPExcel_Chart_Title('Reservas');
// Create the chart
$chart = new PHPExcel_Chart(
'chart1', // name
$title, // title
NULL, // legend
$plotArea, // plotArea
true, // plotVisibleOnly
0, // displayBlanksAs
NULL, // xAxisLabel
$yAxisLabel // yAxisLabel
);
// Set the position where the chart should appear in the worksheet
$chart->setTopLeftPosition('A' . ($indexSheet * 20 + 1));
$chart->setBottomRightPosition('N' . ($indexSheet * 20 + 20));
// Add the chart to the worksheet
$charstSheet->addChart($chart);
$indexSheet++;
}
$objPHPExcel->setActiveSheetIndexByName("Resumen");
if (!$pdf){
$this->export_excel($objPHPExcel, $nameFile);
} else {
$this->export_pdf($objPHPExcel, $nameFile);
}
}
}
這是我如何導出到Excel
public function export_excel($objPHPExcel,$nameFile){
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
$objWriter->setIncludeCharts(TRUE);
$date = new DateTime();
$nameFile = $nameFile.'_'.$date->getTimestamp().'.xlsx';
$objWriter->save('outputfiles/'. $nameFile);
$url = Router::url('/outputfiles/', true). $nameFile;
$this->set(array('url' =>$url,'_serialize' => array('url')));
}
@ Fred -ii,@GrumpyCrouton,對不起,我不是問如何改變單元格的顏色,我看到了,我想改變圖表中條形的顏色。 – lfal