-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add plotObservable shorthand function
- Loading branch information
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |