Skip to content

v5.0.0 — Chart.js 3/4 support, hardened toHtml(), JavaScript callbacks

Latest

Choose a tag to compare

@bbsnly bbsnly released this 13 Aug 10:03
· 1 commit to main since this release
b334d76

v5 modernizes the package for Chart.js 3 and 4, fixes several long-standing behavioral bugs, and adds support for real JavaScript callbacks in chart configs. Most code keeps working unchanged — read the upgrade guide before updating.

Breaking changes

  • Chart.js 3.0+ is now required (4.x recommended). beginAtZero() emits the modern options.scales.y.beginAtZero syntax and merges into existing scales instead of replacing them. If you are still on Chart.js 2, stay on v4.
  • toHtml() was rewritten. The signature is now toHtml(string $element, bool $uniqueId = true) — the second parameter disables the random id suffix; the old undocumented ?Chart parameter is gone. Element names must match [A-Za-z][A-Za-z0-9_-]* or an InvalidArgumentException is thrown. Cleanup listens to pagehide (back/forward-cache aware) instead of the deprecated unload, and re-rendering into the same id destroys the previous chart first.
  • Serialization shape. A chart whose type was never set omits "type" entirely (previously "type": null), and toJson() encodes empty data/options as {} the way Chart.js expects. toArray()/get() remain PHP-facing and still return arrays — always use toJson() (or json_encode($chart)) when feeding Chart.js.
  • Stricter magic setters. Calling an option setter with anything other than exactly one argument throws an ArgumentCountError. Previously $options->responsive() silently assigned false.
  • Interfaces extend \JsonSerializable. Third-party implementers of ChartInterface/ConfigInterface must add a jsonSerialize() method.

New

  • JsExpression (#47): wrap tooltip formatters, tick callbacks, and other scriptable options so toJson()/toHtml() embed them as real JavaScript instead of dead strings. Only ever wrap code you wrote yourself — never user input.
  • Chart instances are registered in window.bbsnlyChartJSInstances for access from your own JavaScript.

Fixed

  • toArray() no longer mutates the object graph — nested Dataset objects stay objects, even when references are held into the config.
  • isset(), empty(), and unset() now work on magic config properties.
  • Reading a never-set property no longer leaks "key": null into the output.
  • Cloning a chart or config no longer shares state with the original.
  • The composer test command documented in the README now exists (plus composer analyse).

Under the hood

  • PHPStan at level: max over src and tests; 100% line coverage.
  • Generated JavaScript is now tested behaviorally: executed in Node against a DOM stub, including the callback and bfcache lifecycle paths.
  • CI matrix covers PHP 8.1–8.5.