PHPOffice / PhpSpreadsheet

A pure PHP library for reading and writing spreadsheet files
https://phpspreadsheet.readthedocs.io
MIT License
13.37k stars 3.47k forks source link

Is there a way to set the font size of data labels in a pie chart directly in PHP using PhpSpreadsheet? #4201

Closed Lumimimi closed 3 weeks ago

Lumimimi commented 4 weeks ago

This is:

- [ ] a bug report
- [x] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior? In PhpSpreadsheet, I would like to be able to set the font size of data labels (both category names and percentages) in a pie chart directly from the PHP code.

What is the current behavior? Currently, there doesn’t seem to be a direct way to modify the font size of pie chart data labels (like category names and percentages) within PhpSpreadsheet. I have reviewed the documentation and tried various settings, but could not find a solution for this.

What are the steps to reproduce? Below is a sample code to generate a pie chart with data labels. However, I cannot find a way to adjust the font size of these labels (category names and values shown on the chart) directly:

<?php
<?php

require __DIR__ . '/vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Chart\Chart;
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
use PhpOffice\PhpSpreadsheet\Chart\Layout;
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;

$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();

// Sample data for pie chart
$data = [
    ['Category', 'Value'],
    ['Category A', 40],
    ['Category B', 30],
    ['Category C', 20],
    ['Category D', 10],
];
$worksheet->fromArray($data, null, 'A1');

// Create data series for the pie chart
$categories = [new DataSeriesValues('String', 'Worksheet!$A$2:$A$5', null, 4)];
$values = [new DataSeriesValues('Number', 'Worksheet!$B$2:$B$5', null, 4)];

// Set layout for data labels
$layout = new Layout();
$layout->setShowVal(true); // Display values
$layout->setShowCatName(true); // Display category names

$series = new DataSeries(
    DataSeries::TYPE_PIECHART, // Chart type: Pie chart
    null,
    range(0, count($values) - 1),
    [],
    $categories,
    $values
);

$plotArea = new PlotArea($layout, [$series]);
$chart = new Chart('Pie Chart', null, null, $plotArea);

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

// Save to file
$writer = new Xlsx($spreadsheet);
$writer->setIncludeCharts(true);
$writer->save('pie_chart_example.xlsx');

If this is an issue with reading a specific spreadsheet file, then it may be appropriate to provide a sample file that demonstrates the problem; but please keep it as small as possible, and sanitize any confidential information before uploading.

What features do you think are causing the issue

Which versions of PhpSpreadsheet and PHP are affected? I am using PhpSpreadsheet version versions : * 3.3.0 and PHP version PHP 8.2.9.

oleibman commented 4 weeks ago

Yes, there is a way, but I think I need to make it easier. So, even though I am going to tell you how to do it, I want to leave this ticket open while I add the necessary code to simplify it. The following code changes the font name used for the labels, but you can just as easily change the size.

$font = new Font();
$font->setLatin('Times New Roman');
$layoutArray = ['labelFont' => $font];
$layout = new Layout($layoutArray);

Layout has a labelFont property and a getLabelFont method, but no setLabelFont, so you have to initialize that property in the constructor - I think a set method should be added. I think it's also likely that the writer should use the font name if the Latin name is missing, which would allow users to specify the much more familiar setName rather than setLatin.

Lumimimi commented 3 weeks ago

Thank you very much for your response.