提问人:Eamonn 提问时间:11/7/2023 更新时间:11/22/2023 访问量:45
PHPSpreadsheet worksheeet 中的多个条形图
PHPSpreadsheet multiple barcharts in worksheeet
问:
我正在尝试在 PHPSpreadsheet 中的单个工作表上呈现多个条形图。我使用记录的示例中的代码作为起点。(现在)没有抛出任何错误,但图表没有呈现。
初始数据集如下:
[
[0] =>
[
[title] => How did you find Session 1?
[total] => 49
[options] =>
[
[very helpful] => 31
[helpful] => 16
[not helpful] => 2
]
]
[1] =>
[
[title] => How did you find Session 2 (if applicable)?
[total] => 49
[options] =>
[
[very helpful] => 26
[helpful] => 18
[not helpful] => 5
]
]
[2] =>
[
[title] => How did you find Session 3?
[total] => 49
[options] =>
[
[very helpful] => 27
[helpful] => 19
[not helpful] => 3
]
]
]
我有一个初始工作表,我只列出了一些参与数据,然后我转到下一个条形图的工作表。
if(!empty($barcharts))
{
$alphabet = Helper::alphabet(true, 2); // gives me an uppercase alphabet, iterated twice: ...X,Y,Z,AA,AB,AC, etc...
$chart_left_col_index = 0;
$chart_col_width = 8;
// blue, orange, red
$colors = [
'00abb8', 'eb8500', 'b8292f',
];
$sheet = $excel_obj->createSheet();
$sheet->setTitle('Charts');
$barcharts = array_values($barcharts);
foreach($barcharts as $i => $barchart)
{
$bars = [];
foreach($barchart['options'] as $key => $num)
$bars[] = [$key, $num];
$num_points = count($bars);
$start_row = 1; $end_row = $num_points;
$chart_keys_col = $alphabet[$chart_left_col_index];
$chart_values_col = $alphabet[$chart_left_col_index + 1];
$chart_right_col = $alphabet[$chart_left_col_index + $chart_col_width];
$row_count = $start_row;
foreach($bars as $row)
{
$sheet->setCellValue($chart_keys_col . $row_count, $row[0]);
$sheet->setCellValue($chart_values_col . $row_count, $row[1]);
$row_count++;
}
$series_labels = [
//new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$C$1', null, 1), // not applicable
];
$x_axis_tick_keys = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING,
"Worksheet!${$chart_keys_col}${$start_row}:${$chart_keys_col}${$end_row}",
null,
$num_points),
];
$series_values = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER,
"Worksheet!${$chart_values_col}${$start_row}:${$chart_values_col}${$end_row}",
null,
$num_points,
[],
null,
$colors),
];
// Build the dataseries
$series = new DataSeries(
DataSeries::TYPE_BARCHART, // plotType
null,
range(0, count($series_values) - 1), // plotOrder
$series_labels, // plotLabel
$x_axis_tick_keys, // plotCategory
$series_values // plotValues
);
// Set up a layout object for the chart
$layout = new Layout();
$layout->setShowVal(true);
$layout->setShowPercent(true);
// Set the series in the plot area
// Set the chart legend
$plot_area = new PlotArea($layout, [$series]);
$legend = new Legend(Legend::POSITION_RIGHT, null, false);
$title = new Title($barchart['title']);
$chart = new Chart(
"chart{$i}", // name
$title, // title
$legend, // legend
$plot_area, // plotArea
true, // plotVisibleOnly
DataSeries::EMPTY_AS_GAP, // displayBlanksAs
null, // xAxisLabel
null // yAxisLabel
);
// Set the position where the chart should appear in the worksheet
$chart->setTopLeftPosition($chart_keys_col . '10');
$chart->setBottomRightPosition($chart_right_col . '30');
// Add the chart to the worksheet
$sheet->addChart($chart);
$chart_left_col_index = $chart_left_col_index + $chart_col_width + 1;
}
}
数据集正在正确写入工作表:
工作表坐标字符串在回显时是正确的。我像这样保存文件:
$objWriter = new Xlsx($excel_obj);
$objWriter->save($exports_path . O_DE . $file_name);
$url = O_URL_APPS . 'dashboard/assets/exports/' . $file_name;
$return = ['url'=>$url];
我必须进行哪些更正才能呈现图表?
答:
1赞
Oluwafemi Sule
11/22/2023
#1
PhpOffice\PhpSpreadsheet\Writer\Xlsx 编写器公开了一个 setIncludeCharts 方法,用于将其配置为在生成的电子表格中包含图表。
$objWriter->setIncludeCharts(true);
$objWriter->save($exports_path . O_DE . $file_name);
评论