blockcharts 0.0.1 → 0.0.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/README.md +171 -169
- package/package.json +1 -1
- package/release/blocks/guide.axis.js +2 -2
- package/release/blocks/guide.grid.js +2 -2
- package/release/blocks/guide.kpi.js +2 -2
- package/release/blocks/guide.legend.js +2 -2
- package/release/blocks/interaction.animate.js +2 -2
- package/release/blocks/interaction.brush.js +2 -2
- package/release/blocks/interaction.crosshair.js +2 -2
- package/release/blocks/interaction.legend-filter.js +2 -2
- package/release/blocks/interaction.tooltip.js +2 -2
- package/release/blocks/interaction.zoom-controls.js +2 -2
- package/release/blocks/interaction.zoom.js +2 -2
- package/release/blocks/lib.columns.js +2 -2
- package/release/blocks/lib.curve.js +2 -2
- package/release/blocks/lib.pointer.js +2 -2
- package/release/blocks/lib.stats.js +2 -2
- package/release/blocks/lib.ticks.js +2 -2
- package/release/blocks/lib.view.js +2 -2
- package/release/blocks/mark.arc.js +2 -2
- package/release/blocks/mark.area.js +2 -2
- package/release/blocks/mark.boxplot.js +2 -2
- package/release/blocks/mark.candlestick.js +2 -2
- package/release/blocks/mark.line.js +2 -2
- package/release/blocks/mark.point.js +2 -2
- package/release/blocks/mark.polygon.js +2 -2
- package/release/blocks/mark.rect.js +2 -2
- package/release/blocks/mark.rule.js +2 -2
- package/release/blocks/mark.sankey.js +2 -2
- package/release/blocks/mark.text.js +2 -2
- package/release/blocks/mark.violin.js +2 -2
- package/release/blocks/renderer.canvas.js +2 -2
- package/release/blocks/renderer.svg.js +2 -2
- package/release/blocks/scale.band.js +2 -2
- package/release/blocks/scale.color.js +2 -2
- package/release/blocks/scale.linear.js +2 -2
- package/release/blocks/scale.log.js +2 -2
- package/release/blocks/scale.sequential.js +2 -2
- package/release/blocks/scale.size.js +2 -2
- package/release/blocks/scale.time.js +2 -2
- package/release/blocks/transform.aggregate.js +2 -2
- package/release/blocks/transform.bin.js +2 -2
- package/release/blocks/transform.calculate.js +2 -2
- package/release/blocks/transform.calendar.js +2 -2
- package/release/blocks/transform.contour.js +2 -2
- package/release/blocks/transform.decimate.js +2 -2
- package/release/blocks/transform.density.js +2 -2
- package/release/blocks/transform.filter.js +2 -2
- package/release/blocks/transform.fold.js +2 -2
- package/release/blocks/transform.funnel.js +2 -2
- package/release/blocks/transform.hexbin.js +2 -2
- package/release/blocks/transform.highlight.js +2 -2
- package/release/blocks/transform.marimekko.js +2 -2
- package/release/blocks/transform.quartiles.js +2 -2
- package/release/blocks/transform.sankey.js +2 -2
- package/release/blocks/transform.sort.js +2 -2
- package/release/blocks/transform.stack.js +2 -2
- package/release/blocks/transform.treemap.js +2 -2
- package/release/blocks/transform.waterfall.js +2 -2
- package/release/blocks/transform.window.js +2 -2
- package/release/core.js +3 -3
- package/release/kits/basic.js +22 -22
- package/release/kits/basic.mjs +22 -22
- package/release/kits/composition.js +36 -36
- package/release/kits/composition.mjs +36 -36
- package/release/kits/dashboard.js +34 -34
- package/release/kits/dashboard.mjs +34 -34
- package/release/kits/full.js +61 -61
- package/release/kits/full.mjs +61 -61
- package/release/kits/timeseries.js +32 -32
- package/release/kits/timeseries.mjs +32 -32
- package/release/loader.js +2 -2
- package/release/manifest.json +212 -212
package/README.md
CHANGED
|
@@ -1,211 +1,207 @@
|
|
|
1
1
|
# blockcharts
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/blockcharts)
|
|
4
|
+
[](https://github.com/boligolov/blockcharts/actions/workflows/ci.yml)
|
|
5
|
+
[](LICENSE)
|
|
4
6
|
|
|
5
7
|
**Atomic charts for HTML dashboards: a page carries only the chart code it needs. Nothing more.**
|
|
6
8
|
|
|
7
9
|
blockcharts is a chart library cut into atoms — a scale, a bar, an axis, a tooltip, each a block of <!-- build:block-range -->0.4–12 KB<!-- /build -->.
|
|
8
|
-
A page gets the core (about <!-- build:core-gzip -->12 KB<!-- /build --> gzipped) and exactly the blocks its own charts use, never the whole library
|
|
9
|
-
|
|
10
|
-
light `.html` report with interactive charts that opens offline and can be emailed.
|
|
10
|
+
A page gets the core (about <!-- build:core-gzip -->12 KB<!-- /build --> gzipped) and exactly the blocks its own charts use, never the whole library.
|
|
11
|
+
Charts are described in JSON, drawn in SVG or on a canvas, and every part can be customized, down to blocks of your own.
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
**[Website](https://blockcharts.online)** · **[Gallery](https://blockcharts.online/examples/)** · **[Guide](https://blockcharts.online/guide/)** · **[Blocks](https://blockcharts.online/blocks/)** · [Principles](docs/spec/principles.md)
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
```
|
|
17
|
-
/plugin marketplace add boligolov/blockcharts
|
|
18
|
-
/plugin install blockcharts@blockcharts
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
It updates by itself once auto-update is on for the marketplace (`/plugin` → *Marketplaces*), or by hand with
|
|
22
|
-
`claude plugin update blockcharts@blockcharts`.
|
|
23
|
-
|
|
24
|
-
**Claude.ai and Claude Desktop** — download **[blockcharts.skill](https://blockcharts.online/blockcharts.skill)**
|
|
25
|
-
and upload it under *Settings → Capabilities → Skills* (skills need code execution enabled).
|
|
26
|
-
|
|
27
|
-
**Any other agent that reads skill folders** — copy [`skills/blockcharts/`](skills/blockcharts/) into its
|
|
28
|
-
skills folder (`~/.claude/skills/` for Claude Code). It needs Node.js 22+.
|
|
29
|
-
|
|
30
|
-
Then ask — *"an HTML report of monthly revenue by region from sales.csv"*, *"a dashboard of these KPIs"*.
|
|
31
|
-
Scopes and details: [docs/skill.md](docs/skill.md).
|
|
32
|
-
|
|
33
|
-
## How it works
|
|
34
|
-
|
|
35
|
-
Charts are drawn by small independent **blocks** (`scale.time`, `mark.rect`, `guide.axis`,
|
|
36
|
-
`interaction.zoom`, …). A chart is a JSON spec that names them; the blocks a page needs are read from its
|
|
37
|
-
specs, closed over their dependencies, and only those go into the page: a bar chart with labels and a tooltip
|
|
38
|
-
carries <!-- build:bar -->10 of the 58 blocks, 71 KB of code instead of 214<!-- /build -->; a whole executive dashboard
|
|
39
|
-
(KPI tiles, a trend, a donut, a waterfall, a funnel, animated) carries <!-- build:dashboard-blocks -->24<!-- /build -->. Nobody lists blocks by hand, so the list is never too long.
|
|
40
|
-
|
|
41
|
-
The agent never writes chart code. It writes the JSON, and a composer bundled with the skill checks every
|
|
42
|
-
chart against the real data, reports each mistake with its place in the JSON, and only then writes the page.
|
|
43
|
-
|
|
44
|
-
**Customizable at every level**: block parameters (formats, locales, axes, tooltips), CSS variables and
|
|
45
|
-
classes (brand colors, dark mode, print), your own set of blocks (a kit per page, or the loader), functions
|
|
46
|
-
by name (`BC.defineFn`), and blocks of your own — a small script calling `BC.define`, the same contract the
|
|
47
|
-
built-in blocks use ([an example](https://blockcharts.online/embed/block/)).
|
|
15
|
+
[](https://blockcharts.online/examples/executive-dashboard/)
|
|
48
16
|
|
|
49
|
-
|
|
17
|
+
*KPI tiles, a trend against plan, a donut, a waterfall and a funnel: one standalone HTML file,
|
|
18
|
+
[live in the gallery](https://blockcharts.online/examples/executive-dashboard/).*
|
|
50
19
|
|
|
51
|
-
|
|
52
|
-
from blocks so that a page carries exactly the code its charts need — and what follows from that.
|
|
20
|
+
## Quick start
|
|
53
21
|
|
|
54
|
-
|
|
55
|
-
- **Blocks are atoms** (§2): one role, one job; a bar chart is `band + linear + rect`, a new chart is a recipe.
|
|
56
|
-
- **Customizable at every level** (§3): parameters, CSS, your own set of blocks, functions, your own blocks.
|
|
57
|
-
- **The page is the product** (§4): one HTML file that opens from disk, offline, anywhere.
|
|
58
|
-
- **The chart is data** (§5–6), **checked before it is written** (§7).
|
|
59
|
-
- **Heavy data stays light** (§8), **same input, same bytes** (§9), **nothing leaves the page** (§10).
|
|
60
|
-
- **One set of files, three ways in** (§11) and the **requirements** for pages, blocks and composers (§12).
|
|
22
|
+
One script tag, and a chart described in JSON where it should appear:
|
|
61
23
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
The same blocks work without the skill.
|
|
65
|
-
|
|
66
|
-
### Three ways to use it
|
|
67
|
-
|
|
68
|
-
| You want | Use | Files |
|
|
69
|
-
| --- | --- | --- |
|
|
70
|
-
| One report file, nothing to host | the **composer** | `node scripts/compose.js page.json report.html` |
|
|
71
|
-
| Charts in your own page, one `<script>` from a CDN | a **kit** | `release/kits/<name>.js` (or `.mjs`) |
|
|
72
|
-
| Fetch only what a page uses, from a CDN or a folder | the **loader** | `release/loader.js` + `core.js` + `blocks/*.js` |
|
|
24
|
+
```html
|
|
25
|
+
<script src="https://cdn.jsdelivr.net/npm/blockcharts@0.0.2/release/kits/basic.js"></script>
|
|
73
26
|
|
|
74
|
-
|
|
27
|
+
<script type="application/json" id="bc-data">
|
|
28
|
+
{ "sales": [ { "region": "North", "total": 42 }, { "region": "South", "total": 58 }, { "region": "East", "total": 31 } ] }
|
|
29
|
+
</script>
|
|
30
|
+
<script type="application/json" data-bc-chart>
|
|
31
|
+
{ "data": "sales",
|
|
32
|
+
"scales": { "x": { "type": "band" }, "y": { "type": "linear", "zero": true } },
|
|
33
|
+
"guides": [ { "type": "axis", "scale": "x" }, { "type": "axis", "scale": "y" } ],
|
|
34
|
+
"marks": [ { "type": "rect", "x": "region", "y": "total" } ],
|
|
35
|
+
"interaction": [ { "type": "tooltip" } ] }
|
|
36
|
+
</script>
|
|
37
|
+
```
|
|
75
38
|
|
|
76
|
-
|
|
39
|
+
Charts draw themselves when the page loads. With a bundler or a framework:
|
|
77
40
|
|
|
78
41
|
```sh
|
|
79
|
-
|
|
80
|
-
cd blockcharts
|
|
81
|
-
npm install
|
|
82
|
-
npm run build && npm run manifest
|
|
83
|
-
node scripts/compose.js skills/blockcharts/recipes/report-from-csv.json report.html
|
|
42
|
+
npm install blockcharts
|
|
84
43
|
```
|
|
85
44
|
|
|
86
|
-
|
|
45
|
+
```js
|
|
46
|
+
import BC from 'blockcharts';
|
|
87
47
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
"title": "Sales",
|
|
91
|
-
"data": { "sales": [ { "region": "North", "total": 42 }, { "region": "South", "total": 58 } ] },
|
|
92
|
-
"charts": [
|
|
93
|
-
{
|
|
94
|
-
"title": "Sales by region",
|
|
95
|
-
"spec": {
|
|
96
|
-
"data": "sales",
|
|
97
|
-
"scales": { "x": { "type": "band" }, "y": { "type": "linear", "zero": true } },
|
|
98
|
-
"guides": [ { "type": "grid", "scale": "y" }, { "type": "axis", "scale": "x" }, { "type": "axis", "scale": "y" } ],
|
|
99
|
-
"marks": [ { "type": "rect", "x": "region", "y": "total" } ],
|
|
100
|
-
"interaction": [ { "type": "tooltip" } ]
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
]
|
|
104
|
-
}
|
|
48
|
+
BC.data('sales', rows);
|
|
49
|
+
const chart = BC.chart(document.getElementById('chart'), spec); // update, setView, getView, destroy, ready
|
|
105
50
|
```
|
|
106
51
|
|
|
107
|
-
|
|
52
|
+
## Why blockcharts
|
|
108
53
|
|
|
109
|
-
|
|
54
|
+
- **Only the code your charts use.** The blocks a page needs are read from its specs and closed over their
|
|
55
|
+
dependencies: a bar chart with labels and a tooltip carries <!-- build:bar -->10 of the 58 blocks, 71 KB of code instead of 214<!-- /build -->; a whole
|
|
56
|
+
executive dashboard (KPI tiles, a trend, a donut, a waterfall, a funnel, animated) carries <!-- build:dashboard-blocks -->24<!-- /build -->. Nobody lists
|
|
57
|
+
blocks by hand, so the list is never too long.
|
|
58
|
+
- **Charts are data, checked before they are drawn.** A chart is a plain JSON spec, not code. It is checked against
|
|
59
|
+
the real data, and every mistake comes back with its place in the JSON.
|
|
60
|
+
- **One file that opens anywhere.** The composer turns JSON and data into a single HTML report that opens from disk,
|
|
61
|
+
offline, can be emailed or printed, and loads nothing from the network. The same input always gives the same bytes.
|
|
62
|
+
- **Customizable at every level.** Block parameters (formats, locales, axes, tooltips), CSS variables and classes
|
|
63
|
+
(brand colors, dark mode, print), your own set of blocks, functions by name (`BC.defineFn`), and blocks of your
|
|
64
|
+
own — a small script calling `BC.define`, the same contract the built-in blocks use
|
|
65
|
+
([an example](https://blockcharts.online/embed/block/)).
|
|
66
|
+
- **Heavy data stays light.** A canvas renderer for tens of thousands of points, decimation, aggregation, and large
|
|
67
|
+
datasets stored gzipped in the page.
|
|
68
|
+
- **No dependencies.** Plain ES2020 in every modern browser, typed for TypeScript, MIT-licensed.
|
|
110
69
|
|
|
111
|
-
|
|
70
|
+
What it is built on: [the blockcharts principles](docs/spec/principles.md), v<!-- build:principles-version -->1.1<!-- /build -->.
|
|
112
71
|
|
|
113
|
-
|
|
72
|
+
## What it draws
|
|
114
73
|
|
|
115
|
-
|
|
74
|
+
Bars (stacked, grouped, 100%), lines and areas, scatter and bubbles, hexbins and density contours, pie and donut,
|
|
75
|
+
heatmaps and calendars, histograms, boxplots and violins, waterfalls, funnels, treemaps, sankey and marimekko
|
|
76
|
+
diagrams, candlesticks, KPI tiles with sparklines, bullet and target lines — with tooltips, a crosshair, zoom and
|
|
77
|
+
brush, legend filters and animation.
|
|
116
78
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
```
|
|
79
|
+
Every kind is live in the [gallery](https://blockcharts.online/examples/), with its JSON and the HTML to embed it. The
|
|
80
|
+
<!-- build:recipes -->48<!-- /build --> ready-made pages behind it are in [`skills/blockcharts/recipes/`](skills/blockcharts/recipes/), to copy.
|
|
120
81
|
|
|
121
|
-
|
|
122
|
-
<script src="https://cdn.jsdelivr.net/npm/blockcharts@0.0.1/release/kits/timeseries.js"></script>
|
|
82
|
+
## Ways to ship it
|
|
123
83
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
```
|
|
84
|
+
| You want | Use |
|
|
85
|
+
| --- | --- |
|
|
86
|
+
| Charts in your own page, one `<script>` | a **kit**: the core and a set of blocks in one file |
|
|
87
|
+
| Each page to fetch only what it uses | the **loader**: `loader.js`, the core and single block files |
|
|
88
|
+
| A bundler or a framework component | the **npm package**: `import BC from 'blockcharts'` |
|
|
89
|
+
| One report file, nothing to host | the **composer**: JSON and data in, one HTML file out |
|
|
131
90
|
|
|
132
|
-
|
|
91
|
+
### Kits
|
|
92
|
+
|
|
93
|
+
A kit is the core and a set of blocks in one file: a classic script (`kits/<name>.js`) or an ES module
|
|
94
|
+
(`kits/<name>.mjs`, `export default BC`; browsers load modules over http and https only, not from `file://`).
|
|
133
95
|
|
|
134
96
|
<!-- build:kits -->
|
|
135
97
|
|
|
136
98
|
| Kit | Blocks | Contents | Minified (gzip) |
|
|
137
99
|
|---|---|---|---|
|
|
138
100
|
| `basic` | 19 | Bars, lines, points and areas on linear, log, time and band scales, with axes, grid, legend and tooltip, and fields calculated from the data. | 112 KB (42 KB) |
|
|
139
|
-
| `timeseries` | 29 | Long series over time: lines and areas with zoom, brush, legend filter, decimation, aggregation, wide tables and moving averages; also the canvas renderer for very large series. |
|
|
140
|
-
| `composition` | 33 | Parts of a whole and distributions: stacked bars and areas, pie, heatmap, histogram, boxplot, waterfall, funnel, treemap, aggregation. |
|
|
101
|
+
| `timeseries` | 29 | Long series over time: lines and areas with zoom, brush, legend filter, decimation, aggregation, wide tables and moving averages; also the canvas renderer for very large series. | 141 KB (52 KB) |
|
|
102
|
+
| `composition` | 33 | Parts of a whole and distributions: stacked bars and areas, pie, heatmap, histogram, boxplot, waterfall, funnel, treemap, aggregation. | 149 KB (54 KB) |
|
|
141
103
|
| `dashboard` | 31 | Business dashboards: KPI tiles with sparklines, bars, lines and areas, waterfall, funnel, treemap, bullet and target lines, donut, with axes, grid, legend, tooltip and legend filter. | 149 KB (55 KB) |
|
|
142
104
|
| `full` | 58 | Every block. | 226 KB (81 KB) |
|
|
143
105
|
|
|
144
106
|
<!-- /build -->
|
|
145
107
|
|
|
146
|
-
Everything
|
|
108
|
+
Everything is minified (esbuild, ES2020) and stripped of the parameter docs, which a running chart never reads.
|
|
109
|
+
Charts mount themselves on `DOMContentLoaded`; or call `BC.chart(element, spec)` and keep the handle.
|
|
147
110
|
|
|
148
|
-
|
|
149
|
-
|
|
111
|
+
### The loader
|
|
112
|
+
|
|
113
|
+
```html
|
|
114
|
+
<script src="https://cdn.jsdelivr.net/npm/blockcharts@0.0.2/release/loader.js" data-auto></script>
|
|
115
|
+
<!-- your data and <script data-bc-chart> specs -->
|
|
150
116
|
```
|
|
151
117
|
|
|
152
|
-
`
|
|
118
|
+
The loader reads `manifest.json` next to it, loads the core, asks it which blocks the charts on the page need, loads
|
|
119
|
+
only those (with their dependencies) and draws. By hand:
|
|
153
120
|
|
|
154
|
-
|
|
121
|
+
```js
|
|
122
|
+
await BCLoader.load({
|
|
123
|
+
base: 'https://cdn.jsdelivr.net/npm/blockcharts@0.0.2/release/', // default: the folder of loader.js
|
|
124
|
+
blocks: ['interaction.zoom'], // extra blocks besides the ones found in the page
|
|
125
|
+
scan: true, // find charts on the page (default)
|
|
126
|
+
mount: true, // draw them when loaded (default)
|
|
127
|
+
integrity: true, // put the manifest's SRI hashes on the script tags
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### npm
|
|
155
132
|
|
|
156
133
|
```sh
|
|
157
134
|
npm install blockcharts
|
|
158
135
|
```
|
|
159
136
|
|
|
160
|
-
The package ships the minified files of `release/` (kits, blocks, loader, manifest, types), not the composer or the scripts, and resolves them through `exports`:
|
|
161
|
-
|
|
162
137
|
```js
|
|
163
138
|
import BC from 'blockcharts'; // the full kit, typed
|
|
164
139
|
import BC from 'blockcharts/kits/basic.mjs'; // a smaller kit
|
|
165
140
|
import manifest from 'blockcharts/manifest.json' with { type: 'json' };
|
|
166
141
|
const BC = require('blockcharts'); // CommonJS: the same full kit
|
|
167
|
-
// a <script src> names the file where it is: node_modules/blockcharts/release/kits/timeseries.js (exports are for import/require)
|
|
168
142
|
```
|
|
169
143
|
|
|
170
|
-
|
|
144
|
+
The package carries the built files: kits, single blocks, the loader, the manifest and the types. Every kit has a
|
|
145
|
+
`.d.mts` (and a `.d.ts` for `require`); `blockcharts/bc.d.ts` declares the global `BC` and every spec type. A
|
|
146
|
+
`<script src>` names the file where it is: `node_modules/blockcharts/release/kits/basic.js`.
|
|
171
147
|
|
|
172
|
-
|
|
148
|
+
### CDN and integrity
|
|
173
149
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
The loader reads `manifest.json` next to it, loads the core, asks it which blocks the charts on the page need, loads those (and their dependencies), and mounts. Manual control:
|
|
150
|
+
jsDelivr and unpkg serve every file of the package at its path in it:
|
|
151
|
+
`https://cdn.jsdelivr.net/npm/blockcharts@<version>/release/<file>` (or `https://unpkg.com/blockcharts@<version>/release/<file>`),
|
|
152
|
+
where `<file>` is `core.js`, `blocks/<role.type>.js`, `kits/<name>.js` or `.mjs`, `loader.js`, `manifest.json` or
|
|
153
|
+
`bc.d.ts`. Pin an exact version (`@0.0.2`, not `@0` or `@latest`): files of a version never change, and
|
|
154
|
+
`manifest.json` lists a `sha384` hash of every file for the `integrity` attribute:
|
|
180
155
|
|
|
181
|
-
```
|
|
182
|
-
|
|
183
|
-
base: 'https://cdn.jsdelivr.net/npm/blockcharts@0.0.1/release/', // default: the folder of loader.js
|
|
184
|
-
blocks: ['interaction.zoom'], // extra blocks besides the ones found in the page
|
|
185
|
-
scan: true, // find charts on the page (default)
|
|
186
|
-
mount: true, // mount them when loaded (default)
|
|
187
|
-
integrity: true, // put the manifest's SRI hashes on the script tags
|
|
188
|
-
});
|
|
156
|
+
```html
|
|
157
|
+
<script src="https://cdn.jsdelivr.net/npm/blockcharts@0.0.2/release/kits/basic.js" integrity="sha384-..." crossorigin="anonymous"></script>
|
|
189
158
|
```
|
|
190
159
|
|
|
191
|
-
|
|
160
|
+
### The composer: one standalone report
|
|
192
161
|
|
|
193
|
-
|
|
194
|
-
`https://cdn.jsdelivr.net/npm/blockcharts@<version>/release/<file>` (or `https://unpkg.com/blockcharts@<version>/release/<file>`),
|
|
195
|
-
where `<file>` is `core.js`, `blocks/<role.type>.js`, `kits/<name>.js` or `.mjs`, `loader.js`, `manifest.json` or `bc.d.ts`.
|
|
196
|
-
The loader takes the core and the blocks from the folder it came from, so it needs nothing more than its own address.
|
|
162
|
+
The composer is a script of this repository, not part of the npm package: run it in a clone.
|
|
197
163
|
|
|
198
|
-
|
|
164
|
+
```sh
|
|
165
|
+
git clone https://github.com/boligolov/blockcharts.git
|
|
166
|
+
cd blockcharts
|
|
167
|
+
npm install
|
|
168
|
+
npm run build && npm run manifest
|
|
169
|
+
node scripts/compose.js skills/blockcharts/recipes/report-from-csv.json report.html
|
|
170
|
+
```
|
|
199
171
|
|
|
200
|
-
`
|
|
172
|
+
`page.json`:
|
|
201
173
|
|
|
202
|
-
```
|
|
203
|
-
|
|
174
|
+
```json
|
|
175
|
+
{
|
|
176
|
+
"title": "Sales",
|
|
177
|
+
"data": { "sales": [ { "region": "North", "total": 42 }, { "region": "South", "total": 58 } ] },
|
|
178
|
+
"charts": [
|
|
179
|
+
{
|
|
180
|
+
"title": "Sales by region",
|
|
181
|
+
"spec": {
|
|
182
|
+
"data": "sales",
|
|
183
|
+
"scales": { "x": { "type": "band" }, "y": { "type": "linear", "zero": true } },
|
|
184
|
+
"guides": [ { "type": "grid", "scale": "y" }, { "type": "axis", "scale": "x" }, { "type": "axis", "scale": "y" } ],
|
|
185
|
+
"marks": [ { "type": "rect", "x": "region", "y": "total" } ],
|
|
186
|
+
"interaction": [ { "type": "tooltip" } ]
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
]
|
|
190
|
+
}
|
|
204
191
|
```
|
|
205
192
|
|
|
206
|
-
|
|
193
|
+
The composer works out which blocks the charts need, checks every spec against the data (an unknown field, a wrong
|
|
194
|
+
scale type, … each reported with its path in the JSON), draws each chart once in a sandbox to catch what only shows
|
|
195
|
+
up when drawing, and writes the page; nothing is written until every chart is valid. Having checked everything, the
|
|
196
|
+
page carries a lighter core, without the checks and, unless a dataset is stored gzipped, without the data decoder:
|
|
197
|
+
about <!-- build:core-saving -->14 KB<!-- /build --> less per report.
|
|
207
198
|
|
|
208
|
-
|
|
199
|
+
Datasets can be rows, `{"columns": {...}}`, `{"csv": "text"}`, or `{"csvFile": "orders.csv"}` (next to `page.json`);
|
|
200
|
+
large ones are stored gzipped automatically. A dashboard is the same file with a grid: `"layout": {"columns": 4}` and
|
|
201
|
+
a `span` per chart (KPI tiles `span: 1`, a trend `span: 3`), with a `title` and a `subtitle` on top. Every chart sits
|
|
202
|
+
in a card, and on a phone the cards stack.
|
|
203
|
+
|
|
204
|
+
## How a chart is described
|
|
209
205
|
|
|
210
206
|
```json
|
|
211
207
|
{
|
|
@@ -219,7 +215,10 @@ Always pin an exact version in the CDN path (`@0.0.1`, not `@0` or `@latest`); f
|
|
|
219
215
|
}
|
|
220
216
|
```
|
|
221
217
|
|
|
222
|
-
Scales are declared once and named. Marks read data through channels (`"x": "region"` means field `region` on
|
|
218
|
+
Scales are declared once and named. Marks read data through channels (`"x": "region"` means the field `region` on
|
|
219
|
+
the scale named `x`). Guides attach to scales, not to marks, and several scales per direction are fine (a dual axis).
|
|
220
|
+
A block is named by its `type`, so `{"type": "rect"}` is `mark.rect`. Custom tooltip content is a function registered
|
|
221
|
+
with `BC.defineFn(name, fn)` and named in the spec (`"content": "name"`), so specs stay plain JSON.
|
|
223
222
|
|
|
224
223
|
## Blocks
|
|
225
224
|
|
|
@@ -239,49 +238,52 @@ The <!-- build:blocks -->58<!-- /build --> blocks, by role:
|
|
|
239
238
|
|
|
240
239
|
<!-- /build -->
|
|
241
240
|
|
|
242
|
-
Every parameter of every block
|
|
243
|
-
|
|
244
|
-
Custom tooltip content is a function registered in a script with `BC.defineFn(name, fn)` and referenced from the spec by name (`"content": "name"`), so specs stay plain JSON.
|
|
241
|
+
Every parameter of every block: [the block reference](https://blockcharts.online/blocks/), also in
|
|
242
|
+
[`skills/blockcharts/reference/blocks.md`](skills/blockcharts/reference/blocks.md).
|
|
245
243
|
|
|
246
|
-
##
|
|
247
|
-
|
|
248
|
-
`site/` is an [Astro](https://astro.build) project: a gallery with every recipe live (its JSON, the composed report, the HTML to embed it), demos of every way to embed (one script tag, the loader, an ES module, the handle API with a custom tooltip, themes, data forms), a guide, and the block reference generated from the code. Every snippet shown is the code that runs above it.
|
|
249
|
-
|
|
250
|
-
```sh
|
|
251
|
-
npm run site # builds the library, the release and the site -> site/dist, then checks links and charts
|
|
252
|
-
SITE_BASE=/blockcharts/ npm run site # for a site served from a subfolder (GitHub Pages)
|
|
253
|
-
npm run dev -w site # develop it (run npm run build and npm run manifest first)
|
|
254
|
-
```
|
|
244
|
+
## Browser support
|
|
255
245
|
|
|
256
|
-
|
|
246
|
+
Chrome and Edge 80, Firefox 74, Safari 14.1 or later: the code is ES2020, and compact numbers (`12K`) are `Intl`'s. A
|
|
247
|
+
dataset stored gzipped, which the composer does with a large one, is read with `DecompressionStream`: Firefox 113 and
|
|
248
|
+
Safari 16.4 (Chrome and Edge 80); an older browser shows a message in the chart's place. The composer needs Node 22+.
|
|
257
249
|
|
|
258
250
|
## Development
|
|
259
251
|
|
|
260
252
|
```sh
|
|
261
253
|
npm install
|
|
262
|
-
npm test
|
|
254
|
+
npm test # build, then the core, blocks, canvas, composer, CSV, skill and release tests
|
|
263
255
|
npm run test:chrome # layout in a real browser (the system Chrome)
|
|
264
|
-
npm run
|
|
265
|
-
npm run
|
|
266
|
-
npm run site # the website -> site/dist
|
|
267
|
-
npm run test:site # every page of it in Chrome at phone widths
|
|
256
|
+
npm run release # kits, single blocks and the loader -> release/
|
|
257
|
+
npm run skill # the skill's generated parts and the figures this README quotes
|
|
268
258
|
```
|
|
269
259
|
|
|
270
|
-
Layout, tests and releasing: [docs/development.md](docs/development.md). What is left to do:
|
|
260
|
+
Layout, tests, the website and releasing: [docs/development.md](docs/development.md). What is left to do:
|
|
271
261
|
[ROADMAP.md](ROADMAP.md). Design decisions: [docs/Design.md](docs/Design.md).
|
|
272
262
|
|
|
273
|
-
##
|
|
263
|
+
## For AI agents: the blockcharts skill
|
|
274
264
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
265
|
+
An agent skill turns a table or a CSV file into one light `.html` report with interactive charts. The agent writes
|
|
266
|
+
only the JSON; the composer bundled with the skill checks every chart against the data and writes the page.
|
|
267
|
+
|
|
268
|
+
**Claude Code** — from this repository, which is a plugin marketplace:
|
|
269
|
+
|
|
270
|
+
```
|
|
271
|
+
/plugin marketplace add boligolov/blockcharts
|
|
272
|
+
/plugin install blockcharts@blockcharts
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
It updates by itself once auto-update is on for the marketplace (`/plugin` → *Marketplaces*), or by hand with
|
|
276
|
+
`claude plugin update blockcharts@blockcharts`.
|
|
277
|
+
|
|
278
|
+
**Claude.ai and Claude Desktop** — download **[blockcharts.skill](https://blockcharts.online/blockcharts.skill)**
|
|
279
|
+
and upload it under *Settings → Capabilities → Skills* (skills need code execution enabled).
|
|
278
280
|
|
|
279
|
-
|
|
281
|
+
**Any other agent that reads skill folders** — copy [`skills/blockcharts/`](skills/blockcharts/) into its skills
|
|
282
|
+
folder (`~/.claude/skills/` for Claude Code). It needs Node.js 22+.
|
|
280
283
|
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
with a release of the npm package.
|
|
284
|
+
Then ask — *"an HTML report of monthly revenue by region from sales.csv"*, *"a dashboard of these KPIs"*. Scopes and
|
|
285
|
+
details: [docs/skill.md](docs/skill.md). The skill is versioned apart from the library (`metadata.version` in
|
|
286
|
+
`SKILL.md`, `version` in its `plugin.json`), so that Claude Code offers each update.
|
|
285
287
|
|
|
286
288
|
## License
|
|
287
289
|
|
package/package.json
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
/*! blockcharts 0.0.
|
|
2
|
-
"use strict";(function(){const nt="var(--bc-axis, #888)",$="var(--bc-text, #444)",S="var(--bc-label, var(--bc-text, #444))",A=t=>t.ticks===!0?9:8,j=(t,e)=>t.line==="on"?!0:t.line==="off"?!1:e==="top"||e==="bottom";function I(t,e){return t.position?t.position:t.scale&&e.orientation(t.scale)==="vertical"?"left":"bottom"}const F=new WeakMap,E=.71,ot=14,W=19,K=20,L=6,st=.85,it=/^\s*-?\d[\d.,:\/-]*(T[\d:.]+Z?)?\s*$/;function z(t,e){let i=0,o=0;for(const n of e.spec.guides||[])if(n!==t)if(n.type==="axis"){const s=n.scale?I(n,e):"top",a=(s==="left"||s==="right")&&et.measure(n,e)[s]||0;s==="left"?i+=a:s==="right"&&(o+=a)}else{const s=BC.get(`guide.${n.type}`),a=s&&s.measure?s.measure(n,e):{};i+=a.left||0,o+=a.right||0}return{x:e.plot.x+i,w:e.plot.w-i-o}}function lt(t,e,i){const o=t.scale?e.scales[t.scale]:void 0;if(!o||o.input!=="category"||i==="left"||i==="right"||e.orientation(t.scale)!=="horizontal")return 0;const n=o.domain(),s=z(t,e).w;return n.length<2||s/n.length<ot||n.every(a=>typeof a=="number"||a instanceof Date||it.test(String(a)))||q(t,e,s)>=n.length?0:s/n.length<W?90:45}const rt=/[、。,.・:;?!)」』】〕》〉ーゝゞ々ぁぃぅぇぉっゃゅょゎァィゥェォッャュョヮヵヶ]/,at=/[(「『【〔《〈]/,V=/\d/;function G(t,e){const i=[];for(let o=1;o<t.length;o++){const n=t[o-1],s=t[o];!e.wide(n)||rt.test(s)||at.test(n)||V.test(s)&&!(o>=2&&V.test(t[o-2]))||i.push(o)}return i}function H(t,e,i){const o=Array.from(t),n=(r,u)=>i.measure(o.slice(r,u).join(""),11).w;let s=-1,a=1/0;for(const r of G(o,i)){const u=n(0,r);if(u>e)break;const m=Math.max(u,n(r,o.length));m<a&&(a=m,s=r)}if(s<0){const r=G(o,i);if(!r.length)return[t];s=r[0]}return[o.slice(0,s).join(""),o.slice(s).join("")]}function q(t,e,i){const o=t.scale,n=e.orientation(o)==="horizontal";let s=0;for(const a of e.scales[o].domain()){const r=String(a);if(!n){s=Math.max(s,e.measure(r,11).h);continue}const u=e.measure(r,11).w,m=r.split(/\s+/).filter(Boolean),l=m.length===1&&e.wide(r)?Math.max(...H(r,1/0,e).map(f=>e.measure(f,11).w)):m.reduce((f,h)=>Math.max(f,e.measure(h,11).w),0);s=Math.max(s,Math.min(u,Math.max(l,u/2)))}return Math.floor(i/(n?s*.9+4:s+3))}const C=t=>t.length>K?t.slice(0,K-1)+"\u2026":t;function P(t,e){return ft(ct(t,e),t,e)}function ft(t,e,i){if(i.scales[e.scale].input==="category"||i.orientation(e.scale)!=="horizontal")return t;const o=X(e),n=t.filter((u,m)=>m===0||u.label!==t[m-1].label);if(n.length<2)return n;let s=1/0;for(let u=1;u<n.length;u++)s=Math.min(s,Math.abs(n[u].pos-n[u-1].pos));const a=n.reduce((u,m)=>Math.max(u,i.measure(m.label,11).w),0),r=s>0?Math.ceil((a+L)/s):1;return r>1?n.filter((u,m)=>o(m,n.length,r)):n}const X=t=>(e,i,o)=>(t.anchor==="end"?i-1-e:e)%o===0;function ut(t,e){if(t.format===void 0||e.orientation(t.scale)!=="horizontal")return 0;const i=BC.formatter(t.format),o=e.scales[t.scale].domain(),n=o.reduce((s,a)=>Math.max(s,e.measure(i(a),11).w),0);return Math.min(o.length,Math.floor(e.plot.w/(n+L)))}function ht(t,e){const i=t.scale,o=e.scales[i],n=typeof t.count=="number"&&t.count>=1?Math.round(t.count):void 0;if(o.input!=="category")return n===void 0?e.ticks(i):e.ticks(i,n);const s=Math.max(e.ticks(i).length,ut(t,e)),a=n===void 0?s:Math.min(n,s);if(t.anchor!=="end")return e.ticks(i,a);const r=e.ticks(i,o.domain().length),u=Math.max(1,Math.ceil(r.length/Math.max(1,a))),m=X(t);return r.filter((l,f)=>m(f,r.length,u))}function ct(t,e){const i=e.scales[t.scale],o=F.has(t)?e.ticks(t.scale,i.domain().length):ht(t,e);if(t.format===void 0||i.output!=="position")return o;const{input:n}=i,s=BC.formatter(t.format,n==="date"?"date":n==="category"?void 0:"number");return o.map(a=>({value:a.value,pos:a.pos,label:s(a.value)}))}const Q=(t,e)=>F.has(t)?P(t,e).map(i=>({...i,label:C(i.label)})):P(t,e),_=t=>typeof t.title=="string"?t.title:"",N=11*1.25,mt=.3;function U(t,e,i){if(i.measure(t,11).w<=e)return t;const o=Array.from(t);let n=0,s=o.length;for(;n<s;){const a=n+s+1>>1;i.measure(o.slice(0,a).join("")+"\u2026",11).w<=e?n=a:s=a-1}return o.slice(0,n).join("").trimEnd()+"\u2026"}function dt(t,e,i){const o=t.split(/\s+/).filter(Boolean);if(o.length<2){if(!i.wide(t))return null;const a=Array.from(t);let r=0;for(;r<a.length-1&&i.measure(a.slice(0,r+1).join(""),11).w<=e;)r++;return r?[a.slice(0,r).join(""),a.slice(r).join("")]:null}if(i.measure(o[0],11).w>e)return null;let n=o[0],s=1;for(;s<o.length-1&&i.measure(n+" "+o[s],11).w<=e;)n+=" "+o[s++];return[n,o.slice(s).join(" ")]}function gt(t,e){const i=e.scales[t.scale],o=i.input==="category"?i.domain().length:0,n=Math.max(44,e.size.w*mt),s=o>0&&e.plot.h/o>=2*N+2;return a=>{if(e.measure(a,11).w<=n)return[a];const r=s?dt(a,n,e):null;return r?[r[0],U(r[1],n,e)]:[U(a,n,e)]}}function D(t,e,i){if(t.scale&&(i==="left"||i==="right"))return gt(t,e);const o=t.scale?e.scales[t.scale]:void 0,n=o&&o.input==="category"?o.domain().length:0;if(!n)return a=>[a];const s=e.plot.w/n-6;return a=>Z(a,s,e)}function Z(t,e,i){if(i.measure(t,11).w<=e)return[t];const o=t.split(/\s+/).filter(Boolean);if(o.length<2)return i.wide(t)?H(t,e,i):[t];let n=o[0],s=1;for(;s<o.length-1&&i.measure(n+" "+o[s],11).w<=e;)n+=" "+o[s++];return[n,o.slice(s).join(" ")]}const J=new WeakMap;function B(t,e){const{text:i,from:o,to:n}=t;if(i===void 0&&o===void 0&&n===void 0)return null;if(typeof i!="string"||typeof o!="string"||typeof n!="string")throw new Error('an axis that names spans needs all three of "text", "from" and "to"');const s=I(t,e);if(s==="left"||s==="right")throw new Error('"text", "from" and "to" name spans along a horizontal axis (top or bottom)');return{text:i,from:o,to:n}}function wt(t,e,i){const o=B(t,e),n=e.table.columns,s=n[o.text],a=n[o.from],r=n[o.to],u=e.scales[t.scale];if(!s||!a||!r||!u)return[];const m=t.format!==void 0?BC.formatter(t.format):null,l=e.plot,f=g=>i.x+(g-l.x)*i.w/l.w,h=[],c=new Set;for(let g=0;g<e.table.length;g++){const T=s[g];if(T==null||T==="")continue;const O=m?m(T):String(T),d=u(a[g]),b=u(r[g]);if(!O||typeof d!="number"||typeof b!="number"||!isFinite(d)||!isFinite(b))continue;const k=(d+b)/2;if(k<l.x-.5||k>l.x+l.w+.5)continue;const y=`${d},${b}|${O}`;if(c.has(y))continue;c.add(y);const p=f(Math.min(d,b)),w=f(Math.max(d,b));h.push({label:O,lo:p,hi:w,mid:(p+w)/2})}return h.sort((g,T)=>g.mid-T.mid)}function Tt(t,e,i,o){const n=t.map(l=>{const f=Z(l.label,l.hi-l.lo-L,e),h=f.reduce((g,T)=>Math.max(g,e.textWidth(T,11)),0),c=f.reduce((g,T)=>Math.max(g,e.measure(T,11).w),h);return{...l,lines:f,w:h,room:c,x:l.mid}}),s=n.length;if(!s)return{named:n,ok:!0};const a=[0];for(let l=1;l<s;l++)a.push(a[l-1]+(n[l-1].w+n[l].w)/2+L);const r=[];for(let l=0;l<s;l++){let f={first:l,last:l,sum:n[l].mid-a[l]};for(;r.length;){const h=r[r.length-1];if(h.sum/(h.last-h.first+1)<f.sum/(f.last-f.first+1))break;r.pop(),f={first:h.first,last:f.last,sum:h.sum+f.sum}}r.push(f)}for(const l of r)for(let f=l.first;f<=l.last;f++)n[f].x=l.sum/(l.last-l.first+1)+a[f];const u=l=>a[l]-a[l-1];n[s-1].x=Math.min(n[s-1].x,o-n[s-1].room/2);for(let l=s-2;l>=0;l--)n[l].x=Math.min(n[l].x,n[l+1].x-u(l+1));n[0].x=Math.max(n[0].x,i+n[0].room/2);for(let l=1;l<s;l++)n[l].x=Math.max(n[l].x,n[l-1].x+u(l));const m=n[s-1].x+n[s-1].room/2<=o+.5&&n.every(l=>Math.abs(l.x-l.mid)<=l.w/2);return{named:n,ok:m}}const bt=2;function Y(t,e,i){const o=i?z(t,e):{x:e.plot.x,w:e.plot.w},n=wt(t,e,o).filter(r=>r.hi-r.lo>=bt),s=i?void 0:J.get(t),a=[];for(const r of n){const u=a[a.length-1];if(u&&r.mid-u.mid<W){r.hi-r.lo>u.hi-u.lo&&(a[a.length-1]=r);continue}a.push(r)}if(!s){const r=m=>Tt(m,e,0,e.size.w);let u=r(n);if(!u.ok){const m=n.map((c,g)=>g).sort((c,g)=>n[c].hi-n[c].lo-(n[g].hi-n[g].lo)),l=c=>{const g=new Set(m.slice(0,c));return n.filter((T,O)=>!g.has(O))};let f=1,h=n.length;for(;f<h;){const c=f+h>>1;r(l(c)).ok?h=c:f=c+1}u=r(l(f))}if(s===0||u.named.length>=a.length)return{angle:0,named:u.named}}return{angle:45,named:a.map(r=>({...r,label:C(r.label),lines:[C(r.label)],w:0,room:0,x:r.mid}))}}function tt(t,e,i,o,n){const s=o===90?0:E,a=o===90?1:E;let r=0,u=0,m=0;for(const f of n){const h=e.textWidth(f.label,11),c=(h+11)*s;r=Math.max(r,h*a),i==="bottom"?u=Math.max(u,e.plot.x-(f.pos-c)):m=Math.max(m,f.pos+c-(e.plot.x+e.plot.w))}const l={overhang:{top:0,right:m,bottom:0,left:u}};return l[i]=A(t)+r+11*E+4+(_(t)?20.4:0),l}const et={role:"guide",type:"axis",version:20,params:{scale:{kind:"scale",required:!0},position:{kind:"enum",values:["top","right","bottom","left"]},title:{kind:"string"},format:{kind:"format"},line:{kind:"enum",values:["auto","on","off"],default:"auto"},ticks:{kind:"boolean",default:!1},count:{kind:"number"},anchor:{kind:"enum",values:["start","end"],default:"start"},text:{kind:"field"},from:{kind:"field"},to:{kind:"field"}},fits:q,measure(t,e){if(!t.scale)return{};const i=I(t,e),o=i==="left"||i==="right";if(B(t,e)){const{angle:l,named:f}=Y(t,e,!0);if(J.set(t,l),l)return tt(t,e,i,l,f.map(d=>({pos:d.x,label:d.label})));const h=z(t,e);let c=0,g=0,T=0;for(const d of f)c=Math.max(c,d.lines.length>1?d.lines.length*N:e.measure(d.label,11).h),g=Math.max(g,h.x-(d.x-d.room/2)),T=Math.max(T,d.x+d.room/2-(h.x+h.w));const O={overhang:{top:0,right:T,bottom:0,left:g}};return O[i]=A(t)+c+4+(_(t)?20.4:0),O}const n=lt(t,e,i);if(n?F.set(t,n):F.delete(t),n)return tt(t,e,i,n,Q(t,e));let s=0;const a={top:0,right:0,bottom:0,left:0},r=e.plot,u=D(t,e,i);for(const l of P(t,e)){const f=u(l.label),h=f.reduce((c,g)=>{const T=e.measure(g,11);return{w:Math.max(c.w,T.w),h:c.h}},{w:0,h:f.length>1?f.length*N:e.measure(l.label,11).h});if(s=Math.max(s,o?h.w:h.h),o){const c=l.pos-(f.length-1)/2*N;a.top=Math.max(a.top,r.y-(l.pos-h.h/2),r.y-(c-st*11)),a.bottom=Math.max(a.bottom,l.pos+h.h/2-(r.y+r.h))}else a.left=Math.max(a.left,r.x-(l.pos-h.w/2)),a.right=Math.max(a.right,l.pos+h.w/2-(r.x+r.w))}const m={overhang:a};return m[i]=A(t)+s+4+(_(t)?20.4:0),m},render(t,e,i){if(!t.scale)return{};const o=I(t,e),{x:n,y:s,w:a,h:r}=e.plot,u=B(t,e),m=u?[]:Q(t,e),l=[],f=_(t);let h,c=0;const g=A(t),T=t.ticks===!0;if(o==="left"||o==="right"){const d=o==="left"?-1:1,b=o==="left"?n-i.left:n+a+i.right;h=j(t,o)?`M${b} ${s}V${s+r}`:"";const k=D(t,e,o);for(const w of m){T&&(h+=`M${b} ${w.pos}H${b+d*5}`);const M=k(w.label),x=M.length===1&&M[0]===w.label?void 0:w.label;M.forEach((v,R)=>{c=Math.max(c,e.measure(v,11).w),l.push({type:"text",x:b+d*g,y:w.pos+(R-(M.length-1)/2)*N,text:v,size:11,anchor:d<0?"end":"start",baseline:"middle",style:{fill:S},cls:"bc-axis-label",title:x})})}const y=b+d*(g+c+4+20.4/2);if(!!f&&Array.from(f).every(w=>e.wide(w))&&f.length*12*1.1<=r){const w=Array.from(f);w.forEach((M,x)=>{l.push({type:"text",x:y,y:s+r/2+(x-(w.length-1)/2)*12*1.1,text:M,size:12,weight:600,anchor:"middle",baseline:"middle",style:{fill:$},cls:"bc-axis-title"})})}else f&&l.push({type:"text",x:y,y:s+r/2,text:f,size:12,weight:600,anchor:"middle",baseline:"middle",rotate:d<0?-90:90,style:{fill:$},cls:"bc-axis-title"})}else{const d=o==="top"?-1:1,b=o==="top"?s-i.top:s+r+i.bottom;h=j(t,o)?`M${n} ${b}H${n+a}`:"";const k=(p,w,M)=>{c=Math.max(c,e.textWidth(w,11)*(M===90?1:E)+11*E),l.push({type:"text",x:p,y:b+d*g,text:w,size:11,anchor:d<0?"start":"end",baseline:"middle",rotate:-M,style:{fill:S},cls:"bc-axis-label"})},y=(p,w,M)=>{c=Math.max(c,M.length>1?M.length*N:e.measure(w,11).h),(d<0?M.slice().reverse():M).forEach((v,R)=>{l.push({type:"text",x:p,y:b+d*(g+R*N),text:v,size:11,anchor:"middle",baseline:d<0?"auto":"hanging",style:{fill:S},cls:"bc-axis-label"})})};if(u){const{angle:p,named:w}=Y(t,e,!1);for(const M of w)T&&(h+=`M${M.mid} ${b}V${b+d*5}`),p?k(M.mid,M.label,p):y(M.x,M.label,M.lines)}else{const p=D(t,e,o);for(const w of m)T&&(h+=`M${w.pos} ${b}V${b+d*5}`),F.has(t)?k(w.pos,w.label,F.get(t)):y(w.pos,w.label,p(w.label))}if(f){const p=b+d*(g+c+4+2);l.push({type:"text",x:n+a/2,y:p,text:f,size:12,weight:600,anchor:"middle",baseline:d<0?"auto":"hanging",style:{fill:$},cls:"bc-axis-title"})}}return{axes:[...h?[{type:"path",d:h,cls:"bc-axis",style:{fill:"none",stroke:nt,strokeWidth:1}}]:[],...l]}}};BC.define(et)})();
|
|
1
|
+
/*! blockcharts 0.0.2 | MIT License */
|
|
2
|
+
"use strict";(function(E){const ot="var(--bc-axis, #888)",S="var(--bc-text, #444)",z="var(--bc-label, var(--bc-text, #444))",I=t=>t.ticks===!0?9:8,K=(t,e)=>t.line==="on"?!0:t.line==="off"?!1:e==="top"||e==="bottom";function L(t,e){return t.position?t.position:t.scale&&e.orientation(t.scale)==="vertical"?"left":"bottom"}const F=new WeakMap,A=.71,st=14,V=19,G=20,_=6,it=.85,lt=/^\s*-?\d[\d.,:\/-]*(T[\d:.]+Z?)?\s*$/;function P(t,e){let i=0,o=0;for(const n of e.spec.guides||[])if(n!==t)if(n.type==="axis"){const s=n.scale?L(n,e):"top",a=(s==="left"||s==="right")&&nt.measure(n,e)[s]||0;s==="left"?i+=a:s==="right"&&(o+=a)}else{const s=E.get(`guide.${n.type}`),a=s&&s.measure?s.measure(n,e):{};i+=a.left||0,o+=a.right||0}return{x:e.plot.x+i,w:e.plot.w-i-o}}function rt(t,e,i){const o=t.scale?e.scales[t.scale]:void 0;if(!o||o.input!=="category"||i==="left"||i==="right"||e.orientation(t.scale)!=="horizontal")return 0;const n=o.domain(),s=P(t,e).w;return n.length<2||s/n.length<st||n.every(a=>typeof a=="number"||a instanceof Date||lt.test(String(a)))||X(t,e,s)>=n.length?0:s/n.length<V?90:45}const at=/[、。,.・:;?!)」』】〕》〉ーゝゞ々ぁぃぅぇぉっゃゅょゎァィゥェォッャュョヮヵヶ]/,ft=/[(「『【〔《〈]/,H=/\d/;function q(t,e){const i=[];for(let o=1;o<t.length;o++){const n=t[o-1],s=t[o];!e.wide(n)||at.test(s)||ft.test(n)||H.test(s)&&!(o>=2&&H.test(t[o-2]))||i.push(o)}return i}function B(t,e,i){const o=Array.from(t),n=(r,u)=>i.measure(o.slice(r,u).join(""),11).w;let s=-1,a=1/0;for(const r of q(o,i)){const u=n(0,r);if(u>e)break;const m=Math.max(u,n(r,o.length));m<a&&(a=m,s=r)}if(s<0){const r=q(o,i);if(!r.length)return[t];s=r[0]}return[o.slice(0,s).join(""),o.slice(s).join("")]}function X(t,e,i){const o=t.scale,n=e.orientation(o)==="horizontal";let s=0;for(const a of e.scales[o].domain()){const r=String(a);if(!n){s=Math.max(s,e.measure(r,11).h);continue}const u=e.measure(r,11).w,m=r.split(/\s+/).filter(Boolean),l=m.length===1&&e.wide(r)?Math.max(...B(r,1/0,e).map(f=>e.measure(f,11).w)):m.reduce((f,h)=>Math.max(f,e.measure(h,11).w),0);s=Math.max(s,Math.min(u,Math.max(l,u/2)))}return Math.floor(i/(n?s*.9+4:s+3))}const D=t=>t.length>G?t.slice(0,G-1)+"\u2026":t;function R(t,e){return ut(mt(t,e),t,e)}function ut(t,e,i){if(i.scales[e.scale].input==="category"||i.orientation(e.scale)!=="horizontal")return t;const o=Q(e),n=t.filter((u,m)=>m===0||u.label!==t[m-1].label);if(n.length<2)return n;let s=1/0;for(let u=1;u<n.length;u++)s=Math.min(s,Math.abs(n[u].pos-n[u-1].pos));const a=n.reduce((u,m)=>Math.max(u,i.measure(m.label,11).w),0),r=s>0?Math.ceil((a+_)/s):1;return r>1?n.filter((u,m)=>o(m,n.length,r)):n}const Q=t=>(e,i,o)=>(t.anchor==="end"?i-1-e:e)%o===0;function ht(t,e){if(t.format===void 0||e.orientation(t.scale)!=="horizontal")return 0;const i=E.formatter(t.format),o=e.scales[t.scale].domain(),n=o.reduce((s,a)=>Math.max(s,e.measure(i(a),11).w),0);return Math.min(o.length,Math.floor(e.plot.w/(n+_)))}function ct(t,e){const i=t.scale,o=e.scales[i],n=typeof t.count=="number"&&t.count>=1?Math.round(t.count):void 0;if(o.input!=="category")return n===void 0?e.ticks(i):e.ticks(i,n);const s=Math.max(e.ticks(i).length,ht(t,e)),a=n===void 0?s:Math.min(n,s);if(t.anchor!=="end")return e.ticks(i,a);const r=e.ticks(i,o.domain().length),u=Math.max(1,Math.ceil(r.length/Math.max(1,a))),m=Q(t);return r.filter((l,f)=>m(f,r.length,u))}function mt(t,e){const i=e.scales[t.scale],o=F.has(t)?e.ticks(t.scale,i.domain().length):ct(t,e);if(t.format===void 0||i.output!=="position")return o;const{input:n}=i,s=E.formatter(t.format,n==="date"?"date":n==="category"?void 0:"number");return o.map(a=>({value:a.value,pos:a.pos,label:s(a.value)}))}const U=(t,e)=>F.has(t)?R(t,e).map(i=>({...i,label:D(i.label)})):R(t,e),x=t=>typeof t.title=="string"?t.title:"",N=11*1.25,dt=.3;function Z(t,e,i){if(i.measure(t,11).w<=e)return t;const o=Array.from(t);let n=0,s=o.length;for(;n<s;){const a=n+s+1>>1;i.measure(o.slice(0,a).join("")+"\u2026",11).w<=e?n=a:s=a-1}return o.slice(0,n).join("").trimEnd()+"\u2026"}function gt(t,e,i){const o=t.split(/\s+/).filter(Boolean);if(o.length<2){if(!i.wide(t))return null;const a=Array.from(t);let r=0;for(;r<a.length-1&&i.measure(a.slice(0,r+1).join(""),11).w<=e;)r++;return r?[a.slice(0,r).join(""),a.slice(r).join("")]:null}if(i.measure(o[0],11).w>e)return null;let n=o[0],s=1;for(;s<o.length-1&&i.measure(n+" "+o[s],11).w<=e;)n+=" "+o[s++];return[n,o.slice(s).join(" ")]}function wt(t,e){const i=e.scales[t.scale],o=i.input==="category"?i.domain().length:0,n=Math.max(44,e.size.w*dt),s=o>0&&e.plot.h/o>=2*N+2;return a=>{if(e.measure(a,11).w<=n)return[a];const r=s?gt(a,n,e):null;return r?[r[0],Z(r[1],n,e)]:[Z(a,n,e)]}}function j(t,e,i){if(t.scale&&(i==="left"||i==="right"))return wt(t,e);const o=t.scale?e.scales[t.scale]:void 0,n=o&&o.input==="category"?o.domain().length:0;if(!n)return a=>[a];const s=e.plot.w/n-6;return a=>J(a,s,e)}function J(t,e,i){if(i.measure(t,11).w<=e)return[t];const o=t.split(/\s+/).filter(Boolean);if(o.length<2)return i.wide(t)?B(t,e,i):[t];let n=o[0],s=1;for(;s<o.length-1&&i.measure(n+" "+o[s],11).w<=e;)n+=" "+o[s++];return[n,o.slice(s).join(" ")]}const Y=new WeakMap;function W(t,e){const{text:i,from:o,to:n}=t;if(i===void 0&&o===void 0&&n===void 0)return null;if(typeof i!="string"||typeof o!="string"||typeof n!="string")throw new Error('an axis that names spans needs all three of "text", "from" and "to"');const s=L(t,e);if(s==="left"||s==="right")throw new Error('"text", "from" and "to" name spans along a horizontal axis (top or bottom)');return{text:i,from:o,to:n}}function Tt(t,e,i){const o=W(t,e),n=e.table.columns,s=n[o.text],a=n[o.from],r=n[o.to],u=e.scales[t.scale];if(!s||!a||!r||!u)return[];const m=t.format!==void 0?E.formatter(t.format):null,l=e.plot,f=g=>i.x+(g-l.x)*i.w/l.w,h=[],c=new Set;for(let g=0;g<e.table.length;g++){const T=s[g];if(T==null||T==="")continue;const O=m?m(T):String(T),d=u(a[g]),b=u(r[g]);if(!O||typeof d!="number"||typeof b!="number"||!isFinite(d)||!isFinite(b))continue;const k=(d+b)/2;if(k<l.x-.5||k>l.x+l.w+.5)continue;const y=`${d},${b}|${O}`;if(c.has(y))continue;c.add(y);const p=f(Math.min(d,b)),w=f(Math.max(d,b));h.push({label:O,lo:p,hi:w,mid:(p+w)/2})}return h.sort((g,T)=>g.mid-T.mid)}function bt(t,e,i,o){const n=t.map(l=>{const f=J(l.label,l.hi-l.lo-_,e),h=f.reduce((g,T)=>Math.max(g,e.textWidth(T,11)),0),c=f.reduce((g,T)=>Math.max(g,e.measure(T,11).w),h);return{...l,lines:f,w:h,room:c,x:l.mid}}),s=n.length;if(!s)return{named:n,ok:!0};const a=[0];for(let l=1;l<s;l++)a.push(a[l-1]+(n[l-1].w+n[l].w)/2+_);const r=[];for(let l=0;l<s;l++){let f={first:l,last:l,sum:n[l].mid-a[l]};for(;r.length;){const h=r[r.length-1];if(h.sum/(h.last-h.first+1)<f.sum/(f.last-f.first+1))break;r.pop(),f={first:h.first,last:f.last,sum:h.sum+f.sum}}r.push(f)}for(const l of r)for(let f=l.first;f<=l.last;f++)n[f].x=l.sum/(l.last-l.first+1)+a[f];const u=l=>a[l]-a[l-1];n[s-1].x=Math.min(n[s-1].x,o-n[s-1].room/2);for(let l=s-2;l>=0;l--)n[l].x=Math.min(n[l].x,n[l+1].x-u(l+1));n[0].x=Math.max(n[0].x,i+n[0].room/2);for(let l=1;l<s;l++)n[l].x=Math.max(n[l].x,n[l-1].x+u(l));const m=n[s-1].x+n[s-1].room/2<=o+.5&&n.every(l=>Math.abs(l.x-l.mid)<=l.w/2);return{named:n,ok:m}}const Mt=2;function tt(t,e,i){const o=i?P(t,e):{x:e.plot.x,w:e.plot.w},n=Tt(t,e,o).filter(r=>r.hi-r.lo>=Mt),s=i?void 0:Y.get(t),a=[];for(const r of n){const u=a[a.length-1];if(u&&r.mid-u.mid<V){r.hi-r.lo>u.hi-u.lo&&(a[a.length-1]=r);continue}a.push(r)}if(!s){const r=m=>bt(m,e,0,e.size.w);let u=r(n);if(!u.ok){const m=n.map((c,g)=>g).sort((c,g)=>n[c].hi-n[c].lo-(n[g].hi-n[g].lo)),l=c=>{const g=new Set(m.slice(0,c));return n.filter((T,O)=>!g.has(O))};let f=1,h=n.length;for(;f<h;){const c=f+h>>1;r(l(c)).ok?h=c:f=c+1}u=r(l(f))}if(s===0||u.named.length>=a.length)return{angle:0,named:u.named}}return{angle:45,named:a.map(r=>({...r,label:D(r.label),lines:[D(r.label)],w:0,room:0,x:r.mid}))}}function et(t,e,i,o,n){const s=o===90?0:A,a=o===90?1:A;let r=0,u=0,m=0;for(const f of n){const h=e.textWidth(f.label,11),c=(h+11)*s;r=Math.max(r,h*a),i==="bottom"?u=Math.max(u,e.plot.x-(f.pos-c)):m=Math.max(m,f.pos+c-(e.plot.x+e.plot.w))}const l={overhang:{top:0,right:m,bottom:0,left:u}};return l[i]=I(t)+r+11*A+4+(x(t)?20.4:0),l}const nt={role:"guide",type:"axis",version:20,params:{scale:{kind:"scale",required:!0},position:{kind:"enum",values:["top","right","bottom","left"]},title:{kind:"string"},format:{kind:"format"},line:{kind:"enum",values:["auto","on","off"],default:"auto"},ticks:{kind:"boolean",default:!1},count:{kind:"number"},anchor:{kind:"enum",values:["start","end"],default:"start"},text:{kind:"field"},from:{kind:"field"},to:{kind:"field"}},fits:X,measure(t,e){if(!t.scale)return{};const i=L(t,e),o=i==="left"||i==="right";if(W(t,e)){const{angle:l,named:f}=tt(t,e,!0);if(Y.set(t,l),l)return et(t,e,i,l,f.map(d=>({pos:d.x,label:d.label})));const h=P(t,e);let c=0,g=0,T=0;for(const d of f)c=Math.max(c,d.lines.length>1?d.lines.length*N:e.measure(d.label,11).h),g=Math.max(g,h.x-(d.x-d.room/2)),T=Math.max(T,d.x+d.room/2-(h.x+h.w));const O={overhang:{top:0,right:T,bottom:0,left:g}};return O[i]=I(t)+c+4+(x(t)?20.4:0),O}const n=rt(t,e,i);if(n?F.set(t,n):F.delete(t),n)return et(t,e,i,n,U(t,e));let s=0;const a={top:0,right:0,bottom:0,left:0},r=e.plot,u=j(t,e,i);for(const l of R(t,e)){const f=u(l.label),h=f.reduce((c,g)=>{const T=e.measure(g,11);return{w:Math.max(c.w,T.w),h:c.h}},{w:0,h:f.length>1?f.length*N:e.measure(l.label,11).h});if(s=Math.max(s,o?h.w:h.h),o){const c=l.pos-(f.length-1)/2*N;a.top=Math.max(a.top,r.y-(l.pos-h.h/2),r.y-(c-it*11)),a.bottom=Math.max(a.bottom,l.pos+h.h/2-(r.y+r.h))}else a.left=Math.max(a.left,r.x-(l.pos-h.w/2)),a.right=Math.max(a.right,l.pos+h.w/2-(r.x+r.w))}const m={overhang:a};return m[i]=I(t)+s+4+(x(t)?20.4:0),m},render(t,e,i){if(!t.scale)return{};const o=L(t,e),{x:n,y:s,w:a,h:r}=e.plot,u=W(t,e),m=u?[]:U(t,e),l=[],f=x(t);let h,c=0;const g=I(t),T=t.ticks===!0;if(o==="left"||o==="right"){const d=o==="left"?-1:1,b=o==="left"?n-i.left:n+a+i.right;h=K(t,o)?`M${b} ${s}V${s+r}`:"";const k=j(t,e,o);for(const w of m){T&&(h+=`M${b} ${w.pos}H${b+d*5}`);const M=k(w.label),v=M.length===1&&M[0]===w.label?void 0:w.label;M.forEach(($,C)=>{c=Math.max(c,e.measure($,11).w),l.push({type:"text",x:b+d*g,y:w.pos+(C-(M.length-1)/2)*N,text:$,size:11,anchor:d<0?"end":"start",baseline:"middle",style:{fill:z},cls:"bc-axis-label",title:v})})}const y=b+d*(g+c+4+20.4/2);if(!!f&&Array.from(f).every(w=>e.wide(w))&&f.length*12*1.1<=r){const w=Array.from(f);w.forEach((M,v)=>{l.push({type:"text",x:y,y:s+r/2+(v-(w.length-1)/2)*12*1.1,text:M,size:12,weight:600,anchor:"middle",baseline:"middle",style:{fill:S},cls:"bc-axis-title"})})}else f&&l.push({type:"text",x:y,y:s+r/2,text:f,size:12,weight:600,anchor:"middle",baseline:"middle",rotate:d<0?-90:90,style:{fill:S},cls:"bc-axis-title"})}else{const d=o==="top"?-1:1,b=o==="top"?s-i.top:s+r+i.bottom;h=K(t,o)?`M${n} ${b}H${n+a}`:"";const k=(p,w,M)=>{c=Math.max(c,e.textWidth(w,11)*(M===90?1:A)+11*A),l.push({type:"text",x:p,y:b+d*g,text:w,size:11,anchor:d<0?"start":"end",baseline:"middle",rotate:-M,style:{fill:z},cls:"bc-axis-label"})},y=(p,w,M)=>{c=Math.max(c,M.length>1?M.length*N:e.measure(w,11).h),(d<0?M.slice().reverse():M).forEach(($,C)=>{l.push({type:"text",x:p,y:b+d*(g+C*N),text:$,size:11,anchor:"middle",baseline:d<0?"auto":"hanging",style:{fill:z},cls:"bc-axis-label"})})};if(u){const{angle:p,named:w}=tt(t,e,!1);for(const M of w)T&&(h+=`M${M.mid} ${b}V${b+d*5}`),p?k(M.mid,M.label,p):y(M.x,M.label,M.lines)}else{const p=j(t,e,o);for(const w of m)T&&(h+=`M${w.pos} ${b}V${b+d*5}`),F.has(t)?k(w.pos,w.label,F.get(t)):y(w.pos,w.label,p(w.label))}if(f){const p=b+d*(g+c+4+2);l.push({type:"text",x:n+a/2,y:p,text:f,size:12,weight:600,anchor:"middle",baseline:d<0?"auto":"hanging",style:{fill:S},cls:"bc-axis-title"})}}return{axes:[...h?[{type:"path",d:h,cls:"bc-axis",style:{fill:"none",stroke:ot,strokeWidth:1}}]:[],...l]}}};E.define(nt)})(globalThis.BC);
|
|
3
3
|
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
/*! blockcharts 0.0.
|
|
2
|
-
"use strict";(function(){const
|
|
1
|
+
/*! blockcharts 0.0.2 | MIT License */
|
|
2
|
+
"use strict";(function(u){const c={role:"guide",type:"grid",version:3,narrow:!0,params:{scale:{kind:"scale",required:!0},count:{kind:"number"}},render(e,n){const t=e.scale;if(!t)return{};const a=n.orientation(t)==="vertical",{x:o,y:i,w:l,h:f}=n.plot;let r="";const s=typeof e.count=="number"&&e.count>=1?Math.round(e.count):void 0;for(const d of s===void 0?n.ticks(t):n.ticks(t,s))r+=a?`M${o} ${d.pos}H${o+l}`:`M${d.pos} ${i}V${i+f}`;return r?{grid:[{type:"path",d:r,cls:"bc-grid",style:{fill:"none",stroke:"var(--bc-grid, #e6e6e6)",strokeWidth:1}}]}:{}}};u.define(c)})(globalThis.BC);
|
|
3
3
|
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
/*! blockcharts 0.0.
|
|
2
|
-
"use strict";(function(){const v="var(--bc-label, var(--bc-text, #666))"
|
|
1
|
+
/*! blockcharts 0.0.2 | MIT License */
|
|
2
|
+
"use strict";(function(m){const v="var(--bc-label, var(--bc-text, #666))",$="var(--bc-kpi, var(--bc-text, #111))",x="var(--bc-good, #12a150)",W="var(--bc-bad, #e5484d)",F="var(--bc-label, #888)",_=["last","first","sum","mean","min","max","count"];function p(t){const o=[];if(!t)return o;for(let n=0;n<t.length;n++){const e=t[n];typeof e=="number"&&isFinite(e)&&o.push(e)}return o}function I(t,o){if(o==="count")return t.length;if(!t.length)return NaN;switch(o){case"first":return t[0];case"sum":return t.reduce((n,e)=>n+e,0);case"mean":return t.reduce((n,e)=>n+e,0)/t.length;case"min":return t.reduce((n,e)=>e<n?e:n);case"max":return t.reduce((n,e)=>e>n?e:n);default:return t[t.length-1]}}const h=(t,o)=>t.baseTable.columns[o]||t.table.columns[o];function U(t,o){const n=typeof t.aggregate=="string"&&_.indexOf(t.aggregate)>=0?t.aggregate:"last",e=p(h(o,t.field)),a=I(e,n);let i=NaN;if(typeof t.against=="string")i=I(p(h(o,t.against)),n);else{const r=t.compare==="first"||t.compare==="none"||t.compare==="previous"?t.compare:n==="last"?"previous":"none";r==="previous"&&e.length>1?i=e[e.length-2]:r==="first"&&e.length>1&&(i=e[0])}return{value:a,base:i}}const V=t=>typeof t.note=="string"?t.note:"";function N(t,o,n,e,a,i){const r=t.textWidth(o,n,e);return r<=a?n:Math.max(Math.min(i,n),Math.floor(n*a/r*2)/2)}function L(t,o,n,e,a){if(t.textWidth(o,n,e)<=a)return o;const i=Array.from(o);let r=0,l=i.length;for(;r<l;){const s=r+l+1>>1;t.textWidth(i.slice(0,s).join("")+"\u2026",n,e)<=a?r=s:l=s-1}return r?i.slice(0,r).join("").trimEnd()+"\u2026":""}function M(t,o){const n=Math.max(1,o.size.w-o.plot.x-2),e=typeof t.size=="number"&&t.size>0&&isFinite(t.size)?t.size:30,a=B(t,o),i=typeof t.label=="string"?t.label:"",r=i&&a?`${i}: ${a}`:i||a,l=N(o,r,12,500,n,10),s=L(o,r,l,500,n),{value:u,base:c}=U(t,o),w=t.format!==void 0?m.formatter(t.format):d=>m.formatter("compact")(d),b=isFinite(u)?w(u):"\u2013",A=N(o,b,e,650,n,8);let f="",G="",O=F;if(isFinite(u)&&isFinite(c)){const d=u-c,k=d>0,z=d===0,J=t.better==="down"?!k:k;O=z?F:J?x:W;const D=t.deltaFormat!==void 0?m.formatter(t.deltaFormat):null,P=E=>k&&!/^[+]/.test(E)?"+"+E:E;let y;if(t.delta==="absolute")y=P((D||w)(d*(typeof t.deltaScale=="number"&&isFinite(t.deltaScale)?t.deltaScale:1)));else{const E=D||m.formatter({style:"percent",maximumFractionDigits:1});y=c!==0?P(E(d/Math.abs(c))):""}f=`${z?"\u25A0":k?"\u25B2":"\u25BC"} ${y}`.trim(),G=y}const g=V(t),T=f&&g?6:0,Z=(d=>(f?o.textWidth(f,d,600):0)+(g?o.textWidth(g,d,400):0))(12),S=T+Z<=n?12:Math.max(10,Math.floor(12*Math.max(0,n-T)/Z*2)/2);f&&(f=L(o,f,S,600,n));const X=g?L(o,g,S,400,n-(f?o.textWidth(f,S,600)+T:0)):"",q=f||g?18:0,R=(r?18:0)+A+q+10,H=[r,b,[G,g].filter(Boolean).join(" ")];return{labelled:!!r,label:s,labelSize:l,value:b,valueSize:A,change:f,changeColor:O,note:X,lineSize:S,height:R,spoken:H}}function B(t,o){if(typeof t.rowField!="string")return"";const n=h(o,t.rowField),e=h(o,t.field);if(!n||!e)return"";const a=typeof t.aggregate=="string"?t.aggregate:"last";let i=-1;for(let l=0;l<e.length;l++){const s=e[l];if(typeof s!="number"||!isFinite(s))continue;const u=i<0?NaN:e[i];if((i<0||a==="last"||a==="max"&&s>u||a==="min"&&s<u)&&(i=l),a==="first")break}const r=i<0?null:n[i];return r==null||r!==r?"":t.rowFormat!==void 0?m.formatter(t.rowFormat)(n[i]):String(n[i])}const j={role:"guide",type:"kpi",version:10,narrow:!0,params:{field:{kind:"field",required:!0},label:{kind:"string"},aggregate:{kind:"enum",values:_,default:"last"},compare:{kind:"enum",values:["previous","first","none"]},against:{kind:"field"},delta:{kind:"enum",values:["percent","absolute"],default:"percent"},deltaFormat:{kind:"format"},deltaScale:{kind:"number",default:1},better:{kind:"enum",values:["up","down"],default:"up"},format:{kind:"format"},note:{kind:"string"},rowField:{kind:"field"},rowFormat:{kind:"format"},size:{kind:"number",default:30}},measure(t,o){const n=typeof t.aggregate=="string"?t.aggregate:"last";if((t.compare==="previous"||t.compare==="first")&&n!=="last"&&typeof t.against!="string")throw new Error(`guide.kpi: compare "${t.compare}" compares one row with another, so it needs aggregate "last" (not "${n}"); to compare a ${n} with something, give "against" a field`);if(typeof t.rowField=="string"&&["last","first","min","max"].indexOf(n)<0)throw new Error(`guide.kpi: rowField names one row, and aggregate "${n}" comes from many; use last, first, min or max`);return{top:M(t,o).height}},render(t,o,n){if(typeof t.field!="string"||!h(o,t.field))return{};const e=M(t,o),a=o.plot.x;let i=o.plot.y-n.top-e.height;const r=[],l=(u,c,w,b,A,f)=>r.push({type:"text",x:c,y:i,text:u,size:w,weight:b,baseline:"hanging",style:{fill:A},cls:f});e.label&&l(e.label,a,e.labelSize,500,v,"bc-kpi-label"),e.labelled&&(i+=18),l(e.value,a,e.valueSize,650,$,"bc-kpi-value"),i+=e.valueSize+6;let s=a;e.change&&(l(e.change,a,e.lineSize,600,e.changeColor,"bc-kpi-delta"),s=a+o.textWidth(e.change,e.lineSize,600)+6),e.note&&l(e.note,s,e.lineSize,400,v,"bc-kpi-note");for(const u of e.spoken)o.say(u);return{axes:r}}};m.define(j)})(globalThis.BC);
|
|
3
3
|
|