使用PHPExcel创建PHPExcel条形图

3

我已经完成了从SQL表格的数据导入到Excel的工作,但是我还想用PHPEXCEL将数据转换成柱形图。有人知道如何做吗?以下是我的PHPEXCEL代码:

$sql = "SELECT sum(Ticket) AS count, Applications FROM Temp_table GROUP BY Apps";
$result = $conn->query($sql);

if($result->num_rows > 0)
{
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);

    $rownumber = 2;

    $objPHPExcel->getActiveSheet()->setCellValue('A1', 'Applications');
    $objPHPExcel->getActiveSheet()->setCellValue('B1', 'Count');

    while($row = $result->fetch_assoc())

    {   
    $objPHPExcel->getActiveSheet()->setCellValue('A'.$rownumber, $row["Applications"]);
    $objPHPExcel->getActiveSheet()->setCellValue('B'.$rownumber, $row["count"]);
        $rownumber++;

    }
    $objPHPExcel->getActiveSheet()->setTitle('Sheet1');

    }

你看过PHPExcel图表示例吗?它们展示了如何创建大多数类型的图表。 - Mark Baker
你有相关链接吗?我查了一下,但是没太看懂。 - Rock Linux
1
图表创建示例,文件名类似于 33chartcreate-bar.php,请参见 Chart Creation examples - Mark Baker
谢谢。我今天会尝试这个。 - Rock Linux
1个回答

4
这里是条形图的代码。
//  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', 'Data!$D$1', NULL, 1), //  2011
);
//  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', 'Data!$A$2:$A$5', NULL, 6),  //  Q1 to Q4
);
//  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', 'Data!$D$2:$D$5', NULL, 6),

);
//  Build the dataseries
$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
);
//  Set additional dataseries parameters

//  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('Topic Relevance');
$yAxisLabel = new PHPExcel_Chart_Title('Points');
//  Create the chart
$chart = new PHPExcel_Chart(
  'chart1',   // name
  $title,     // title
  $legend,    // legend
  $plotArea,    // plotArea
  true,     // plotVisibleOnly
  0,        // displayBlanksAs
  NULL,     // xAxisLabel
  $yAxisLabel   // yAxisLabel
);
//  Set the position where the chart should appear in the worksheet
$chart->setTopLeftPosition('B10');
$chart->setBottomRightPosition('F25');
//  Add the chart to the worksheet
$objWorksheet->addChart($chart);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
//this line is necessary to display chart in excel
    $objWriter->setIncludeCharts(TRUE);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接