PHPSpreadsheet 工作表中的多个条形图

问题描述 投票:0回答:1

我正在尝试在 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];

我必须进行哪些更正才能呈现图表?

phpspreadsheet
1个回答
0
投票

PhpOffice\PhpSpreadsheet\Writer\Xlsx writer 公开了一个 setIncludeCharts 方法来将其配置为在生成的电子表格中包含图表。

$objWriter->setIncludeCharts(true);
$objWriter->save($exports_path . O_DE . $file_name);
© www.soinside.com 2019 - 2024. All rights reserved.