Skip to content

Commit a647e0d

Browse files
authored
Add docs on using from Node.js (#12039)
This has come up a couple of times in Discord, so I thought it would be helpful to have official docs.
1 parent 44908f5 commit a647e0d

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

docs/.vuepress/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ export default defineConfig({
294294
'getting-started/installation',
295295
'getting-started/integration',
296296
'getting-started/usage',
297+
'getting-started/using-from-node-js',
297298
]
298299
},
299300
{

docs/getting-started/index.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
Let's get started with Chart.js!
44

55
* **[Follow a step-by-step guide](./usage) to get up to speed with Chart.js**
6-
* [Install Chart.js](./installation) from npm or a CDN
6+
* [Install Chart.js](./installation) from npm or a CDN
77
* [Integrate Chart.js](./integration) with bundlers, loaders, and front-end frameworks
8+
* [Use Chart.js from Node.js](./using-from-node-js)
89

910
Alternatively, see the example below or check [samples](../samples).
1011

@@ -63,7 +64,7 @@ Now that we have a canvas, we can include Chart.js from a CDN.
6364
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
6465
```
6566

66-
Finally, we can create a chart. We add a script that acquires the `myChart` canvas element and instantiates `new Chart` with desired configuration: `bar` chart type, labels, data points, and options.
67+
Finally, we can create a chart. We add a script that acquires the `myChart` canvas element and instantiates `new Chart` with desired configuration: `bar` chart type, labels, data points, and options.
6768

6869
```html
6970
<script>
@@ -90,4 +91,4 @@ Finally, we can create a chart. We add a script that acquires the `myChart` canv
9091
</script>
9192
```
9293

93-
You can see all the ways to use Chart.js in the [step-by-step guide](./usage).
94+
You can see all the ways to use Chart.js in the [step-by-step guide](./usage).
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Using from Node.js
2+
3+
You can use Chart.js in Node.js for server-side generation of plots with help from an NPM package such as [node-canvas](https://github.com/Automattic/node-canvas) or [skia-canvas](https://skia-canvas.org/).
4+
5+
Sample usage:
6+
7+
```js
8+
import {CategoryScale, Chart, LinearScale, LineController, LineElement, PointElement} from 'chart.js';
9+
import {Canvas} from 'skia-canvas';
10+
import fsp from 'node:fs/promises';
11+
12+
Chart.register([
13+
CategoryScale,
14+
LineController,
15+
LineElement,
16+
LinearScale,
17+
PointElement
18+
]);
19+
20+
const canvas = new Canvas(400, 300);
21+
const chart = new Chart(
22+
canvas, // TypeScript needs "as any" here
23+
{
24+
type: 'line',
25+
data: {
26+
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
27+
datasets: [{
28+
label: '# of Votes',
29+
data: [12, 19, 3, 5, 2, 3],
30+
borderColor: 'red'
31+
}]
32+
}
33+
}
34+
);
35+
const pngBuffer = await canvas.toBuffer('png', {matte: 'white'});
36+
await fsp.writeFile('output.png', pngBuffer);
37+
chart.destroy();
38+
```

0 commit comments

Comments
 (0)