Skip to content

Commit

Permalink
add plotVega function
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkeller committed Apr 8, 2023
1 parent 125ab98 commit e13a970
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 23 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
===

# 3.0.0

> 2023-04-07
Breaking change. Set `view` to `true` by default. Adds the `plotVega` export as a convenient and simpler API for plotting Vega-Lite specs and [Vega-Lite-API](https://github.com/vega/vega-lite-api) charts.

# 2.0.2

> 2023-03-25
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as plot } from './src/plot.js';
export { default as plotVega } from './src/plotVega.js';
export { default as plotHistogram } from './src/plotHistogram.js';
2 changes: 1 addition & 1 deletion lib/showChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import renderInBrowser from './renderInBrowser.js';

export default async function showChart(
chart,
{ args, library, libraryCode, now, bounds = {}, css = '', title = 'My chart', instance = 0 } = {}
{ args, library, libraryCode, now, bounds = {}, css = '', title = 'Chart', instance = 0 } = {}
) {
const page = await launchContextPage(bounds, instance, { title: `${title} - ${now}` });
/**
Expand Down
4 changes: 2 additions & 2 deletions src/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import getLibraryCode from '../utils/getLibraryCode.js';
* @param {Function|Object} [chart] An async function that returns a function that returns HTML.
* @param {String} [options] Options
* @param {String|String[]} [options.library] Specify what library to load to render the plot. Built-in options are `'observablehq/plot'`, `'vega-lite'` and `'plotly'`. Other strings will be interpreted as custom JavaScript to insert. This field can also be an array of strings, if you need to add multiple scripts.
* @param {Boolean} [options.view=true] If true, show the chart in a popup window.
* @param {String} [options.outPath=''] A filepath to write the image.
* @param {String} [options.css] Any CSS that you want injected into the page to tweak styles.
* @param {Boolean} [options.view=false] If true, show the chart in a popup window.
* @param {Boolean} [options.debug] Whether to run the screenshot browser in headfull mode.
*/
export default async function plot(
chart,
args = [],
{ library = 'observablehq/plot', outPath = '', view = false, css, title, debug = false } = {}
{ library = 'observablehq/plot', outPath = '', view = true, css, title, debug = false } = {}
) {
notify({ m: 'Generating plot...', v: outPath, d: ['magenta', 'bold'] });

Expand Down
4 changes: 2 additions & 2 deletions src/plotHistogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import getLibraryCode from '../utils/getLibraryCode.js';
* @param {String} [options.outDir] The *directory* – not a specific file name – to write the various files out to.
* @param {String} [options.fill='#000'] A hex code or field name. Defaults to `'#000'`.
* @param {String} [options.css] Any CSS that you want injected into the page to tweak styles.
* @param {Boolean} [options.view=false] If true, show the chart in a popup window.
* @param {Boolean} [options.view=true] If true, show the chart in a popup window.
* @param {String} [options.breakoutFields=true] For each field passed into `options.fields` write out a separate PNG. Set this to false to put everything on the same scale.
* @param {String} [options.columns=true] Draw columns. If `false`, only draw lines.
*/
Expand All @@ -29,7 +29,7 @@ export default async function plotHistogram(
outDir,
fill = '#000',
css,
view = false,
view = true,
breakoutFields = true,
columns = true,
debug = false
Expand Down
16 changes: 16 additions & 0 deletions src/plotVega.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import plot from './plot.js';

/**
* Plot Vega charts
* @param {Function|Object} [chart] A Vega-Lite-API chart or a Vega-Lite spec.
* @param {String} [options] Options
* @param {Boolean} [options.view=true] If true, show the chart in a popup window.
* @param {String} [options.outPath=''] A filepath to write the image.
* @param {String} [options.css] Any CSS that you want injected into the page to tweak styles.
* @param {Boolean} [options.debug] Whether to run the screenshot browser in headfull mode.
*/
export default async function plotVega(chart, options) {
const c = s => s;
const spec = typeof chart.toSpec === 'function' ? chart.toSpec() : chart;
await plot(c, [spec], { ...options, library: 'vega-lite', view: true });
}
30 changes: 30 additions & 0 deletions test/plotVega.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint-disable no-undef */
import * as vl from 'vega-lite-api';
import plotVega from '../src/plotVega.js';

const data = {
values: [
{ a: 'A', b: 28 },
{ a: 'B', b: 55 },
{ a: 'C', b: 43 },
{ a: 'D', b: 91 },
{ a: 'E', b: 81 },
{ a: 'F', b: 53 },
{ a: 'G', b: 19 },
{ a: 'H', b: 87 },
{ a: 'I', b: 52 }
]
};

const chart = vl
.markBar()
.description('A simple bar chart with embedded data.')
.data(data)
.encode(
vl.x().fieldO('a'),
vl.y().fieldQ('b')
);

await plotVega(chart, {
outPath: 'test/tmp/vega-lite-api_line-plot.png'
});
29 changes: 29 additions & 0 deletions test/plotVegaSpec.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* eslint-disable no-undef */
import plotVega from '../src/plotVega.js';

const data = {
values: [
{ a: 'A', b: 28 },
{ a: 'B', b: 55 },
{ a: 'C', b: 43 },
{ a: 'D', b: 91 },
{ a: 'E', b: 81 },
{ a: 'F', b: 53 },
{ a: 'G', b: 19 },
{ a: 'H', b: 87 },
{ a: 'I', b: 52 }
]
};

const spec = {
$schema: 'https://vega.github.io/schema/vega-lite/v5.json',
description: 'A simple bar chart with embedded data.',
data,
mark: 'bar',
encoding: {
x: { field: 'a', type: 'ordinal' },
y: { field: 'b', type: 'quantitative' }
}
};

await plotVega(spec);
18 changes: 9 additions & 9 deletions test/vega-lite-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import plot from '../src/plot.js';

const dataset = {
values: [
{a: 'A', b: 28},
{a: 'B', b: 55},
{a: 'C', b: 43},
{a: 'D', b: 91},
{a: 'E', b: 81},
{a: 'F', b: 53},
{a: 'G', b: 19},
{a: 'H', b: 87},
{a: 'I', b: 52}
{ a: 'A', b: 28 },
{ a: 'B', b: 55 },
{ a: 'C', b: 43 },
{ a: 'D', b: 91 },
{ a: 'E', b: 81 },
{ a: 'F', b: 53 },
{ a: 'G', b: 19 },
{ a: 'H', b: 87 },
{ a: 'I', b: 52 }
]
};

Expand Down
18 changes: 9 additions & 9 deletions test/vega-lite.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import plot from '../src/plot.js';

const dataset = {
values: [
{a: 'A', b: 28},
{a: 'B', b: 55},
{a: 'C', b: 43},
{a: 'D', b: 91},
{a: 'E', b: 81},
{a: 'F', b: 53},
{a: 'G', b: 19},
{a: 'H', b: 87},
{a: 'I', b: 52}
{ a: 'A', b: 28 },
{ a: 'B', b: 55 },
{ a: 'C', b: 43 },
{ a: 'D', b: 91 },
{ a: 'E', b: 81 },
{ a: 'F', b: 53 },
{ a: 'G', b: 19 },
{ a: 'H', b: 87 },
{ a: 'I', b: 52 }
]
};

Expand Down

0 comments on commit e13a970

Please sign in to comment.