gpt4 book ai didi

PHPExcel图形设计(边框、图形颜色、图形内部位置)

转载 作者:可可西里 更新时间:2023-11-01 13:42:59 27 4
gpt4 key购买 nike

我正在使用 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文件。

要更改图表的边框颜色和厚度,请在公共(public)函数 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); 

应该在 Chart.php 文件的第 106 行附近。

显然,将“000000”替换为您希望成为图表边框颜色的任何 Web 颜色。要完全删除边框颜色,请插入:

$objWriter->startElement('c:spPr');
$objWriter->startElement('a:ln');
$objWriter->startElement('a:noFill');
$objWriter->endElement();
$objWriter->endElement();
$objWriter->endElement();

相反。

接下来,要更改绘图区域在图表中的位置,请在 Chart.php 文件中向下滚动到私有(private)函数 _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 语句应该足以使每个图表类型的更改更加动态。

关于PHPExcel图形设计(边框、图形颜色、图形内部位置),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18612897/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com