如何更改使用phpexcel生成的折线图的样式?

sha*_*nth 14 php phpexcel

我正在使用Github库中的示例生成折线图.

我想要的是为图表中的每一行设置几个自定义样式的选项,因为我们在excel表中手动执行,如:

在图表中选择Line,格式化数据系列,然后:

  • 标记选项>无
  • 线型>宽度> 2.5pt(默认为1pt)
  • 线条样式>平滑线条

如何为折线图中生成的线设置以上三个选项?到目前为止,这是我的代码,用于设置图表的布局和数据系列:

$series = new PHPExcel_Chart_DataSeries(
        PHPExcel_Chart_DataSeries::TYPE_LINECHART,
        PHPExcel_Chart_DataSeries::GROUPING_STANDARD,
        range(0, count($dataSeriesValues)-1),       
        $dataseriesLabels,                          
        $xAxisTickValues,                           
        $dataSeriesValues                       
    );

    $layout1 = new PHPExcel_Chart_Layout();
    $layout1->setShowVal(TRUE);

    $plotarea = new PHPExcel_Chart_PlotArea($layout1, array($series));
Run Code Online (Sandbox Code Playgroud)

有人可以提供提示,因为我在示例中找不到它.

Bas*_*ein 8

关于获得标记选项>无的第一个问题:

在PHPExcel/Chart/Renderer/jpgraph.php287行的文件中:

$marker = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotValuesByIndex($i)->getPointMarker();
Run Code Online (Sandbox Code Playgroud)

将此更改为

$marker = 'none';
Run Code Online (Sandbox Code Playgroud)

要删除标记,请注意这是一个有点hacky修复,通常它会循环所有标记,并且工具中没有自己设置标记的功能.(这就是为什么你在例子中找不到它)

您还可以在Excel2007/Chart.php 第792行删除或编辑代码,在这里您可以看到if ($plotSeriesMarker)代码,将其更改为"none"或仅删除此部分.


对于第二个问题:线型>宽度> 2.5pt(默认为1pt)

在该文件PHPExcel/Writer/Excel2007/Chart.php
在线路781在功能上_writePlotGroup可以看到线绘制代码

if ($groupType == PHPExcel_Chart_DataSeries::TYPE_LINECHART) {
    $objWriter->startElement('c:spPr');
        $objWriter->startElement('a:ln');
            $objWriter->writeAttribute('w', 12700);
        $objWriter->endElement();
    $objWriter->endElement();
}
Run Code Online (Sandbox Code Playgroud)

在这里,您可以使用例如更改宽度:

$objWriter->writeAttribute('w', '40000'); 
Run Code Online (Sandbox Code Playgroud)

对于第三个问题:线型>平滑线

构造函数PHPExcel_Chart_DataSeries是

/**
* Create a new PHPExcel_Chart_DataSeries
*/
public function __construct($plotType = null, $plotGrouping = null, $plotOrder = array(), $plotLabel = array(), $plotCategory = array(), $plotValues = array(), $smoothLine = null, $plotStyle = null)
Run Code Online (Sandbox Code Playgroud)

所以在你之后$dataSeriesValues你可以定义一个smoothline和一个plotStyle值.
传递true给smoothline参数可以为您提供流畅的线条样式.

如果由于某种原因不起作用,Chart.php你可以在第272行找到

$objWriter->writeAttribute('val', (integer) $plotGroup->getSmoothLine() );
Run Code Online (Sandbox Code Playgroud)

将此更改为

$objWriter->writeAttribute('val', 1 );
Run Code Online (Sandbox Code Playgroud)

它应该是肯定的.