使用phpExcel在pdf中绘制条形图

使用phpExcel在pdf中绘制条形图,php,phpexcel,Php,Phpexcel,我尝试执行示例33chartcreate-bar.php 这就是我在生成的pdf文件中看到的。为什么我在将文件保存为pdf时看不到图表?当我将文件生成为Excel文件时,我确实看到了图表 这是密码 $rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF; $rendererLibrary = 'mpdf60'; $rendererLibraryPath = 'libraries/' . $rendererLibrary; if (!PHP

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

这就是我在生成的pdf文件中看到的。为什么我在将文件保存为pdf时看不到图表?当我将文件生成为Excel文件时,我确实看到了图表

这是密码

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

如果您使用的是PHP,您可以集成这个JavaScript库:chartjs.org。

33chartcreate bar.PHP
不会生成PDF文件,因此,您必须修改它以使用PDF编写器:您是否修改了
33chartcreate bar.php
文件,以便为PDF渲染器和jpgraph副本正确配置它?@markbaker是的,我已经修改了。您突出显示了PHPExcel中的一个bug;我只是对1.8分支进行了一个修复,该分支将更正提供给PDF生成器的HTML标记的生成,尽管在PDF中尝试渲染时仍会出现一个损坏的图像that@MarkBakerPlease请在推送修复程序后通知我。该修复程序已推送,现在应该适用于HTML编写器,虽然我仍然需要弄清楚为什么PDF Writer会显示一个损坏的图像嘿,它可能会导出到它,值得一试。