|
1 | 1 | Plot |
2 | 2 | === |
3 | 3 |
|
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. |
5 | 5 |
|
6 | 6 | - [Motivation](#motivation) |
7 | 7 | - [Installing](#installing) |
@@ -30,15 +30,60 @@ npm install @mhkeller/plot |
30 | 30 |
|
31 | 31 | A generic function to render HTML, view and screenshot it. |
32 | 32 |
|
| 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 | + |
33 | 76 | ```javascript |
34 | 77 | import { plot } from '@mhkeller/plot`; |
35 | 78 |
|
| 79 | +const dataset = /* read in your dataset ... */ |
| 80 | +
|
36 | 81 | // Create a function that returns html |
37 | | -const chart = ds => { |
| 82 | +const chart = data => { |
38 | 83 | return Plot.plot({ |
39 | 84 | marks: [ |
40 | 85 | Plot.rectY( |
41 | | - ds, |
| 86 | + data, |
42 | 87 | Plot.binX( |
43 | 88 | { y: 'count' }, |
44 | 89 | { |
|
0 commit comments