Skip to content

Commit 8f03639

Browse files
committed
initial commit
0 parents  commit 8f03639

File tree

12 files changed

+7206
-0
lines changed

12 files changed

+7206
-0
lines changed

.babelrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"sourceMaps": true,
3+
"presets": [
4+
["env", {
5+
"targets": {
6+
"node": "current"
7+
}
8+
}]
9+
],
10+
"ignore": []
11+
}

.gitignore

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
11+
# Directory for instrumented libs generated by jscoverage/JSCover
12+
lib-cov
13+
14+
# Coverage directory used by tools like istanbul
15+
coverage
16+
17+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
18+
.grunt
19+
20+
# node-waf configuration
21+
.lock-wscript
22+
23+
# Optional npm cache directory
24+
.npm
25+
26+
# Optional REPL history
27+
.node_repl_history
28+
29+
# Compiled binary addons (http://nodejs.org/api/addons.html)
30+
build/Release
31+
32+
33+
# Visual Studio files
34+
/.vs/
35+
36+
# VS Code cache files
37+
/.vscode/.browse.VC*
38+
39+
# Dependency directory
40+
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
41+
/node_modules/
42+
43+
# Bin directory
44+
/bin/
45+
46+
# NPM files
47+
.npmrc

.npmignore

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
11+
# Directory for instrumented libs generated by jscoverage/JSCover
12+
lib-cov
13+
14+
# Coverage directory used by tools like istanbul
15+
coverage
16+
17+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
18+
.grunt
19+
20+
# node-waf configuration
21+
.lock-wscript
22+
23+
# Optional npm cache directory
24+
.npm
25+
26+
# Optional REPL history
27+
.node_repl_history
28+
29+
# Compiled binary addons (http://nodejs.org/api/addons.html)
30+
build/Release
31+
32+
33+
# Visual Studio files
34+
/.vs/
35+
36+
# VS Code cache files
37+
/.vscode/.browse.VC*
38+
39+
# Dependency directory
40+
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
41+
/node_modules/
42+
43+
# Bin directory
44+
# /bin/
45+
46+
# NPM files
47+
.npmrc
48+
49+
50+
51+
###################
52+
#### npmignore ####
53+
54+
/.vscode/
55+
/spec/
56+
.babelrc
57+
.travis.yml
58+
tsconfig.json
59+
tslint.json
60+
webpack.config.js

.vscode/launch.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"type": "node",
6+
"request": "launch",
7+
"name": "Launch Program",
8+
"program": "${workspaceRoot}/src/index.ts",
9+
"outFiles": [
10+
"${workspaceRoot}/bin/index.js"
11+
]
12+
}
13+
]
14+
}

LICENSE.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# ISC License (ISC)
2+
3+
### Copyright (c) 2018, Shellyl_N and Authors
4+
#### https://github.com/shellyln
5+
6+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby
7+
granted, provided that the above copyright notice and this permission notice appear in all copies.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING
10+
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
11+
DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
12+
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
13+
OR PERFORMANCE OF THIS SOFTWARE.

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Chart.js server side rendering example on the node.js environment.
2+
3+
Render various charts using Chart.js into the SVG format.
4+
5+
Chart.js uses the HTML5 Canvas API.
6+
However, the node.js environment does not have the Canvas API by default.
7+
With [red-agate-svg-canvas](https://www.npmjs.com/package/red-agate-svg-canvas),
8+
you can render the charts on the server side.
9+
10+
11+
## Get started
12+
13+
```bash
14+
$ git clone https://github.com/shellyln/chart.js-node-ssr-example.git
15+
$ cd chart.js-node-ssr-example
16+
$ rm -rf ./.git
17+
18+
$ npm install
19+
$ npm run build
20+
$ npm start
21+
```
22+
23+
24+
## Example
25+
26+
```ts
27+
import { SvgCanvas,
28+
Rect2D,
29+
SvgCanvas2DGradient } from 'red-agate-svg-canvas/modules';
30+
import * as ChartJs from 'chart.js';
31+
32+
// Get the global scope.
33+
// If running on a node, "g" points to a "global" object.
34+
// When running on the browser, "g" points to the "window" object.
35+
const g = Function('return this')();
36+
37+
// Chart options
38+
// https://www.chartjs.org/docs/latest/getting-started/usage.html
39+
const opts: any = { ... };
40+
41+
42+
function main() {
43+
// SvgCanvas has a "CanvasRenderingContext2D"-compatible interface.
44+
const ctx = new SvgCanvas();
45+
46+
// SvgCanvas lacks the canvas property.
47+
(ctx as any).canvas = {
48+
width: 800,
49+
height: 400,
50+
style: {
51+
width: '800px',
52+
height: '400px',
53+
},
54+
};
55+
56+
// SvgCanvas does not have font glyph information,
57+
// so manually set the ratio of (font height / font width).
58+
ctx.fontHeightRatio = 2;
59+
60+
// Chart.js needs a "HTMLCanvasElement"-like interface that has "getContext()" method.
61+
// "getContext()" should returns a "CanvasRenderingContext2D"-compatible interface.
62+
const el = { getContext: () => ctx };
63+
64+
// If "devicePixelRatio" is not set, Chart.js get the devicePixelRatio from "window" object.
65+
// node.js environment has no window object.
66+
opts.options.devicePixelRatio = 1;
67+
68+
// Disable animations.
69+
opts.options.animation = false;
70+
opts.options.events = [];
71+
opts.options.responsive = false;
72+
73+
// Chart.js needs the "CanvasGradient" in the global scope.
74+
const savedGradient = g.CanvasGradient;
75+
g.CanvasGradient = SvgCanvas2DGradient;
76+
try {
77+
const chart = new ChartJs.Chart(el as any, opts);
78+
} finally {
79+
if (savedGradient) {
80+
g.CanvasGradient = savedGradient;
81+
}
82+
}
83+
84+
// Render as SVG.
85+
const svgString = ctx.render(new Rect2D(0, 0 , 800, 400), 'px');
86+
console.log(svgString);
87+
}
88+
```
89+
90+
91+
## Notes
92+
93+
To import the [red-agate-svg-canvas](https://www.npmjs.com/package/red-agate-svg-canvas), you need to use `babel` + `webpack`.

0 commit comments

Comments
 (0)