使用phpExcel在pdf中绘制条形图

时间:2015-08-03 16:32:19

标签: php phpexcel

我尝试执行示例33chartcreate-bar.php

这就是我在生成的pdf文件中看到的内容。为什么在将文件保存为pdf时没有看到图表?我在将文件生成为Excel文件时看到了图表。

enter image description here

enter image description here

这是代码

$rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
$rendererLibrary = 'mpdf60';
$rendererLibraryPath = 'libraries/' . $rendererLibrary;


if (!PHPExcel_Settings::setPdfRenderer(
        $rendererName,
        $rendererLibraryPath
    )) {
    die(
            'NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
            EOL .
            'at the top of this script as appropriate for your directory structure'
        );
}


//  Change these values to select the Rendering library that you wish to use
//      for Chart images, and its directory location on your server
$rendererName = PHPExcel_Settings::CHART_RENDERER_JPGRAPH;
$rendererLibrary = 'jpgraph-3.5.0b1/jpgraph-3.5.0b1/src';
$rendererLibraryPath = 'libraries/' . $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'
        );
    }


$objPHPExcel = new PHPExcel();
$objWorksheet = $objPHPExcel->getActiveSheet();

$inputFileName = 'writeBarChart.pdf';
$objWorksheet->fromArray(
    array(
        array('',   2010,   2011,   2012),
        array('Q1',   12,   15,     21),
        array('Q2',   56,   73,     86),
        array('Q3',   52,   61,     69),
        array('Q4',   30,   32,     0),
    )
);


$dataSeriesLabels = array(
    new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$B$1', NULL, 1),   //  2010
    new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$C$1', NULL, 1),   //  2011
    new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$1', NULL, 1),   //  2012
);

$xAxisTickValues = array(
    new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$A$2:$A$5', NULL, 4),  //  Q1 to Q4
);

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


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

$series->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_BAR);


$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('Test Bar Chart');
$yAxisLabel = new PHPExcel_Chart_Title('Value ($k)');

$chart = new PHPExcel_Chart(
    'chart1',       // name
    $title,         // title
    $legend,        // legend
    $plotArea,      // plotArea
    true,           // plotVisibleOnly
    0,              // displayBlanksAs
    NULL,           // xAxisLabel
    $yAxisLabel     // yAxisLabel
);

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

$objWorksheet->addChart($chart);

echo date('H:i:s') , " Write to Excel2007 format" , EOL;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->setIncludeCharts(TRUE);
$outputFileName = $inputFileName;

$objWriter->save($outputFileName);
echo date('H:i:s') , " File written to " , $outputFileName , EOL;

$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);

1 个答案:

答案 0 :(得分:0)

如果您使用PHP,则可以集成此JavaScript库:chartjs.org。