Skip to content

PowerPoint2007 Writer : Honour the format code of an axis - #903

Open
dkulyk wants to merge 2 commits into
PHPOffice:masterfrom
sapientpro:fix/chart-axis-format-code
Open

PowerPoint2007 Writer : Honour the format code of an axis#903
dkulyk wants to merge 2 commits into
PHPOffice:masterfrom
sapientpro:fix/chart-axis-format-code

Conversation

@dkulyk

@dkulyk dkulyk commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Description

writeAxis() writes the axis' format code and then immediately tells the renderer to ignore it:

// src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php:2466
$objWriter->startElement('c:numFmt');
$objWriter->writeAttribute('formatCode', $oAxis->getFormatCode());
$objWriter->writeAttribute('sourceLinked', '1');   // ← take the format from the source data
$objWriter->endElement();

sourceLinked="1" means "the number format comes from the source data", so the formatCode sitting next to it is discarded by PowerPoint. The attribute is in the file, which is why the issue reads as "it still doesn't work" rather than "nothing is written".

Why '0' is the right value. The library already writes it correctly everywhere else — in the same file, all three data-label sites use sourceLinked="0":

line what sourceLinked
877 data labels (getDlblNumFormat()) 0
1305 data labels 0
1445 data labels 0
2468 axis (getFormatCode()) 1

The axis is the only one out of line. It has been hardcoded to '1' since b957fb9 (2016-03-02, the Gridlines implementation) and was never revisited.

The fix is not an unconditional '0' — that would take the inherited format away from every deck that never called the setter. Axis::DEFAULT_FORMAT_CODE is 'general', so:

$objWriter->writeAttribute('sourceLinked', Chart\Axis::DEFAULT_FORMAT_CODE === $oAxis->getFormatCode() ? '1' : '0');
  • an axis nobody configured → byte-identical output to before;
  • an axis with an explicit format code → the format code is finally honoured.

Fixes #632

Possibly related, deliberately not claimed here. #610 and #420 report chart series data not being formatted as numbers. They may share this cause, but I have not measured them.

The ODPresentation writer never reads getFormatCode() at all. ODF expresses a number format as a <number:number-style> element referenced through style:data-style-name, not as an attribute, and there is no such machinery in that writer today — that is separate work, not a matter of copying this line across. Hence the Available only on the PowerPoint2007 Writer note in the documentation, matching the existing one on Crossing.

Checklist:

  • My CI is 🟢
  • I have covered by unit tests my new code (check build/coverage for coverage report)
    Axis::setFormatCode() had no writer-side coverage at all — AxisTest only exercises the getter/setter, and numFmt appeared nowhere in the writer tests. PptChartsTest::testAxisFormatCode now covers both branches (default and explicit format code) and validates each against the ECMA-376 schema.
  • I have updated the documentation to describe the changes
    setFormatCode was not mentioned anywhere in docs/. Added a Number format section to docs/usage/shapes/chart.md, next to Bounds and Crossing.
  • I have updated the changelog

@coveralls

coveralls commented Aug 20, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 92.159%. remained the same — sapientpro:fix/chart-axis-format-code into PHPOffice:master

@dkulyk
dkulyk force-pushed the fix/chart-axis-format-code branch from a617c81 to 0f45264 Compare August 22, 2026 10:27
dkulyk added 2 commits August 23, 2026 15:09
`writeAxis()` wrote `sourceLinked="1"` right next to the `formatCode` it had
just written, and that attribute tells the renderer to take the number format
from the source data instead. The format code was therefore visible in the file
and discarded by PowerPoint, which is why `Axis::setFormatCode()` reads as
having no effect at all.

The library already knows the right value: the three data-label sites in the
same file (`getDlblNumFormat()`) all write `sourceLinked="0"`. Only the axis was
out of line, hardcoded to `'1'` since b957fb9 (2016-03-02, the Gridlines
implementation).

Link the format only when no format code was asked for, so that a default axis
keeps inheriting the format of its data and nothing changes for a deck that
never called the setter.

Fixes PHPOffice#632
@dkulyk
dkulyk force-pushed the fix/chart-axis-format-code branch from 0f45264 to 5f3ead7 Compare August 23, 2026 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Formatting axis labels with format code

2 participants