Output API
On this page
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(): arrayThe Vega-Lite specification as a PHP array, with $schema and config.
toSpec()
->toSpec(): arrayThe specification for nesting in a composition: without $schema or config.
toJson()
->toJson(bool $pretty = false): stringThe specification as JSON; pretty adds indentation.
embed()
->embed( array $options = [], Assets|false|null $assets = null, array $attributes = [],): stringAn 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): stringA complete HTML page that draws the chart, loading the libraries from the CDN unless assets says otherwise.
svg()
->svg(?Export $export = null): stringAn SVG image rendered by vl-convert.
png()
->png(float $scale = 2.0, ?Export $export = null): stringA PNG image rendered by vl-convert; scale multiplies the pixel dimensions.
pdf()
->pdf(?Export $export = null): stringA PDF document rendered by vl-convert.
save()
->save(string $path, float $scale = 2.0, ?Export $export = null): stringWrites to a file whose extension picks the format: svg, png, jpg, pdf, html, or json.
Escape hatches
transform()
->transform(array ...$transforms): PlotVega-Lite transforms applied to the data before every layer.
raw()
->raw(array $spec): PlotRaw Vega-Lite merged into the top-level spec last (nested objects merge; lists and scalars replace).
apply()
->apply(callable ...$fns): PlotApplies a function to the plot: pipe-friendly composition of reusable plot fragments.
Html
Html::container()
Html::container( Chart|array $chart, array $options = [], array $attributes = [],): stringThe container element for a chart.
Html::deferred()
Html::deferred( string $url, array $options = [], array $attributes = [], string $text = 'Vega-Lite specification',): stringA 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 = [],): stringA 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 = '',): stringA 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): stringHTML 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'): AssetsThe pinned libraries from jsDelivr, with subresource integrity; base replaces the CDN address.
Assets::local()
Assets::local(string $base): AssetsCopies 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): AssetsExplicit URLs for each library.
Assets::inline()
Assets::inline(string $directory): AssetsEmbeds the three files from a directory containing vega.min.js, vega-lite.min.js and vega-embed.min.js.
Assets::none()
Assets::none(): AssetsNo library tags: for pages that already load vega-embed (bundled with npm, say).
$assets->scripts()
->scripts(bool $loader = true): stringScript tags for the libraries, followed by the phiz loader.
Assets::loader()
Assets::loader(): stringThe phiz loader script: renders every [data-phiz] element and re-renders when its spec attribute changes.
Export
Export::find()
Export::find(): ?stringThe vl-convert binary path, or null when none is found.
Export::available()
Export::available(): boolWhether the vl-convert binary can be found.
$export->svg()
->svg(Chart|array $chart): stringThe chart rendered to SVG.
$export->png()
->png(Chart|array $chart, float $scale = 2.0, ?int $ppi = null): stringPNG 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): stringThe chart rendered to JPEG; scale multiplies the pixel dimensions, and quality runs from 0 to 100.
$export->pdf()
->pdf(Chart|array $chart): stringThe chart rendered to PDF.
$export->vega()
->vega(Chart|array $chart): arrayThe compiled Vega (not Vega-Lite) spec.
$export->save()
->save(Chart|array $chart, string $path, float $scale = 2.0): stringWrites the chart to a file whose extension picks the format: svg, png, jpg/jpeg, pdf, html, or json.