@schemacast/react 0.0.0-stage → 0.5.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 +21 -0
- package/README.md +37 -2
- package/dist/auto-visual.d.ts +32 -0
- package/dist/auto-visual.js +27 -0
- package/dist/field-by-key.d.ts +3 -0
- package/dist/field-by-key.js +7 -0
- package/dist/format-field-value.d.ts +16 -0
- package/dist/format-field-value.js +84 -0
- package/dist/implemented-archetypes.d.ts +2 -0
- package/dist/implemented-archetypes.js +17 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +10 -0
- package/dist/use-selected-view-id.d.ts +6 -0
- package/dist/use-selected-view-id.js +17 -0
- package/dist/view-switcher.d.ts +26 -0
- package/dist/view-switcher.js +63 -0
- package/dist/views/archetype-view.d.ts +16 -0
- package/dist/views/archetype-view.js +71 -0
- package/dist/views/bar-chart-options.d.ts +22 -0
- package/dist/views/bar-chart-options.js +99 -0
- package/dist/views/bar-chart-view.d.ts +17 -0
- package/dist/views/bar-chart-view.js +25 -0
- package/dist/views/build-hierarchy-tree-data.d.ts +8 -0
- package/dist/views/build-hierarchy-tree-data.js +32 -0
- package/dist/views/build-node-graph-data.d.ts +20 -0
- package/dist/views/build-node-graph-data.js +44 -0
- package/dist/views/chart-series-data.d.ts +31 -0
- package/dist/views/chart-series-data.js +85 -0
- package/dist/views/chart-value-axes.d.ts +41 -0
- package/dist/views/chart-value-axes.js +209 -0
- package/dist/views/data-table-constants.d.ts +3 -0
- package/dist/views/data-table-constants.js +3 -0
- package/dist/views/data-table-rows.d.ts +8 -0
- package/dist/views/data-table-rows.js +50 -0
- package/dist/views/data-table-view.d.ts +16 -0
- package/dist/views/data-table-view.js +76 -0
- package/dist/views/detail-card-view.d.ts +16 -0
- package/dist/views/detail-card-view.js +59 -0
- package/dist/views/edge-weight-width.d.ts +23 -0
- package/dist/views/edge-weight-width.js +47 -0
- package/dist/views/hierarchy-tree-view.d.ts +16 -0
- package/dist/views/hierarchy-tree-view.js +58 -0
- package/dist/views/json-tree-view.d.ts +15 -0
- package/dist/views/json-tree-view.js +115 -0
- package/dist/views/kanban-view.d.ts +16 -0
- package/dist/views/kanban-view.js +41 -0
- package/dist/views/kpi-grid-view.d.ts +16 -0
- package/dist/views/kpi-grid-view.js +34 -0
- package/dist/views/line-chart-options.d.ts +16 -0
- package/dist/views/line-chart-options.js +94 -0
- package/dist/views/line-chart-view.d.ts +16 -0
- package/dist/views/line-chart-view.js +31 -0
- package/dist/views/node-graph-options.d.ts +26 -0
- package/dist/views/node-graph-options.js +111 -0
- package/dist/views/node-graph-view.d.ts +16 -0
- package/dist/views/node-graph-view.js +28 -0
- package/dist/views/pie-chart-view.d.ts +16 -0
- package/dist/views/pie-chart-view.js +56 -0
- package/dist/views/timeline-view.d.ts +16 -0
- package/dist/views/timeline-view.js +86 -0
- package/dist/views/use-echarts.d.ts +4 -0
- package/dist/views/use-echarts.js +233 -0
- package/dist/visual-spec-view.d.ts +28 -0
- package/dist/visual-spec-view.js +31 -0
- package/package.json +56 -3
- package/styles.css +688 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { FieldBindings } from '@schemacast/core';
|
|
2
|
+
export interface HierarchyTreeNode {
|
|
3
|
+
readonly name: string;
|
|
4
|
+
readonly children?: HierarchyTreeNode[];
|
|
5
|
+
}
|
|
6
|
+
export declare function buildHierarchyTreeData(root: Record<string, unknown> | undefined, bindings: FieldBindings): HierarchyTreeNode | null;
|
|
7
|
+
export declare function collectHierarchyNames(node: HierarchyTreeNode): string[];
|
|
8
|
+
//# sourceMappingURL=build-hierarchy-tree-data.d.ts.map
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export function buildHierarchyTreeData(root, bindings) {
|
|
2
|
+
if (root === undefined) {
|
|
3
|
+
return null;
|
|
4
|
+
}
|
|
5
|
+
const titleKey = bindings.title ?? 'name';
|
|
6
|
+
const childrenKey = bindings.groupBy ?? 'children';
|
|
7
|
+
return mapTreeNode(root, titleKey, childrenKey);
|
|
8
|
+
}
|
|
9
|
+
function mapTreeNode(row, titleKey, childrenKey) {
|
|
10
|
+
const node = { name: String(row[titleKey] ?? '') };
|
|
11
|
+
const children = row[childrenKey];
|
|
12
|
+
if (!Array.isArray(children)) {
|
|
13
|
+
return node;
|
|
14
|
+
}
|
|
15
|
+
const mapped = children
|
|
16
|
+
.filter((child) => typeof child === 'object' && child !== null && !Array.isArray(child))
|
|
17
|
+
.map((child) => mapTreeNode(child, titleKey, childrenKey));
|
|
18
|
+
if (mapped.length > 0) {
|
|
19
|
+
return { ...node, children: mapped };
|
|
20
|
+
}
|
|
21
|
+
return node;
|
|
22
|
+
}
|
|
23
|
+
export function collectHierarchyNames(node) {
|
|
24
|
+
const names = [node.name];
|
|
25
|
+
if (node.children !== undefined) {
|
|
26
|
+
for (const child of node.children) {
|
|
27
|
+
names.push(...collectHierarchyNames(child));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return names;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=build-hierarchy-tree-data.js.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { FieldBindings, VisualSpec } from '@schemacast/core';
|
|
2
|
+
export interface GraphNodeDatum {
|
|
3
|
+
readonly id: string;
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly category?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface GraphLinkDatum {
|
|
8
|
+
readonly source: string;
|
|
9
|
+
readonly target: string;
|
|
10
|
+
readonly label?: string;
|
|
11
|
+
/** Numeric weight from the `edgeWeight` binding; absent when the edge has none */
|
|
12
|
+
readonly weight?: number;
|
|
13
|
+
/** Line width in pixels, set on every link when the graph has an `edgeWeight` binding */
|
|
14
|
+
readonly width?: number;
|
|
15
|
+
}
|
|
16
|
+
export declare function buildNodeGraphData(spec: VisualSpec, bindings: FieldBindings): {
|
|
17
|
+
readonly nodes: GraphNodeDatum[];
|
|
18
|
+
readonly links: GraphLinkDatum[];
|
|
19
|
+
};
|
|
20
|
+
//# sourceMappingURL=build-node-graph-data.d.ts.map
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { readEdgeWeight, scaleEdgeWidths } from './edge-weight-width.js';
|
|
2
|
+
export function buildNodeGraphData(spec, bindings) {
|
|
3
|
+
const nodeIdKey = bindings.nodeId ?? 'id';
|
|
4
|
+
const titleKey = bindings.title;
|
|
5
|
+
const sourceKey = bindings.edgeSource ?? 'source';
|
|
6
|
+
const targetKey = bindings.edgeTarget ?? 'target';
|
|
7
|
+
const categoryKey = bindings.color;
|
|
8
|
+
const nodes = spec.data.map((row) => {
|
|
9
|
+
const id = String(row[nodeIdKey] ?? '');
|
|
10
|
+
const name = titleKey !== undefined ? String(row[titleKey] ?? id) : id;
|
|
11
|
+
if (categoryKey === undefined) {
|
|
12
|
+
return { id, name };
|
|
13
|
+
}
|
|
14
|
+
return { id, name, category: formatNodeCategory(row[categoryKey]) };
|
|
15
|
+
});
|
|
16
|
+
const edges = spec.edges ?? [];
|
|
17
|
+
const weightKey = bindings.edgeWeight;
|
|
18
|
+
const weights = edges.map((edge) => weightKey === undefined ? undefined : readEdgeWeight(edge[weightKey]));
|
|
19
|
+
const widths = weightKey === undefined ? undefined : scaleEdgeWidths(weights);
|
|
20
|
+
const links = edges.map((edge, index) => {
|
|
21
|
+
const source = String(edge[sourceKey] ?? '');
|
|
22
|
+
const target = String(edge[targetKey] ?? '');
|
|
23
|
+
const weight = weights[index];
|
|
24
|
+
const width = widths?.[index];
|
|
25
|
+
return {
|
|
26
|
+
source,
|
|
27
|
+
target,
|
|
28
|
+
...(edge['label'] === undefined ? {} : { label: String(edge['label']) }),
|
|
29
|
+
...(weight === undefined ? {} : { weight }),
|
|
30
|
+
...(width === undefined ? {} : { width }),
|
|
31
|
+
};
|
|
32
|
+
});
|
|
33
|
+
return { nodes, links };
|
|
34
|
+
}
|
|
35
|
+
function formatNodeCategory(value) {
|
|
36
|
+
if (typeof value === 'string' && value.trim() !== '') {
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
if (typeof value === 'boolean' || (typeof value === 'number' && Number.isFinite(value))) {
|
|
40
|
+
return String(value);
|
|
41
|
+
}
|
|
42
|
+
return 'Uncategorized';
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=build-node-graph-data.js.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type FieldDescriptor, type SemanticType } from '@schemacast/core';
|
|
2
|
+
import type { XAXisComponentOption } from 'echarts';
|
|
3
|
+
export declare function readNumericSeriesValue(value: unknown, semanticType?: SemanticType): number | null;
|
|
4
|
+
export declare function categoryValues(rows: readonly Record<string, unknown>[], categoryKey: string, fields: readonly FieldDescriptor[]): string[];
|
|
5
|
+
export declare function buildCategoryAxis(rows: readonly Record<string, unknown>[], categoryKey: string, fields: readonly FieldDescriptor[]): XAXisComponentOption;
|
|
6
|
+
/**
|
|
7
|
+
* True when the x field is a plain number (a step, epoch or version), so the chart needs a value
|
|
8
|
+
* axis. Dates and categories keep the category axis.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* isNumericAxisField(fieldByKey(spec.fields, 'epoch')); // true
|
|
12
|
+
*/
|
|
13
|
+
export declare function isNumericAxisField(field: FieldDescriptor | undefined): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Builds a value-type x axis for a numeric x field, named after the field.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* const axis = buildNumericXAxis(spec.fields, 'epoch');
|
|
19
|
+
*/
|
|
20
|
+
export declare function buildNumericXAxis(fields: readonly FieldDescriptor[], xKey: string): XAXisComponentOption;
|
|
21
|
+
/**
|
|
22
|
+
* Pairs each row's numeric x with its y so a value axis plots at the real x position. Rows whose
|
|
23
|
+
* x is not a number are skipped.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* numericXSeries(rows, 'epoch', 'loss', fields); // [[1, 2.4], [2, 1.9]]
|
|
27
|
+
*/
|
|
28
|
+
export declare function numericXSeries(rows: readonly Record<string, unknown>[], xKey: string, yKey: string, fields: readonly FieldDescriptor[]): [number, number | null][];
|
|
29
|
+
export declare function numericSeries(rows: readonly Record<string, unknown>[], valueKey: string, fields?: readonly FieldDescriptor[]): (number | null)[];
|
|
30
|
+
export declare function seriesLabel(fields: readonly FieldDescriptor[], key: string): string;
|
|
31
|
+
//# sourceMappingURL=chart-series-data.d.ts.map
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { parseNumericValue } from '@schemacast/core';
|
|
2
|
+
import { fieldByKey } from '../field-by-key.js';
|
|
3
|
+
import { formatFieldValue } from '../format-field-value.js';
|
|
4
|
+
const MAX_CATEGORY_AXIS_LABEL_LENGTH = 16;
|
|
5
|
+
export function readNumericSeriesValue(value, semanticType) {
|
|
6
|
+
return parseNumericValue(value, semanticType);
|
|
7
|
+
}
|
|
8
|
+
export function categoryValues(rows, categoryKey, fields) {
|
|
9
|
+
return rows.map((row) => formatFieldValue(row[categoryKey], fieldByKey(fields, categoryKey)));
|
|
10
|
+
}
|
|
11
|
+
export function buildCategoryAxis(rows, categoryKey, fields) {
|
|
12
|
+
return {
|
|
13
|
+
type: 'category',
|
|
14
|
+
data: categoryValues(rows, categoryKey, fields),
|
|
15
|
+
axisLabel: {
|
|
16
|
+
interval: 0,
|
|
17
|
+
hideOverlap: false,
|
|
18
|
+
rotate: 35,
|
|
19
|
+
formatter: (value) => truncateCategoryAxisLabel(value),
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* True when the x field is a plain number (a step, epoch or version), so the chart needs a value
|
|
25
|
+
* axis. Dates and categories keep the category axis.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* isNumericAxisField(fieldByKey(spec.fields, 'epoch')); // true
|
|
29
|
+
*/
|
|
30
|
+
export function isNumericAxisField(field) {
|
|
31
|
+
return field?.semanticType === 'number';
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Builds a value-type x axis for a numeric x field, named after the field.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* const axis = buildNumericXAxis(spec.fields, 'epoch');
|
|
38
|
+
*/
|
|
39
|
+
export function buildNumericXAxis(fields, xKey) {
|
|
40
|
+
return {
|
|
41
|
+
type: 'value',
|
|
42
|
+
scale: true,
|
|
43
|
+
name: fieldByKey(fields, xKey)?.label ?? xKey,
|
|
44
|
+
nameLocation: 'middle',
|
|
45
|
+
nameGap: 28,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Pairs each row's numeric x with its y so a value axis plots at the real x position. Rows whose
|
|
50
|
+
* x is not a number are skipped.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* numericXSeries(rows, 'epoch', 'loss', fields); // [[1, 2.4], [2, 1.9]]
|
|
54
|
+
*/
|
|
55
|
+
export function numericXSeries(rows, xKey, yKey, fields) {
|
|
56
|
+
const xType = fieldByKey(fields, xKey)?.semanticType;
|
|
57
|
+
const yType = fieldByKey(fields, yKey)?.semanticType;
|
|
58
|
+
const points = [];
|
|
59
|
+
for (const row of rows) {
|
|
60
|
+
const x = readNumericSeriesValue(row[xKey], xType);
|
|
61
|
+
if (x !== null) {
|
|
62
|
+
points.push([x, readNumericSeriesValue(row[yKey], yType)]);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return points;
|
|
66
|
+
}
|
|
67
|
+
export function numericSeries(rows, valueKey, fields) {
|
|
68
|
+
const semanticType = fields === undefined ? undefined : fieldByKey(fields, valueKey)?.semanticType;
|
|
69
|
+
return rows.map((row) => readNumericSeriesValue(row[valueKey], semanticType));
|
|
70
|
+
}
|
|
71
|
+
export function seriesLabel(fields, key) {
|
|
72
|
+
return fieldByKey(fields, key)?.label ?? key;
|
|
73
|
+
}
|
|
74
|
+
function truncateCategoryAxisLabel(value) {
|
|
75
|
+
const label = String(value);
|
|
76
|
+
const characters = Array.from(label);
|
|
77
|
+
if (characters.length <= MAX_CATEGORY_AXIS_LABEL_LENGTH) {
|
|
78
|
+
return label;
|
|
79
|
+
}
|
|
80
|
+
return `${characters
|
|
81
|
+
.slice(0, MAX_CATEGORY_AXIS_LABEL_LENGTH - 1)
|
|
82
|
+
.join('')
|
|
83
|
+
.trimEnd()}…`;
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=chart-series-data.js.map
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type FieldDescriptor, type VisualSpec } from '@schemacast/core';
|
|
2
|
+
import type { TooltipComponentFormatterCallbackParams, YAXisComponentOption } from 'echarts';
|
|
3
|
+
/** Identifies the field and side used to render one value axis. */
|
|
4
|
+
export interface ValueAxisDefinition {
|
|
5
|
+
readonly fieldKey: string;
|
|
6
|
+
readonly position: 'left' | 'right';
|
|
7
|
+
}
|
|
8
|
+
/** Contains shared y-axis assignments for bar and line chart series. */
|
|
9
|
+
export interface ValueAxisPlan {
|
|
10
|
+
readonly axes: readonly ValueAxisDefinition[];
|
|
11
|
+
readonly seriesAxisIndexes: readonly number[];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Chooses one or two value axes from field units and series magnitudes. A line chart also
|
|
15
|
+
* splits a 0-1 metric from a larger one (`isLineChart`), see `findSecondaryAxisSeries`.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const axisPlan = buildValueAxisPlan(spec, ['revenue_usd', 'growth_pct']);
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare function buildValueAxisPlan(spec: VisualSpec, yKeys: readonly string[], rows?: readonly Record<string, unknown>[], isLineChart?: boolean): ValueAxisPlan;
|
|
23
|
+
/**
|
|
24
|
+
* Builds a themed-ready, unit-aware y-axis shared by bar and line charts.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* const axis = buildValueAxis(spec.fields, { fieldKey: 'error_rate', position: 'left' });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare function buildValueAxis(fields: readonly FieldDescriptor[], definition: ValueAxisDefinition): YAXisComponentOption;
|
|
32
|
+
/**
|
|
33
|
+
* Formats a chart tooltip using full precision and field units.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* const tooltip = formatChartTooltip(params, spec.fields, ['p95_latency_ms']);
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export declare function formatChartTooltip(parameters: TooltipComponentFormatterCallbackParams, fields: readonly FieldDescriptor[], yKeys: readonly string[]): string;
|
|
41
|
+
//# sourceMappingURL=chart-value-axes.d.ts.map
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { findSecondaryAxisSeries, measureSeriesMagnitude, } from '@schemacast/core';
|
|
2
|
+
import { fieldByKey } from '../field-by-key.js';
|
|
3
|
+
import { formatFieldValue, isUnitConfirmed } from '../format-field-value.js';
|
|
4
|
+
import { seriesLabel } from './chart-series-data.js';
|
|
5
|
+
/**
|
|
6
|
+
* Chooses one or two value axes from field units and series magnitudes. A line chart also
|
|
7
|
+
* splits a 0-1 metric from a larger one (`isLineChart`), see `findSecondaryAxisSeries`.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* const axisPlan = buildValueAxisPlan(spec, ['revenue_usd', 'growth_pct']);
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export function buildValueAxisPlan(spec, yKeys, rows = spec.data, isLineChart = false) {
|
|
15
|
+
const firstYKey = yKeys[0];
|
|
16
|
+
if (firstYKey === undefined) {
|
|
17
|
+
return { axes: [], seriesAxisIndexes: [] };
|
|
18
|
+
}
|
|
19
|
+
if (yKeys.length < 2) {
|
|
20
|
+
return {
|
|
21
|
+
axes: [{ fieldKey: firstYKey, position: 'left' }],
|
|
22
|
+
seriesAxisIndexes: yKeys.map(() => 0),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
const firstUnit = unitKey(fieldByKey(spec.fields, firstYKey));
|
|
26
|
+
const differentUnits = yKeys.some((key) => unitKey(fieldByKey(spec.fields, key)) !== firstUnit);
|
|
27
|
+
if (differentUnits) {
|
|
28
|
+
const secondYKey = yKeys.find((key) => unitKey(fieldByKey(spec.fields, key)) !== firstUnit);
|
|
29
|
+
if (secondYKey === undefined) {
|
|
30
|
+
return {
|
|
31
|
+
axes: [{ fieldKey: firstYKey, position: 'left' }],
|
|
32
|
+
seriesAxisIndexes: yKeys.map(() => 0),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
axes: [
|
|
37
|
+
{ fieldKey: firstYKey, position: 'left' },
|
|
38
|
+
{ fieldKey: secondYKey, position: 'right' },
|
|
39
|
+
],
|
|
40
|
+
seriesAxisIndexes: yKeys.map((key) => unitKey(fieldByKey(spec.fields, key)) === firstUnit ? 0 : 1),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
const magnitudes = yKeys.map((key) => measureSeriesMagnitude(rows, key, fieldByKey(spec.fields, key)?.semanticType));
|
|
44
|
+
const rightIndexes = findSecondaryAxisSeries(magnitudes, isLineChart);
|
|
45
|
+
const firstRightIndex = rightIndexes[0];
|
|
46
|
+
const firstLeftIndex = yKeys.findIndex((_, index) => !rightIndexes.includes(index));
|
|
47
|
+
const rightYKey = firstRightIndex === undefined ? undefined : yKeys[firstRightIndex];
|
|
48
|
+
const leftYKey = yKeys[firstLeftIndex];
|
|
49
|
+
if (rightYKey === undefined || leftYKey === undefined) {
|
|
50
|
+
return {
|
|
51
|
+
axes: [{ fieldKey: firstYKey, position: 'left' }],
|
|
52
|
+
seriesAxisIndexes: yKeys.map(() => 0),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
axes: [
|
|
57
|
+
{ fieldKey: leftYKey, position: 'left' },
|
|
58
|
+
{ fieldKey: rightYKey, position: 'right' },
|
|
59
|
+
],
|
|
60
|
+
seriesAxisIndexes: yKeys.map((_, index) => (rightIndexes.includes(index) ? 1 : 0)),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Builds a themed-ready, unit-aware y-axis shared by bar and line charts.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* const axis = buildValueAxis(spec.fields, { fieldKey: 'error_rate', position: 'left' });
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export function buildValueAxis(fields, definition) {
|
|
72
|
+
const field = fieldByKey(fields, definition.fieldKey);
|
|
73
|
+
const label = seriesLabel(fields, definition.fieldKey);
|
|
74
|
+
const unit = fieldUnit(field);
|
|
75
|
+
return {
|
|
76
|
+
type: 'value',
|
|
77
|
+
name: unit === undefined ? label : appendAxisUnit(label, unit),
|
|
78
|
+
nameLocation: 'middle',
|
|
79
|
+
nameGap: 56,
|
|
80
|
+
position: definition.position,
|
|
81
|
+
splitNumber: 5,
|
|
82
|
+
alignTicks: definition.position === 'right',
|
|
83
|
+
splitLine: { show: definition.position === 'left' },
|
|
84
|
+
axisLabel: {
|
|
85
|
+
formatter: (value) => formatCompactAxisValue(value, field),
|
|
86
|
+
hideOverlap: true,
|
|
87
|
+
margin: 10,
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function appendAxisUnit(label, unit) {
|
|
92
|
+
const normalizedLabel = label.trimEnd().toLowerCase();
|
|
93
|
+
const normalizedSuffix = `(${unit.toLowerCase()})`;
|
|
94
|
+
return normalizedLabel.endsWith(normalizedSuffix) ? label : `${label} (${unit})`;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Formats a chart tooltip using full precision and field units.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* const tooltip = formatChartTooltip(params, spec.fields, ['p95_latency_ms']);
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
export function formatChartTooltip(parameters, fields, yKeys) {
|
|
105
|
+
const tooltipParameters = Array.isArray(parameters) ? parameters : [parameters];
|
|
106
|
+
const firstParameter = tooltipParameters[0];
|
|
107
|
+
if (firstParameter === undefined) {
|
|
108
|
+
return '';
|
|
109
|
+
}
|
|
110
|
+
const headingText = firstParameter.name === '' ? readAxisValueLabel(firstParameter) : firstParameter.name;
|
|
111
|
+
const heading = `<strong>${escapeTooltipText(headingText)}</strong>`;
|
|
112
|
+
const values = tooltipParameters.map((parameter) => {
|
|
113
|
+
const seriesKey = parameter.seriesIndex === undefined ? undefined : yKeys[parameter.seriesIndex];
|
|
114
|
+
const field = seriesKey === undefined ? undefined : fieldByKey(fields, seriesKey);
|
|
115
|
+
const label = parameter.seriesName ?? (seriesKey === undefined ? 'Value' : seriesLabel(fields, seriesKey));
|
|
116
|
+
// A numeric x axis feeds [x, y] pairs; the y is the second item.
|
|
117
|
+
const plotted = Array.isArray(parameter.value) ? parameter.value[1] : parameter.value;
|
|
118
|
+
const value = formatTooltipValue(plotted, field);
|
|
119
|
+
return `${escapeTooltipText(label)}: ${escapeTooltipText(value)}`;
|
|
120
|
+
});
|
|
121
|
+
return [heading, ...values].join('<br/>');
|
|
122
|
+
}
|
|
123
|
+
/** A value x axis leaves the point name empty, so the hovered x comes from `axisValueLabel`. */
|
|
124
|
+
function readAxisValueLabel(parameter) {
|
|
125
|
+
if ('axisValueLabel' in parameter && typeof parameter.axisValueLabel === 'string') {
|
|
126
|
+
return parameter.axisValueLabel;
|
|
127
|
+
}
|
|
128
|
+
return '';
|
|
129
|
+
}
|
|
130
|
+
function formatCompactAxisValue(value, field) {
|
|
131
|
+
const numericValue = typeof value === 'number' ? value : Number(value);
|
|
132
|
+
if (!Number.isFinite(numericValue)) {
|
|
133
|
+
return formatFieldValue(value, field);
|
|
134
|
+
}
|
|
135
|
+
const compactNumber = new Intl.NumberFormat('en-US', {
|
|
136
|
+
notation: 'compact',
|
|
137
|
+
maximumFractionDigits: 1,
|
|
138
|
+
});
|
|
139
|
+
const isPercent = field?.formatHint === 'percent' || field?.semanticType === 'percentage';
|
|
140
|
+
if (isPercent && isUnitConfirmed(field)) {
|
|
141
|
+
const normalizedValue = numericValue <= 1 && numericValue >= 0 ? numericValue * 100 : numericValue;
|
|
142
|
+
return `${compactNumber.format(normalizedValue)}%`;
|
|
143
|
+
}
|
|
144
|
+
if (field?.semanticType === 'currency' && isUnitConfirmed(field)) {
|
|
145
|
+
const currencyHint = field.formatHint ?? 'USD';
|
|
146
|
+
if (currencyHint === 'USD') {
|
|
147
|
+
return new Intl.NumberFormat('en-US', {
|
|
148
|
+
style: 'currency',
|
|
149
|
+
currency: currencyHint,
|
|
150
|
+
notation: 'compact',
|
|
151
|
+
maximumFractionDigits: 1,
|
|
152
|
+
}).format(numericValue);
|
|
153
|
+
}
|
|
154
|
+
return `${compactNumber.format(numericValue)} ${currencyHint}`;
|
|
155
|
+
}
|
|
156
|
+
const unit = fieldUnit(field);
|
|
157
|
+
const formattedValue = Math.abs(numericValue) < 1
|
|
158
|
+
? String(Number(numericValue.toPrecision(2)))
|
|
159
|
+
: compactNumber.format(numericValue);
|
|
160
|
+
return unit === undefined ? formattedValue : `${formattedValue} ${unit}`;
|
|
161
|
+
}
|
|
162
|
+
function formatTooltipValue(value, field) {
|
|
163
|
+
const formattedValue = formatFieldValue(value, field);
|
|
164
|
+
if (field === undefined ||
|
|
165
|
+
field.semanticType === 'currency' ||
|
|
166
|
+
field.semanticType === 'percentage' ||
|
|
167
|
+
field.formatHint === 'percent') {
|
|
168
|
+
return formattedValue;
|
|
169
|
+
}
|
|
170
|
+
const unit = fieldUnit(field);
|
|
171
|
+
const numericValue = typeof value === 'number' ? value : Number(value);
|
|
172
|
+
return unit !== undefined && Number.isFinite(numericValue)
|
|
173
|
+
? `${formattedValue} ${unit}`
|
|
174
|
+
: formattedValue;
|
|
175
|
+
}
|
|
176
|
+
function fieldUnit(field) {
|
|
177
|
+
if (field === undefined) {
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
const isMoneyOrPercent = field.semanticType === 'currency' || field.semanticType === 'percentage';
|
|
181
|
+
if (isMoneyOrPercent || field.formatHint === 'percent') {
|
|
182
|
+
return isUnitConfirmed(field) ? describeConfirmedUnit(field) : undefined;
|
|
183
|
+
}
|
|
184
|
+
if (field.formatHint !== undefined && field.formatHint !== 'iso-date') {
|
|
185
|
+
return field.formatHint;
|
|
186
|
+
}
|
|
187
|
+
return /_ms$/i.test(field.key) ? 'ms' : undefined;
|
|
188
|
+
}
|
|
189
|
+
function describeConfirmedUnit(field) {
|
|
190
|
+
if (field.semanticType === 'currency') {
|
|
191
|
+
return field.formatHint ?? 'USD';
|
|
192
|
+
}
|
|
193
|
+
return '%';
|
|
194
|
+
}
|
|
195
|
+
function unitKey(field) {
|
|
196
|
+
const semanticType = field?.semanticType ?? 'unknown';
|
|
197
|
+
return `${semanticType}:${fieldUnit(field) ?? 'unitless'}`;
|
|
198
|
+
}
|
|
199
|
+
function escapeTooltipText(text) {
|
|
200
|
+
const entities = {
|
|
201
|
+
'&': '&',
|
|
202
|
+
'<': '<',
|
|
203
|
+
'>': '>',
|
|
204
|
+
'"': '"',
|
|
205
|
+
"'": ''',
|
|
206
|
+
};
|
|
207
|
+
return text.replace(/[&<>"']/g, (character) => entities[character] ?? character);
|
|
208
|
+
}
|
|
209
|
+
//# sourceMappingURL=chart-value-axes.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type FieldDescriptor } from '@schemacast/core';
|
|
2
|
+
export type SortDirection = 'asc' | 'desc';
|
|
3
|
+
export declare function shallowCopyRows(rows: readonly Record<string, unknown>[]): Record<string, unknown>[];
|
|
4
|
+
export declare function resolveTableColumns(columnKeys: readonly string[] | undefined, fields: readonly FieldDescriptor[]): string[];
|
|
5
|
+
export declare function filterRows(rows: readonly Record<string, unknown>[], columns: readonly string[], fields: readonly FieldDescriptor[], searchQuery: string): Record<string, unknown>[];
|
|
6
|
+
export declare function sortRows(rows: readonly Record<string, unknown>[], sortKey: string | undefined, sortDirection: SortDirection, fields: readonly FieldDescriptor[]): Record<string, unknown>[];
|
|
7
|
+
export declare function paginateRows<T>(rows: readonly T[], pageIndex: number, pageSize: number): readonly T[];
|
|
8
|
+
//# sourceMappingURL=data-table-rows.d.ts.map
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { parseNumericValue } from '@schemacast/core';
|
|
2
|
+
import { formatFieldValue } from '../format-field-value.js';
|
|
3
|
+
import { fieldByKey } from '../field-by-key.js';
|
|
4
|
+
export function shallowCopyRows(rows) {
|
|
5
|
+
return rows.map((row) => ({ ...row }));
|
|
6
|
+
}
|
|
7
|
+
export function resolveTableColumns(columnKeys, fields) {
|
|
8
|
+
if (columnKeys !== undefined && columnKeys.length > 0) {
|
|
9
|
+
return [...columnKeys];
|
|
10
|
+
}
|
|
11
|
+
return fields.filter((field) => field.semanticType !== 'nested').map((field) => field.key);
|
|
12
|
+
}
|
|
13
|
+
export function filterRows(rows, columns, fields, searchQuery) {
|
|
14
|
+
const normalized = searchQuery.trim().toLowerCase();
|
|
15
|
+
const copied = shallowCopyRows(rows);
|
|
16
|
+
if (normalized.length === 0) {
|
|
17
|
+
return copied;
|
|
18
|
+
}
|
|
19
|
+
return copied.filter((row) => columns.some((column) => {
|
|
20
|
+
const text = formatFieldValue(row[column], fieldByKey(fields, column)).toLowerCase();
|
|
21
|
+
return text.includes(normalized);
|
|
22
|
+
}));
|
|
23
|
+
}
|
|
24
|
+
export function sortRows(rows, sortKey, sortDirection, fields) {
|
|
25
|
+
if (sortKey === undefined) {
|
|
26
|
+
return shallowCopyRows(rows);
|
|
27
|
+
}
|
|
28
|
+
const sorted = shallowCopyRows(rows);
|
|
29
|
+
const field = fieldByKey(fields, sortKey);
|
|
30
|
+
sorted.sort((left, right) => compareRowValues(left[sortKey], right[sortKey], field));
|
|
31
|
+
if (sortDirection === 'desc') {
|
|
32
|
+
sorted.reverse();
|
|
33
|
+
}
|
|
34
|
+
return sorted;
|
|
35
|
+
}
|
|
36
|
+
function compareRowValues(left, right, field) {
|
|
37
|
+
const leftText = formatFieldValue(left, field);
|
|
38
|
+
const rightText = formatFieldValue(right, field);
|
|
39
|
+
const leftNumber = parseNumericValue(left, field?.semanticType);
|
|
40
|
+
const rightNumber = parseNumericValue(right, field?.semanticType);
|
|
41
|
+
if (leftNumber !== null && rightNumber !== null) {
|
|
42
|
+
return leftNumber - rightNumber;
|
|
43
|
+
}
|
|
44
|
+
return leftText.localeCompare(rightText);
|
|
45
|
+
}
|
|
46
|
+
export function paginateRows(rows, pageIndex, pageSize) {
|
|
47
|
+
const start = pageIndex * pageSize;
|
|
48
|
+
return rows.slice(start, start + pageSize);
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=data-table-rows.js.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { FieldBindings, VisualSpec } from '@schemacast/core';
|
|
2
|
+
import type { ReactElement } from 'react';
|
|
3
|
+
export interface DataTableViewProps {
|
|
4
|
+
readonly spec: VisualSpec;
|
|
5
|
+
readonly bindings: FieldBindings;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Sortable, filterable, paginated `data-table` over `VisualSpec.data`.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```tsx
|
|
12
|
+
* <DataTableView spec={spec} bindings={spec.primaryView.bindings} />
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare function DataTableView({ spec, bindings }: DataTableViewProps): ReactElement;
|
|
16
|
+
//# sourceMappingURL=data-table-view.d.ts.map
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useMemo, useState } from 'react';
|
|
3
|
+
import { fieldByKey } from '../field-by-key.js';
|
|
4
|
+
import { formatFieldValue } from '../format-field-value.js';
|
|
5
|
+
import { DATA_TABLE_PAGE_SIZE } from './data-table-constants.js';
|
|
6
|
+
import { filterRows, paginateRows, resolveTableColumns, sortRows, } from './data-table-rows.js';
|
|
7
|
+
function isQuantitativeField(field) {
|
|
8
|
+
return (field?.semanticType === 'number' ||
|
|
9
|
+
field?.semanticType === 'currency' ||
|
|
10
|
+
field?.semanticType === 'percentage');
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Sortable, filterable, paginated `data-table` over `VisualSpec.data`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```tsx
|
|
17
|
+
* <DataTableView spec={spec} bindings={spec.primaryView.bindings} />
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export function DataTableView({ spec, bindings }) {
|
|
21
|
+
const columns = resolveTableColumns(bindings.columns, spec.fields);
|
|
22
|
+
const [searchQuery, setSearchQuery] = useState('');
|
|
23
|
+
const [sortKey, setSortKey] = useState(undefined);
|
|
24
|
+
const [sortDirection, setSortDirection] = useState('asc');
|
|
25
|
+
const [pageIndex, setPageIndex] = useState(0);
|
|
26
|
+
const displayRows = useMemo(() => {
|
|
27
|
+
const filtered = filterRows(spec.data, columns, spec.fields, searchQuery);
|
|
28
|
+
const sorted = sortRows(filtered, sortKey, sortDirection, spec.fields);
|
|
29
|
+
return sorted;
|
|
30
|
+
}, [columns, searchQuery, sortDirection, sortKey, spec.data, spec.fields]);
|
|
31
|
+
const pageCount = Math.max(1, Math.ceil(displayRows.length / DATA_TABLE_PAGE_SIZE));
|
|
32
|
+
const currentPageIndex = Math.min(pageIndex, pageCount - 1);
|
|
33
|
+
const pageRows = useMemo(() => paginateRows(displayRows, currentPageIndex, DATA_TABLE_PAGE_SIZE), [currentPageIndex, displayRows]);
|
|
34
|
+
const canGoNext = currentPageIndex < pageCount - 1;
|
|
35
|
+
const canGoPrev = currentPageIndex > 0;
|
|
36
|
+
const showStreamingEmpty = spec.isStreaming && displayRows.length === 0;
|
|
37
|
+
const showPagination = pageCount > 1;
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
if (pageIndex !== currentPageIndex) {
|
|
40
|
+
setPageIndex(currentPageIndex);
|
|
41
|
+
}
|
|
42
|
+
}, [currentPageIndex, pageIndex]);
|
|
43
|
+
const handleSort = (columnKey) => {
|
|
44
|
+
if (sortKey === columnKey) {
|
|
45
|
+
setSortDirection((direction) => (direction === 'asc' ? 'desc' : 'asc'));
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
setSortKey(columnKey);
|
|
49
|
+
setSortDirection('asc');
|
|
50
|
+
setPageIndex(0);
|
|
51
|
+
};
|
|
52
|
+
return (_jsxs("div", { className: "schemacast-data-table-view", "data-testid": "data-table-view", children: [_jsx("div", { className: "schemacast-data-table-toolbar", children: _jsxs("label", { className: "schemacast-data-table-search", children: [_jsx("span", { children: "Search" }), _jsx("input", { type: "search", className: "schemacast-data-table-search-input", "data-testid": "data-table-search", placeholder: "Search rows...", value: searchQuery, onChange: (event) => {
|
|
53
|
+
setSearchQuery(event.target.value);
|
|
54
|
+
setPageIndex(0);
|
|
55
|
+
} })] }) }), showStreamingEmpty ? (_jsx("p", { "data-testid": "data-table-streaming-empty", children: "Preparing table rows from stream\u2026" })) : null, _jsx("div", { className: "schemacast-data-table-scroll", children: _jsxs("table", { className: "schemacast-data-table", children: [_jsx("thead", { children: _jsx("tr", { children: columns.map((columnKey) => {
|
|
56
|
+
const field = fieldByKey(spec.fields, columnKey);
|
|
57
|
+
const label = field?.label ?? columnKey;
|
|
58
|
+
const isSorted = sortKey === columnKey;
|
|
59
|
+
const isQuantitative = isQuantitativeField(field);
|
|
60
|
+
return (_jsx("th", { className: isQuantitative
|
|
61
|
+
? 'schemacast-data-table-header schemacast-data-table-header--numeric'
|
|
62
|
+
: 'schemacast-data-table-header', scope: "col", children: _jsxs("button", { type: "button", className: "schemacast-data-table-sort-button", "data-testid": `data-table-sort-${columnKey}`, "aria-sort": isSorted ? (sortDirection === 'asc' ? 'ascending' : 'descending') : 'none', onClick: () => {
|
|
63
|
+
handleSort(columnKey);
|
|
64
|
+
}, children: [label, isSorted ? (_jsx("span", { className: "schemacast-data-table-sort-indicator", "data-testid": `data-table-sort-indicator-${columnKey}`, "aria-hidden": "true", children: sortDirection === 'asc' ? '↑' : '↓' })) : null] }) }, columnKey));
|
|
65
|
+
}) }) }), _jsx("tbody", { children: pageRows.map((row, rowIndex) => (_jsx("tr", { className: "schemacast-data-table-row", "data-testid": "data-table-row", children: columns.map((columnKey) => {
|
|
66
|
+
const field = fieldByKey(spec.fields, columnKey);
|
|
67
|
+
return (_jsx("td", { className: isQuantitativeField(field)
|
|
68
|
+
? 'schemacast-data-table-cell schemacast-data-table-cell--numeric'
|
|
69
|
+
: 'schemacast-data-table-cell', children: formatFieldValue(row[columnKey], field) }, columnKey));
|
|
70
|
+
}) }, `row-${currentPageIndex}-${rowIndex}`))) })] }) }), !showStreamingEmpty && displayRows.length === 0 ? (_jsx("p", { "data-testid": "data-table-no-matches", children: "No rows match the current filter." })) : null, showPagination ? (_jsxs("div", { className: "schemacast-data-table-pagination", "data-testid": "data-table-pagination", children: [_jsxs("span", { children: ["Page ", currentPageIndex + 1, " of ", pageCount] }), _jsxs("div", { className: "schemacast-data-table-pagination-controls", children: [_jsx("button", { type: "button", className: "schemacast-data-table-pagination-button", "data-testid": "data-table-page-prev", disabled: !canGoPrev, onClick: () => {
|
|
71
|
+
setPageIndex(currentPageIndex - 1);
|
|
72
|
+
}, children: "Previous" }), _jsx("button", { type: "button", className: "schemacast-data-table-pagination-button", "data-testid": "data-table-page-next", disabled: !canGoNext, onClick: () => {
|
|
73
|
+
setPageIndex(currentPageIndex + 1);
|
|
74
|
+
}, children: "Next" })] })] })) : null] }));
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=data-table-view.js.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { FieldBindings, VisualSpec } from '@schemacast/core';
|
|
2
|
+
import type { ReactElement } from 'react';
|
|
3
|
+
export interface DetailCardViewProps {
|
|
4
|
+
readonly spec: VisualSpec;
|
|
5
|
+
readonly bindings: FieldBindings;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Renders a single-record `detail-card` from `VisualSpec.data[0]` and field bindings.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```tsx
|
|
12
|
+
* <DetailCardView spec={spec} bindings={spec.primaryView.bindings} />
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare function DetailCardView({ spec, bindings }: DetailCardViewProps): ReactElement;
|
|
16
|
+
//# sourceMappingURL=detail-card-view.d.ts.map
|