You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHPspreadsheet的3D簇状条形图中设置数据标签为粗体

问题:PHPspreadsheet 1.23版本中设置3D簇状条形图数据标签为粗体

我已使用PHPspreadsheet 1.23版本创建出3D簇状条形图,但希望将每个柱形的数据标签设置为粗体以提升可见性,多次尝试均未成功。当前图表中绿色柱形上的数值“2”可见性较差,需将其设置为粗体。

当前实现代码

$worksheet = $this->spreadsheet->getActiveSheet();

// 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
$dataSeriesLabelsTotals = [
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$H$30', null, 1),   // Quantity OK
//    new DataSeriesValues(),
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$J$30', null, 1),     // Quantity NOK
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$L$30', null, 1),     // Quantity Re-work
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$R$30', null, 1),     // Quantity total
];
// Set the X-Axis Labels
//     Datatype
//     Cell reference for data
//     Format Code
//     Number of datapoints in series
//     Data values
//     Data Marker
$xAxisTickValuesTotals = [
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Worksheet!$A$20:$A$21', null, 1), // Labels X
];
// 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
$dataSeriesValuesTotals = [
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$H$' . ($cursor + 1) . ':$I$'. ($cursor + 1) , null, 1), 
    // new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$Z$3' , null, 1),        // Quantity OK
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$J$' . ($cursor + 1) . ':$K$'. ($cursor + 1) , null, 1),
    // new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$Z$3' , null, 1),        // Quantity NOK
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$L$' . ($cursor + 1) . ':$M$'. ($cursor + 1) , null, 1),
    // new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$Z$3' , null, 1),        // Quantity Re-work
  new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Worksheet!$R$' . ($cursor + 1) . ':$S$'. ($cursor + 1) , null, 1),      // Quantity total 
];

// Build the dataseries
$series1 = new DataSeries(
    DataSeries::TYPE_BARCHART_3D, // plotType
    DataSeries::GROUPING_CLUSTERED, // plotGrouping
    range(0, count($dataSeriesValuesTotals) - 1), // plotOrder
    $dataSeriesLabelsTotals, // plotLabel
    $xAxisTickValuesTotals, // plotCategory
    $dataSeriesValuesTotals // plotValues
);

// Set additional dataseries parameters
//     Make it a vertical column rather than a horizontal bar graph
$series1->setPlotDirection(DataSeries::DIRECTION_VERTICAL);

// Set labels in chart columns
$layoutTotals = new Layout();
$layoutTotals->setShowVal(true);

// Set the series in the plot area
$plotAreaTotals = new PlotArea($layoutTotals, [$series1]);

// Set the chart legend
$legendTotals = new ChartLegend(ChartLegend::POSITION_LEFT, null, false);

$titleTotals = new Title('TOTALS');

// Create the chart
$chartTotals = new Chart(
    'Chart Totals', // name
    $titleTotals, // title
    $legendTotals, // legend
    $plotAreaTotals, // plotArea
    false, // plotVisibleOnly
    DATASERIES::EMPTY_AS_GAP, // displayBlanksAs
    null, // xAxisLabel
    null,
      // yAxisLabel
);

// Set the position where the chart should appear in the worksheet
$chartTotals->setTopLeftPosition('B' . ($cursor + 3));
$chartTotals->setBottomRightPosition('M' . ($cursor + 20));

// Add the chart to the worksheet
$worksheet->addChart($chartTotals);

当前图表效果

当前3D簇状条形图

解决方法

要设置数据标签为粗体,需要通过Font对象定义粗体样式,再将其绑定到数据标签(DataLabel),最后关联到图表布局中。修改代码中设置布局的部分:

// 创建粗体字体对象
$boldFont = new \PhpOffice\PhpSpreadsheet\Style\Font();
$boldFont->setBold(true);

// 创建数据标签并配置样式与显示规则
$dataLabel = new \PhpOffice\PhpSpreadsheet\Chart\DataLabel();
$dataLabel->setFont($boldFont);
$dataLabel->setShowVal(true); // 确保显示数据标签数值

// 将数据标签应用到布局
$layoutTotals = new Layout();
$layoutTotals->setDataLabel($dataLabel);

替换原代码中:

// Set labels in chart columns
$layoutTotals = new Layout();
$layoutTotals->setShowVal(true);

说明

  • 首先实例化Font对象并开启粗体属性
  • 创建DataLabel对象,绑定粗体字体并开启数值显示
  • 将配置好的DataLabel赋值给Layout,这样所有数据系列的标签都会应用该粗体样式

内容的提问来源于stack exchange,提问作者Tigane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 07:35:07