phiz

Guides

Interaction

Interaction in phiz is expressed as Vega-Lite parameters, so it works in any page that renders the specification. Static exports draw the initial state.

Tooltips, pan, and zoom

tooltip() adds tooltips to every layer. With no argument the tooltip shows the encoded fields, and a list of fields shows those fields. interactive() binds the scales to dragging and scrolling.

use Phiz\Datasets;
use Phiz\Plot;

$plot = Plot::of(Datasets::mtcars())
    ->aes(x: 'wt', y: 'mpg', color: 'hp')
    ->point(size: 70)
    ->tooltip(['model', 'mpg', 'wt', 'hp'])
    ->interactive()
    ->scaleColor(scheme: 'plasma');

Brushing and linked views

brush() adds a drag-to-select interval named brush. Marks outside the selection turn gray. Another layer, or another plot in a composition, can react with filter: ['param' => 'brush']. Brush the scatterplot to filter the bar chart:

use Phiz\Compose;
use Phiz\Datasets;
use Phiz\Plot;

$mpg = Datasets::mpg();

$plot = Compose::stack(
    Plot::of($mpg)->aes(x: 'displ', y: 'hwy', color: 'drv')->point()->brush(encodings: ['x', 'y'])->size(420, 220),
    Plot::of($mpg)->aes(x: 'class', fill: 'drv')->bar(filter: ['param' => 'brush'])->size(420, 140),
);

Legend highlighting

highlight() binds a selection to the legend of an aesthetic. Clicking an entry fades the other groups. Shift-click selects several.

use Phiz\Datasets;
use Phiz\Plot;

$long = [];
foreach (Datasets::economics() as $row) {
    if ($row['date'] >= '2000-01-01') {
        $long[] = ['date' => $row['date'], 'series' => 'Savings rate (%)', 'value' => $row['psavert']];
        $long[] = ['date' => $row['date'], 'series' => 'Median weeks unemployed', 'value' => $row['uempmed']];
    }
}

$plot = Plot::of($long)
    ->aes(x: 'date', y: 'value', color: 'series')
    ->line(linewidth: 2)
    ->highlight()
    ->labs(x: null, y: null, color: 'Click a series');

Sliders and inputs

slider() and dropdown() add parameters bound to inputs. Expressions in filters and expr() values read them by name.

use Phiz\Datasets;
use Phiz\Plot;

use function Phiz\expr;

$plot = Plot::of(Datasets::mpg())
    ->aes(x: 'displ', y: 'hwy')
    ->slider('cutoff', 15, 40, step: 1, value: 30, label: 'Highway mpg cutoff')
    ->point(filter: 'datum.hwy >= cutoff', color: '#D62728')
    ->point(filter: 'datum.hwy < cutoff', color: '#BBBBBB')
    ->hline(expr('cutoff'), linetype: 'dashed');

param() and select() add any Vega-Lite parameter.

From phpstats/phiz@bc7ed3e.