5

我正在使用 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);   

有没有办法做到这一点自定义图表?

4

6 回答 6

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语句应该足以使更改在每个图表类型的基础上更加动态。

于 2013-10-16T06:17:22.800 回答
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 出错并允许您为折线图着色

于 2014-01-10T11:43:35.207 回答
1

在我的情况下,我想更改饼图的原始颜色,我可以通过编辑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');
于 2016-09-08T13:51:18.150 回答
1

只花了几个小时看一下-> 更改图表的颜色。

打开文件 Theme.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();
    }
}

希望这对你有用:-)

于 2017-05-05T14:07:00.317 回答
0

当前版本:PHPExcel 1.7.9 不允许做任何你想做的事情。

于 2013-09-06T12:38:18.083 回答
0

在饼图中,我发现 PHPExcel 仅写入 1 个来自 serie 的数据点,编号 3(为什么不是 5,或 1,我不知道 =)),因此如果您想自定义饼图的颜色,您必须编辑文件 Classes/函数_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]);

于 2015-04-05T21:38:46.333 回答