Skip to content

Commit

Permalink
add plotObservable shorthand function
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkeller committed Apr 28, 2023
1 parent 502b8c4 commit 8aebca2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 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.1.0

> 2023-04-29
Add the `plotObservable` function as a shorthand for using Observablehq/plot charts.

# 3.0.0

> 2023-04-07
Expand Down
15 changes: 15 additions & 0 deletions src/plotObservable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import plot from './plot.js';

/**
* Plot Vega charts
* @param {Object} [chartConfig] An @observablehq/plot function
* @param {String} [options] Options
* @param {String} [options.outPath=''] A filepath to write the image.
* @param {Boolean} [options.view=true] If true, show the chart in a popup window.
* @param {String} [options.css] Any CSS that you want injected into the page to tweak styles.
* @param {String} [options.title='Chart'] If `view` is true, add a title to the window's page. A timestamp will be appended to this.
* @param {Boolean} [options.debug] Whether to run the screenshot browser in headfull mode.
*/
export default async function plotObservable(chartConfig, data, options = {}) {
await plot(chartConfig, [data], { ...options, library: 'observablehq/plot' });
}
38 changes: 38 additions & 0 deletions test/plotObservable.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* eslint-disable no-undef */
import { readDataSync } from 'indian-ocean';
import * as aq from 'arquero';

import plotObservable from '../src/plotObservable.js';

const events = readDataSync('./test/data/purchase_data.csv');

const dataset = aq
.from(events)
.derive({ date: aq.escape(d => new Date(d.date.split('T')[0])) })
.groupby('date', 'brand')
.rollup({ value: d => aq.op.sum(d.price_in_usd) })
.orderby('date', 'brand')
.objects();

const chart = data => Plot.plot({
marks: [
Plot.line(data, {
x: 'date',
y: 'value',
stroke: 'brand',
strokeWidth: 2,
curve: 'linear'
})
],
width: 554,
height: 130,
x: { ticks: 3 },
marginLeft: 50,
color: {
legend: true,
width: 554,
columns: '120px'
}
});

await plotObservable(chart, dataset);

0 comments on commit 8aebca2

Please sign in to comment.