Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/pearmini/g2-ssr-node
- Owner: pearmini
- License: isc
- Created: 2023-11-23T08:51:59.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-07-11T12:53:11.000Z (4 months ago)
- Last Synced: 2024-09-18T08:53:01.562Z (about 2 months ago)
- Language: JavaScript
- Homepage:
- Size: 413 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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!"));
});
```