正如 Rzangue 所说,PHPExcel 目前不提供这样做的简单方法,但是,如果您不介意对使用 PHPExcel 创建的所有图表的更改进行硬编码,您可以对 PHPExcel/Classes/Writer 进行以下建议的更改/Excel2007/Chart.php 文件。
要更改图表的边框颜色,请在公共函数 writeChart() 中添加:
$cBorderColor = "000000";
$objWriter->startElement('c:spPr');
$objWriter->startElement('a:ln');
$objWriter->startElement('a:solidFill');
$objWriter->startElement('a:srgbClr');
$objWriter->writeAttribute('val',$cBorderColor);
$objWriter->endElement();
$objWriter->endElement();
$objWriter->endElement();
$objWriter->endElement();
后:
$objWriter->startElement('c:showDLblsOverMax');
$objWriter->writeAttribute('val', 0);
$objWriter->endElement();
$objWriter->endElement();
但之前:
$this->_writePrintSettings($objWriter);
应该在 Chart.php 文件的第 106 行附近。
显然用您希望成为图表边框颜色的任何网络颜色替换“000000”。要完全删除边框颜色,请插入:
$objWriter->startElement('c:spPr');
$objWriter->startElement('a:ln');
$objWriter->startElement('a:noFill');
$objWriter->endElement();
$objWriter->endElement();
$objWriter->endElement();
反而。
接下来,要更改图表中绘图区域的位置,请在 Chart.php 文件中向下滚动到私有函数 _writeLayout()。
删除函数中除开/关括号外的所有代码{}
。在函数中,添加:
$layoutTarget = "inner";
$xMode = "edge";
$yMode = "edge";
$xOffset = 0.1; //The left margin in percentage of graph width.
$yOffset = 0.1; //The top margin in percentage of graph width.
$paWidth = 0.9; //The percentage width of the plot area relative to the graph width;
$paHeight = 0.9; //The percentage height of the plot area relative to the graph height;
$objWriter->startElement('c:layout');
$objWriter->startElement('c:manualLayout');
$objWriter->startElement('c:layoutTarget');
$objWriter->writeAttribute('val',$layoutTarget);
$objWriter->endElement();
$objWriter->startElement('c:xMode');
$objWriter->writeAttribute('val',$xMode);
$objWriter->endElement();
$objWriter->startElement('c:yMode');
$objWriter->writeAttribute('val',$yMode);
$objWriter->endElement();
$objWriter->startElement('c:x');
$objWriter->writeAttribute('val',$xOffset);
$objWriter->endElement();
$objWriter->startElement('c:y');
$objWriter->writeAttribute('val',$yOffset);
$objWriter->endElement();
$objWriter->startElement('c:w');
$objWriter->writeAttribute('val',$paWidth);
$objWriter->endElement();
$objWriter->startElement('c:h');
$objWriter->writeAttribute('val',$paHeight);
$objWriter->endElement();
$objWriter->endElement();
$objWriter->endElement();
然后,您可以根据需要调整 x/y 偏移和 w/h。
要控制/更改每个数据系列的颜色,请在:
private function _writePlotGroup()
前:
foreach($plotSeriesOrder as $plotSeriesIdx => $plotSeriesRef) {
添加:
$ci=-1;
$colorNDX=array();
$colorNDX[0] = "111111";
$colorNDX[1] = "222222";
$colorNDX[2] = "333333";
$colorNDX[3] = "444444";
$colorNDX[4] = "555555";
$colorNDX[5] = "666666";
$colorNDX[6] = "777777";
依此类推,确保为所有系列数据添加足够的颜色索引,并将 111111,222222,333333 明显更改为您喜欢的网页颜色。
此外,之后:
foreach($plotSeriesOrder as $plotSeriesIdx => $plotSeriesRef) {
添加:
$ci++;
之后:
// Labels
$plotSeriesLabel = $plotGroup->getPlotLabelByIndex($plotSeriesRef);
if ($plotSeriesLabel && ($plotSeriesLabel->getPointCount() > 0)) {
$objWriter->startElement('c:tx');
$objWriter->startElement('c:strRef');
$this->_writePlotSeriesLabel($plotSeriesLabel, $objWriter);
$objWriter->endElement();
$objWriter->endElement();
}
添加:
$objWriter->startElement('c:spPr');
$objWriter->startElement('a:solidFill');
$objWriter->startElement('a:srgbClr');
$objWriter->writeAttribute('val',$colorNDX[$ci]);
$objWriter->endElement();
$objWriter->endElement();
$objWriter->endElement();
让我知道这是否有帮助。同样,这些更改将应用于 PHPExcel 生成的所有图表,但是,一对放置得当的if
语句应该足以使更改在每个图表类型的基础上更加动态。