phpstats / phiz
A ggplot2-flavored grammar of graphics for PHP that emits Vega-Lite
Requires
- php: >=8.3 <8.6
- ext-json: *
Requires (Dev)
- opis/json-schema: ^2.4
- orchestra/testbench: ^10.0
- pestphp/pest: ^4.0
- phpstats/phibble: ^0.5 || ^0.6 || ^0.7 || ^0.8
- phpstats/phit-polyfill: ^0.4
- phpstats/phplyr: ^0.4
- squizlabs/php_codesniffer: ^3.13
Suggests
- illuminate/view: For the <x-phiz> Blade component (auto-discovered in Laravel)
- phpstats/phibble: Data frames that phiz plots directly
- phpstats/phit-polyfill: Formula predictions (effect_plot, marginal_means) without the phit extension
Provides
None
Conflicts
None
Replaces
None
README
A ggplot2-flavored grammar of graphics for PHP that emits Vega-Lite.
phiz describes a chart as data, mappings from columns to visual properties, and layers of marks, as R's ggplot2 does. It compiles the description to a Vega-Lite specification. The browser draws the specification with vega-embed, and the vl-convert binary renders it to SVG, PNG, or PDF.
PHP 8.5+:
use Phiz\Datasets; use function Phiz\{aes, geom_point, geom_smooth, ggplot, labs}; $plot = ggplot(Datasets::mpg(), aes(x: 'displ', y: 'hwy')) |> geom_point(alpha: 0.6) |> geom_smooth(method: 'lm') |> labs(x: 'Displacement (L)', y: 'Highway mpg'); $plot->save('chart.html');
PHP 8.3–8.4:
use Phiz\Datasets; use Phiz\Plot; $plot = Plot::of(Datasets::mpg()) ->aes(x: 'displ', y: 'hwy') ->point(alpha: 0.6) ->smooth(method: 'lm') ->labs(x: 'Displacement (L)', y: 'Highway mpg'); $plot->save('chart.html');
Features
- Layers of points, lines, bars, distributions, intervals, fitted curves, and contours
- ggplot2's scales, label formatters, guides, coordinates, facets, and themes
- Tooltips, zoom, brushing, legend highlighting, and sliders
- Plots of model fits and posterior draws
- HTML output, and SVG, PNG, and PDF export through vl-convert
- The
<x-phiz>Blade component, which redraws with Livewire - Charts that follow a page's dark mode
- Binning and downsampling in PHP for large data
Installation
composer require phpstats/phiz
phiz supports PHP 8.3, 8.4, and 8.5 (composer.json allows >=8.3 <8.6), and CI tests all three. It has no required dependencies beyond PHP. These are optional:
- the vl-convert binary, for SVG, PNG, and PDF export
- Laravel, for the
<x-phiz>Blade component, whose service provider is discovered automatically - phit, for predictions from formula fits in the statistics helpers
Documentation
The documentation is at https://phiz.phpstats.org/docs:
- Get started builds and saves a first chart.
- Coming from ggplot2 translates ggplot2 code and lists the differences.
- Gallery shows the 46 examples in
examples/catalog.phpwith their code. - Functions and methods lists every function and method, with links to their signatures.
Development
composer test
composer lint
Development covers the test layers, the gallery, and how the documentation is rendered.
License
phiz is released under the MIT license. Third-party notices lists the projects whose behavior parts of phiz follow, and the bundled datasets' sources and licenses.