phiz

Output API

A plot compiles to a Vega-Lite specification, which it returns as an array or JSON, embeds in HTML, or exports to SVG, PNG, or PDF through vl-convert. The escape hatches add raw Vega-Lite where phiz has no method. Rendering covers the options for each output.

Output: toArray() · toSpec() · toJson() · embed() · html() · svg() · png() · pdf() · save()

Escape hatches: transform() · raw() · apply()

Html: Html::container() · Html::deferred() · Html::embed() · Html::page() · Html::attributes()

Assets: Assets::cdn() · Assets::local() · Assets::urls() · Assets::inline() · Assets::none() · $assets->scripts() · Assets::loader()

Export: Export::find() · Export::available() · $export->svg() · $export->png() · $export->jpeg() · $export->pdf() · $export->vega() · $export->save()

Output

toArray()

->toArray(): array

The Vega-Lite specification as a PHP array, with $schema and config.

toSpec()

->toSpec(): array

The specification for nesting in a composition: without $schema or config.

toJson()

->toJson(bool $pretty = false): string

The specification as JSON; pretty adds indentation.

embed()

->embed(    array $options = [],    Assets|false|null $assets = null,    array $attributes = [],): string

An HTML fragment that renders the chart: a container div plus, unless $assets is false, the script tags.

html()

->html(?string $title = null, array $options = [], ?Assets $assets = null): string

A complete HTML page that draws the chart, loading the libraries from the CDN unless assets says otherwise.

svg()

->svg(?Export $export = null): string

An SVG image rendered by vl-convert.

png()

->png(float $scale = 2.0, ?Export $export = null): string

A PNG image rendered by vl-convert; scale multiplies the pixel dimensions.

pdf()

->pdf(?Export $export = null): string

A PDF document rendered by vl-convert.

save()

->save(string $path, float $scale = 2.0, ?Export $export = null): string

Writes to a file whose extension picks the format: svg, png, jpg, pdf, html, or json.

Escape hatches

transform()

->transform(array ...$transforms): Plot

Vega-Lite transforms applied to the data before every layer.

raw()

->raw(array $spec): Plot

Raw Vega-Lite merged into the top-level spec last (nested objects merge; lists and scalars replace).

apply()

->apply(callable ...$fns): Plot

Applies a function to the plot: pipe-friendly composition of reusable plot fragments.

Html

Html::container()

Html::container(    Chart|array $chart,    array $options = [],    array $attributes = [],): string

The container element for a chart.

Html::deferred()

Html::deferred(    string $url,    array $options = [],    array $attributes = [],    string $text = 'Vega-Lite specification',): string

A container that loads its spec from a URL when it scrolls into view, for pages with many charts. The URL is the href of a link inside the container, so the page still links to each spec without JavaScript, and tools that rewrite links move the spec files with the page.

Html::embed()

Html::embed(    Chart|array $chart,    array $options = [],    Assets|false|null $assets = null,    array $attributes = [],): string

A self-contained fragment: the container plus script tags (null assets means the pinned CDN copies).

Html::page()

Html::page(    Chart|array|null $chart,    ?string $title = null,    array $options = [],    ?Assets $assets = null,    string $body = '',): string

A complete HTML page that draws the chart, or holds body when chart is null; assets defaults to the CDN.

Html::attributes()

Html::attributes(array $attributes): string

HTML attributes from a name => value map: true writes a bare attribute, and null or false leaves it out.

Assets

Assets::cdn()

Assets::cdn(string $base = 'https://cdn.jsdelivr.net/npm'): Assets

The pinned libraries from jsDelivr, with subresource integrity; base replaces the CDN address.

Assets::local()

Assets::local(string $base): Assets

Copies served from your own URL prefix: {base}/vega.min.js, {base}/vega-lite.min.js, {base}/vega-embed.min.js.

Assets::urls()

Assets::urls(string $vega, string $vegaLite, string $vegaEmbed): Assets

Explicit URLs for each library.

Assets::inline()

Assets::inline(string $directory): Assets

Embeds the three files from a directory containing vega.min.js, vega-lite.min.js and vega-embed.min.js.

Assets::none()

Assets::none(): Assets

No library tags: for pages that already load vega-embed (bundled with npm, say).

$assets->scripts()

->scripts(bool $loader = true): string

Script tags for the libraries, followed by the phiz loader.

Assets::loader()

Assets::loader(): string

The phiz loader script: renders every [data-phiz] element and re-renders when its spec attribute changes.

Export

Export::find()

Export::find(): ?string

The vl-convert binary path, or null when none is found.

Export::available()

Export::available(): bool

Whether the vl-convert binary can be found.

$export->svg()

->svg(Chart|array $chart): string

The chart rendered to SVG.

$export->png()

->png(Chart|array $chart, float $scale = 2.0, ?int $ppi = null): string

PNG bytes; scale multiplies the pixel size (2 for high-DPI screens), ppi sets the embedded resolution.

$export->jpeg()

->jpeg(Chart|array $chart, float $scale = 2.0, ?int $quality = null): string

The chart rendered to JPEG; scale multiplies the pixel dimensions, and quality runs from 0 to 100.

$export->pdf()

->pdf(Chart|array $chart): string

The chart rendered to PDF.

$export->vega()

->vega(Chart|array $chart): array

The compiled Vega (not Vega-Lite) spec.

$export->save()

->save(Chart|array $chart, string $path, float $scale = 2.0): string

Writes the chart to a file whose extension picks the format: svg, png, jpg/jpeg, pdf, html, or json.