Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/pearmini/g2-ssr-node

The tool for G2 spec's SSR(Server-Side Rendering) in Node.js.
https://github.com/pearmini/g2-ssr-node

Last synced: 24 days ago
JSON representation

The tool for G2 spec's SSR(Server-Side Rendering) in Node.js.

Awesome Lists containing this project

README

        

# @berryv/g2-ssr-node

The tool for G2 spec's SSR(Server-Side Rendering) in Node.js.

## Installing

Install via a package manager such as Yarn or NPM.

```bash
$ npm install @berryv/g2-ssr-node
```

Use CLI to convert a G2 specification file named in `bar.json` into a png file named `bar.png`.

```bash
$ g2-ssr-node g2png -i ./bar.json -o bar.png
```

In Node, use `render` to convert a G2 specification into a png file named `bar.png`.

```js
const fs = require("fs");
const { render } = require("@berry/g2-ssr-node");

const spec = {
type: "interval",
data: [
{ genre: "Sports", sold: 275 },
{ genre: "Strategy", sold: 115 },
{ genre: "Action", sold: 120 },
{ genre: "Shooter", sold: 350 },
{ genre: "Other", sold: 150 },
],
encode: { x: "genre", y: "sold", color: "genre" },
};

render(spec).then((canvas) => {
const out = fs.createWriteStream("./bar.png");
const stream = canvas.createJPEGStream();
stream.pipe(out);
stream.on("finish", () => console.log("Convert successfully!"));
});
```

## Development

```bash
$ npm i
$ npm link
$ npm run build
$ npm run test:png
```

## API Reference

- [CLI](#cli) - Convert spec in command line.
- [Node](#node) - Convert spec in Node.js.

### CLI

# subcommand.**g2png**

```txt
Usage: g2-ssr-node g2png [options]

Convert a G2 spec to an PNG image

Options:
-i, --input filename for the input spec
-o, --output filename for the output image
-h, --help display help for command
```

For example, convert a G2 specification file named in `bar.json` into a png file named `bar.png`:

```bash
$ g2-ssr-node g2png -i ./bar.json -o bar.png
```

# subcommand.**g2jpeg**

```txt
Usage: g2-ssr-node g2pdf [options]

Convert a G2 spec to a PDF

Options:
-i, --input filename for the input spec
-o, --output filename for the output pdf
-h, --help display help for command
```

For example, convert a G2 specification file named in `bar.json` into a png file named `bar.jpeg`:

```bash
$ g2-ssr-node g2jpeg -i ./bar.json -o bar.jpeg
```

# subcommand.**g2pdf**

```txt
Usage: g2-ssr-node g2jpeg [options]

Convert a G2 spec to an JPEG image

Options:
-i, --input filename for the input spec
-o, --output filename for the output image
-h, --help display help for command
```

For example, convert a G2 specification file named in `bar.json` into a png file named `bar.pdf`:

```bash
$ g2-ssr-node g2pdf -i ./bar.json -o bar.pdf
```

### Node

# node.**renderImage(_options_)**

Renders a G2 specification into a image canvas in [node-canvas](https://github.com/Automattic/node-canvas) resolved by a promise. Then creates a stream from the returned canvas and write into a file, supporting the following stream types:

- [canvas.createPNGStream](https://github.com/Automattic/node-canvas#canvascreatepngstream)
- [canvas.createJPEGStream](https://github.com/Automattic/node-canvas#canvascreatejpegstream)

For example, convert a G2 specification into a png file named `bar.png`.

```js
const fs = require("fs");
const { render } = require("@berry/g2-ssr-node");

const spec = {
type: "interval",
data: [
{ genre: "Sports", sold: 275 },
{ genre: "Strategy", sold: 115 },
{ genre: "Action", sold: 120 },
{ genre: "Shooter", sold: 350 },
{ genre: "Other", sold: 150 },
],
encode: { x: "genre", y: "sold", color: "genre" },
};

renderImage(spec).then((canvas) => {
const out = fs.createWriteStream("./bar.png");
const stream = canvas.createJPEGStream();
stream.pipe(out);
stream.on("finish", () => console.log("Convert successfully!"));
});
```

# node.**renderPDF(_options_)**

Renders a G2 specification into a pdf canvas in [node-canvas](https://github.com/Automattic/node-canvas) resolved by a promise. Then creates a pdf stream from the returned canvas and write into a file:

- [canvas.createPDFStream](https://github.com/Automattic/node-canvas#canvascreatepdfstream)

For example, convert a G2 specification into a png file named `bar.png`.

```js
const fs = require("fs");
const { render } = require("@berry/g2-ssr-node");

const spec = {
type: "interval",
data: [
{ genre: "Sports", sold: 275 },
{ genre: "Strategy", sold: 115 },
{ genre: "Action", sold: 120 },
{ genre: "Shooter", sold: 350 },
{ genre: "Other", sold: 150 },
],
encode: { x: "genre", y: "sold", color: "genre" },
};

renderPDF(spec).then((canvas) => {
const out = fs.createWriteStream("./bar.png");
const stream = canvas.createPDFStream();
stream.pipe(out);
stream.on("finish", () => console.log("Convert successfully!"));
});
```