phiz

Guide

phiz

phiz draws charts from PHP data. A chart is described in parts: the data, a mapping from columns to visual properties such as position, color, and size, and one or more layers of marks such as points, lines, or bars. Scales, facets, labels, and themes adjust the result. This way of describing charts comes from R’s ggplot2, but the pages here do not assume you know it.

phiz compiles the description to a Vega-Lite specification, a JSON document that describes the chart. The browser draws the specification with vega-embed, and the vl-convert command-line tool renders it to SVG, PNG, or PDF. No JavaScript is written by hand.

use Phiz\Datasets;
use Phiz\Plot;

$plot = Plot::of(Datasets::mpg())
    ->aes(x: 'displ', y: 'hwy', color: 'drv')
    ->point(alpha: 0.5)
    ->smooth(method: 'lm')
    ->labs(title: 'Larger engines travel fewer highway miles per gallon', x: 'Displacement (L)', y: 'Highway mpg', color: 'Drive');

Each chart in these pages is drawn from the code shown with it.

Install

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. Examples written with the pipe operator |> need PHP 8.5. Each one also has a method-chain version for PHP 8.3 and 8.4.

phiz has no required dependencies beyond PHP. These are optional:

  • the vl-convert binary, for SVG, PNG, and PDF export (Rendering)
  • Laravel, for the <x-phiz> Blade component, whose service provider is discovered automatically (Laravel and Livewire)
  • phit, for predictions from formula fits in the statistics helpers (Statistics)

Get started builds a first chart and saves it.

What phiz covers

  • An immutable Plot builder with fluent methods, and ggplot2-style functions for PHP’s pipe operator.
  • More than 40 geoms, including points, lines, areas, bars, histograms, densities, boxplots, violins, intervals, reference lines, text, heatmaps, hexagonal bins, smoothers, 2D densities and contours, QQ plots, and arcs.
  • Scales, label formatters, guides, coordinate flips, limits, polar and fixed coordinates, facets, compositions, labels, captions, and themes modeled on ggplot2’s complete themes.
  • Tooltips, pan and zoom, brushing, legend highlighting, sliders, and linked views, expressed as Vega-Lite parameters.
  • Statistical plots from phit model results and CmdStanPHP draws: coefficient plots, regression diagnostics, predicted values with confidence bands, marginal means, random effects, and posterior intervals.
  • HTML output, static export, and the <x-phiz> Blade component, which redraws when a Livewire component updates.

The phpstats family

phiz is the visualization package of the phpstats family. phit fits models, phibble provides data frames, phplyr provides dplyr-style functions, and CmdStanPHP runs Stan. phiz depends on none of them, and plots their tables and results directly.

From phpstats/phiz@bc7ed3e.