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

How can i change the style of the line chart generated with phpexcel?

我正在使用 Github library 中的示例生成折线图。

我想要的是为图表中的每一行设置一些自定义样式的选项,就像我们在 excel sheet 中手动设置的一样:

Select 图表中的线条,格式化数据系列,然后:

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

$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));

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

第一个问题得到标记选项 > None:

在文件 PHPExcel/Chart/Renderer/jpgraph.php 的第 287 行:

$marker = $this->_chart->getPlotArea()->getPlotGroupByIndex($groupID)->getPlotValuesByIndex($i)->getPointMarker();

把这个改成

$marker = 'none';

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

您也可以删除或编辑 Excel2007/Chart.php line 792 处的代码,这里您可以看到 if ($plotSeriesMarker) 代码,将其更改为 "none" 或只删除这部分.


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

在文件中PHPExcel/Writer/Excel2007/Chart.php
在函数 _writePlotGroupline 781 处可以看到画线代码

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

在这里你可以改变宽度,例如:

$objWriter->writeAttribute('w', '40000'); 

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

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)

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

如果由于某种原因不起作用,您可以在 Chart.php 中找到 line 272

$objWriter->writeAttribute('val', (integer) $plotGroup->getSmoothLine() );

将此更改为

$objWriter->writeAttribute('val', 1 );

它肯定会起作用。

在此回复时您的第一个问题的解决方案; Marker Options > None 很简单,不涉及修改基本代码。创建 $dataSeriesValues 对象时(第 86 行),您可以定义所需的标记类型或 'none'.

$dataSeriesValues = array(
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B:$B', null, 4, array(), 'none'),
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C:$C', null, 4, array(), 'none'),
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D:$D', null, 4, array(), 'none'),
);