繁体   English   中英

使用phpExcel以pdf格式绘制Barchart

[英]draw Barchart in a pdf using phpExcel

我试图执行示例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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM