Skip to content

Commit fc2aa75

Browse files
committed
update docs and examples for vega-lite
1 parent 8268fd6 commit fc2aa75

5 files changed

Lines changed: 67 additions & 22 deletions

File tree

README.md

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Plot
22
===
33

4-
> A small node library to display charts in popup windows and save them as pngs. Supports [observablehq/plot](https://observablehq.com/@observablehq/plot), [vega-lite](https://vega.github.io/vega-lite/) and [plotly](https://plotly.com/javascript/) out of the box.
4+
> A small node library to display charts in popup windows and save them as pngs. Supports [Observablehq/plot](https://observablehq.com/@observablehq/plot), [Vega-lite](https://vega.github.io/vega-lite/) and [Plotly](https://plotly.com/javascript/) out of the box.
55
66
- [Motivation](#motivation)
77
- [Installing](#installing)
@@ -30,15 +30,60 @@ npm install @mhkeller/plot
3030

3131
A generic function to render HTML, view and screenshot it.
3232

33+
If your plot function requires a DOM element ID to render into (as Vega-lite and Plotly do), a `#body` element is added to the page for you to use.
34+
35+
```javascript
36+
import { plot } from '@mhkeller/plot';
37+
38+
const dataset = {
39+
values: [
40+
{a: 'A', b: 28},
41+
{a: 'B', b: 55},
42+
{a: 'C', b: 43},
43+
{a: 'D', b: 91},
44+
{a: 'E', b: 81},
45+
{a: 'F', b: 53},
46+
{a: 'G', b: 19},
47+
{a: 'H', b: 87},
48+
{a: 'I', b: 52}
49+
]
50+
};
51+
52+
const chart = data => {
53+
const spec = {
54+
$schema: 'https://vega.github.io/schema/vega-lite/v5.json',
55+
description: 'A simple bar chart with embedded data.',
56+
data,
57+
mark: 'bar',
58+
encoding: {
59+
x: {field: 'a', type: 'ordinal'},
60+
y: {field: 'b', type: 'quantitative'}
61+
}
62+
};
63+
return vegaEmbed('#body', spec);
64+
}
65+
66+
await plot(chart, [dataset], {
67+
library: 'vega-lite',
68+
outPath: 'test/tmp/vega-lite_line-plot.png',
69+
view: true,
70+
title: 'Vega line chart'
71+
});
72+
```
73+
74+
If the plot function simply creates HTML, then this function can simply return from that function and the HTML will be appended to the `#body` element automatically such as in this Observablehq/plot example.
75+
3376
```javascript
3477
import { plot } from '@mhkeller/plot`;
3578
79+
const dataset = /* read in your dataset ... */
80+
3681
// Create a function that returns html
37-
const chart = ds => {
82+
const chart = data => {
3883
return Plot.plot({
3984
marks: [
4085
Plot.rectY(
41-
ds,
86+
data,
4287
Plot.binX(
4388
{ y: 'count' },
4489
{

test/plot-multi.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import plot from '../src/plot.js';
66

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

9-
const data = aq
9+
const dataset = aq
1010
.from(events)
1111
.derive({ date: aq.escape(d => new Date(d.date.split('T')[0])) })
1212
.groupby('date', 'brand')
@@ -18,10 +18,10 @@ const data = aq
1818
// d.date = utcFormat('%a')(d.date);
1919
// })
2020

21-
const chart = ds =>{
21+
const chart = data =>{
2222
return Plot.plot({
2323
marks: [
24-
Plot.line(ds, {
24+
Plot.line(data, {
2525
x: 'date',
2626
y: 'value',
2727
stroke: 'brand',
@@ -40,19 +40,19 @@ const chart = ds =>{
4040
}
4141
});
4242
}
43-
await plot(chart, [data], {
43+
await plot(chart, [dataset], {
4444
outPath: 'test/tmp/line-plot.png',
4545
view: true,
4646
title: 'Line chart'
4747
});
4848

49-
await plot(chart, [data], {
49+
await plot(chart, [dataset], {
5050
outPath: 'test/tmp/line-plot.png',
5151
view: true,
5252
title: 'Line chart 2'
5353
});
5454

55-
await plot(chart, [data], {
55+
await plot(chart, [dataset], {
5656
outPath: 'test/tmp/line-plot.png',
5757
view: true,
5858
title: 'Line chart3'

test/plot.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ import plot from '../src/plot.js';
55

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

8-
const data = aq
8+
const dataset = aq
99
.from(events)
1010
.derive({ date: aq.escape(d => new Date(d.date.split('T')[0])) })
1111
.groupby('date', 'brand')
1212
.rollup({ value: d => aq.op.sum(d.price_in_usd) })
1313
.orderby('date', 'brand')
1414
.objects();
1515

16-
const chart = ds => {
16+
const chart = data => {
1717
return Plot.plot({
1818
marks: [
19-
Plot.line(ds, {
19+
Plot.line(data, {
2020
x: 'date',
2121
y: 'value',
2222
stroke: 'brand',
@@ -36,7 +36,7 @@ const chart = ds => {
3636
});
3737
};
3838

39-
await plot(chart, [data], {
39+
await plot(chart, [dataset], {
4040
library: 'observablehq/plot',
4141
outPath: 'test/tmp/line-plot.png',
4242
view: true,

test/plotly.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@ const trace2 = {
2525
}
2626
};
2727

28-
const layout = {
28+
const chartLayout = {
2929
width: 500,
3030
height: 500
3131
};
3232

33-
const data = [trace1, trace2];
33+
const dataset = [trace1, trace2];
3434

35-
const chart = (ds, l) => {
36-
Plotly.newPlot('body', ds, l);
35+
const chart = (data, layout) => {
36+
Plotly.newPlot('body', data, layout);
3737
};
3838

39-
await plot(chart, [data, layout], {
39+
await plot(chart, [dataset, chartLayout], {
4040
library: 'plotly',
4141
outPath: 'test/tmp/plotly_line-plot.png',
4242
view: true,

test/vega-lite.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-undef */
22
import plot from '../src/plot.js';
33

4-
const data = {
4+
const dataset = {
55
values: [
66
{a: 'A', b: 28},
77
{a: 'B', b: 55},
@@ -15,11 +15,11 @@ const data = {
1515
]
1616
};
1717

18-
const chart = ds => {
18+
const chart = data => {
1919
const spec = {
2020
$schema: 'https://vega.github.io/schema/vega-lite/v5.json',
2121
description: 'A simple bar chart with embedded data.',
22-
data: ds,
22+
data,
2323
mark: 'bar',
2424
encoding: {
2525
x: {field: 'a', type: 'ordinal'},
@@ -29,7 +29,7 @@ const chart = ds => {
2929
return vegaEmbed('#body', spec);
3030
}
3131

32-
await plot(chart, [data], {
32+
await plot(chart, [dataset], {
3333
library: 'vega-lite',
3434
outPath: 'test/tmp/vega-lite_line-plot.png',
3535
view: true,

0 commit comments

Comments
 (0)