我正在使用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);
有没有办法自定义图表?
答案 0 :(得分:7)
正如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
语句应该足以使每个图表类型的更改更加动态。
答案 1 :(得分:2)
使用LineCharts时,添加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出错并允许您使用彩色线图
答案 2 :(得分:1)
在我的情况下,我想要更改为饼图的原始颜色,我能够通过编辑PHPExcel_Writer_Excel2007_Theme
类而无需编辑其原始作者 Excel2007 来实现此目的:< / p>
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');
答案 3 :(得分:1)
花了几个小时看看 - &gt;改变图形的颜色。
打开文件Theme.php \ PHPExcel \类\ PHPExcel \作家\ 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();
}
}
希望这对你有用: - )
答案 4 :(得分:0)
当前版本:PHPExcel 1.7.9不允许执行任何操作。
答案 5 :(得分:0)
在饼图中我发现PHPExcel只从serie写了1个数据点,3号(为什么不是5,或1,我不知道=)),因此如果你想自定义馅饼的颜色,你必须编辑函数_writePlotGroup中的文件类/ 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]);