@chartpng/render 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ChartPNG
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,166 @@
1
- # Temporary Holding Version
1
+ # @chartpng/render
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Draws a Chart.js v4 config as a PNG on the server, with no browser. It uses Chart.js and
4
+ [`@napi-rs/canvas`](https://github.com/Brooooooklyn/canvas), and ships its own font, so it works the
5
+ same on a laptop, in a container and in a serverless function. It is the engine behind the hosted
6
+ service at [chartpng.dev](https://chartpng.dev).
7
+
8
+ It draws bar, line, pie, doughnut, radar, polar area, scatter and bubble charts, with a default
9
+ look (IBM Plex, a blue and orange palette, values drawn on the chart) and a few report templates.
10
+
11
+ ## Install
12
+
13
+ ```sh
14
+ npm install @chartpng/render
15
+ ```
16
+
17
+ Node 22 or later.
18
+
19
+ ## Use it
20
+
21
+ ```js
22
+ import { writeFileSync } from 'node:fs';
23
+ import { render } from '@chartpng/render';
24
+
25
+ const config = { type: 'bar', data: { labels: ['A', 'B', 'C'], datasets: [{ data: [3, 5, 2] }] } };
26
+ writeFileSync('chart.png', render(config, { width: 600, height: 360, dpr: 2 }));
27
+ ```
28
+
29
+ `render(config, options)` takes the config as an object or as text, and returns the PNG bytes as a
30
+ `Buffer`. It throws a `RenderError` with a short `code` (`bad_json`, `bad_type`, `bad_size`,
31
+ `draw_failed` and so on) when it cannot draw.
32
+
33
+ | Option | Meaning | Default |
34
+ |---|---|---|
35
+ | `width`, `height` | logical size in pixels, 50 to 2000 | 600 by 360 |
36
+ | `dpr` | device pixel ratio, 1 or 2 | 1 |
37
+ | `background` | any CSS colour | white |
38
+ | `theme` | `default`, `editorial`, `mono`, `colorblind`, `vivid`, `ocean`, or `'false'` for plain Chart.js colours | `default` |
39
+ | `palette` | your own colours as hex, a list or a comma-separated string | the theme's |
40
+ | `preset` | a report template, an output preset, or both, comma-separated | none |
41
+
42
+ `repair(config)` returns the config as it will be drawn and the list of fixes it needed. The names
43
+ `RenderError`, `FIX_CODES`, `LIMITS`, `THEME_NAMES`, `PRESET_NAMES` and `OUTPUT_PRESET_NAMES` are
44
+ exported too.
45
+
46
+ ## From a table
47
+
48
+ ```js
49
+ import { fromTable, render } from '@chartpng/render';
50
+
51
+ const csv = 'Month,Signups,Trials\nJan,12,4\nFeb,19,7\nMar,7,3';
52
+ const { config, alt, notes } = fromTable(csv, { type: 'line', title: 'Signups', last: 30 });
53
+ writeFileSync('signups.png', render(config, { width: 800, height: 400, dpr: 2 }));
54
+ ```
55
+
56
+ `fromTable(text, options)` reads CSV, TSV, semicolons or cells copied from a spreadsheet with the same
57
+ reader as [chartpng.dev/make](https://chartpng.dev/make): the first column holds the labels and every
58
+ numeric column becomes a series, up to 50 rows and 6 series. `type` is `bar`, `horizontal`, `line`,
59
+ `pie`, `doughnut` or `ranking`; `last` keeps the first line and the last n rows. It returns the
60
+ config, an alt text, and the notes on what it assumed (delimiter, header, decimal mark). It throws a
61
+ `RenderError` with code `bad_table`.
62
+
63
+ To keep a chart in a GitHub README up to date from a CSV, use the
64
+ [ChartPNG chart action](https://github.com/chartpng/chart-action), which runs this package.
65
+
66
+ No watermark is drawn. The hosted service adds one on its free plan; this library has no such code
67
+ path.
68
+
69
+ ## Command line
70
+
71
+ ```sh
72
+ chartpng-render config.json -o out.png --width 800 --height 400 --dpr 2 --theme mono
73
+ cat config.json | chartpng-render - -o out.png --preset kpi
74
+ chartpng-render data.csv --table line --title "Signups" --last 30 -o signups.png
75
+ ```
76
+
77
+ Options: `--width`, `--height`, `--dpr`, `--bkg`, `--theme`, `--palette`, `--preset`. With
78
+ `--table <type>` the input is read as a table, `--title` and `--last` apply, and the alt text is
79
+ printed on standard output. An input of `-` is read from standard input. On a config that cannot be drawn it prints the message to standard
80
+ error and exits with code 1; a wrong command line exits with 2.
81
+
82
+ ## Presets and themes
83
+
84
+ A report preset is set in the config, as `options.plugins.chartpng.preset`, or with the `preset`
85
+ option. It is a template for one kind of report chart:
86
+
87
+ | Preset | Chart type | Draws |
88
+ |---|---|---|
89
+ | `kpi` | line | one big number, a change chip against `previous`, a sparkline |
90
+ | `ranking` | bar | horizontal bars sorted by value, the first one highlighted |
91
+ | `comparison` | bar or line | the previous period in grey, the change over the current one |
92
+ | `funnel` | bar | centred stages as wide as their value, with the share of the stage before (up to 12) |
93
+ | `gauge` | doughnut | a half ring up to `max`, with the value in large type |
94
+
95
+ `prefix`, `suffix`, `previous` and `max` are set next to `preset`. `email` and `slack` are output
96
+ presets: they pick a size, a pixel ratio and an opaque background that suit an inbox or a chat, and
97
+ keep all text at 11 px or more. `"preset": "kpi,email"` combines one of each. A theme or a preset that
98
+ does not exist is a `RenderError`. [`examples/`](examples) has configs and the images the command
99
+ line made from them:
100
+
101
+ ```sh
102
+ chartpng-render examples/kpi.json -o examples/kpi.png --width 600 --height 300 --dpr 2
103
+ chartpng-render examples/ranking.json -o examples/ranking.png --theme editorial
104
+ ```
105
+
106
+ ![kpi preset](examples/kpi.png)
107
+ ![ranking preset](examples/ranking.png)
108
+
109
+ ## Lenient parsing
110
+
111
+ Language models write almost-JSON. When the config is text, `render()` repairs what it can and
112
+ draws. The config can arrive wrapped in a code fence, with single quotes, unquoted keys and trailing
113
+ commas, as here (`examples/lenient.txt`):
114
+
115
+ ````
116
+ ```json
117
+ {
118
+ type: 'line',
119
+ data: {
120
+ labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
121
+ datasets: [
122
+ { label: 'This week', data: [40, 52, 49, 63, 71, 58, 80], },
123
+ { label: 'Last week', data: [35, 48, 55, 50, 60, 66, 64], },
124
+ ],
125
+ },
126
+ options: { plugins: { title: { display: true, text: 'Visits' } } },
127
+ }
128
+ ```
129
+ ````
130
+
131
+ `repair()` reports `trailing_comma`, `code_fence`, `single_quotes` and `unquoted_keys`, and the
132
+ image is the one below, drawn with `--theme editorial`. It also fixes double-encoded JSON, a missing
133
+ `type` (it becomes `bar`) and the common Chart.js v2 spellings (`xAxes`, `gridLines`, a top-level
134
+ `legend`). Function strings are removed, never run. Valid v4 JSON is not touched.
135
+
136
+ The cases the parser is held to are in [`corpus/almost-json.json`](corpus/README.md): what it
137
+ repairs, with the codes it reports, and what it refuses on purpose.
138
+
139
+ ![lenient example](examples/lenient.png)
140
+
141
+ ## Limits
142
+
143
+ The limits of the hosted service are the defaults, and they are not options:
144
+
145
+ - config of at most 16 KB, nested at most 16 levels;
146
+ - `ticks.count` and `ticks.maxTicksLimit` of at most 100;
147
+ - 50 to 2000 pixels a side, dpr 1 or 2 (so up to 16 megapixels);
148
+ - a ceiling on how much a chart may fill, and no numbers too large to draw.
149
+
150
+ The library never fetches a URL, never reads a file other than the config the command line is
151
+ given, never reads an environment variable and never runs a function found in a config. The
152
+ [security notes](SECURITY.md) say what that does and does not protect you from.
153
+
154
+ ## ChartPNG and QuickChart
155
+
156
+ [chartpng.dev](https://chartpng.dev) is the hosted service built on this engine: `GET /chart?c=` with
157
+ the config, plus `w`, `h`, `dpr`, `bkg`, `theme`, `palette` and `preset`. Its URL format is compatible with
158
+ that of [QuickChart](https://quickchart.io): the `c=` parameter and QuickChart's parameter names
159
+ (`chart`, `width`, `height`, `backgroundColor`, `devicePixelRatio`) are accepted. This
160
+ package has no HTTP side: it is the drawing code, for when you want the PNG inside your own
161
+ process.
162
+
163
+ ## Licence
164
+
165
+ MIT, see [LICENSE](LICENSE). Chart.js, `@napi-rs/canvas` and the embedded IBM Plex Sans font have
166
+ their own licences, listed in [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md).
package/SECURITY.md ADDED
@@ -0,0 +1,34 @@
1
+ # Security
2
+
3
+ ## Reporting a problem
4
+
5
+ Email hola@chartpng.dev with what you found and a config that shows it. Please do not open a public
6
+ issue for something that lets a config crash, hang or exhaust the memory of a process. You will get
7
+ an answer, not a bounty.
8
+
9
+ ## What the library does with an untrusted config
10
+
11
+ `render()` and the command line treat the config as data.
12
+
13
+ - It never runs code from a config. Function strings (`"function(v) { ... }"`) are dropped by the
14
+ repair step and counted as the `function_string` fix; nothing calls `eval` or `new Function`. An object config is
15
+ converted with `JSON.stringify` before drawing, so function values are discarded and never reach
16
+ Chart.js, and a getter is read once as plain data.
17
+ - It never fetches a URL. Nothing in the built package calls `fetch` or opens a network connection.
18
+ - It reads no file and no environment variable. The command line reads the config you point it at
19
+ (or standard input) and writes the file you give to `-o`, and nothing else.
20
+ - It bounds the work a config can ask for: 16 KB of config, nesting of at most 16 levels, at most
21
+ 100 ticks per axis, images from 50 to 2000 pixels a side at dpr 1 or 2 (up to 16 megapixels), a
22
+ ceiling on how much a chart may fill, and a refusal of numbers too large to draw. These are the
23
+ limits of the hosted service. They are in `LIMITS` and are not options.
24
+
25
+ ## What it does not do
26
+
27
+ - It does not run in a sandbox. It is Chart.js and a native canvas library in your process. A bug
28
+ in either could still be reached by a config; keep both up to date.
29
+ - The limits bound the work of one render, not the number of renders. If you accept configs from
30
+ strangers, put a rate limit and a timeout in front of it, and consider a separate process.
31
+ - A render is synchronous and blocks the event loop while it draws. The hosted service measures
32
+ about 30 ms for an ordinary chart; a large one takes longer.
33
+ - It does not clean text drawn into the image. Titles and labels are drawn as given.
34
+ - Chart.js and the canvas library are third-party code with their own advisories.
@@ -0,0 +1,253 @@
1
+ # Third-party notices
2
+
3
+ @chartpng/render is MIT licensed (see LICENSE). This file lists the licence of what the package
4
+ contains and of what it depends on. The texts were copied from the licence files in the installed
5
+ packages (chart.js 4.6.1, @kurkle/color 0.3.4, @napi-rs/canvas 1.0.13 and its darwin-arm64 binary
6
+ package) and from the font licence kept in this repository at src/fonts/OFL.txt, on 17 November 2026.
7
+
8
+ | Component | Version | Licence | How it reaches you |
9
+ |---|---|---|---|
10
+ | IBM Plex Sans (latin and latin-ext, weights 400 and 600) | from @fontsource/ibm-plex-sans 5.3.0 | SIL Open Font License 1.1 | Embedded as base64 in dist/src/fonts/plex.js: redistributed |
11
+ | chart.js | 4.6.1 | MIT | npm dependency |
12
+ | @kurkle/color | 0.3.4 | MIT | dependency of chart.js |
13
+ | @napi-rs/canvas | 1.0.13 | MIT | npm dependency |
14
+ | @napi-rs/canvas-* (one native binary per platform) | 1.0.13 | MIT (checked for darwin-arm64 only) | optional dependencies of @napi-rs/canvas |
15
+
16
+ ## IBM Plex Sans
17
+
18
+ Embedded in this package. The OFL requires that each copy bundled with software carries the
19
+ copyright notice and the licence; this file is that copy. The text below is the one kept in
20
+ src/fonts/OFL.txt.
21
+
22
+ IBM's own licence file (https://github.com/IBM/plex/blob/master/LICENSE.txt, read on 17 November
23
+ 2026) opens with a copyright line that declares "Plex" as a Reserved Font Name. The copy below,
24
+ which came with the fonts, names IBM Corp. as the copyright holder without that declaration.
25
+
26
+ ```
27
+ Copyright 2019 IBM Corp. All rights reserved. IBMPlexSans-Italic[wdth,wght].ttf: Copyright 2019 IBM Corp. All rights reserved.
28
+
29
+ This Font Software is licensed under the SIL Open Font License, Version 1.1.
30
+ This license is copied below, and is also available with a FAQ at:
31
+ http://scripts.sil.org/OFL
32
+
33
+
34
+ -----------------------------------------------------------
35
+ SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
36
+ -----------------------------------------------------------
37
+
38
+ PREAMBLE
39
+ The goals of the Open Font License (OFL) are to stimulate worldwide
40
+ development of collaborative font projects, to support the font creation
41
+ efforts of academic and linguistic communities, and to provide a free and
42
+ open framework in which fonts may be shared and improved in partnership
43
+ with others.
44
+
45
+ The OFL allows the licensed fonts to be used, studied, modified and
46
+ redistributed freely as long as they are not sold by themselves. The
47
+ fonts, including any derivative works, can be bundled, embedded,
48
+ redistributed and/or sold with any software provided that any reserved
49
+ names are not used by derivative works. The fonts and derivatives,
50
+ however, cannot be released under any other type of license. The
51
+ requirement for fonts to remain under this license does not apply
52
+ to any document created using the fonts or their derivatives.
53
+
54
+ DEFINITIONS
55
+ "Font Software" refers to the set of files released by the Copyright
56
+ Holder(s) under this license and clearly marked as such. This may
57
+ include source files, build scripts and documentation.
58
+
59
+ "Reserved Font Name" refers to any names specified as such after the
60
+ copyright statement(s).
61
+
62
+ "Original Version" refers to the collection of Font Software components as
63
+ distributed by the Copyright Holder(s).
64
+
65
+ "Modified Version" refers to any derivative made by adding to, deleting,
66
+ or substituting -- in part or in whole -- any of the components of the
67
+ Original Version, by changing formats or by porting the Font Software to a
68
+ new environment.
69
+
70
+ "Author" refers to any designer, engineer, programmer, technical
71
+ writer or other person who contributed to the Font Software.
72
+
73
+ PERMISSION & CONDITIONS
74
+ Permission is hereby granted, free of charge, to any person obtaining
75
+ a copy of the Font Software, to use, study, copy, merge, embed, modify,
76
+ redistribute, and sell modified and unmodified copies of the Font
77
+ Software, subject to the following conditions:
78
+
79
+ 1) Neither the Font Software nor any of its individual components,
80
+ in Original or Modified Versions, may be sold by itself.
81
+
82
+ 2) Original or Modified Versions of the Font Software may be bundled,
83
+ redistributed and/or sold with any software, provided that each copy
84
+ contains the above copyright notice and this license. These can be
85
+ included either as stand-alone text files, human-readable headers or
86
+ in the appropriate machine-readable metadata fields within text or
87
+ binary files as long as those fields can be easily viewed by the user.
88
+
89
+ 3) No Modified Version of the Font Software may use the Reserved Font
90
+ Name(s) unless explicit written permission is granted by the corresponding
91
+ Copyright Holder. This restriction only applies to the primary font name as
92
+ presented to the users.
93
+
94
+ 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
95
+ Software shall not be used to promote, endorse or advertise any
96
+ Modified Version, except to acknowledge the contribution(s) of the
97
+ Copyright Holder(s) and the Author(s) or with their explicit written
98
+ permission.
99
+
100
+ 5) The Font Software, modified or unmodified, in part or in whole,
101
+ must be distributed entirely under this license, and must not be
102
+ distributed under any other license. The requirement for fonts to
103
+ remain under this license does not apply to any document created
104
+ using the Font Software.
105
+
106
+ TERMINATION
107
+ This license becomes null and void if any of the above conditions are
108
+ not met.
109
+
110
+ DISCLAIMER
111
+ THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
112
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
113
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
114
+ OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
115
+ COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
116
+ INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
117
+ DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
118
+ FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
119
+ OTHER DEALINGS IN THE FONT SOFTWARE.
120
+ ```
121
+
122
+ ## chart.js
123
+
124
+ ```
125
+ The MIT License (MIT)
126
+
127
+ Copyright (c) 2014-2024 Chart.js Contributors
128
+
129
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
130
+
131
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
132
+
133
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
134
+ ```
135
+
136
+ ## @kurkle/color
137
+
138
+ ```
139
+ The MIT License (MIT)
140
+
141
+ Copyright (c) 2018-2024 Jukka Kurkela
142
+
143
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
144
+
145
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
146
+
147
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
148
+ ```
149
+
150
+ ## @napi-rs/canvas
151
+
152
+ ```
153
+ MIT License
154
+
155
+ Copyright (c) 2020 lynweklm@gmail.com
156
+
157
+ Permission is hereby granted, free of charge, to any person obtaining a copy
158
+ of this software and associated documentation files (the "Software"), to deal
159
+ in the Software without restriction, including without limitation the rights
160
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
161
+ copies of the Software, and to permit persons to whom the Software is
162
+ furnished to do so, subject to the following conditions:
163
+
164
+ The above copyright notice and this permission notice shall be included in all
165
+ copies or substantial portions of the Software.
166
+
167
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
168
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
169
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
170
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
171
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
172
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
173
+ SOFTWARE.
174
+
175
+ ---
176
+
177
+ src/svg_doc/broken_image.png is Chromium's broken-image icon
178
+ (third_party/blink/public/default_100_percent/blink/broken_image.png),
179
+ used under the Chromium license:
180
+
181
+ Copyright 2015 The Chromium Authors
182
+
183
+ Redistribution and use in source and binary forms, with or without
184
+ modification, are permitted provided that the following conditions are
185
+ met:
186
+
187
+ * Redistributions of source code must retain the above copyright
188
+ notice, this list of conditions and the following disclaimer.
189
+ * Redistributions in binary form must reproduce the above
190
+ copyright notice, this list of conditions and the following disclaimer
191
+ in the documentation and/or other materials provided with the
192
+ distribution.
193
+ * Neither the name of Google LLC nor the names of its
194
+ contributors may be used to endorse or promote products derived from
195
+ this software without specific prior written permission.
196
+
197
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
198
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
199
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
200
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
201
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
202
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
203
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
204
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
205
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
206
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
207
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
208
+ ```
209
+
210
+ ### What the native binary contains
211
+
212
+ @napi-rs/canvas draws with Skia, compiled into one native binary per platform and installed with
213
+ it, not by this package. The software bill of materials that ships with the darwin-arm64 binary
214
+ (sbom.cdx.json, version 1.0.13) lists these third-party libraries vendored with Skia:
215
+
216
+ | Library | Licence in the bill of materials |
217
+ |---|---|
218
+ | expat | MIT |
219
+ | freetype | FTL |
220
+ | brotli | MIT |
221
+ | harfbuzz | MIT |
222
+ | icu | ICU |
223
+ | libjxl | BSD-3-Clause |
224
+ | highway | Apache-2.0 |
225
+ | libjpeg-turbo | BSD-3-Clause AND IJG AND Zlib |
226
+ | libpng | libpng-2.0 |
227
+ | libwebp | BSD-3-Clause |
228
+ | wuffs | Apache-2.0 |
229
+ | zlib | Zlib |
230
+
231
+ and 121 Rust crates under these licences (crates per licence expression):
232
+
233
+ | Licence | Crates |
234
+ |---|---|
235
+ | MIT OR Apache-2.0 | 67 |
236
+ | MIT | 27 |
237
+ | Apache-2.0 OR MIT | 8 |
238
+ | Unlicense OR MIT | 4 |
239
+ | MPL-2.0 | 4 |
240
+ | BSD-2-Clause | 3 |
241
+ | Zlib OR Apache-2.0 OR MIT | 2 |
242
+ | Apache-2.0 | 1 |
243
+ | Apache-2.0 / MIT | 1 |
244
+ | Zlib | 1 |
245
+ | ISC | 1 |
246
+ | MIT/Apache-2.0 | 1 |
247
+ | (MIT OR Apache-2.0) AND Unicode-3.0 | 1 |
248
+
249
+ The bill of materials does not state Skia's own licence, and this package has not read it from a
250
+ file. Nothing in this package copies the binary: it is installed from npm next to your code. If
251
+ you redistribute the binary yourself (a container image, a bundled app), read the licence files
252
+ of @napi-rs/canvas and of the platform package, and carry the notices that the licences above
253
+ (FreeType, ICU, BSD-3-Clause, MPL-2.0) ask for.
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ import '../dist/packages/render/cli.js';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,88 @@
1
+ // chartpng-render config.json -o out.png [--width n] [--height n] [--dpr 1|2] [--bkg colour] [--theme name] [--palette hex,hex] [--preset name]
2
+ // The only file it reads is the config (or standard input when the file is -); the only file it writes is -o.
3
+ import { readFileSync, writeFileSync } from 'node:fs';
4
+ import { parseArgs } from 'node:util';
5
+ import { fromTable, render, RenderError, TABLE_TYPES } from './index.js';
6
+ const USAGE = `Usage: chartpng-render <config.json | -> -o <out.png> [options]
7
+ chartpng-render <table.csv | -> --table <type> -o <out.png> [--title text] [--last n] [options]
8
+
9
+ -o, --out <file> where to write the PNG (required)
10
+ --width <n> 50 to 2000, default 600
11
+ --height <n> 50 to 2000, default 360
12
+ --dpr <1|2> device pixel ratio, default 1
13
+ --bkg <colour> background, default white
14
+ --theme <name> default, editorial, mono, colorblind, vivid, ocean, or false
15
+ --palette <hex,hex> brand colours
16
+ --preset <name> kpi, ranking, comparison, funnel, gauge, email or slack
17
+ --table <type> read the input as a table (CSV, TSV, or cells from a spreadsheet) and draw it
18
+ as ${TABLE_TYPES.join(', ')}; the alt text is printed on standard output
19
+ --title <text> with --table, the title above the chart
20
+ --last <n> with --table, keep only the last n rows (1 to 50)
21
+ -h, --help show this text
22
+
23
+ An input of - is read from standard input. Exit code 1 means the config could not be drawn.`;
24
+ function fail(message, code) {
25
+ console.error(message);
26
+ process.exit(code);
27
+ }
28
+ let parsed;
29
+ try {
30
+ parsed = parseArgs({
31
+ allowPositionals: true,
32
+ options: {
33
+ out: { type: 'string', short: 'o' },
34
+ width: { type: 'string' },
35
+ height: { type: 'string' },
36
+ dpr: { type: 'string' },
37
+ bkg: { type: 'string' },
38
+ theme: { type: 'string' },
39
+ palette: { type: 'string' },
40
+ preset: { type: 'string' },
41
+ table: { type: 'string' },
42
+ title: { type: 'string' },
43
+ last: { type: 'string' },
44
+ help: { type: 'boolean', short: 'h' },
45
+ },
46
+ });
47
+ }
48
+ catch (error) {
49
+ fail(`${error.message}\n\n${USAGE}`, 2);
50
+ }
51
+ const { values, positionals } = parsed;
52
+ if (values.help) {
53
+ console.log(USAGE);
54
+ process.exit(0);
55
+ }
56
+ if (positionals.length !== 1 || !values.out)
57
+ fail(USAGE, 2);
58
+ let text;
59
+ try {
60
+ text = readFileSync(positionals[0] === '-' ? 0 : positionals[0], 'utf8');
61
+ }
62
+ catch (error) {
63
+ fail(`could not read ${positionals[0]}: ${error.message}`, 1);
64
+ }
65
+ const number = (value) => (value === undefined ? undefined : Number(value));
66
+ if ((values.title !== undefined || values.last !== undefined) && values.table === undefined)
67
+ fail(`--title and --last need --table\n\n${USAGE}`, 2);
68
+ try {
69
+ let config = text;
70
+ if (values.table !== undefined) {
71
+ const table = fromTable(text, { type: values.table, title: values.title, last: number(values.last) });
72
+ config = table.config;
73
+ console.log(table.alt);
74
+ }
75
+ const png = render(config, {
76
+ width: number(values.width),
77
+ height: number(values.height),
78
+ dpr: number(values.dpr),
79
+ background: values.bkg,
80
+ theme: values.theme,
81
+ palette: values.palette,
82
+ preset: values.preset,
83
+ });
84
+ writeFileSync(values.out, png);
85
+ }
86
+ catch (error) {
87
+ fail(error instanceof RenderError ? error.message : `could not draw this chart: ${error.message}`, 1);
88
+ }
@@ -0,0 +1,61 @@
1
+ import { LIMITS, RenderError } from '../../src/lib/render.js';
2
+ import type { ChartConfig } from '../../src/lib/render.js';
3
+ import { FIX_CODES } from '../../src/lib/repair.js';
4
+ import type { FixCode } from '../../src/lib/repair.js';
5
+ import { OUTPUT_PRESET_NAMES, PRESET_NAMES } from '../../src/lib/presets.js';
6
+ import { THEME_NAMES } from '../../src/lib/theme.js';
7
+ import { TABLE_MAX_ROWS } from '../../src/lib/table-chart.js';
8
+ import type { TableChartType } from '../../src/lib/table-chart.js';
9
+ export { FIX_CODES, LIMITS, OUTPUT_PRESET_NAMES, PRESET_NAMES, RenderError, THEME_NAMES, TABLE_MAX_ROWS };
10
+ export type { ChartConfig, FixCode, TableChartType };
11
+ /** The chart types a table can be drawn as. */
12
+ export declare const TABLE_TYPES: TableChartType[];
13
+ export interface RenderOptions {
14
+ /** Logical width in pixels, 50 to 2000. Default 600. */
15
+ width?: number;
16
+ /** Logical height in pixels, 50 to 2000. Default 360. */
17
+ height?: number;
18
+ /** Device pixel ratio, 1 or 2. Default 1. */
19
+ dpr?: 1 | 2;
20
+ /** Any CSS colour. Default white. */
21
+ background?: string;
22
+ /** One of THEME_NAMES, or 'false' to draw with Chart.js defaults. */
23
+ theme?: string;
24
+ /** Brand colours as hex, as a list or a comma-separated string. */
25
+ palette?: string | string[];
26
+ /** A report preset (PRESET_NAMES), an output preset (OUTPUT_PRESET_NAMES), or both, comma-separated. */
27
+ preset?: string;
28
+ }
29
+ /**
30
+ * Draws a Chart.js config as a PNG. The config is a plain object or the almost-JSON
31
+ * text a language model writes; it is data and never code. Throws RenderError.
32
+ */
33
+ export declare function render(config: object | string, options?: RenderOptions): Buffer;
34
+ export interface Repair {
35
+ config: ChartConfig;
36
+ /** What was repaired; empty when the config was valid as sent. */
37
+ fixes: FixCode[];
38
+ }
39
+ /** The config as render() reads it, with the list of fixes that were needed. Throws RenderError. */
40
+ export declare function repair(config: object | string): Repair;
41
+ export interface TableOptions {
42
+ /** bar, horizontal, line, pie, doughnut or ranking. Default bar. */
43
+ type?: TableChartType;
44
+ /** Drawn above the chart, up to 60 characters. */
45
+ title?: string;
46
+ /** Keep only the last n rows under the first line, for a file that grows a row at a time. */
47
+ last?: number;
48
+ }
49
+ export interface FromTable {
50
+ config: ChartConfig;
51
+ /** A description of the chart for the image's alt text. */
52
+ alt: string;
53
+ /** Every assumption made while reading: delimiter, header, decimal mark, skipped columns. */
54
+ notes: string[];
55
+ }
56
+ /**
57
+ * Turns a table (CSV, TSV, semicolons, or cells copied from a spreadsheet) into a Chart.js
58
+ * config, with the same reader as chartpng.dev/make. The first column holds the labels and
59
+ * every numeric column becomes a series. Throws RenderError with code 'bad_table'.
60
+ */
61
+ export declare function fromTable(text: string, options?: TableOptions): FromTable;