如何固定图表的y轴的初始值

时间:2020-10-26 10:23:50

标签: php phpspreadsheet phpoffice

(分组的)第一个条形的尺寸超过某个阈值(在此示例中为25)时,y轴而不是从值0开始,而是从任意值开始,在当前示例中为23。 同样的情况发生在更多的柱线上,但取值不同并且取决于它们的组合方式。 有没有办法确定轴的起始值?

在代码下方复制问题。

<?php

require_once(__DIR__ . '/../../../cron/vendor/autoload.php');

use PhpOffice\PhpSpreadsheet\Chart\Chart;
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
use PhpOffice\PhpSpreadsheet\Chart\Legend;
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
use PhpOffice\PhpSpreadsheet\Chart\Title;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

$spreadsheet = new Spreadsheet();

/**********************************************************************************/


$arrayTable = [
    ['a',26,5,0,0,0]
];

$spreadsheet->getActiveSheet()->fromArray(
    $arrayTable,
    null,
    'A4',
    true
);

$labels = ['range1', 'range2', 'range3', 'range4', 'range5'];
$dataSeriesLabels = []; 
foreach ($labels as $label) {     
    $dataSeriesLabels[] = new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, $label, null, 1);
}

$dataSource = 'Worksheet!$A$4:$A$4';
$xAxisTickValues = [
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, $dataSource, null, 1),
];

$dataSeriesValues = [];

$cols = ['B','C','D','E', 'F'];

foreach ($cols as $col) {
    $dataSource = 'Worksheet!$' . $col . '$4:$' . $col . '$4';
    $dataSeriesValues[] = new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, $dataSource, null, $count);
}

$series = new DataSeries(
    DataSeries::TYPE_BARCHART, // plotType
    DataSeries::GROUPING_STACKED, // plotGrouping
    range(0, count($dataSeriesValues) - 1), // plotOrder
    $dataSeriesLabels, // plotLabel
    $xAxisTickValues, // plotCategory
    $dataSeriesValues // plotValues
);

$series->setPlotDirection(DataSeries::DIRECTION_BAR);

$layout = null;
$plotArea = new PlotArea($layout, [$series]);
$legend = new Legend( Legend::POSITION_RIGHT, null, false);
$title = new Title('TEST');
$yAxisLabel = new Title('');

$chart = new Chart(
    'test', // name
    $title, // title
    $legend, // legend
    $plotArea, // plotArea
    true, // plotVisibleOnly
    'gap', // displayBlanksAs
    null, // xAxisLabel
    $yAxisLabel  // yAxisLabel
);

$chart->setTopLeftPosition('H1');
$chart->setBottomRightPosition('R18');

$spreadsheet->getActiveSheet()->addChart($chart);

// Save Excel 2007 file
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->setIncludeCharts(true);
$writer->save('test.xlsx');

chart image

0 个答案:

没有答案