@casoon/chartlet 0.1.0-alpha.2
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 +21 -0
- package/README.md +28 -0
- package/package.json +41 -0
- package/src/Chart.astro +39 -0
- package/src/index.d.ts +1 -0
- package/src/render.d.mts +20 -0
- package/src/render.mjs +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 CASOON
|
|
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
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# @casoon/chartlet
|
|
2
|
+
|
|
3
|
+
Build static, accessible SVG or HTML charts during an Astro build. The component runs only on
|
|
4
|
+
the server and ships no chart JavaScript to the browser.
|
|
5
|
+
|
|
6
|
+
The current alpha adapter calls the `chartlet` CLI, so install both the package and the CLI
|
|
7
|
+
(Rust 1.88 or newer). The CLI must be on `PATH`, or set `CHARTLET_BIN` to its absolute path.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @casoon/chartlet@alpha
|
|
11
|
+
cargo install chartlet --version 0.1.0-alpha.2
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
```astro
|
|
15
|
+
---
|
|
16
|
+
import Chart from '@casoon/chartlet/astro';
|
|
17
|
+
import chart from '../data/monthly-revenue.json';
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
<Chart id="monthly-revenue" spec={chart} />
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
`id` must be stable and unique within the page. It prevents accessibility ID collisions when
|
|
24
|
+
the same specification is embedded more than once.
|
|
25
|
+
|
|
26
|
+
Use `format="svg"` for the graphic alone or `format="html"` for the default figure, caption,
|
|
27
|
+
source, and data-table output. The HTML table uses a native disclosure by default; set
|
|
28
|
+
`table="visible"` to show it permanently.
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@casoon/chartlet",
|
|
3
|
+
"version": "0.1.0-alpha.2",
|
|
4
|
+
"description": "Build-time SVG and HTML charts for static websites",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/casoon/chartlet.git",
|
|
10
|
+
"directory": "packages/chartlet"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"src",
|
|
14
|
+
"README.md",
|
|
15
|
+
"LICENSE"
|
|
16
|
+
],
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./src/index.d.ts",
|
|
20
|
+
"import": "./src/render.mjs"
|
|
21
|
+
},
|
|
22
|
+
"./astro": "./src/Chart.astro"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"astro": ">=6.0.0"
|
|
26
|
+
},
|
|
27
|
+
"peerDependenciesMeta": {
|
|
28
|
+
"astro": {
|
|
29
|
+
"optional": true
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"astro": "^7.3.2"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"test": "node --test"
|
|
37
|
+
},
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=22.12.0"
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/Chart.astro
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
---
|
|
2
|
+
import { renderChart } from './render.mjs';
|
|
3
|
+
|
|
4
|
+
export interface Props {
|
|
5
|
+
id: string;
|
|
6
|
+
spec: Record<string, unknown>;
|
|
7
|
+
format?: 'svg' | 'html';
|
|
8
|
+
table?: 'details' | 'visible';
|
|
9
|
+
strict?: boolean;
|
|
10
|
+
binary?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const {
|
|
14
|
+
id,
|
|
15
|
+
spec,
|
|
16
|
+
format = 'html',
|
|
17
|
+
table = 'details',
|
|
18
|
+
strict = false,
|
|
19
|
+
binary,
|
|
20
|
+
} = Astro.props;
|
|
21
|
+
|
|
22
|
+
if (!id) {
|
|
23
|
+
throw new Error('Chart requires a stable, page-unique id.');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const rendered = renderChart(spec, {
|
|
27
|
+
format,
|
|
28
|
+
table,
|
|
29
|
+
strict,
|
|
30
|
+
binary,
|
|
31
|
+
idPrefix: id,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
for (const warning of rendered.warnings) {
|
|
35
|
+
console.warn(`[chartlet] ${warning}`);
|
|
36
|
+
}
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
<Fragment set:html={rendered.content} />
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./render.mjs";
|
package/src/render.d.mts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export type ChartFormat = "svg" | "html";
|
|
2
|
+
export type TableMode = "details" | "visible";
|
|
3
|
+
|
|
4
|
+
export interface RenderChartOptions {
|
|
5
|
+
format?: ChartFormat;
|
|
6
|
+
table?: TableMode;
|
|
7
|
+
idPrefix?: string;
|
|
8
|
+
strict?: boolean;
|
|
9
|
+
binary?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface RenderChartResult {
|
|
13
|
+
content: string;
|
|
14
|
+
warnings: string[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export declare function renderChart(
|
|
18
|
+
spec: Record<string, unknown>,
|
|
19
|
+
options?: RenderChartOptions,
|
|
20
|
+
): RenderChartResult;
|
package/src/render.mjs
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
|
|
3
|
+
const MAX_OUTPUT_BYTES = 16 * 1024 * 1024;
|
|
4
|
+
|
|
5
|
+
export function renderChart(spec, options = {}) {
|
|
6
|
+
const format = options.format ?? "html";
|
|
7
|
+
const table = options.table ?? "details";
|
|
8
|
+
const binary = options.binary ?? process.env.CHARTLET_BIN ?? "chartlet";
|
|
9
|
+
const args = ["render", "-", "--format", format, "--table", table];
|
|
10
|
+
|
|
11
|
+
if (options.idPrefix) {
|
|
12
|
+
args.push("--id-prefix", options.idPrefix);
|
|
13
|
+
}
|
|
14
|
+
if (options.strict) {
|
|
15
|
+
args.push("--strict");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const result = spawnSync(binary, args, {
|
|
19
|
+
input: JSON.stringify(spec),
|
|
20
|
+
encoding: "utf8",
|
|
21
|
+
maxBuffer: MAX_OUTPUT_BYTES,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
if (result.error) {
|
|
25
|
+
if (result.error.code === "ENOENT") {
|
|
26
|
+
throw new Error(
|
|
27
|
+
`chartlet executable not found at ${JSON.stringify(binary)}. Install the CLI or set CHARTLET_BIN.`,
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
throw result.error;
|
|
31
|
+
}
|
|
32
|
+
if (result.status !== 0) {
|
|
33
|
+
throw new Error(result.stderr.trim() || `chartlet exited with status ${result.status}`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
content: result.stdout.trimEnd(),
|
|
38
|
+
warnings: result.stderr
|
|
39
|
+
.split("\n")
|
|
40
|
+
.map((warning) => warning.trim())
|
|
41
|
+
.filter(Boolean),
|
|
42
|
+
};
|
|
43
|
+
}
|