@samizdata/mozaic 0.0.0-stage → 0.2.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 +674 -0
- package/README.md +44 -2
- package/dist/annotations/Arrow.svelte +50 -0
- package/dist/annotations/Arrow.svelte.d.ts +18 -0
- package/dist/annotations/Callout.svelte +87 -0
- package/dist/annotations/Callout.svelte.d.ts +22 -0
- package/dist/annotations/Label.svelte +55 -0
- package/dist/annotations/Label.svelte.d.ts +19 -0
- package/dist/annotations/RefBand.svelte +46 -0
- package/dist/annotations/RefBand.svelte.d.ts +18 -0
- package/dist/annotations/RefLine.svelte +72 -0
- package/dist/annotations/RefLine.svelte.d.ts +19 -0
- package/dist/annotations/callout-geometry.d.ts +23 -0
- package/dist/annotations/callout-geometry.js +45 -0
- package/dist/annotations/index.d.ts +6 -0
- package/dist/annotations/index.js +6 -0
- package/dist/core/Canvas.svelte +55 -0
- package/dist/core/Canvas.svelte.d.ts +19 -0
- package/dist/core/Facets.svelte +80 -0
- package/dist/core/Facets.svelte.d.ts +49 -0
- package/dist/core/Html.svelte +38 -0
- package/dist/core/Html.svelte.d.ts +13 -0
- package/dist/core/Plot.svelte +202 -0
- package/dist/core/Plot.svelte.d.ts +60 -0
- package/dist/core/Svg.svelte +58 -0
- package/dist/core/Svg.svelte.d.ts +17 -0
- package/dist/core/accessor.d.ts +5 -0
- package/dist/core/accessor.js +3 -0
- package/dist/core/context.d.ts +25 -0
- package/dist/core/context.js +69 -0
- package/dist/core/index.d.ts +10 -0
- package/dist/core/index.js +9 -0
- package/dist/core/scales.d.ts +16 -0
- package/dist/core/scales.js +32 -0
- package/dist/core/types.d.ts +78 -0
- package/dist/core/types.js +1 -0
- package/dist/core/units.d.ts +6 -0
- package/dist/core/units.js +10 -0
- package/dist/embed/Embed.svelte +50 -0
- package/dist/embed/Embed.svelte.d.ts +14 -0
- package/dist/embed/code.d.ts +18 -0
- package/dist/embed/code.js +9 -0
- package/dist/embed/host.d.ts +6 -0
- package/dist/embed/host.js +20 -0
- package/dist/embed/index.d.ts +6 -0
- package/dist/embed/index.js +5 -0
- package/dist/embed/manifest.d.ts +16 -0
- package/dist/embed/manifest.js +1 -0
- package/dist/embed/params.svelte.d.ts +10 -0
- package/dist/embed/params.svelte.js +27 -0
- package/dist/embed/resize.d.ts +13 -0
- package/dist/embed/resize.js +28 -0
- package/dist/export/download.d.ts +13 -0
- package/dist/export/download.js +52 -0
- package/dist/export/figure-to-svg.d.ts +21 -0
- package/dist/export/figure-to-svg.js +187 -0
- package/dist/export/fonts.d.ts +26 -0
- package/dist/export/fonts.js +112 -0
- package/dist/export/index.d.ts +2 -0
- package/dist/export/index.js +2 -0
- package/dist/frame/Byline.svelte +26 -0
- package/dist/frame/Byline.svelte.d.ts +8 -0
- package/dist/frame/Chart.svelte +144 -0
- package/dist/frame/Chart.svelte.d.ts +37 -0
- package/dist/frame/ChartTools.svelte +259 -0
- package/dist/frame/ChartTools.svelte.d.ts +17 -0
- package/dist/frame/DataDownload.svelte +34 -0
- package/dist/frame/DataDownload.svelte.d.ts +34 -0
- package/dist/frame/DataTable.svelte +49 -0
- package/dist/frame/DataTable.svelte.d.ts +34 -0
- package/dist/frame/Footer.svelte +39 -0
- package/dist/frame/Footer.svelte.d.ts +10 -0
- package/dist/frame/Header.svelte +21 -0
- package/dist/frame/Header.svelte.d.ts +9 -0
- package/dist/frame/Logo.svelte +57 -0
- package/dist/frame/Logo.svelte.d.ts +8 -0
- package/dist/frame/Notes.svelte +29 -0
- package/dist/frame/Notes.svelte.d.ts +9 -0
- package/dist/frame/Source.svelte +29 -0
- package/dist/frame/Source.svelte.d.ts +9 -0
- package/dist/frame/Subtitle.svelte +22 -0
- package/dist/frame/Subtitle.svelte.d.ts +9 -0
- package/dist/frame/Title.svelte +31 -0
- package/dist/frame/Title.svelte.d.ts +9 -0
- package/dist/frame/index.d.ts +12 -0
- package/dist/frame/index.js +12 -0
- package/dist/guides/AxisX.svelte +101 -0
- package/dist/guides/AxisX.svelte.d.ts +22 -0
- package/dist/guides/AxisY.svelte +84 -0
- package/dist/guides/AxisY.svelte.d.ts +21 -0
- package/dist/guides/ColorRamp.svelte +93 -0
- package/dist/guides/ColorRamp.svelte.d.ts +25 -0
- package/dist/guides/GridX.svelte +35 -0
- package/dist/guides/GridX.svelte.d.ts +9 -0
- package/dist/guides/GridY.svelte +35 -0
- package/dist/guides/GridY.svelte.d.ts +9 -0
- package/dist/guides/Key.svelte +48 -0
- package/dist/guides/Key.svelte.d.ts +14 -0
- package/dist/guides/Legend.svelte +112 -0
- package/dist/guides/Legend.svelte.d.ts +26 -0
- package/dist/guides/SizeLegend.svelte +62 -0
- package/dist/guides/SizeLegend.svelte.d.ts +15 -0
- package/dist/guides/index.d.ts +9 -0
- package/dist/guides/index.js +9 -0
- package/dist/guides/ticks.d.ts +6 -0
- package/dist/guides/ticks.js +15 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +14 -0
- package/dist/interactive/ButtonGroup.svelte +98 -0
- package/dist/interactive/ButtonGroup.svelte.d.ts +42 -0
- package/dist/interactive/PointerArea.svelte +49 -0
- package/dist/interactive/PointerArea.svelte.d.ts +18 -0
- package/dist/interactive/Scroller.svelte +147 -0
- package/dist/interactive/Scroller.svelte.d.ts +54 -0
- package/dist/interactive/Tooltip.svelte +66 -0
- package/dist/interactive/Tooltip.svelte.d.ts +19 -0
- package/dist/interactive/index.d.ts +4 -0
- package/dist/interactive/index.js +4 -0
- package/dist/interactive/tooltip-position.d.ts +12 -0
- package/dist/interactive/tooltip-position.js +16 -0
- package/dist/locale/definitions.d.ts +145 -0
- package/dist/locale/definitions.js +138 -0
- package/dist/locale/index.d.ts +23 -0
- package/dist/locale/index.js +34 -0
- package/dist/marks/Arcs.svelte +93 -0
- package/dist/marks/Arcs.svelte.d.ts +47 -0
- package/dist/marks/Area.svelte +44 -0
- package/dist/marks/Area.svelte.d.ts +43 -0
- package/dist/marks/Arrows.svelte +93 -0
- package/dist/marks/Arrows.svelte.d.ts +50 -0
- package/dist/marks/Bars.svelte +111 -0
- package/dist/marks/Bars.svelte.d.ts +61 -0
- package/dist/marks/Beeswarm.svelte +84 -0
- package/dist/marks/Beeswarm.svelte.d.ts +42 -0
- package/dist/marks/Dumbbell.svelte +71 -0
- package/dist/marks/Dumbbell.svelte.d.ts +42 -0
- package/dist/marks/GeoPath.svelte +56 -0
- package/dist/marks/GeoPath.svelte.d.ts +42 -0
- package/dist/marks/Hemicycle.svelte +182 -0
- package/dist/marks/Hemicycle.svelte.d.ts +78 -0
- package/dist/marks/Intervals.svelte +128 -0
- package/dist/marks/Intervals.svelte.d.ts +60 -0
- package/dist/marks/Line.svelte +64 -0
- package/dist/marks/Line.svelte.d.ts +43 -0
- package/dist/marks/LineEndLabels.svelte +70 -0
- package/dist/marks/LineEndLabels.svelte.d.ts +41 -0
- package/dist/marks/Points.svelte +70 -0
- package/dist/marks/Points.svelte.d.ts +46 -0
- package/dist/marks/Rects.svelte +92 -0
- package/dist/marks/Rects.svelte.d.ts +47 -0
- package/dist/marks/Rules.svelte +46 -0
- package/dist/marks/Rules.svelte.d.ts +15 -0
- package/dist/marks/Slope.svelte +112 -0
- package/dist/marks/Slope.svelte.d.ts +41 -0
- package/dist/marks/Text.svelte +75 -0
- package/dist/marks/Text.svelte.d.ts +47 -0
- package/dist/marks/Treemap.svelte +89 -0
- package/dist/marks/Treemap.svelte.d.ts +45 -0
- package/dist/marks/Voronoi.svelte +61 -0
- package/dist/marks/Voronoi.svelte.d.ts +39 -0
- package/dist/marks/Waffle.svelte +69 -0
- package/dist/marks/Waffle.svelte.d.ts +41 -0
- package/dist/marks/bar-path.d.ts +5 -0
- package/dist/marks/bar-path.js +15 -0
- package/dist/marks/index.d.ts +20 -0
- package/dist/marks/index.js +20 -0
- package/dist/recipes/ArrowChart.svelte +247 -0
- package/dist/recipes/ArrowChart.svelte.d.ts +73 -0
- package/dist/recipes/BarChart.svelte +122 -0
- package/dist/recipes/BarChart.svelte.d.ts +58 -0
- package/dist/recipes/BarDotChart.svelte +204 -0
- package/dist/recipes/BarDotChart.svelte.d.ts +75 -0
- package/dist/recipes/ColumnChart.svelte +119 -0
- package/dist/recipes/ColumnChart.svelte.d.ts +56 -0
- package/dist/recipes/LineChart.svelte +159 -0
- package/dist/recipes/LineChart.svelte.d.ts +61 -0
- package/dist/recipes/MosaicChart.svelte +256 -0
- package/dist/recipes/MosaicChart.svelte.d.ts +83 -0
- package/dist/recipes/ScatterChart.svelte +167 -0
- package/dist/recipes/ScatterChart.svelte.d.ts +63 -0
- package/dist/recipes/StackedBarChart.svelte +222 -0
- package/dist/recipes/StackedBarChart.svelte.d.ts +83 -0
- package/dist/recipes/index.d.ts +10 -0
- package/dist/recipes/index.js +19 -0
- package/dist/styles/base.d.ts +5 -0
- package/dist/styles/base.js +102 -0
- package/dist/styles/check.d.ts +8 -0
- package/dist/styles/check.js +32 -0
- package/dist/styles/color.d.ts +11 -0
- package/dist/styles/color.js +29 -0
- package/dist/styles/index.d.ts +9 -0
- package/dist/styles/index.js +17 -0
- package/dist/styles/types.d.ts +90 -0
- package/dist/styles/types.js +1 -0
- package/dist/table/Sparkline.svelte +59 -0
- package/dist/table/Sparkline.svelte.d.ts +16 -0
- package/dist/table/Table.svelte +287 -0
- package/dist/table/Table.svelte.d.ts +44 -0
- package/dist/table/csv.d.ts +3 -0
- package/dist/table/csv.js +12 -0
- package/dist/table/index.d.ts +4 -0
- package/dist/table/index.js +3 -0
- package/dist/table/types.d.ts +26 -0
- package/dist/table/types.js +1 -0
- package/dist/transforms/bin.d.ts +15 -0
- package/dist/transforms/bin.js +18 -0
- package/dist/transforms/dodge.d.ts +10 -0
- package/dist/transforms/dodge.js +35 -0
- package/dist/transforms/group.d.ts +14 -0
- package/dist/transforms/group.js +14 -0
- package/dist/transforms/hemicycle.d.ts +30 -0
- package/dist/transforms/hemicycle.js +51 -0
- package/dist/transforms/index.d.ts +8 -0
- package/dist/transforms/index.js +8 -0
- package/dist/transforms/labels.d.ts +6 -0
- package/dist/transforms/labels.js +20 -0
- package/dist/transforms/smooth.d.ts +61 -0
- package/dist/transforms/smooth.js +105 -0
- package/dist/transforms/stack.d.ts +25 -0
- package/dist/transforms/stack.js +61 -0
- package/dist/transforms/treemap.d.ts +21 -0
- package/dist/transforms/treemap.js +31 -0
- package/package.json +152 -3
- package/src/lib/recipes/ArrowChart.svelte +247 -0
- package/src/lib/recipes/BarChart.svelte +122 -0
- package/src/lib/recipes/BarDotChart.svelte +204 -0
- package/src/lib/recipes/ColumnChart.svelte +119 -0
- package/src/lib/recipes/LineChart.svelte +159 -0
- package/src/lib/recipes/MosaicChart.svelte +256 -0
- package/src/lib/recipes/ScatterChart.svelte +167 -0
- package/src/lib/recipes/StackedBarChart.svelte +222 -0
- package/src/lib/recipes/index.ts +20 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
@component
|
|
3
|
+
A tiny inline line chart, e.g. in a table cell. Not a full chart: no axes.
|
|
4
|
+
The last point gets a dot.
|
|
5
|
+
-->
|
|
6
|
+
<script lang="ts">
|
|
7
|
+
import { extent } from 'd3-array';
|
|
8
|
+
import { scaleLinear } from 'd3-scale';
|
|
9
|
+
import { line } from 'd3-shape';
|
|
10
|
+
|
|
11
|
+
interface Props {
|
|
12
|
+
values: number[];
|
|
13
|
+
width?: number;
|
|
14
|
+
height?: number;
|
|
15
|
+
stroke?: string;
|
|
16
|
+
/** Shared y domain, so several sparklines are comparable. */
|
|
17
|
+
domain?: [number, number];
|
|
18
|
+
label?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let { values, width = 80, height = 20, stroke = 'var(--mz-color-text)', domain, label }: Props = $props();
|
|
22
|
+
|
|
23
|
+
const x = $derived(
|
|
24
|
+
scaleLinear()
|
|
25
|
+
.domain([0, Math.max(1, values.length - 1)])
|
|
26
|
+
.range([2, width - 3])
|
|
27
|
+
);
|
|
28
|
+
const y = $derived(
|
|
29
|
+
scaleLinear()
|
|
30
|
+
.domain(domain ?? (extent(values) as [number, number]))
|
|
31
|
+
.range([height - 3, 3])
|
|
32
|
+
);
|
|
33
|
+
const d = $derived(
|
|
34
|
+
line<number>()
|
|
35
|
+
.x((_, i) => x(i))
|
|
36
|
+
.y((v) => y(v))(values) ?? ''
|
|
37
|
+
);
|
|
38
|
+
const last = $derived(values.length - 1);
|
|
39
|
+
</script>
|
|
40
|
+
|
|
41
|
+
<svg
|
|
42
|
+
class="mz-sparkline"
|
|
43
|
+
{width}
|
|
44
|
+
{height}
|
|
45
|
+
viewBox="0 0 {width} {height}"
|
|
46
|
+
role={label ? 'img' : 'presentation'}
|
|
47
|
+
aria-label={label}
|
|
48
|
+
>
|
|
49
|
+
<path {d} fill="none" style:stroke stroke-width="1.5" stroke-linejoin="round" stroke-linecap="round" />
|
|
50
|
+
{#if last >= 0}<circle cx={x(last)} cy={y(values[last])} r="2.25" style:fill={stroke} />{/if}
|
|
51
|
+
</svg>
|
|
52
|
+
|
|
53
|
+
<style>
|
|
54
|
+
.mz-sparkline {
|
|
55
|
+
display: inline-block;
|
|
56
|
+
vertical-align: middle;
|
|
57
|
+
overflow: visible;
|
|
58
|
+
}
|
|
59
|
+
</style>
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
interface Props {
|
|
2
|
+
values: number[];
|
|
3
|
+
width?: number;
|
|
4
|
+
height?: number;
|
|
5
|
+
stroke?: string;
|
|
6
|
+
/** Shared y domain, so several sparklines are comparable. */
|
|
7
|
+
domain?: [number, number];
|
|
8
|
+
label?: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A tiny inline line chart, e.g. in a table cell. Not a full chart: no axes.
|
|
12
|
+
* The last point gets a dot.
|
|
13
|
+
*/
|
|
14
|
+
declare const Sparkline: import("svelte").Component<Props, {}, "">;
|
|
15
|
+
type Sparkline = ReturnType<typeof Sparkline>;
|
|
16
|
+
export default Sparkline;
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
@component
|
|
3
|
+
A data table with sortable columns, locale-aware formatting, optional search
|
|
4
|
+
and a "show all" toggle, plus inline bar, heat and sparkline cells. Use it
|
|
5
|
+
on its own or inside `<Chart>`.
|
|
6
|
+
-->
|
|
7
|
+
<script lang="ts" generics="T">
|
|
8
|
+
import { untrack } from 'svelte';
|
|
9
|
+
import { ascending, descending, max as d3max } from 'd3-array';
|
|
10
|
+
import { useChart } from '../core/context.js';
|
|
11
|
+
import Sparkline from './Sparkline.svelte';
|
|
12
|
+
import type { Column } from './types.js';
|
|
13
|
+
|
|
14
|
+
interface Props {
|
|
15
|
+
data: T[];
|
|
16
|
+
columns: Column<T>[];
|
|
17
|
+
/** Initial sort column key. */
|
|
18
|
+
sortBy?: string;
|
|
19
|
+
sortDirection?: 'ascending' | 'descending';
|
|
20
|
+
/** Show a search box. */
|
|
21
|
+
search?: boolean;
|
|
22
|
+
/** Rows shown before "Show all". */
|
|
23
|
+
maxRows?: number;
|
|
24
|
+
/** Visible caption (also the accessible name). */
|
|
25
|
+
caption?: string;
|
|
26
|
+
stickyHeader?: boolean;
|
|
27
|
+
class?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let {
|
|
31
|
+
data,
|
|
32
|
+
columns,
|
|
33
|
+
sortBy,
|
|
34
|
+
sortDirection = 'descending',
|
|
35
|
+
search = false,
|
|
36
|
+
maxRows,
|
|
37
|
+
caption,
|
|
38
|
+
stickyHeader = false,
|
|
39
|
+
class: className = ''
|
|
40
|
+
}: Props = $props();
|
|
41
|
+
|
|
42
|
+
const chart = useChart();
|
|
43
|
+
const strings = $derived(chart.locale.strings);
|
|
44
|
+
|
|
45
|
+
// Sort state starts from the props, then follows the reader's clicks.
|
|
46
|
+
let sortKey = $state<string | undefined>(untrack(() => sortBy));
|
|
47
|
+
let direction = $state<'ascending' | 'descending'>(untrack(() => sortDirection));
|
|
48
|
+
let query = $state('');
|
|
49
|
+
let expanded = $state(false);
|
|
50
|
+
|
|
51
|
+
const isNumeric = (c: Column<T>) => data.some((d) => typeof c.value(d) === 'number');
|
|
52
|
+
// Bars and sparklines start at the left edge, so their headers do too.
|
|
53
|
+
const align = (c: Column<T>) =>
|
|
54
|
+
c.align ?? (isNumeric(c) && c.type !== 'sparkline' && c.type !== 'bar' ? 'right' : 'left');
|
|
55
|
+
const maxima = $derived(
|
|
56
|
+
Object.fromEntries(columns.map((c) => [c.key, c.max ?? (d3max(data, (d) => Math.abs(Number(c.value(d)))) || 1)]))
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
const filtered = $derived.by(() => {
|
|
60
|
+
const q = query.trim().toLocaleLowerCase(chart.locale.lang);
|
|
61
|
+
if (!q) return data;
|
|
62
|
+
const searchable = columns.filter((c) => c.searchable ?? !isNumeric(c));
|
|
63
|
+
return data.filter((d) =>
|
|
64
|
+
searchable.some((c) =>
|
|
65
|
+
String(c.value(d) ?? '')
|
|
66
|
+
.toLocaleLowerCase(chart.locale.lang)
|
|
67
|
+
.includes(q)
|
|
68
|
+
)
|
|
69
|
+
);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const sorted = $derived.by(() => {
|
|
73
|
+
const column = columns.find((c) => c.key === sortKey);
|
|
74
|
+
if (!column) return filtered;
|
|
75
|
+
const compare = direction === 'ascending' ? ascending : descending;
|
|
76
|
+
return filtered.toSorted((a, b) => {
|
|
77
|
+
const va = column.value(a);
|
|
78
|
+
const vb = column.value(b);
|
|
79
|
+
if (typeof va === 'string' && typeof vb === 'string')
|
|
80
|
+
return (direction === 'ascending' ? 1 : -1) * va.localeCompare(vb, chart.locale.lang);
|
|
81
|
+
return compare(va as number, vb as number);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const visible = $derived(maxRows && !expanded ? sorted.slice(0, maxRows) : sorted);
|
|
86
|
+
|
|
87
|
+
function toggleSort(column: Column<T>) {
|
|
88
|
+
if (sortKey === column.key) direction = direction === 'ascending' ? 'descending' : 'ascending';
|
|
89
|
+
else {
|
|
90
|
+
sortKey = column.key;
|
|
91
|
+
direction = isNumeric(column) ? 'descending' : 'ascending';
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function text(column: Column<T>, d: T): string {
|
|
96
|
+
const v = column.value(d);
|
|
97
|
+
if (column.format) return column.format(v, d);
|
|
98
|
+
if (typeof v === 'number') return chart.locale.number(v);
|
|
99
|
+
if (v instanceof Date) return chart.locale.timeFormat('%e %b %Y')(v);
|
|
100
|
+
return v == null ? '' : String(v);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function contrast(color: string): string {
|
|
104
|
+
// Near-black or white text on a heat cell: whichever has the higher WCAG contrast ratio.
|
|
105
|
+
const m = color.match(/^#([0-9a-f]{6})$/i);
|
|
106
|
+
if (!m) return 'inherit';
|
|
107
|
+
const n = parseInt(m[1], 16);
|
|
108
|
+
const [r, g, b] = [(n >> 16) & 255, (n >> 8) & 255, n & 255].map((c) => {
|
|
109
|
+
const s = c / 255;
|
|
110
|
+
return s <= 0.03928 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
|
|
111
|
+
});
|
|
112
|
+
const luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
113
|
+
const dark = 0.0056; // luminance of #111111
|
|
114
|
+
return (luminance + 0.05) / (dark + 0.05) >= 1.05 / (luminance + 0.05) ? '#111111' : '#ffffff';
|
|
115
|
+
}
|
|
116
|
+
</script>
|
|
117
|
+
|
|
118
|
+
<div class="mz-table-wrap {className}">
|
|
119
|
+
{#if search && chart.medium !== 'print'}
|
|
120
|
+
<input
|
|
121
|
+
class="mz-table-search"
|
|
122
|
+
type="search"
|
|
123
|
+
placeholder={strings.search}
|
|
124
|
+
aria-label={strings.search}
|
|
125
|
+
bind:value={query}
|
|
126
|
+
/>
|
|
127
|
+
{/if}
|
|
128
|
+
<div class="mz-table-scroll">
|
|
129
|
+
<table class="mz-table" class:sticky={stickyHeader}>
|
|
130
|
+
{#if caption}<caption>{caption}</caption>{/if}
|
|
131
|
+
<thead>
|
|
132
|
+
<tr>
|
|
133
|
+
{#each columns as column (column.key)}
|
|
134
|
+
<th
|
|
135
|
+
scope="col"
|
|
136
|
+
style:width={column.width}
|
|
137
|
+
style:text-align={align(column)}
|
|
138
|
+
aria-sort={sortKey === column.key ? direction : undefined}
|
|
139
|
+
>
|
|
140
|
+
{#if column.sortable !== false && chart.medium !== 'print'}
|
|
141
|
+
<button type="button" onclick={() => toggleSort(column)}>
|
|
142
|
+
{column.label}<span class="mz-sort" aria-hidden="true"
|
|
143
|
+
>{sortKey === column.key ? (direction === 'ascending' ? '▲' : '▼') : ''}</span
|
|
144
|
+
>
|
|
145
|
+
</button>
|
|
146
|
+
{:else}
|
|
147
|
+
{column.label}
|
|
148
|
+
{/if}
|
|
149
|
+
</th>
|
|
150
|
+
{/each}
|
|
151
|
+
</tr>
|
|
152
|
+
</thead>
|
|
153
|
+
<tbody>
|
|
154
|
+
{#each visible as d}
|
|
155
|
+
<tr>
|
|
156
|
+
{#each columns as column, ci (column.key)}
|
|
157
|
+
{@const v = column.value(d)}
|
|
158
|
+
{#if column.type === 'heat' && column.color}
|
|
159
|
+
{@const bg = column.color(v, d)}
|
|
160
|
+
<td style:text-align={align(column)} style:background={bg} style:color={contrast(bg)}
|
|
161
|
+
>{text(column, d)}</td
|
|
162
|
+
>
|
|
163
|
+
{:else if column.type === 'bar'}
|
|
164
|
+
<td class="mz-bar-cell">
|
|
165
|
+
<span
|
|
166
|
+
class="mz-cell-bar"
|
|
167
|
+
style:width="{(Math.abs(Number(v)) / maxima[column.key]) * 100}%"
|
|
168
|
+
style:background={column.color?.(v, d) ?? chart.theme.palettes.categorical[0]}
|
|
169
|
+
></span>
|
|
170
|
+
<span class="mz-cell-value">{text(column, d)}</span>
|
|
171
|
+
</td>
|
|
172
|
+
{:else if column.type === 'sparkline' && column.series}
|
|
173
|
+
<td><Sparkline values={column.series(d)} /></td>
|
|
174
|
+
{:else if ci === 0}
|
|
175
|
+
<th scope="row" style:text-align={align(column)}>{text(column, d)}</th>
|
|
176
|
+
{:else}
|
|
177
|
+
<td style:text-align={align(column)}>{text(column, d)}</td>
|
|
178
|
+
{/if}
|
|
179
|
+
{/each}
|
|
180
|
+
</tr>
|
|
181
|
+
{/each}
|
|
182
|
+
{#if visible.length === 0}
|
|
183
|
+
<tr><td colspan={columns.length} class="mz-empty">{strings.noData}</td></tr>
|
|
184
|
+
{/if}
|
|
185
|
+
</tbody>
|
|
186
|
+
</table>
|
|
187
|
+
</div>
|
|
188
|
+
{#if maxRows && sorted.length > maxRows && chart.medium !== 'print'}
|
|
189
|
+
<button class="mz-table-more" type="button" onclick={() => (expanded = !expanded)}>
|
|
190
|
+
{expanded ? strings.showFewer : strings.showAll(sorted.length)}
|
|
191
|
+
</button>
|
|
192
|
+
{/if}
|
|
193
|
+
</div>
|
|
194
|
+
|
|
195
|
+
<style>
|
|
196
|
+
.mz-table-wrap {
|
|
197
|
+
font-family: var(--mz-font-body, system-ui, sans-serif);
|
|
198
|
+
font-size: var(--mz-size-small, 0.8125rem);
|
|
199
|
+
color: var(--mz-color-text, #1a1a1a);
|
|
200
|
+
}
|
|
201
|
+
.mz-table-scroll {
|
|
202
|
+
overflow-x: auto;
|
|
203
|
+
}
|
|
204
|
+
.mz-table {
|
|
205
|
+
width: 100%;
|
|
206
|
+
border-collapse: collapse;
|
|
207
|
+
font-variant-numeric: tabular-nums;
|
|
208
|
+
}
|
|
209
|
+
caption {
|
|
210
|
+
text-align: left;
|
|
211
|
+
font-weight: var(--mz-weight-strong, 700);
|
|
212
|
+
padding-bottom: 0.375rem;
|
|
213
|
+
}
|
|
214
|
+
th,
|
|
215
|
+
td {
|
|
216
|
+
padding: 0.375rem 0.5rem;
|
|
217
|
+
border-bottom: 1px solid var(--mz-color-grid, #e4e4e4);
|
|
218
|
+
vertical-align: middle;
|
|
219
|
+
}
|
|
220
|
+
thead th {
|
|
221
|
+
font-family: var(--mz-font-label, inherit);
|
|
222
|
+
font-weight: var(--mz-weight-label, 700);
|
|
223
|
+
text-transform: var(--mz-label-transform, none);
|
|
224
|
+
letter-spacing: var(--mz-label-tracking, normal);
|
|
225
|
+
color: var(--mz-color-muted, #5c5c5c);
|
|
226
|
+
border-bottom: 1px solid var(--mz-color-text, #1a1a1a);
|
|
227
|
+
white-space: nowrap;
|
|
228
|
+
}
|
|
229
|
+
tbody th {
|
|
230
|
+
font-weight: var(--mz-weight-strong, 600);
|
|
231
|
+
}
|
|
232
|
+
.sticky thead th {
|
|
233
|
+
position: sticky;
|
|
234
|
+
top: 0;
|
|
235
|
+
background: var(--mz-color-bg, #fff);
|
|
236
|
+
}
|
|
237
|
+
thead button {
|
|
238
|
+
all: unset;
|
|
239
|
+
cursor: pointer;
|
|
240
|
+
}
|
|
241
|
+
thead button:focus-visible {
|
|
242
|
+
outline: 2px solid var(--mz-color-accent, #2a78d6);
|
|
243
|
+
}
|
|
244
|
+
.mz-sort {
|
|
245
|
+
display: inline-block;
|
|
246
|
+
width: 1em;
|
|
247
|
+
font-size: 0.7em;
|
|
248
|
+
text-align: center;
|
|
249
|
+
}
|
|
250
|
+
.mz-bar-cell {
|
|
251
|
+
position: relative;
|
|
252
|
+
min-width: 6rem;
|
|
253
|
+
}
|
|
254
|
+
.mz-cell-bar {
|
|
255
|
+
position: absolute;
|
|
256
|
+
left: 0.5rem;
|
|
257
|
+
top: 25%;
|
|
258
|
+
height: 50%;
|
|
259
|
+
max-width: calc(100% - 1rem);
|
|
260
|
+
border-radius: 0 min(var(--mz-radius, 2px), 2px) min(var(--mz-radius, 2px), 2px) 0;
|
|
261
|
+
opacity: 0.35;
|
|
262
|
+
}
|
|
263
|
+
.mz-cell-value {
|
|
264
|
+
position: relative;
|
|
265
|
+
}
|
|
266
|
+
.mz-empty {
|
|
267
|
+
text-align: center;
|
|
268
|
+
color: var(--mz-color-muted, #5c5c5c);
|
|
269
|
+
}
|
|
270
|
+
.mz-table-search,
|
|
271
|
+
.mz-table-more {
|
|
272
|
+
margin: 0.5rem 0;
|
|
273
|
+
padding: 0.35rem 0.6rem;
|
|
274
|
+
border: 1px solid var(--mz-color-border, #e4e4e4);
|
|
275
|
+
border-radius: var(--mz-radius, 4px);
|
|
276
|
+
background: var(--mz-color-bg, #fff);
|
|
277
|
+
color: inherit;
|
|
278
|
+
font: inherit;
|
|
279
|
+
}
|
|
280
|
+
.mz-table-more {
|
|
281
|
+
cursor: pointer;
|
|
282
|
+
font-family: var(--mz-font-label, inherit);
|
|
283
|
+
font-weight: var(--mz-weight-label, 600);
|
|
284
|
+
text-transform: var(--mz-control-transform, none);
|
|
285
|
+
letter-spacing: var(--mz-control-tracking, normal);
|
|
286
|
+
}
|
|
287
|
+
</style>
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { Column } from './types.js';
|
|
2
|
+
declare function $$render<T>(): {
|
|
3
|
+
props: {
|
|
4
|
+
data: T[];
|
|
5
|
+
columns: Column<T>[];
|
|
6
|
+
/** Initial sort column key. */
|
|
7
|
+
sortBy?: string;
|
|
8
|
+
sortDirection?: "ascending" | "descending";
|
|
9
|
+
/** Show a search box. */
|
|
10
|
+
search?: boolean;
|
|
11
|
+
/** Rows shown before "Show all". */
|
|
12
|
+
maxRows?: number;
|
|
13
|
+
/** Visible caption (also the accessible name). */
|
|
14
|
+
caption?: string;
|
|
15
|
+
stickyHeader?: boolean;
|
|
16
|
+
class?: string;
|
|
17
|
+
};
|
|
18
|
+
exports: {};
|
|
19
|
+
bindings: "";
|
|
20
|
+
slots: {};
|
|
21
|
+
events: {};
|
|
22
|
+
};
|
|
23
|
+
declare class __sveltets_Render<T> {
|
|
24
|
+
props(): ReturnType<typeof $$render<T>>['props'];
|
|
25
|
+
events(): ReturnType<typeof $$render<T>>['events'];
|
|
26
|
+
slots(): ReturnType<typeof $$render<T>>['slots'];
|
|
27
|
+
bindings(): "";
|
|
28
|
+
exports(): {};
|
|
29
|
+
}
|
|
30
|
+
interface $$IsomorphicComponent {
|
|
31
|
+
new <T>(options: import('svelte').ComponentConstructorOptions<ReturnType<__sveltets_Render<T>['props']>>): import('svelte').SvelteComponent<ReturnType<__sveltets_Render<T>['props']>, ReturnType<__sveltets_Render<T>['events']>, ReturnType<__sveltets_Render<T>['slots']>> & {
|
|
32
|
+
$$bindings?: ReturnType<__sveltets_Render<T>['bindings']>;
|
|
33
|
+
} & ReturnType<__sveltets_Render<T>['exports']>;
|
|
34
|
+
<T>(internal: unknown, props: ReturnType<__sveltets_Render<T>['props']> & {}): ReturnType<__sveltets_Render<T>['exports']>;
|
|
35
|
+
z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A data table with sortable columns, locale-aware formatting, optional search
|
|
39
|
+
* and a "show all" toggle, plus inline bar, heat and sparkline cells. Use it
|
|
40
|
+
* on its own or inside `<Chart>`.
|
|
41
|
+
*/
|
|
42
|
+
declare const Table: $$IsomorphicComponent;
|
|
43
|
+
type Table<T> = InstanceType<typeof Table<T>>;
|
|
44
|
+
export default Table;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
function escape(value) {
|
|
2
|
+
if (value == null)
|
|
3
|
+
return '';
|
|
4
|
+
const text = value instanceof Date ? value.toISOString().slice(0, 10) : String(value);
|
|
5
|
+
return /[",\n\r]/.test(text) ? `"${text.replace(/"/g, '""')}"` : text;
|
|
6
|
+
}
|
|
7
|
+
/** CSV text from rows and columns (raw values, not formatted). */
|
|
8
|
+
export function toCSV(rows, columns) {
|
|
9
|
+
const header = columns.map((c) => escape(c.label)).join(',');
|
|
10
|
+
const body = rows.map((d) => columns.map((c) => escape(c.value(d))).join(','));
|
|
11
|
+
return [header, ...body].join('\n') + '\n';
|
|
12
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export interface Column<T> {
|
|
2
|
+
/** Unique id for the column. */
|
|
3
|
+
key: string;
|
|
4
|
+
label: string;
|
|
5
|
+
value: (d: T) => unknown;
|
|
6
|
+
/** Text shown in the cell; defaults to locale number formatting for numbers. */
|
|
7
|
+
format?: (value: any, d: T) => string;
|
|
8
|
+
/** Defaults to right for numbers, left otherwise. */
|
|
9
|
+
align?: 'left' | 'right' | 'center';
|
|
10
|
+
sortable?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Cell display: plain text, an inline bar (numbers), a heat cell coloured by
|
|
13
|
+
* `color`, or a sparkline from `series`.
|
|
14
|
+
*/
|
|
15
|
+
type?: 'text' | 'bar' | 'heat' | 'sparkline';
|
|
16
|
+
/** For `bar`: the value that fills the cell; defaults to the column maximum. */
|
|
17
|
+
max?: number;
|
|
18
|
+
/** For `heat` (and optionally `bar`): value → CSS colour. */
|
|
19
|
+
color?: (value: any, d: T) => string;
|
|
20
|
+
/** For `sparkline`: the values to plot. */
|
|
21
|
+
series?: (d: T) => number[];
|
|
22
|
+
/** CSS width, e.g. "30%". */
|
|
23
|
+
width?: string;
|
|
24
|
+
/** Include in the search filter (text columns by default). */
|
|
25
|
+
searchable?: boolean;
|
|
26
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface Bin<T> {
|
|
2
|
+
x0: number;
|
|
3
|
+
x1: number;
|
|
4
|
+
count: number;
|
|
5
|
+
values: T[];
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Histogram bins. `thresholds` is a target bin count, or explicit bin edges
|
|
9
|
+
* including both ends (`[0, 5, 10]` → bins 0–5 and 5–10; the last bin includes its upper edge).
|
|
10
|
+
* Returns `{ x0, x1, count, values }` per bin.
|
|
11
|
+
*/
|
|
12
|
+
export declare function bin<T>(data: T[], value: (d: T) => number, options?: {
|
|
13
|
+
thresholds?: number | number[];
|
|
14
|
+
domain?: [number, number];
|
|
15
|
+
}): Bin<T>[];
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { bin as d3bin, extent } from 'd3-array';
|
|
2
|
+
/**
|
|
3
|
+
* Histogram bins. `thresholds` is a target bin count, or explicit bin edges
|
|
4
|
+
* including both ends (`[0, 5, 10]` → bins 0–5 and 5–10; the last bin includes its upper edge).
|
|
5
|
+
* Returns `{ x0, x1, count, values }` per bin.
|
|
6
|
+
*/
|
|
7
|
+
export function bin(data, value, options = {}) {
|
|
8
|
+
const generator = d3bin().value(value);
|
|
9
|
+
const edges = Array.isArray(options.thresholds) ? options.thresholds : undefined;
|
|
10
|
+
const domain = options.domain ?? (edges ? [edges[0], edges[edges.length - 1]] : extent(data, value));
|
|
11
|
+
if (domain[0] !== undefined)
|
|
12
|
+
generator.domain(domain);
|
|
13
|
+
if (edges)
|
|
14
|
+
generator.thresholds(edges.slice(1, -1));
|
|
15
|
+
else if (typeof options.thresholds === 'number')
|
|
16
|
+
generator.thresholds(options.thresholds);
|
|
17
|
+
return generator(data).map((b) => ({ x0: b.x0 ?? 0, x1: b.x1 ?? 0, count: b.length, values: [...b] }));
|
|
18
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Beeswarm layout: given pixel positions along one axis, returns offsets on
|
|
3
|
+
* the other axis so circles of `radius` do not overlap. Offsets alternate
|
|
4
|
+
* either side of 0 (or only positive with `side: 'positive'`).
|
|
5
|
+
* Returns offsets in the original order.
|
|
6
|
+
*/
|
|
7
|
+
export declare function dodge(positions: number[], radius: number, options?: {
|
|
8
|
+
padding?: number;
|
|
9
|
+
side?: 'both' | 'positive';
|
|
10
|
+
}): number[];
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Beeswarm layout: given pixel positions along one axis, returns offsets on
|
|
3
|
+
* the other axis so circles of `radius` do not overlap. Offsets alternate
|
|
4
|
+
* either side of 0 (or only positive with `side: 'positive'`).
|
|
5
|
+
* Returns offsets in the original order.
|
|
6
|
+
*/
|
|
7
|
+
export function dodge(positions, radius, options = {}) {
|
|
8
|
+
const { padding = 1, side = 'both' } = options;
|
|
9
|
+
const diameter = radius * 2 + padding;
|
|
10
|
+
const d2 = diameter * diameter;
|
|
11
|
+
const order = positions.map((p, i) => ({ p, i })).sort((a, b) => a.p - b.p);
|
|
12
|
+
const placed = [];
|
|
13
|
+
const result = new Array(positions.length);
|
|
14
|
+
for (const { p, i } of order) {
|
|
15
|
+
// Neighbours close enough on the main axis to collide.
|
|
16
|
+
const near = placed.filter((q) => Math.abs(q.p - p) < diameter);
|
|
17
|
+
let y = 0;
|
|
18
|
+
if (near.some((q) => (q.p - p) ** 2 + (q.y - y) ** 2 < d2)) {
|
|
19
|
+
// Candidate offsets: just touching each neighbour, smallest |y| first.
|
|
20
|
+
const candidates = [];
|
|
21
|
+
for (const q of near) {
|
|
22
|
+
const dy = Math.sqrt(Math.max(0, d2 - (q.p - p) ** 2));
|
|
23
|
+
candidates.push(q.y + dy);
|
|
24
|
+
if (side === 'both')
|
|
25
|
+
candidates.push(q.y - dy);
|
|
26
|
+
}
|
|
27
|
+
candidates.sort((a, b) => Math.abs(a) - Math.abs(b));
|
|
28
|
+
y =
|
|
29
|
+
candidates.find((c) => (side === 'both' || c >= 0) && near.every((q) => (q.p - p) ** 2 + (q.y - c) ** 2 >= d2 - 1e-6)) ?? 0;
|
|
30
|
+
}
|
|
31
|
+
placed.push({ p, y });
|
|
32
|
+
result[i] = y;
|
|
33
|
+
}
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Group rows by a key: `[{ key, values }]`, in order of first appearance. */
|
|
2
|
+
export declare function groupBy<T, K>(data: T[], key: (d: T) => K): {
|
|
3
|
+
key: K;
|
|
4
|
+
values: T[];
|
|
5
|
+
}[];
|
|
6
|
+
/** Group and reduce: `rollup(rows, d => d.region, v => sum(v, d => d.value))` → `[{ key, value }]`. */
|
|
7
|
+
export declare function rollup<T, K, V>(data: T[], key: (d: T) => K, reduce: (values: T[]) => V): {
|
|
8
|
+
key: K;
|
|
9
|
+
value: V;
|
|
10
|
+
}[];
|
|
11
|
+
/** Rebase a series so the value at `baseIndex` equals `base` (default 100). */
|
|
12
|
+
export declare function indexTo<T>(data: T[], value: (d: T) => number, baseIndex?: number, base?: number): (T & {
|
|
13
|
+
index: number;
|
|
14
|
+
})[];
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { group as d3group, rollups } from 'd3-array';
|
|
2
|
+
/** Group rows by a key: `[{ key, values }]`, in order of first appearance. */
|
|
3
|
+
export function groupBy(data, key) {
|
|
4
|
+
return Array.from(d3group(data, key), ([k, values]) => ({ key: k, values }));
|
|
5
|
+
}
|
|
6
|
+
/** Group and reduce: `rollup(rows, d => d.region, v => sum(v, d => d.value))` → `[{ key, value }]`. */
|
|
7
|
+
export function rollup(data, key, reduce) {
|
|
8
|
+
return rollups(data, reduce, key).map(([k, value]) => ({ key: k, value }));
|
|
9
|
+
}
|
|
10
|
+
/** Rebase a series so the value at `baseIndex` equals `base` (default 100). */
|
|
11
|
+
export function indexTo(data, value, baseIndex = 0, base = 100) {
|
|
12
|
+
const start = value(data[baseIndex]);
|
|
13
|
+
return data.map((d) => ({ ...d, index: start ? (value(d) / start) * base : NaN }));
|
|
14
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** One seat in a hemicycle, in unit coordinates: outer radius 1, centre (0, 0), y up. */
|
|
2
|
+
export interface Seat {
|
|
3
|
+
/** Position in seat order: 0 is the far left, the last seat is the far right. */
|
|
4
|
+
index: number;
|
|
5
|
+
/** Row, 0 = innermost. */
|
|
6
|
+
row: number;
|
|
7
|
+
/** Angle in radians, π (left) to 0 (right). */
|
|
8
|
+
angle: number;
|
|
9
|
+
x: number;
|
|
10
|
+
y: number;
|
|
11
|
+
}
|
|
12
|
+
export interface HemicycleOptions {
|
|
13
|
+
/** Number of rows. Defaults to the fewest rows that fit the seats. */
|
|
14
|
+
rows?: number;
|
|
15
|
+
/** Inner radius as a fraction of the outer radius (0–1). */
|
|
16
|
+
innerRadius?: number;
|
|
17
|
+
}
|
|
18
|
+
export interface HemicycleLayout {
|
|
19
|
+
seats: Seat[];
|
|
20
|
+
rows: number;
|
|
21
|
+
/** Distance between rows, in unit coordinates: the largest seat diameter that never overlaps. */
|
|
22
|
+
spacing: number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Seat positions for a parliament chart. Rows are spread evenly between the
|
|
26
|
+
* inner and outer radius, each row gets seats in proportion to its length,
|
|
27
|
+
* and seats are numbered by angle from left to right, so filling parties in
|
|
28
|
+
* order gives each party a wedge.
|
|
29
|
+
*/
|
|
30
|
+
export declare function hemicycleLayout(total: number, { rows, innerRadius }?: HemicycleOptions): HemicycleLayout;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Seat positions for a parliament chart. Rows are spread evenly between the
|
|
3
|
+
* inner and outer radius, each row gets seats in proportion to its length,
|
|
4
|
+
* and seats are numbered by angle from left to right, so filling parties in
|
|
5
|
+
* order gives each party a wedge.
|
|
6
|
+
*/
|
|
7
|
+
export function hemicycleLayout(total, { rows, innerRadius = 0.4 } = {}) {
|
|
8
|
+
const n = Math.max(0, Math.floor(total));
|
|
9
|
+
if (n === 0)
|
|
10
|
+
return { seats: [], rows: 0, spacing: 0 };
|
|
11
|
+
const r0 = Math.min(0.9, Math.max(0, innerRadius));
|
|
12
|
+
const geometry = (count) => {
|
|
13
|
+
const spacing = (1 - r0) / count;
|
|
14
|
+
const radii = Array.from({ length: count }, (_, i) => r0 + spacing * (i + 0.5));
|
|
15
|
+
const capacity = radii.map((r) => Math.floor((Math.PI * r) / spacing) + 1);
|
|
16
|
+
return { spacing, radii, capacity };
|
|
17
|
+
};
|
|
18
|
+
let count = rows && rows > 0 ? Math.floor(rows) : 1;
|
|
19
|
+
if (!rows)
|
|
20
|
+
while (geometry(count).capacity.reduce((a, b) => a + b, 0) < n)
|
|
21
|
+
count++;
|
|
22
|
+
const { spacing, radii, capacity } = geometry(count);
|
|
23
|
+
// Seats per row in proportion to row length (largest remainder), never above capacity
|
|
24
|
+
// unless the caller forced too few rows.
|
|
25
|
+
const length = radii.reduce((a, b) => a + b, 0);
|
|
26
|
+
const exact = radii.map((r) => (n * r) / length);
|
|
27
|
+
const perRow = exact.map(Math.floor);
|
|
28
|
+
const order = exact.map((e, i) => ({ i, rest: e - Math.floor(e) })).sort((a, b) => b.rest - a.rest || b.i - a.i);
|
|
29
|
+
for (let k = 0, left = n - perRow.reduce((a, b) => a + b, 0); left > 0; k++, left--)
|
|
30
|
+
perRow[order[k % count].i]++;
|
|
31
|
+
if (!rows) {
|
|
32
|
+
// Move overflow from full rows outwards (rare, with very few seats).
|
|
33
|
+
for (let i = 0; i < count - 1; i++) {
|
|
34
|
+
const extra = perRow[i] - capacity[i];
|
|
35
|
+
if (extra > 0) {
|
|
36
|
+
perRow[i] -= extra;
|
|
37
|
+
perRow[i + 1] += extra;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const seats = [];
|
|
42
|
+
radii.forEach((r, row) => {
|
|
43
|
+
const m = perRow[row];
|
|
44
|
+
for (let j = 0; j < m; j++) {
|
|
45
|
+
const angle = m === 1 ? Math.PI / 2 : Math.PI * (1 - j / (m - 1));
|
|
46
|
+
seats.push({ row, angle, x: r * Math.cos(angle), y: r * Math.sin(angle) });
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
seats.sort((a, b) => b.angle - a.angle || a.row - b.row);
|
|
50
|
+
return { seats: seats.map((s, index) => ({ index, ...s })), rows: count, spacing };
|
|
51
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { avoidOverlap } from './labels.js';
|
|
2
|
+
export { stack, type StackedRow, type StackOptions } from './stack.js';
|
|
3
|
+
export { bin, type Bin } from './bin.js';
|
|
4
|
+
export { groupBy, indexTo, rollup } from './group.js';
|
|
5
|
+
export { dodge } from './dodge.js';
|
|
6
|
+
export { treemapLayout, type TreemapCell } from './treemap.js';
|
|
7
|
+
export { hemicycleLayout, type HemicycleLayout, type HemicycleOptions, type Seat } from './hemicycle.js';
|
|
8
|
+
export { kernelSmooth, loess, type Kernel, type KernelSmoothOptions, type LoessOptions, type Smoothed } from './smooth.js';
|