Note that there are some explanatory texts on larger screens.

plurals
  1. POFatal error for rendering PHPExcel Stock chart with jpgraph
    primarykey
    data
    text
    <p>I've been working with PHPExcel since a while, and so far it has done great rendering all the charts I needed. However, a problem came when I was trying to create and render (using jpgraph) a stock chart.</p> <p>There wasn't any example set in the download package, but I noticed that PHPExcel do support stock graph. So I looked into the chart.xml file that Excel2007 creates and used that as a hint to make an example of my own. Below are the codes:</p> <pre><code>&lt;?php /** Error reporting */ error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); date_default_timezone_set('Europe/London'); define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '&lt;br /&gt;'); date_default_timezone_set('Europe/London'); /** * PHPExcel * * Copyright (C) 2006 - 2013 PHPExcel * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * @category PHPExcel * @package PHPExcel * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel) * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL * @version ##VERSION##, ##DATE## */ /** PHPExcel */ require_once dirname(__FILE__) . '/../Classes/PHPExcel.php'; $objPHPExcel = new PHPExcel(); $objWorksheet = $objPHPExcel-&gt;getActiveSheet(); $objWorksheet-&gt;fromArray( array( array('Counts', 'Max', 'Max Threshold', 'Min Threshold', 'Min'), array('Count 10', 10, 50, 0, 5), array('Count 30', 20, 50, 0, 10), array('Count 10', 30, 50, 0, 15), array('Count 10', 10, 50, 0, 0), ), null, 'A1', true ); $objWorksheet-&gt;getStyle('B2:E5')-&gt;getNumberFormat()-&gt;setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00); // Set the Labels for each data series we want to plot // Datatype // Cell reference for data // Format Code // Number of datapoints in series // Data values // Data Marker $dataseriesLabels = array( new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', NULL, 1), //Max / High new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', NULL, 1), //Max Threshold / Opening new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$1', NULL, 1), //Min Threshold / Closing new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$E$1', NULL, 1), //Min / Low ); // Set the X-Axis Labels // Datatype // Cell reference for data // Format Code // Number of datapoints in series // Data values // Data Marker $xAxisTickValues = array( new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$5', NULL, 4), // Counts ); // Set the Data values for each data series we want to plot // Datatype // Cell reference for data // Format Code // Number of datapoints in series // Data values // Data Marker $dataSeriesValues = array( new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$5', NULL, 4), new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$5', NULL, 4), new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$5', NULL, 4), new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$E$2:$E$5', NULL, 4), ); // Build the dataseries $series = new PHPExcel_Chart_DataSeries( PHPExcel_Chart_DataSeries::TYPE_STOCKCHART, // plotType null, // plotGrouping - if we set this to not null, then xlsx throws error range(0, count($dataSeriesValues)-1), // plotOrder $dataseriesLabels, // plotLabel $xAxisTickValues, // plotCategory $dataSeriesValues // plotValues ); // Set the series in the plot area $plotarea = new PHPExcel_Chart_PlotArea(NULL, array($series)); // Set the chart legend $legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false); $title = new PHPExcel_Chart_Title('Test Stock Chart'); // Create the chart $chart = new PHPExcel_Chart( 'stock-chart', // name $title, // title $legend, // legend $plotarea, // plotArea true, // plotVisibleOnly 0, // displayBlanksAs NULL, // xAxisLabel NULL // yAxisLabel ); // Set the position where the chart should appear in the worksheet $chart-&gt;setTopLeftPosition('A7'); $chart-&gt;setBottomRightPosition('H20'); // Add the chart to the worksheet $objWorksheet-&gt;addChart($chart); $rendererName = PHPExcel_Settings::CHART_RENDERER_JPGRAPH; $rendererLibrary = 'jpgraph'; $rendererLibraryPath = '../' . $rendererLibrary; if (!PHPExcel_Settings::setChartRenderer( $rendererName, $rendererLibraryPath )) { die( 'NOTICE: Please set the $rendererName and $rendererLibraryPath values' . EOL . 'at the top of this script as appropriate for your directory structure' ); } $chart-&gt;render('chart-stock.jpg'); // Save Excel 2007 file echo date('H:i:s') , " Write to Excel2007 format" , EOL; $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter-&gt;setIncludeCharts(TRUE); $objWriter-&gt;save(str_replace('.php', '.xlsx', __FILE__)); echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL; // Echo memory peak usage echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL; // Echo done echo date('H:i:s') , " Done writing file" , EOL; echo 'File has been created in ' , getcwd() , EOL; </code></pre> <p>The code above, generates the xlsx file fine.</p> <p>But the problem arrives, when I try to render the chart using jpgraph and save it. It throws a PHP Fatal error:</p> <pre><code>Fatal error: Call to a member function getDataValues() on a non-object in I:\wamp\www\phpexcel\Classes\PHPExcel\Chart\Renderer\jpgraph.php on line 525 </code></pre> <p>I looked into the code and it seems, <code>getPlotCategoryByIndex</code> function for some reasons will not return the object. The most obvious thing I can think of is, I am not passing the plot correctly? But then again, I can not think of why and how I'd miss something in the <code>$dataSeriesValues</code>. Can anyone please guide me how to solve this?</p> <p>Also, since I am new, I am not able to put images. So here is a link to download the screenshot, php file and the generated xlsx file.</p> <p><a href="https://dl.dropboxusercontent.com/u/129745839/PHPExcel%20Stockexchange/33chartcreate-stock.zip" rel="nofollow">https://dl.dropboxusercontent.com/u/129745839/PHPExcel%20Stockexchange/33chartcreate-stock.zip</a></p> <p><strong>Found the solution:</strong></p> <p>Okay, after a lot of trying, I finally solved it.</p> <p>As it happens, the stock chart wasn't fully implemented. So I had to write the codes to complete it. Here's how to do this:</p> <p>for <code>/Classes/PHPExcel/Chart/Renderer/jpgraph.php</code> we have to replace following two functions</p> <pre><code>private function _renderPlotStock($groupID) { $seriesCount = $this-&gt;_chart-&gt;getPlotArea()-&gt;getPlotGroupByIndex($groupID)-&gt;getPlotSeriesCount(); $plotOrder = $this-&gt;_chart-&gt;getPlotArea()-&gt;getPlotGroupByIndex($groupID)-&gt;getPlotOrder(); $seriesPlots = array(); //var_dump($seriesCount); $dataValues = array(); // Loop through each data series in turn for($i = 0; $i &lt; $seriesCount; ++$i) { $dataValuesX = $this-&gt;_chart-&gt;getPlotArea()-&gt;getPlotGroupByIndex($groupID)-&gt;getPlotValuesByIndex($i)-&gt;getDataValues(); foreach($dataValuesX as $j =&gt; $dataValueX) { $dataValues[$plotOrder[$i]][$j] = $dataValueX; } } if(empty($dataValues)) { return; } $dataValuesPlot = array(); for($j = 0; $j &lt; count($dataValues[0]); $j++) { for($i = 0; $i &lt; $seriesCount; $i++) { $dataValuesPlot[] = $dataValues[$i][$j]; } } $xAxisLabel = $this-&gt;_chart-&gt;getPlotArea()-&gt;getPlotGroupByIndex($groupID)-&gt;getPlotCategoryByIndex(0)-&gt;getDataValues(); var_dump($xAxisLabel); $seriesPlot = new StockPlot($dataValuesPlot, $xAxisLabel); $seriesPlot-&gt;SetWidth(20); $this-&gt;_graph-&gt;Add($seriesPlot); } // function _renderPlotStock() </code></pre> <p>and</p> <pre><code>private function _renderStockChart($groupCount) { require_once('jpgraph_stock.php'); //$this-&gt;_renderplotara $this-&gt;_renderCartesianPlotArea('intint'); var_dump($groupCount); for($groupID = 0; $groupID &lt; $groupCount; ++$groupID) { $this-&gt;_renderPlotStock($groupID); } } // function _renderStockChart() </code></pre> <p>also, the new example would be something like this</p> <pre><code>&lt;?php /** Error reporting */ error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); date_default_timezone_set('Europe/London'); define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '&lt;br /&gt;'); date_default_timezone_set('Europe/London'); /** * PHPExcel * * Copyright (C) 2006 - 2013 PHPExcel * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * @category PHPExcel * @package PHPExcel * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel) * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL * @version ##VERSION##, ##DATE## */ /** PHPExcel */ require_once dirname(__FILE__) . '/../Classes/PHPExcel.php'; $objPHPExcel = new PHPExcel(); $objWorksheet = $objPHPExcel-&gt;getActiveSheet(); $objWorksheet-&gt;fromArray( array( array('Counts', 'Max', 'Min', 'Min Threshold', 'Max Threshold' ), array(10, 10, 5, 0, 50 ), array(30, 20, 10, 0, 50 ), array(20, 30, 15, 0, 50 ), array(40, 10, 0, 0, 50 ), array(100, 40, 5, 0, 50 ), ), null, 'A1', true ); $objWorksheet-&gt;getStyle('B2:E6')-&gt;getNumberFormat()-&gt;setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00); // Set the Labels for each data series we want to plot // Datatype // Cell reference for data // Format Code // Number of datapoints in series // Data values // Data Marker $dataseriesLabels = array( new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', NULL, 1), //Max / Open new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', NULL, 1), //Min / Close new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$1', NULL, 1), //Min Threshold / Min new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$E$1', NULL, 1), //Max Threshold / Max ); // Set the X-Axis Labels // Datatype // Cell reference for data // Format Code // Number of datapoints in series // Data values // Data Marker $xAxisTickValues = array( new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$6', NULL, 5), // Counts ); // Set the Data values for each data series we want to plot // Datatype // Cell reference for data // Format Code // Number of datapoints in series // Data values // Data Marker $dataSeriesValues = array( new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$B$2:$B$6', NULL, 5), new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$C$2:$C$6', NULL, 5), new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$D$2:$D$6', NULL, 5), new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$E$2:$E$6', NULL, 5), ); // Build the dataseries $series = new PHPExcel_Chart_DataSeries( PHPExcel_Chart_DataSeries::TYPE_STOCKCHART, // plotType null, // plotGrouping - if we set this to not null, then xlsx throws error range(0, count($dataSeriesValues)-1), // plotOrder $dataseriesLabels, // plotLabel $xAxisTickValues, // plotCategory $dataSeriesValues // plotValues ); // Set the series in the plot area $plotarea = new PHPExcel_Chart_PlotArea(NULL, array($series)); // Set the chart legend $legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false); $title = new PHPExcel_Chart_Title('Test Stock Chart'); $xAxisLabel = new PHPExcel_Chart_Title('Counts'); $yAxisLabel = new PHPExcel_Chart_Title('Values'); // Create the chart $chart = new PHPExcel_Chart( 'stock-chart', // name $title, // title $legend, // legend $plotarea, // plotArea true, // plotVisibleOnly 0, // displayBlanksAs $xAxisLabel, // xAxisLabel $yAxisLabel // yAxisLabel ); // Set the position where the chart should appear in the worksheet $chart-&gt;setTopLeftPosition('A7'); $chart-&gt;setBottomRightPosition('H20'); // Add the chart to the worksheet $objWorksheet-&gt;addChart($chart); $rendererName = PHPExcel_Settings::CHART_RENDERER_JPGRAPH; $rendererLibrary = 'jpgraph'; $rendererLibraryPath = '../' . $rendererLibrary; if (!PHPExcel_Settings::setChartRenderer( $rendererName, $rendererLibraryPath )) { die( 'NOTICE: Please set the $rendererName and $rendererLibraryPath values' . EOL . 'at the top of this script as appropriate for your directory structure' ); } if(file_exists('chart-stock.jpg')) { unlink('chart-stock.jpg'); } $chart-&gt;render('chart-stock.jpg'); // Save Excel 2007 file echo date('H:i:s') , " Write to Excel2007 format" , EOL; $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter-&gt;setIncludeCharts(TRUE); $filename = str_replace('.php', '.xlsx', __FILE__); if(file_exists($filename)) { unlink($filename); } $objWriter-&gt;save($filename); echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL; // Echo memory peak usage echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL; // Echo done echo date('H:i:s') , " Done writing file" , EOL; echo 'File has been created in ' , getcwd() , EOL; </code></pre> <p>Hope someone finds it useful.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload