14

I am generating a line chart using the example from Github library.

What i want to have is the option to set few custom styles for each of the lines in the chart, as we do manually in the excel sheet like:

Select Line in the chart, Format Data Series, then:

  • Marker Options > None
  • Line Style > Width > 2.5pt (1pt is default)
  • Line Style > Smoothed line

How can i set the above three options for the lines generated in the line chart? So far, here is my code to set the layout and data series for the chart:

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

Can someone provide a hint, as i can't find it in the example.

shasi kanth
  • 6,987
  • 24
  • 106
  • 158
  • Not sure eh but couldn't this be useful to you? Seems a similar problem: http://stackoverflow.com/questions/18612897/phpexcel-graph-design-border-graph-color-graph-inner-position – nowhere Sep 30 '15 at 07:23

2 Answers2

8

For your first question to get Marker Options > None:

In the file PHPExcel/Chart/Renderer/jpgraph.php on line 287:

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

Change this into

$marker = 'none';

To get rid of the markers, note that this is a bit hacky fix, normally it loops trough all markers and there is no functionality made in the tool to set the marker yourself. (that is why you cannot find it in the examples)

You can also remove or edit the code at Excel2007/Chart.php line 792, here you can see the if ($plotSeriesMarker) code, change it to "none" or just delete this part.


For your second question: Line Style > Width > 2.5pt (1pt is default)

In the file PHPExcel/Writer/Excel2007/Chart.php
At line 781 in function _writePlotGroup you can see the line plotting code

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

Here you can change the width by using for example:

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

For your third question: Line Style > Smoothed line

The construct function of PHPExcel_Chart_DataSeries is

/**
* 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)

So after your $dataSeriesValues you can define a smoothline and a plotStyle value.
Passing true to the smoothline parameter gives you a smooth line style.

If for some reason that does not work, in the Chart.php you can find on line 272

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

Change this to

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

And it should work for sure.

Community
  • 1
  • 1
Niki van Stein
  • 10,564
  • 3
  • 29
  • 62
  • Unfortunately the first and third solutions do not work in my case. – shasi kanth Oct 06 '15 at 07:05
  • Changed the first solution with an alternative – Niki van Stein Oct 06 '15 at 07:08
  • Added an alternative solution for 3 as well, and also found out how to do your second question, hope it works! – Niki van Stein Oct 06 '15 at 07:17
  • Thanks Stein, the first and second solutions worked great. For the first solution, i had to change: **$plotSeriesMarker = $plotSeriesValues->getPointMarker();** to **$plotSeriesMarker = 'none';** Just experimenting with the solution for the smooth line. It does not work. – shasi kanth Oct 07 '15 at 07:22
3

At the time of this response the solution to your first question; Marker Options > None is simple and doesn't involve modifying base code. When creating $dataSeriesValues objects(line 86) you can define which type of marker you want or 'none'.

$dataSeriesValues = array(
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$5', null, 4, array(), 'none'),
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$5', null, 4, array(), 'none'),
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$5', null, 4, array(), 'none'),
);
C. Monroy
  • 31
  • 2
  • You could also call the `setPointMarker` function for each PHPExcel_Chart_DataSeriesValues: `$dataSeriesValues[0]->setPointMarker('none');` – burgund Mar 13 '17 at 15:13