我正在使用PHPExcel构建具有多个图形的Excel工作表,并尝试自定义它们。我只剩下 3 个未解决的问题:1.我希望图表没有边框。2. 我想更改图表线条的颜色。3.我想更改图表在图表区域内的位置。至于现在,这是我构建图表的方式:
$xAxisTickValues = $TruexAxisTickValues;
$series = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_LINECHART, // plotType
PHPExcel_Chart_DataSeries::GROUPING_STANDARD, // plotGrouping
range(0, 10), // plotOrder
null, // plotLabel
$xAxisTickValues, // plotCategory
$values // plotValues
);
$series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL);
$plotarea = new PHPExcel_Chart_PlotArea(null, array($series));
$chart = new PHPExcel_Chart(
'chart1', // name
null, // title
null, // legend
$plotarea, // plotArea
true, // plotVisibleOnly
0, // displayBlanksAs
null, // xAxisLabel
null // yAxisLabel
);
$chart->setTopLeftPosition('C5' );
$chart->setBottomRightPosition('J11' );
$sheet->addChart($chart);
有没有办法做到这一点 自定义图表 ?
正如Rzangue所说,PHPExcel目前没有提供一种简单的方法,但是,如果您不介意对使用PHPExcel创建的所有图形的更改进行硬编码,则可以对PHPExcel/Classes/Writer/Excel2007/Chart.php文件进行以下建议的更改。
要更改图表的边框颜色和粗细,请在公共函数 writeChart(( 中添加:
$cBorderColor = "000000";
$objWriter->startElement('c:spPr');
$objWriter->startElement('a:ln');
$objWriter->writeAttribute('w', '40000');//alters border thickness
$objWriter->startElement('a:solidFill');
$objWriter->startElement('a:srgbClr');
$objWriter->writeAttribute('val',$cBorderColor);//changes the color
$objWriter->endElement();
$objWriter->endElement();
$objWriter->endElement();
$objWriter->endElement();
后:
$objWriter->startElement('c:showDLblsOverMax');
$objWriter->writeAttribute('val', 0);
$objWriter->endElement();
$objWriter->endElement();
但之前:
$this->_writePrintSettings($objWriter);
它应该在图表.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 更改为您喜欢的 Web 颜色。
另外,在:
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
语句应该足以使更改在每个图表类型的基础上更加动态。
使用折线图时添加IIIOXIII的代码,特别是以下块导致Excel 2007为我出错
$objWriter->startElement('c:spPr');
$objWriter->startElement('a:solidFill');
$objWriter->startElement('a:srgbClr');
$objWriter->writeAttribute('val',$colorNDX[$ci]);
$objWriter->endElement();
$objWriter->endElement();
$objWriter->endElement();
首先在上面的块周围添加以下条件语句
if ($groupType !== PHPExcel_Chart_DataSeries::TYPE_LINECHART && $groupType !== PHPExcel_Chart_DataSeries::TYPE_STOCKCHART) {
// above code block
}
然后在块之后大约十几行,代码读取
if ($groupType == PHPExcel_Chart_DataSeries::TYPE_STOCKCHART) {
$objWriter->startElement('a:noFill');
$objWriter->endElement();
}
添加以下内容
$objWriter->startElement('a:solidFill');
$objWriter->startElement('a:srgbClr');
$objWriter->writeAttribute('val',$colorNDX[$ci])
$objWriter->endElement();
$objWriter->endElement();
然后,这将防止Excel出错,并允许您为折线图着色
就我而言,我想更改为饼图的原始颜色,我能够通过编辑 PHPExcel_Writer_Excel2007_Theme
类来实现这一点,而无需编辑其原始编写器 Excel2007,如下所示:
- 复制
Excel2007
文件夹并将其粘贴到具有不同名称的同一文件夹中,例如"Excel2007Custom"> -
打开
Excel2007Custom
文件夹中的所有类并重命名类,例如PHPExcel_Writer_Excel2007_Chart
将成为PHPExcel_Writer_Excel2007Custom_Chart
PHPExcel_Writer_Excel2007_Comments
将变得PHPExcel_Writer_Excel2007Custom_Comments
等等。
- 复制
Excel2007.php
文件并将其粘贴到具有不同名称的同一文件夹中,例如"Excel2007Custom.php"> - 打开类文件
Excel2007Custom.php
,然后:- 重命名类
PHPExcel_Writer_Excel2007
将变为PHPExcel_Writer_Excel2007Custom
- 更改构造函数中的数组
$writerPartsArray
值。
- 重命名类
从:
$writerPartsArray = array(
'stringtable' => 'PHPExcel_Writer_Excel2007_StringTable',
'contenttypes' => 'PHPExcel_Writer_Excel2007_ContentTypes',
'docprops' => 'PHPExcel_Writer_Excel2007_DocProps',
'rels' => 'PHPExcel_Writer_Excel2007_Rels',
'theme' => 'PHPExcel_Writer_Excel2007_Theme',
'style' => 'PHPExcel_Writer_Excel2007_Style',
'workbook' => 'PHPExcel_Writer_Excel2007_Workbook',
'worksheet' => 'PHPExcel_Writer_Excel2007_Worksheet',
'drawing' => 'PHPExcel_Writer_Excel2007_Drawing',
'comments' => 'PHPExcel_Writer_Excel2007_Comments',
'chart' => 'PHPExcel_Writer_Excel2007_Chart',
'relsvba' => 'PHPExcel_Writer_Excel2007_RelsVBA',
'relsribbonobjects' => 'PHPExcel_Writer_Excel2007_RelsRibbon'
);
自:
$writerPartsArray = array(
'stringtable' => 'PHPExcel_Writer_Excel2007Custom_StringTable',
'contenttypes' => 'PHPExcel_Writer_Excel2007Custom_ContentTypes',
'docprops' => 'PHPExcel_Writer_Excel2007Custom_DocProps',
'rels' => 'PHPExcel_Writer_Excel2007Custom_Rels',
'theme' => 'PHPExcel_Writer_Excel2007Custom_Theme',
'style' => 'PHPExcel_Writer_Excel2007Custom_Style',
'workbook' => 'PHPExcel_Writer_Excel2007Custom_Workbook',
'worksheet' => 'PHPExcel_Writer_Excel2007Custom_Worksheet',
'drawing' => 'PHPExcel_Writer_Excel2007Custom_Drawing',
'comments' => 'PHPExcel_Writer_Excel2007Custom_Comments',
'chart' => 'PHPExcel_Writer_Excel2007Custom_Chart',
'relsvba' => 'PHPExcel_Writer_Excel2007Custom_RelsVBA',
'relsribbonobjects' => 'PHPExcel_Writer_Excel2007Custom_RelsRibbon'
);
- 然后编辑类
PHPExcel_Writer_Excel2007Custom_Theme
$_colourScheme
类属性 - 最后我这样称呼班级编写器:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007Custom');
只是花了几个小时来查看 ->更改图表的颜色。
打开文件主题.php''PHPExcel''Classes''PHPExcel''Writer''Excel2007''Theme.php
在底部,您会发现:
private function writeColourScheme($objWriter)
{
foreach (self::$colourScheme as $colourName => $colourValue) {
$objWriter->startElement('a:'.$colourName);
$objWriter->startElement('a:srgbClr');
$objWriter->writeAttribute('val', $colourValue);
$objWriter->endElement();
$objWriter->endElement();
}
}
相反,您必须放置以下内容:
private function writeColourScheme($objWriter)
{
$ci = 0;
$colorNDX=array();
$colorNDX[0] = "a09a9a";
$colorNDX[1] = "1b1b1b";
$colorNDX[2] = "350d0d";
$colorNDX[3] = "ff0000";
$colorNDX[4] = "b9a8a8";
$colorNDX[5] = "a09a9a";
$colorNDX[6] = "ff0000";
$colorNDX[7] = "a09a9a";
$colorNDX[8] = "1b1b1b";
$colorNDX[9] = "ff0000";
$colorNDX[10] = "1b1b1b";
foreach (self::$colourScheme as $colourName => $colourValue) {
$objWriter->startElement('a:'.$colourName);
$objWriter->startElement('a:srgbClr');
$objWriter->writeAttribute('val', $colorNDX[$ci]);
$objWriter->endElement();
$ci++;
$objWriter->endElement();
}
}
希望这对你有用:-(
当前版本:PHPExcel 1.7.9不允许做任何你想做的事情。
在饼图中,我发现 PHPExcel 只写入 1 个来自系列的数据点,数字 3(为什么不是 5 或 1,我不知道 =(,因此如果要自定义饼的颜色,则必须在函数_writePlotGroup中编辑文件 Classes/PHPExcel/Writer/Excel2007/Charts.php
//Getting datapoints and loop around $objWrite->startElement(c:dPt)
$plotSeriesValues = $plotGroup->getPlotValuesByIndex($plotSeriesRef);
if (($groupType == PHPExcel_Chart_DataSeries::TYPE_PIECHART) ||
($groupType == PHPExcel_Chart_DataSeries::TYPE_PIECHART_3D) ||
($groupType == PHPExcel_Chart_DataSeries::TYPE_DONUTCHART)) {
foreach($plotSeriesValues->getDataValues() as $plotSeriesKey => $plotSeriesValue) {
...
/*instead of $objWriter->writeAttribute('val', 3); put after
$objWriter->startElement('c:dPt');
$objWriter->startElement('c:idx');*/
$objWriter->writeAttribute('val', $plotSeriesKey);
//according to previous answer, find the color of pie by index of datapoint in colorNDX
$objWriter->writeAttribute('val',$colorNDX[$plotSeriesKey]);