Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

phpexcel

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.

like image 332
shasi kanth Avatar asked Sep 24 '15 14:09

shasi kanth


2 Answers

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.

like image 53
Bas van Stein Avatar answered Sep 20 '22 23:09

Bas van Stein


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'),
);
like image 40
C. Monroy Avatar answered Sep 20 '22 23:09

C. Monroy