@adecore/charts 0.62.1 → 0.63.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/dist/Root.js +371 -351
- package/dist/SeriesLegend.d.ts +1 -9
- package/dist/annotations.js +111 -266
- package/dist/axes.d.ts +1 -1
- package/dist/axes.js +121 -223
- package/dist/band.d.ts +2 -7
- package/dist/band.js +1 -1
- package/dist/context.d.ts +5 -2
- package/dist/index.d.ts +2 -1
- package/dist/legend.d.ts +9 -1
- package/dist/overlays.js +0 -2
- package/dist/plane.d.ts +25 -0
- package/dist/plane.js +55 -0
- package/dist/scale.d.ts +2 -1
- package/dist/series.js +3 -3
- package/dist/spark.d.ts +2 -1
- package/dist/store.d.ts +6 -5
- package/dist/time-format.d.ts +2 -1
- package/dist/timeline-layout.d.ts +3 -2
- package/package.json +3 -3
- package/src/Heatmap.tsx +0 -1
- package/src/Root.tsx +7 -5
- package/src/SeriesLegend.tsx +1 -16
- package/src/annotations.tsx +10 -38
- package/src/axes.tsx +37 -82
- package/src/band.ts +3 -3
- package/src/context.ts +5 -4
- package/src/index.ts +2 -1
- package/src/legend.ts +14 -1
- package/src/overlays.tsx +0 -2
- package/src/plane.ts +76 -0
- package/src/scale.ts +1 -1
- package/src/scatter.tsx +0 -1
- package/src/series.tsx +3 -3
- package/src/spark.ts +1 -1
- package/src/store.ts +5 -6
- package/src/time-format.ts +1 -1
- package/src/timeline-layout.ts +2 -2
package/dist/store.d.ts
CHANGED
|
@@ -31,23 +31,23 @@ export interface BandEntry {
|
|
|
31
31
|
readonly detail: string | null;
|
|
32
32
|
readonly color: string;
|
|
33
33
|
}
|
|
34
|
-
|
|
34
|
+
interface AxisSizes {
|
|
35
35
|
readonly end: number | null;
|
|
36
36
|
readonly start: number | null;
|
|
37
37
|
}
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
type YAxisSide = 'left' | 'right';
|
|
39
|
+
interface YAxisEntry {
|
|
40
40
|
readonly width: number | null;
|
|
41
41
|
readonly ticks: number | null;
|
|
42
42
|
readonly top: number | null;
|
|
43
43
|
readonly format: ((value: number) => string) | undefined;
|
|
44
44
|
}
|
|
45
|
-
|
|
45
|
+
interface XAxisEntry {
|
|
46
46
|
readonly room: number;
|
|
47
47
|
readonly widest: number | null;
|
|
48
48
|
readonly format: ((x: ChartX) => string) | undefined;
|
|
49
49
|
}
|
|
50
|
-
|
|
50
|
+
interface ChartFormats {
|
|
51
51
|
readonly xTitle?: string;
|
|
52
52
|
}
|
|
53
53
|
export interface ChartStore {
|
|
@@ -70,3 +70,4 @@ export interface ChartStore {
|
|
|
70
70
|
setFormat<Axis extends keyof ChartFormats>(axis: Axis, format: ChartFormats[Axis]): void;
|
|
71
71
|
}
|
|
72
72
|
export declare function createChartStore(): ChartStore;
|
|
73
|
+
export {};
|
package/dist/time-format.d.ts
CHANGED
|
@@ -31,7 +31,7 @@ export declare function timelineExtent(spans: readonly TimelineSpan[], fixedEnd?
|
|
|
31
31
|
readonly total: number;
|
|
32
32
|
};
|
|
33
33
|
export declare function durationTicks(total: number, count?: number): number[];
|
|
34
|
-
|
|
34
|
+
interface TimelineBar {
|
|
35
35
|
readonly left: number;
|
|
36
36
|
readonly width: number;
|
|
37
37
|
readonly label: 'inside' | 'before' | 'after';
|
|
@@ -39,7 +39,7 @@ export interface TimelineBar {
|
|
|
39
39
|
export declare function percentOf(at: number, origin: number, total: number): number;
|
|
40
40
|
export declare function barOf(span: TimelineSpan, origin: number, total: number): TimelineBar;
|
|
41
41
|
export declare function kindTimes(rows: readonly TimelineTreeRow<TimelineSpan>[]): Map<string, number>;
|
|
42
|
-
|
|
42
|
+
type TimelineMove = {
|
|
43
43
|
readonly focus: number;
|
|
44
44
|
} | {
|
|
45
45
|
readonly expand: string;
|
|
@@ -49,3 +49,4 @@ export type TimelineMove = {
|
|
|
49
49
|
readonly select: string;
|
|
50
50
|
};
|
|
51
51
|
export declare function timelineMove(key: string, rows: readonly TimelineTreeRow<TimelineSpan>[], current: number, expanded: ReadonlySet<string>): TimelineMove | undefined;
|
|
52
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adecore/charts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.63.0",
|
|
4
4
|
"description": "Series charts, scatters, heatmaps, trace timelines and sparklines in the theme of @adecore/ui, readable by keyboard and screen reader.",
|
|
5
5
|
"license": "FSL-1.1-MIT",
|
|
6
6
|
"author": "Bas Milius",
|
|
@@ -43,14 +43,14 @@
|
|
|
43
43
|
"lucide-react": "^1.65.0"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
-
"@adecore/ui": "^0.
|
|
46
|
+
"@adecore/ui": "^0.63.0",
|
|
47
47
|
"i18next": "^26.4.2",
|
|
48
48
|
"react": "^19.3.0",
|
|
49
49
|
"react-dom": "^19.3.0",
|
|
50
50
|
"react-i18next": "^17.0.15"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
|
-
"@adecore/ui": "0.
|
|
53
|
+
"@adecore/ui": "0.63.0",
|
|
54
54
|
"@types/bun": "^1.4.4",
|
|
55
55
|
"@types/react": "^19.3.0",
|
|
56
56
|
"@types/react-dom": "^19.3.0",
|
package/src/Heatmap.tsx
CHANGED
|
@@ -12,7 +12,6 @@ import { ValuesBox } from './ValuesBox.tsx';
|
|
|
12
12
|
|
|
13
13
|
export type HeatmapCell = HeatCell;
|
|
14
14
|
|
|
15
|
-
/* A row or a column of a heatmap. */
|
|
16
15
|
export interface HeatmapAxisItem {
|
|
17
16
|
readonly key: string | number;
|
|
18
17
|
/* What the tooltip, the live region and the table call it, such as "Monday" or "09:00". */
|
package/src/Root.tsx
CHANGED
|
@@ -23,6 +23,7 @@ import { seriesColor } from './color.ts';
|
|
|
23
23
|
import { ChartContext, GroupContext, type ChartContextValue } from './context.ts';
|
|
24
24
|
import { containingIndex, keyIndexOf, moveIndex, nearestIndex, snapIndex } from './cursor.ts';
|
|
25
25
|
import { useAlignedAxes } from './group-axes.ts';
|
|
26
|
+
import { COLUMNS, ROWS } from './plane.ts';
|
|
26
27
|
import { movePoint, nearestPoint, type PlacedPoint } from './points.ts';
|
|
27
28
|
import { bucketStepOf, continuousXScale, extentOf, pointXScale, stackSeries, tickCount, yScaleOf } from './scale.ts';
|
|
28
29
|
import { createChartStore } from './store.ts';
|
|
@@ -199,11 +200,11 @@ export function ChartRoot<Row>({
|
|
|
199
200
|
const points = rows === 'points';
|
|
200
201
|
const band = xScale === 'band' && !points;
|
|
201
202
|
const horizontal = orientation === 'horizontal' && band;
|
|
202
|
-
|
|
203
|
-
const series = horizontal ? new Map([...allSeries].filter(([, entry]) => entry.kind
|
|
204
|
-
const markers =
|
|
205
|
-
const bands =
|
|
206
|
-
const dropped =
|
|
203
|
+
const plane = horizontal ? ROWS : COLUMNS;
|
|
204
|
+
const series = horizontal ? new Map([...allSeries].filter(([, entry]) => plane.draws(entry.kind))) : allSeries;
|
|
205
|
+
const markers = plane.draws('marker') ? allMarkers : NO_ENTRIES;
|
|
206
|
+
const bands = plane.draws('band') ? allBands : NO_ENTRIES;
|
|
207
|
+
const dropped = series.size < allSeries.size || markers.size < allMarkers.size || bands.size < allBands.size;
|
|
207
208
|
|
|
208
209
|
useEffect(() => {
|
|
209
210
|
if (orientation === 'horizontal' && !band) {
|
|
@@ -679,6 +680,7 @@ export function ChartRoot<Row>({
|
|
|
679
680
|
band: layout,
|
|
680
681
|
continuous,
|
|
681
682
|
horizontal,
|
|
683
|
+
plane,
|
|
682
684
|
valueRoom,
|
|
683
685
|
y: scaleY,
|
|
684
686
|
yStep,
|
package/src/SeriesLegend.tsx
CHANGED
|
@@ -3,22 +3,7 @@ import clsx from 'clsx';
|
|
|
3
3
|
import { Check, Search } from 'lucide-react';
|
|
4
4
|
import { useId, useState, type ReactNode, type Ref } from 'react';
|
|
5
5
|
import { useTranslation } from 'react-i18next';
|
|
6
|
-
import { matchingItems, toggleGroup } from './legend.ts';
|
|
7
|
-
|
|
8
|
-
export interface SeriesLegendItem {
|
|
9
|
-
readonly key: string;
|
|
10
|
-
readonly label: string;
|
|
11
|
-
/* The full name a screen reader and the hint give, such as with the name of its group; `label` by default. */
|
|
12
|
-
readonly name?: string;
|
|
13
|
-
/* The key of the group it is listed under. */
|
|
14
|
-
readonly group?: string;
|
|
15
|
-
/* A CSS color of the app's own, such as a variable; the mark and the check take it. */
|
|
16
|
-
readonly color?: string;
|
|
17
|
-
/* A series that has nothing to show: listed, but it cannot be turned on. */
|
|
18
|
-
readonly disabled?: boolean;
|
|
19
|
-
/* Why the series shows less than it could, such as "Turned off", under its name and in its hint. */
|
|
20
|
-
readonly status?: string;
|
|
21
|
-
}
|
|
6
|
+
import { matchingItems, toggleGroup, type SeriesLegendItem } from './legend.ts';
|
|
22
7
|
|
|
23
8
|
export interface SeriesLegendGroup {
|
|
24
9
|
readonly key: string;
|
package/src/annotations.tsx
CHANGED
|
@@ -5,6 +5,7 @@ import { xOf } from './access.ts';
|
|
|
5
5
|
import { colorOf } from './color.ts';
|
|
6
6
|
import { useChartContext } from './context.ts';
|
|
7
7
|
import { keyIndexOf } from './cursor.ts';
|
|
8
|
+
import { crisp } from './plane.ts';
|
|
8
9
|
import { useRegistration } from './register.ts';
|
|
9
10
|
import type { ChartColor, ChartXInput } from './types.ts';
|
|
10
11
|
|
|
@@ -44,11 +45,6 @@ export interface ChartRuleProps {
|
|
|
44
45
|
|
|
45
46
|
const MIN_BAND = 4;
|
|
46
47
|
|
|
47
|
-
/* Room a label needs above its line before it moves under it instead. */
|
|
48
|
-
const RULE_LABEL = 18;
|
|
49
|
-
/* Between the line and the label it carries, above it or below it. */
|
|
50
|
-
const RULE_GAP = 7;
|
|
51
|
-
|
|
52
48
|
/*
|
|
53
49
|
* A line at a value across the plot, such as a reference to compare against, drawn above the series: horizontal over
|
|
54
50
|
* columns, vertical through the rows of a horizontal chart.
|
|
@@ -64,41 +60,17 @@ export function ChartRule({ value, label, tone = 'muted', dashed = false, classN
|
|
|
64
60
|
return null;
|
|
65
61
|
}
|
|
66
62
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
// Above the plot, on the side of the line that has the room, so it never covers the bars of the first row.
|
|
70
|
-
const toward = pixel > (chart.plot.left + chart.plot.right) / 2 ? 'end' : 'start';
|
|
71
|
-
|
|
72
|
-
return createPortal(
|
|
73
|
-
<g ref={ref} className={clsx('pointer-events-none', className)} style={{ color: colorOf(tone)! }} aria-hidden>
|
|
74
|
-
<line x1={left} x2={left} y1={chart.plot.top} y2={chart.plot.bottom} stroke="currentColor" strokeDasharray={dashed ? '4 4' : undefined} />
|
|
75
|
-
<text
|
|
76
|
-
x={Math.round(pixel) + (toward === 'end' ? 4 : -4)}
|
|
77
|
-
y={chart.plot.top - RULE_GAP}
|
|
78
|
-
textAnchor={toward}
|
|
79
|
-
strokeWidth={4}
|
|
80
|
-
paintOrder="stroke"
|
|
81
|
-
className="fill-text text-xs font-medium tabular-nums"
|
|
82
|
-
style={{ stroke: chart.halo }}
|
|
83
|
-
>
|
|
84
|
-
{`${label} · ${chart.formatY(value)}`}
|
|
85
|
-
</text>
|
|
86
|
-
</g>,
|
|
87
|
-
target
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const top = Math.round(pixel) + 0.5;
|
|
92
|
-
const below = top - RULE_LABEL < 0;
|
|
63
|
+
const line = chart.plane.valueLine(chart.plot, crisp(pixel));
|
|
64
|
+
const written = chart.plane.ruleLabel(chart.plot, pixel);
|
|
93
65
|
|
|
94
66
|
return createPortal(
|
|
95
67
|
<g ref={ref} className={clsx('pointer-events-none', className)} style={{ color: colorOf(tone)! }} aria-hidden>
|
|
96
|
-
<line x1={
|
|
68
|
+
<line x1={line.x1} x2={line.x2} y1={line.y1} y2={line.y2} stroke="currentColor" strokeDasharray={dashed ? '4 4' : undefined} />
|
|
97
69
|
<text
|
|
98
|
-
x={
|
|
99
|
-
y={
|
|
100
|
-
dy={
|
|
101
|
-
textAnchor=
|
|
70
|
+
x={written.x}
|
|
71
|
+
y={written.y}
|
|
72
|
+
dy={written.dy}
|
|
73
|
+
textAnchor={written.anchor}
|
|
102
74
|
strokeWidth={4}
|
|
103
75
|
paintOrder="stroke"
|
|
104
76
|
className="fill-text text-xs font-medium tabular-nums"
|
|
@@ -121,7 +93,7 @@ export function ChartMarker({ at, label, detail, tone = 'muted', className, ref
|
|
|
121
93
|
const pixel = chart.pixelOfX(x);
|
|
122
94
|
|
|
123
95
|
// A moment outside the axis, such as a deploy before the first bucket, has no place on the plot.
|
|
124
|
-
if (chart.
|
|
96
|
+
if (!chart.plane.draws('marker') || pixel === null || pixel < chart.plot.left || pixel > chart.plot.right) {
|
|
125
97
|
return null;
|
|
126
98
|
}
|
|
127
99
|
|
|
@@ -161,7 +133,7 @@ export function ChartBand({ from, to, label, detail, tone = 'muted', className,
|
|
|
161
133
|
}
|
|
162
134
|
}
|
|
163
135
|
|
|
164
|
-
if (chart.
|
|
136
|
+
if (!chart.plane.draws('band') || left === null || right === null || right <= left) {
|
|
165
137
|
return null;
|
|
166
138
|
}
|
|
167
139
|
|
package/src/axes.tsx
CHANGED
|
@@ -3,7 +3,8 @@ import clsx from 'clsx';
|
|
|
3
3
|
import { useEffect, useId, useRef, useState, useSyncExternalStore, type ReactNode, type Ref } from 'react';
|
|
4
4
|
import { createPortal } from 'react-dom';
|
|
5
5
|
import { useTranslation } from 'react-i18next';
|
|
6
|
-
import { useChartContext } from './context.ts';
|
|
6
|
+
import { useChartContext, type ChartContextValue } from './context.ts';
|
|
7
|
+
import { crisp } from './plane.ts';
|
|
7
8
|
import { useBeforePaint } from './register.ts';
|
|
8
9
|
import { decimalsOf, tickCount } from './scale.ts';
|
|
9
10
|
import { formatTime, timeLabelOf } from './time-format.ts';
|
|
@@ -85,9 +86,7 @@ const X_TICK_SPACING = 96;
|
|
|
85
86
|
const GUESS = 48;
|
|
86
87
|
const SLANT = (40 * Math.PI) / 180;
|
|
87
88
|
const DASH = '2 5';
|
|
88
|
-
/* The row an x axis title takes under its labels. */
|
|
89
89
|
const X_TITLE_ROOM = 21;
|
|
90
|
-
/* The baseline of a y axis title from the top of the chart, and the room above the plot it needs. */
|
|
91
90
|
const Y_TITLE_BASELINE = 15;
|
|
92
91
|
const Y_TITLE_ROOM = 28;
|
|
93
92
|
/* The names of the rows of a horizontal chart: their widest and the share of the chart they may take, and their line. */
|
|
@@ -103,9 +102,6 @@ const textWidth = (node: SVGTextElement): number =>
|
|
|
103
102
|
|
|
104
103
|
const widestIn = (group: SVGGElement | null): number => Math.max(0, ...[...(group?.querySelectorAll('text') ?? [])].map(textWidth));
|
|
105
104
|
|
|
106
|
-
/* A line sits on the middle of a pixel, so one pixel wide stays one pixel wide. */
|
|
107
|
-
const crisp = (pixel: number): number => Math.round(pixel) + 0.5;
|
|
108
|
-
|
|
109
105
|
/*
|
|
110
106
|
* The labels, from left to right, with every one left out that comes nearer to the one before it than `spacing`.
|
|
111
107
|
* A label on an end of the axis wins over its neighbor, so both ends stay named.
|
|
@@ -129,96 +125,55 @@ const apart = <Label extends { readonly x: number; readonly end?: boolean }>(lab
|
|
|
129
125
|
/* Gridlines on the ticks, a heavier one on the ground. */
|
|
130
126
|
export function ChartGrid({ axis = 'y', dashed = false, lines = 'all', className, ref }: ChartGridProps) {
|
|
131
127
|
const chart = useChartContext('Chart.Grid');
|
|
132
|
-
const { plot, y,
|
|
128
|
+
const { plot, y, plane } = chart;
|
|
133
129
|
const ground = y.domain[0] <= 0 && y.domain[1] >= 0 ? 0 : y.domain[0];
|
|
130
|
+
const ticks = lines === 'ground' ? [ground] : axis === 'x' ? [] : y.ticks;
|
|
131
|
+
const between = lines === 'ground' || axis === 'y' ? [] : betweenOf(chart);
|
|
134
132
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
return (
|
|
141
|
-
<g ref={ref} className={clsx('pointer-events-none', className)} aria-hidden>
|
|
142
|
-
{ticks.map((tick) => {
|
|
143
|
-
const pixel = y.at(tick);
|
|
133
|
+
return (
|
|
134
|
+
<g ref={ref} className={clsx('pointer-events-none', className)} aria-hidden>
|
|
135
|
+
{ticks.map((tick) => {
|
|
136
|
+
const pixel = y.at(tick);
|
|
137
|
+
const line = pixel === null ? null : plane.valueLine(plot, crisp(pixel));
|
|
144
138
|
|
|
145
|
-
|
|
146
|
-
<line
|
|
147
|
-
key={tick}
|
|
148
|
-
x1={crisp(pixel)}
|
|
149
|
-
x2={crisp(pixel)}
|
|
150
|
-
y1={plot.top}
|
|
151
|
-
y2={plot.bottom}
|
|
152
|
-
strokeDasharray={dashed && tick !== ground ? DASH : undefined}
|
|
153
|
-
className={dashed ? 'stroke-border' : tick === ground ? 'stroke-border-strong' : 'stroke-border-soft'}
|
|
154
|
-
/>
|
|
155
|
-
);
|
|
156
|
-
})}
|
|
157
|
-
{rows.map((pixel) => (
|
|
139
|
+
return line === null ? null : (
|
|
158
140
|
<line
|
|
159
|
-
key={
|
|
160
|
-
x1={
|
|
161
|
-
x2={
|
|
162
|
-
y1={
|
|
163
|
-
y2={
|
|
164
|
-
strokeDasharray={dashed ? DASH : undefined}
|
|
165
|
-
className={dashed ? 'stroke-border' : 'stroke-border-soft'}
|
|
141
|
+
key={tick}
|
|
142
|
+
x1={line.x1}
|
|
143
|
+
x2={line.x2}
|
|
144
|
+
y1={line.y1}
|
|
145
|
+
y2={line.y2}
|
|
146
|
+
strokeDasharray={dashed && tick !== ground ? DASH : undefined}
|
|
147
|
+
className={dashed ? 'stroke-border' : tick === ground ? 'stroke-border-strong' : 'stroke-border-soft'}
|
|
166
148
|
/>
|
|
167
|
-
)
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
if (lines === 'ground') {
|
|
173
|
-
const pixel = y.at(ground);
|
|
149
|
+
);
|
|
150
|
+
})}
|
|
151
|
+
{between.map((pixel) => {
|
|
152
|
+
const line = plane.bucketLine(plot, crisp(pixel));
|
|
174
153
|
|
|
175
|
-
|
|
176
|
-
<g ref={ref} className={clsx('pointer-events-none', className)} aria-hidden>
|
|
177
|
-
<line x1={plot.left} x2={plot.right} y1={crisp(pixel)} y2={crisp(pixel)} className={dashed ? 'stroke-border' : 'stroke-border-strong'} />
|
|
178
|
-
</g>
|
|
179
|
-
);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
const columns =
|
|
183
|
-
continuous !== null
|
|
184
|
-
? continuous.ticks(tickCount(plot.width, X_TICK_SPACING)).map((tick) => continuous.at(tick))
|
|
185
|
-
: chart.xs.map((_, index) => chart.centerOf(index));
|
|
186
|
-
|
|
187
|
-
return (
|
|
188
|
-
<g ref={ref} className={clsx('pointer-events-none', className)} aria-hidden>
|
|
189
|
-
{axis !== 'x' &&
|
|
190
|
-
y.ticks.map((tick) => {
|
|
191
|
-
const pixel = y.at(tick);
|
|
192
|
-
|
|
193
|
-
return pixel === null ? null : (
|
|
194
|
-
<line
|
|
195
|
-
key={tick}
|
|
196
|
-
x1={plot.left}
|
|
197
|
-
x2={plot.right}
|
|
198
|
-
y1={crisp(pixel)}
|
|
199
|
-
y2={crisp(pixel)}
|
|
200
|
-
strokeDasharray={dashed && tick !== ground ? DASH : undefined}
|
|
201
|
-
className={dashed ? 'stroke-border' : tick === ground ? 'stroke-border-strong' : 'stroke-border-soft'}
|
|
202
|
-
/>
|
|
203
|
-
);
|
|
204
|
-
})}
|
|
205
|
-
{axis !== 'y' &&
|
|
206
|
-
(band === null || chart.xs.length > 0) &&
|
|
207
|
-
columns.map((pixel) => (
|
|
154
|
+
return (
|
|
208
155
|
<line
|
|
209
156
|
key={pixel}
|
|
210
|
-
x1={
|
|
211
|
-
x2={
|
|
212
|
-
y1={
|
|
213
|
-
y2={
|
|
157
|
+
x1={line.x1}
|
|
158
|
+
x2={line.x2}
|
|
159
|
+
y1={line.y1}
|
|
160
|
+
y2={line.y2}
|
|
214
161
|
strokeDasharray={dashed ? DASH : undefined}
|
|
215
162
|
className={dashed ? 'stroke-border' : 'stroke-border-soft'}
|
|
216
163
|
/>
|
|
217
|
-
)
|
|
164
|
+
);
|
|
165
|
+
})}
|
|
218
166
|
</g>
|
|
219
167
|
);
|
|
220
168
|
}
|
|
221
169
|
|
|
170
|
+
/* Where the lines between the buckets stand: on the ticks of a continuous axis, else through the middle of every bucket. */
|
|
171
|
+
function betweenOf({ continuous, plot, xs, centerOf }: ChartContextValue): number[] {
|
|
172
|
+
return continuous !== null
|
|
173
|
+
? continuous.ticks(tickCount(plot.width, X_TICK_SPACING)).map((tick) => continuous.at(tick))
|
|
174
|
+
: xs.map((_, index) => centerOf(index));
|
|
175
|
+
}
|
|
176
|
+
|
|
222
177
|
/*
|
|
223
178
|
* The labels under the plot: one per bucket on a band scale as far as they fit, round ticks on a continuous one. In a
|
|
224
179
|
* horizontal chart, the name of every row left of the plot.
|
|
@@ -611,7 +566,7 @@ export function ChartGroupAxis<Row>({ children, className, ref }: ChartGroupAxis
|
|
|
611
566
|
const target = chart.elements.groups;
|
|
612
567
|
|
|
613
568
|
// The groups of a horizontal chart read from the gaps between their rows and from the tooltip.
|
|
614
|
-
if (target === null || chart.band === null || chart.
|
|
569
|
+
if (target === null || chart.band === null || !chart.plane.draws('groups')) {
|
|
615
570
|
return null;
|
|
616
571
|
}
|
|
617
572
|
|
package/src/band.ts
CHANGED
|
@@ -7,7 +7,7 @@ const MAX_COLUMN = 56;
|
|
|
7
7
|
/* Share of a bucket a column takes when it is alone in it; the rest is air between buckets. */
|
|
8
8
|
const COLUMN_SHARE = 0.72;
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
interface BandGroup {
|
|
11
11
|
readonly key: string;
|
|
12
12
|
readonly first: number;
|
|
13
13
|
readonly last: number;
|
|
@@ -28,7 +28,7 @@ export interface BandLayout {
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
/* The run of equal keys each bucket belongs to, in the order of the data; a key that comes back later starts a run of its own. */
|
|
31
|
-
|
|
31
|
+
function groupRuns(keys: readonly string[]): { key: string; first: number; last: number }[] {
|
|
32
32
|
const runs: { key: string; first: number; last: number }[] = [];
|
|
33
33
|
|
|
34
34
|
keys.forEach((key, index) => {
|
|
@@ -161,7 +161,7 @@ export const LABEL_INSET = 6;
|
|
|
161
161
|
/* The height of a row of labels and what a label is taken to measure per character until it has been drawn once, per size. */
|
|
162
162
|
const LABEL_SIZES = { '2xs': { row: 16, char: 8 }, xs: { row: 18, char: 9 } } as const;
|
|
163
163
|
|
|
164
|
-
|
|
164
|
+
type ValueLabelSize = keyof typeof LABEL_SIZES;
|
|
165
165
|
|
|
166
166
|
const LABEL_AIR = 3;
|
|
167
167
|
|
package/src/context.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { createContext, useContext } from 'react';
|
|
2
2
|
import type { BandLayout } from './band.ts';
|
|
3
3
|
import type { AxisWidths } from './group-axes.ts';
|
|
4
|
+
import type { ChartPlane } from './plane.ts';
|
|
4
5
|
import type { ContinuousXScale, Stacked, YAxisScale } from './scale.ts';
|
|
5
6
|
import type { BandEntry, ChartStore, MarkerEntry, SeriesEntry } from './store.ts';
|
|
6
7
|
import type { ChartNote, ChartRect, ChartX, ChartXScale } from './types.ts';
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
interface ChartSlots {
|
|
9
10
|
readonly legend: HTMLElement | null;
|
|
10
11
|
readonly table: HTMLElement | null;
|
|
11
12
|
readonly tooltip: HTMLElement | null;
|
|
@@ -29,6 +30,8 @@ export interface ChartContextValue {
|
|
|
29
30
|
* `x`, so `centerOf` and `bucketAt` measure down and `pixelOfY` across.
|
|
30
31
|
*/
|
|
31
32
|
readonly horizontal: boolean;
|
|
33
|
+
/* What the orientation changes for the parts that draw lines, labels and annotations; `horizontal` stays for the ones that lay out bars and axes. */
|
|
34
|
+
readonly plane: ChartPlane;
|
|
32
35
|
/* The room right of the plot of a horizontal chart that its value labels take. */
|
|
33
36
|
readonly valueRoom: number;
|
|
34
37
|
readonly height: number;
|
|
@@ -85,7 +88,6 @@ export interface ChartContextValue {
|
|
|
85
88
|
readonly positions: readonly (readonly [number, number] | null)[];
|
|
86
89
|
/* The series of a point chart, in the order they first appear, each with its rows in the order of the data. */
|
|
87
90
|
readonly pointGroups: readonly { readonly key: string; readonly indexes: readonly number[] }[];
|
|
88
|
-
/* The series whose points are all dimmed. */
|
|
89
91
|
readonly dimmedGroups: ReadonlySet<string>;
|
|
90
92
|
/* The categorical color of the series of a row, for a part without a tone of its own. */
|
|
91
93
|
readonly groupColorOf: (index: number) => string;
|
|
@@ -98,7 +100,6 @@ export interface ChartContextValue {
|
|
|
98
100
|
readonly readable: readonly (readonly [string, SeriesEntry])[];
|
|
99
101
|
/* The surface under the chart, which the halo around a label and the ring of a point are drawn in. */
|
|
100
102
|
readonly halo: string;
|
|
101
|
-
/* The keyboard moved the cursor last, not the pointer. */
|
|
102
103
|
readonly byKeyboard: boolean;
|
|
103
104
|
/* The bucket or point the keyboard is on, apart from one the pointer passes over; -1 for none. */
|
|
104
105
|
readonly keyIndex: number;
|
|
@@ -119,7 +120,7 @@ export function useChartContext(part: string): ChartContextValue {
|
|
|
119
120
|
/* The `Chart.Bars` a `Chart.Bar` is drawn in. */
|
|
120
121
|
export const BarsContext = createContext<{ id: string; stack: boolean } | null>(null);
|
|
121
122
|
|
|
122
|
-
|
|
123
|
+
interface GroupCursor {
|
|
123
124
|
readonly x: ChartX | null;
|
|
124
125
|
/* The chart the pointer or the keyboard is in, the one that shows its tooltip. */
|
|
125
126
|
readonly owner: string | null;
|
package/src/index.ts
CHANGED
|
@@ -18,7 +18,8 @@ export type {
|
|
|
18
18
|
} from './overlays.tsx';
|
|
19
19
|
export type { ChartRootProps } from './Root.tsx';
|
|
20
20
|
export type { ChartEndLabelsProps, ChartFrontierProps, ChartPointSeries } from './scatter.tsx';
|
|
21
|
-
export { SeriesLegend, type SeriesLegendGroup, type
|
|
21
|
+
export { SeriesLegend, type SeriesLegendGroup, type SeriesLegendProps } from './SeriesLegend.tsx';
|
|
22
|
+
export type { SeriesLegendItem } from './legend.ts';
|
|
22
23
|
export type { ChartAreaProps, ChartBarProps, ChartBarTone, ChartBarsProps, ChartLineProps, ChartPointProps } from './series.tsx';
|
|
23
24
|
export { Sparkline, type SparklineProps } from './Sparkline.tsx';
|
|
24
25
|
export type { TimelineAxisProps, TimelineLegendProps, TimelineMomentProps, TimelineRootProps, TimelineRowState, TimelineRowsProps } from './timeline.tsx';
|
package/src/legend.ts
CHANGED
|
@@ -1,4 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
export interface SeriesLegendItem {
|
|
2
|
+
readonly key: string;
|
|
3
|
+
readonly label: string;
|
|
4
|
+
/* The full name a screen reader and the hint give, such as with the name of its group; `label` by default. */
|
|
5
|
+
readonly name?: string;
|
|
6
|
+
/* The key of the group it is listed under. */
|
|
7
|
+
readonly group?: string;
|
|
8
|
+
/* A CSS color of the app's own, such as a variable; the mark and the check take it. */
|
|
9
|
+
readonly color?: string;
|
|
10
|
+
/* A series that has nothing to show: listed, but it cannot be turned on. */
|
|
11
|
+
readonly disabled?: boolean;
|
|
12
|
+
/* Why the series shows less than it could, such as "Turned off", under its name and in its hint. */
|
|
13
|
+
readonly status?: string;
|
|
14
|
+
}
|
|
2
15
|
|
|
3
16
|
/* The items whose label or full name holds the query, ignoring case and the space around it. */
|
|
4
17
|
export function matchingItems<Item extends SeriesLegendItem>(items: readonly Item[], query: string): Item[] {
|
package/src/overlays.tsx
CHANGED
|
@@ -143,7 +143,6 @@ function rowPlace(chart: ChartContextValue, placement: ChartTooltipPlacement, ca
|
|
|
143
143
|
};
|
|
144
144
|
}
|
|
145
145
|
|
|
146
|
-
/* The swatch of a series: its icon in its color, else a dash for a line and a square for a column. */
|
|
147
146
|
function Swatch({ color, entry, size }: { color: string; entry: SeriesEntry; size: 12 | 14 }) {
|
|
148
147
|
if (entry.icon !== null) {
|
|
149
148
|
return (
|
|
@@ -156,7 +155,6 @@ function Swatch({ color, entry, size }: { color: string; entry: SeriesEntry; siz
|
|
|
156
155
|
return <span className={clsx('shrink-0', entry.kind === 'bar' ? 'size-2 rounded-xs' : 'h-0.5 w-3 rounded-full')} style={{ background: color }} />;
|
|
157
156
|
}
|
|
158
157
|
|
|
159
|
-
/* A dashed stroke for a marker, a square for a band, in their color. */
|
|
160
158
|
function NoteSwatch({ note }: { note: ChartNote }) {
|
|
161
159
|
return note.kind === 'marker' ? (
|
|
162
160
|
<span className="flex w-2 shrink-0 justify-center">
|
package/src/plane.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { SeriesEntry } from './store.ts';
|
|
2
|
+
import type { ChartRect } from './types.ts';
|
|
3
|
+
|
|
4
|
+
/* What a part draws, which a layout may leave out: the kinds of series, and the annotations and labels that lie across the buckets. */
|
|
5
|
+
export type Drawn = SeriesEntry['kind'] | 'marker' | 'band' | 'groups';
|
|
6
|
+
|
|
7
|
+
/* A segment across the plot, in the coordinates of its svg. */
|
|
8
|
+
interface PlotLine {
|
|
9
|
+
readonly x1: number;
|
|
10
|
+
readonly x2: number;
|
|
11
|
+
readonly y1: number;
|
|
12
|
+
readonly y2: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface RuleLabel {
|
|
16
|
+
readonly x: number;
|
|
17
|
+
readonly y: number;
|
|
18
|
+
readonly dy?: string;
|
|
19
|
+
readonly anchor: 'start' | 'end';
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/*
|
|
23
|
+
* How the buckets and the values lie on the plot. A part asks the plane where its lines and labels go
|
|
24
|
+
* and what it may draw, so none of them branches on the orientation.
|
|
25
|
+
*/
|
|
26
|
+
export interface ChartPlane {
|
|
27
|
+
readonly draws: (part: Drawn) => boolean;
|
|
28
|
+
/* The line at a pixel of the value axis, across the buckets. */
|
|
29
|
+
readonly valueLine: (plot: ChartRect, pixel: number) => PlotLine;
|
|
30
|
+
/* The line at a pixel of the bucket axis, across the values. */
|
|
31
|
+
readonly bucketLine: (plot: ChartRect, pixel: number) => PlotLine;
|
|
32
|
+
/* Where the label of a reference line goes: `pixel` is where the line crosses the value axis. */
|
|
33
|
+
readonly ruleLabel: (plot: ChartRect, pixel: number) => RuleLabel;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/* A line one pixel wide is drawn on the middle of a pixel, so it stays sharp. */
|
|
37
|
+
export const crisp = (pixel: number): number => Math.round(pixel) + 0.5;
|
|
38
|
+
|
|
39
|
+
/* Room a label needs above its line before it moves under it instead. */
|
|
40
|
+
const RULE_LABEL = 18;
|
|
41
|
+
/* Between the line and the label it carries, above it or below it. */
|
|
42
|
+
const RULE_GAP = 7;
|
|
43
|
+
/* Between the line and the label it carries, beside it. */
|
|
44
|
+
const RULE_SIDE = 4;
|
|
45
|
+
|
|
46
|
+
const across = (plot: ChartRect, pixel: number): PlotLine => ({ x1: plot.left, x2: plot.right, y1: pixel, y2: pixel });
|
|
47
|
+
|
|
48
|
+
const upright = (plot: ChartRect, pixel: number): PlotLine => ({ x1: pixel, x2: pixel, y1: plot.top, y2: plot.bottom });
|
|
49
|
+
|
|
50
|
+
/* The buckets are columns along the bottom and the values run up. */
|
|
51
|
+
export const COLUMNS: ChartPlane = {
|
|
52
|
+
draws: () => true,
|
|
53
|
+
valueLine: across,
|
|
54
|
+
bucketLine: upright,
|
|
55
|
+
ruleLabel: (plot, pixel) => {
|
|
56
|
+
const below = crisp(pixel) - RULE_LABEL < 0;
|
|
57
|
+
|
|
58
|
+
return { x: Math.round(plot.right), y: Math.round(pixel) + (below ? RULE_GAP : -RULE_GAP), ...(below ? { dy: '0.8em' } : {}), anchor: 'end' };
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/*
|
|
63
|
+
* The buckets are rows down the chart and the values run to the right. Only columns turn into rows: a line, a
|
|
64
|
+
* point, a marker or a band across rows would read nothing a bar does not.
|
|
65
|
+
*/
|
|
66
|
+
export const ROWS: ChartPlane = {
|
|
67
|
+
draws: (part) => part === 'bar',
|
|
68
|
+
valueLine: upright,
|
|
69
|
+
bucketLine: across,
|
|
70
|
+
ruleLabel: (plot, pixel) => {
|
|
71
|
+
// Above the plot, on the side of the line that has the room, so it never covers the bars of the first row.
|
|
72
|
+
const anchor = pixel > (plot.left + plot.right) / 2 ? 'end' : 'start';
|
|
73
|
+
|
|
74
|
+
return { x: Math.round(pixel) + (anchor === 'end' ? RULE_SIDE : -RULE_SIDE), y: plot.top - RULE_GAP, anchor };
|
|
75
|
+
}
|
|
76
|
+
};
|
package/src/scale.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { scaleLinear, scaleLog, scaleTime } from 'd3-scale';
|
|
2
2
|
import type { ChartXOptions, ChartYOptions, ChartYScale } from './types.ts';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
interface StackInput {
|
|
5
5
|
readonly id: string;
|
|
6
6
|
readonly values: readonly (number | null)[];
|
|
7
7
|
/* Series with the same stack key pile up in the order they are listed; without one a series stands on zero. */
|
package/src/scatter.tsx
CHANGED
|
@@ -12,7 +12,6 @@ export interface ChartFrontierProps {
|
|
|
12
12
|
readonly lowerX?: boolean;
|
|
13
13
|
/* A high value is the better one; false for a lower value, such as a time. */
|
|
14
14
|
readonly higherY?: boolean;
|
|
15
|
-
/* The width of the band in pixels. */
|
|
16
15
|
readonly width?: number;
|
|
17
16
|
/* `--border` by default, an alpha over what is behind it. */
|
|
18
17
|
readonly tone?: ChartColor;
|