PHPOffice / PHPExcel

ARCHIVED
Other
11.46k stars 4.19k forks source link

How to display a chart onto the webbrowser? #641

Open anandcu3 opened 9 years ago

anandcu3 commented 9 years ago

I have used phpexcel to create a chart and download the xlsx file containing the chart. I want to however display the chart . How can I do this? Is this possible?

$objPHPExcel = new PHPExcel ();
$objWorksheet = $objPHPExcel->getActiveSheet ();
$objWorksheet->fromArray ( $data_array );
$dataseriesLabels = array (
new PHPExcel_Chart_DataSeriesValues ( 'String', 'Worksheet!$A$2', null, 3 ) ,
new PHPExcel_Chart_DataSeriesValues ( 'String', 'Worksheet!$A$3', null, 3 ),
new PHPExcel_Chart_DataSeriesValues ( 'String', 'Worksheet!$A$4', null, 3 )
 );
$xAxisTickValues = array (
new PHPExcel_Chart_DataSeriesValues ( 'String', 'Worksheet!$B$1:$F$1', null, 3 ), 
);

$dataSeriesValues = array (
new PHPExcel_Chart_DataSeriesValues ( 'Number', 'Worksheet!$B$2:$G$2', null, 6 ),
new PHPExcel_Chart_DataSeriesValues ( 'Number', 'Worksheet!$B$3:$G$3', null, 6 ),
new PHPExcel_Chart_DataSeriesValues ( 'Number', 'Worksheet!$B$4:$G$4', null, 6 )

);

  $series = new PHPExcel_Chart_DataSeries ( PHPExcel_Chart_DataSeries::TYPE_BARCHART, //    plotType
PHPExcel_Chart_DataSeries::GROUPING_STANDARD, // plotGrouping
range ( 0, count ( $dataSeriesValues ) - 1 ), // plotOrder
$dataseriesLabels, // plotLabel
$xAxisTickValues, // plotCategory
$dataSeriesValues ); // plotValues

$series->setPlotDirection ( PHPExcel_Chart_DataSeries::DIRECTION_COL );
$plotarea = new PHPExcel_Chart_PlotArea ( null, array ( $series) );
$legend = new PHPExcel_Chart_Legend ( PHPExcel_Chart_Legend::POSITION_RIGHT, null, false     );
$title = new PHPExcel_Chart_Title ( 'Vendor Comparison Chart' );
$yAxisLabel = new PHPExcel_Chart_Title ( 'Rating' );

$chart = new PHPExcel_Chart ( 'chart1', $title, $legend,$plotarea,true, 0, null,$yAxisLabel ); 

$chart->setTopLeftPosition ( 'A7' );
$chart->setBottomRightPosition ( 'Q28' );

 $objWorksheet->addChart ( $chart );
$objWriter = PHPExcel_IOFactory::createWriter ( $objPHPExcel, 'HTML' );
$objWriter->setIncludeCharts ( TRUE );
$objWriter->save ( "php://output" );

This displays only the table data but not the chart. How do I display the chart also?

doshimehul commented 9 years ago

Hello , Even I am trying to achieve this but facing problem, one additional step which I have tried as per documentation is to set Chart Renderer using following syntax,

$rendererName = PHPExcel_Settings::CHART_RENDERER_JPGRAPH;
$rendererLibrary = 'jpgraph3.5.0b1/src';
$rendererLibraryPath = '/php/libraries/Charts/' . $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'
    );
}

you can check file phpoffice/phpexcel/Examples/36chartreadwriteHTML.php

Let me know if that works for you!!

I am also looking to export chart into PDFs.

liuyang1 commented 8 years ago

It doesn't work. error msg: 15:14:15 Load Test from Excel2007 file 36writeLineChart1.xlsx 15:14:15 Iterate worksheets looking at the charts Worksheet: Data chart1 - "Sales" lineChart 15:14:15 Write Tests to HTML file

Strict Standards: Non-static method JpGraphError::Install() should not be called statically in /usr/share/jpgraph/jpgraph.php on line 243

Fatal error: Call to undefined method Legend::SetPos() in /home/liuyang/tars/PHPExcel/Classes/PHPExcel/Chart/Renderer/jpgraph.php on line 189

Any suggestion?