PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 5, 2022

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

 October 05, 2022     php, phpexcel     No comments   

Issue

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.


Solution

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.



Answered By - Bas van Stein
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing