'PHPExcel change bar color of column chart
I'm using PHPExcel library in order to make an summary excel with 4 column charts. I managed to do that, but now I want to change the color of the columns and I didn't find any way to do it. Any help will be very appreciated.
This is how I build my excel file
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);
}
}
}
This is how I export to 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')));
}
Solution 1:[1]
I think is a hacky solution but I didn't find in that moment any public method that allowed me change the color. I solved it by changing the value of variable 'accent1' in the line 122 on Theme file located in PhpExcel/Writer/Excel2007 folders. Note that I'm using Excel2007 for writing.
I post this in case any one finds it useful.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |