到目前为止,我已经通过PHPExcel成功地将数据从Mysql导出到Excel中,制作了我的表格,格式化了,而且一切都很笨拙。
然而,到目前为止,从所述表格制作图表的任何尝试都失败了。不仅仅是在Google搜索结束几天之后,我还没有找到关于如何从MySQL填充图表值的简单示例/教程。
这一切归结为:
Taken from https://github.com/affinitybridge/phpexcel/blob/master/Tests/33chartcreate-pie.php
/** PHPExcel */
include 'PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objWorksheet = $objPHPExcel->getActiveSheet();
$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),
)
);
我该如何填充$ objWorksheet-> fromArray来自a)Mysql或b)excel。
如果是SQL,我需要为我数据库中的所有表运行此查询。
$ sql =" SELECT Functional_Area
,Passed__Status
,Blocked__Status
,Failed__Status
FROM $ tbl_name WHERE 1;
上下文:我有几个功能区,每个功能区在Passed / Failed / Blocked中都有不同的值。每个功能区域需要1个图表。平均而言,每张表有14个功能区。
然后,我会为每个功能区构建一个饼图(或条形图,我并不挑剔)。
选项2)Excel
由于我已经在excel中说过数据,我需要填充以下数据:
同样,每个ROW(ROW 5(FA = EPG)的1个图形将是1个图形,ROW 6(FA = VOD)将是另一个图形等等。)
这必须在每个工作表中重复...
到目前为止我的尝试在下面,这为每个工作表(这很好)产生了一个图表,但是它是空的(不好)。
foreach (glob("*.xls") as $f)
{
$inputFileName = $f;
echo "Found $inputFileName <br/>";
}
// Read your Excel workbook
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
$i = 0;
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)
{
$arrayData[$worksheet->getTitle()] = $worksheet->toArray();
echo "Title: ".$worksheet->getTitle()."<br/>";
$wsTitle = $worksheet->getTitle();
$objWorksheet = $objPHPExcel->setActiveSheetIndexByName($wsTitle);
$highestRow = $worksheet->getHighestRow(); // e.g. 10
$endofData = $highestRow -1;
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$colNumberStart = PHPExcel_Cell::columnIndexFromString("D");
$BeforeTOT = $colNumberStart - 2;
$TOT = $colNumberStart - 1;
echo "Highest Row is ==> $highestRow <br/>End of Data - header is $endofData <br/> Highest Col is ==> $highestColumn <br/> Start col is ==> $colNumberStart and Highest Col end is ==> $highestColumnIndex <br/>";
// 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' . $i} = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$4', null, 1), // 2010
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$G$4', null, 1), // 2011
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$H$4', null, 1), // 2012
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$I$4', null, 1), // 2012
);
displayArray(${'dataseriesLabels' . $i});
// Set the X-Axis Labels
// Datatype
// Cell reference for data
// Format Code
// Number of datapoints in series
// Data values
// Data Marker
${'xAxisTickValues' . $i} = array(
new PHPExcel_Chart_DataSeriesValues('String', 'Worksheet!$D$4:$H$4', null, 19), // Q1 to Q4
);
displayArray(${'xAxisTickValues' . $i});
// 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' . $i} = array(
new PHPExcel_Chart_DataSeriesValues('Number', 'Worksheet!$G$5:$I$5', null, 4),
);
displayArray(${'dataSeriesValues' . $i});
// Build the dataseries
${'series' . $i} = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_PIECHART, // plotType
PHPExcel_Chart_DataSeries::GROUPING_PERCENT_STACKED, // plotGrouping
range(0, count(${'dataseriesLabels' . $i})-1), // plotOrder
${'dataseriesLabels' . $i}, // plotLabel
${'xAxisTickValues' . $i}, // plotCategory
${'dataSeriesValues' . $i} // plotValues
);
displayArray(${'series' . $i});
// Set the series in the plot area
${'plotarea' . $i} = new PHPExcel_Chart_PlotArea(null, array(${'series' . $i}));
// Set the chart legend
${'legend' . $i} = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_TOPRIGHT, null, false);
${'title' . $i} = new PHPExcel_Chart_Title('Test %age-Stacked Area Chart');
${'yAxisLabel' . $i} = new PHPExcel_Chart_Title('Value ($k)');
// Create the chart
${'chart' . $i} = new PHPExcel_Chart(
'chart1', // name
${'title' . $i}, // title
${'legend' . $i}, // legend
${'plotarea' . $i}, // plotArea
true, // plotVisibleOnly
0, // displayBlanksAs
null, // xAxisLabel
${'yAxisLabel' . $i} // yAxisLabel
);
// Set the position where the chart should appear in the worksheet
${'chart' . $i}->setTopLeftPosition('A7');
${'chart' . $i}->setBottomRightPosition('H20');
// Add the chart to the worksheet
$objWorksheet->addChart(${'chart' . $i});
// 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
// Save Excel 2007 file
echo date('H:i:s') , " Write to Excel2007 format" , EOL;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
// Echo memory peak usage
echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;
// Echo done
echo date('H:i:s') , " Done writing file" , EOL;
echo 'File has been created in ' , getcwd() , EOL;
$i++;
}
答案 0 :(得分:0)
一个。如果数据在Excel上,则无需从Mysql查询 湾这是一个有效的代码片段,100%的时间用于填充之前已填充的工作表的堆积条形图:
// 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
$dataseriesLabels1 = array(
new PHPExcel_Chart_DataSeriesValues('String', 'report!$E$4', NULL, 4), // Jan to Dec
new PHPExcel_Chart_DataSeriesValues('String', 'report!$F$4', NULL, 4), // Jan to Dec
new PHPExcel_Chart_DataSeriesValues('String', 'report!$G$4', NULL, 4) // Jan to Dec
);
// 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', 'report!$C$4:$C$20', NULL, 4) // Jan to Dec
);
// 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
$dataSeriesValues1 = array(
new PHPExcel_Chart_DataSeriesValues('Number', 'report!$E$5:$E$20', NULL, 16),
new PHPExcel_Chart_DataSeriesValues('Number', 'report!$F$5:$F$20', NULL, 16),
new PHPExcel_Chart_DataSeriesValues('Number', 'report!$G$5:$G$20', NULL, 16)
);
// Build the dataseries
$series1 = new PHPExcel_Chart_DataSeries(
PHPExcel_Chart_DataSeries::TYPE_BARCHART, // plotType
PHPExcel_Chart_DataSeries::GROUPING_STACKED, // plotGrouping
range(0, count($dataSeriesValues1)-1), // plotOrder
$dataseriesLabels1, // plotLabel
$xAxisTickValues, // plotCategory
$dataSeriesValues1 // plotValues
);
// Set additional dataseries parameters
// Make it a vertical column rather than a horizontal bar graph
$series1->setPlotDirection(PHPExcel_Chart_DataSeries::DIRECTION_COL);
// Set the series in the plot area
$plotarea = new PHPExcel_Chart_PlotArea(NULL, array($series1));
// Set the chart legend
$legend = new PHPExcel_Chart_Legend(PHPExcel_Chart_Legend::POSITION_RIGHT, NULL, false);
$title = new PHPExcel_Chart_Title('Average Weather Chart for Crete');
// Create the chart
$chart = new PHPExcel_Chart(
'chart1', // name
$title, // title
$legend, // legend
$plotarea, // plotArea
true, // plotVisibleOnly
0, // displayBlanksAs
NULL, // xAxisLabel
NULL // yAxisLabel
);
// Set the position where the chart should appear in the worksheet
$chart->setTopLeftPosition('F2');
$chart->setBottomRightPosition('O16');
// Add the chart to the worksheet
$objWorksheet->addChart($chart);
// Instantiate a Writer to create an OfficeOpenXML Excel .xlsx file
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->setIncludeCharts(true);
d。因此,要自动创建图形,过程将是: D1。打开w / e工作表,您需要从中获取数据 D2。读取或硬编码标签,x和y轴的值 D3。创建Chart元素 D4。发布所述元素w / e是理想的。
希望这可以帮助其他一些可怜的编码人员解决PHPExcel + Graphs的麻烦
=============================================== ===========================
更新25/9
我正在尝试动态构建数组,但有点失败。
Fellas, question.
我现在正试图动态填充它,换句话说,构建:
$dataseriesLabels1 = "array(";
foreach ($Labels as $k => $s)
{
echo "Parsing $s <br/>";
$dataseriesLabels1 .= "new PHPExcel_Chart_DataSeriesValues('String', ".$s.", NULL, 4),";
// echo "We have $dataseriesLabels1 in this loop <br/>";
}
$dataseriesLabels1 = rtrim($dataseriesLabels1, "," );
$dataseriesLabels1 .= ");";
echo "BUilt : $dataseriesLabels1 <br/>";
这一切都很好,因为它为我创造了:
array(new PHPExcel_Chart_DataSeriesValues('String', 'Sprint!$D$4:$D$16', NULL, 4),new PHPExcel_Chart_DataSeriesValues('String', 'Sprint!$E$4:$E$16', NULL, 4),new PHPExcel_Chart_DataSeriesValues('String', 'Sprint!$F$4:$F$16', NULL, 4),new PHPExcel_Chart_DataSeriesValues('String', 'Sprint!$G$4:$G$16', NULL, 4),new PHPExcel_Chart_DataSeriesValues('String', 'Sprint!$H$4:$H$16', NULL, 4));
不幸的是,这被解释为字符串,而不是数组,因此图表生成失败:/
想法?