This has come up a couple of times in Discord, so I thought it would be helpful to have official docs.
'getting-started/installation',
'getting-started/integration',
'getting-started/usage',
+ 'getting-started/using-from-node-js',
]
},
{
Let's get started with Chart.js!
* **[Follow a step-by-step guide](./usage) to get up to speed with Chart.js**
-* [Install Chart.js](./installation) from npm or a CDN
+* [Install Chart.js](./installation) from npm or a CDN
* [Integrate Chart.js](./integration) with bundlers, loaders, and front-end frameworks
+* [Use Chart.js from Node.js](./using-from-node-js)
Alternatively, see the example below or check [samples](../samples).
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
```
-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.
+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.
```html
<script>
</script>
```
-You can see all the ways to use Chart.js in the [step-by-step guide](./usage).
\ No newline at end of file
+You can see all the ways to use Chart.js in the [step-by-step guide](./usage).
--- /dev/null
+# Using from Node.js
+
+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/).
+
+Sample usage:
+
+```js
+import {CategoryScale, Chart, LinearScale, LineController, LineElement, PointElement} from 'chart.js';
+import {Canvas} from 'skia-canvas';
+import fsp from 'node:fs/promises';
+
+Chart.register([
+ CategoryScale,
+ LineController,
+ LineElement,
+ LinearScale,
+ PointElement
+]);
+
+const canvas = new Canvas(400, 300);
+const chart = new Chart(
+ canvas, // TypeScript needs "as any" here
+ {
+ type: 'line',
+ data: {
+ labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
+ datasets: [{
+ label: '# of Votes',
+ data: [12, 19, 3, 5, 2, 3],
+ borderColor: 'red'
+ }]
+ }
+ }
+);
+const pngBuffer = await canvas.toBuffer('png', {matte: 'white'});
+await fsp.writeFile('output.png', pngBuffer);
+chart.destroy();
+```