@schemacast/react 0.5.0 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/views/bar-chart-options.js +16 -12
- package/dist/views/chart-series-data.d.ts +31 -1
- package/dist/views/chart-series-data.js +69 -4
- package/dist/views/chart-value-axes.d.ts +5 -1
- package/dist/views/chart-value-axes.js +8 -3
- package/dist/views/line-chart-options.js +9 -5
- package/package.json +2 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { buildLongFormatLine } from '@schemacast/core';
|
|
2
|
-
import { buildCategoryAxis, numericSeries, seriesLabel } from './chart-series-data.js';
|
|
2
|
+
import { buildCategoryAxis, buildSeriesLegend, numericSeries, seriesLabel, shouldOmitSingleSeriesYAxisName, } from './chart-series-data.js';
|
|
3
3
|
import { buildValueAxis, buildValueAxisPlan, formatChartTooltip } from './chart-value-axes.js';
|
|
4
4
|
/**
|
|
5
5
|
* Resolves the row array plotted by a `bar-chart` view, preferring view-scoped
|
|
@@ -50,14 +50,16 @@ export function buildBarChartOption(spec, bindings, viewData) {
|
|
|
50
50
|
return buildGroupedBarOption(spec, xKey, firstYKey, bindings.groupBy);
|
|
51
51
|
}
|
|
52
52
|
const rows = resolveBarChartRows(spec, bindings, viewData);
|
|
53
|
+
const seriesNames = yKeys.map((key) => seriesLabel(spec.fields, key));
|
|
54
|
+
const omitYAxisName = shouldOmitSingleSeriesYAxisName(seriesNames, undefined);
|
|
53
55
|
const axisPlan = buildValueAxisPlan(spec, yKeys, rows);
|
|
54
|
-
const axes = axisPlan.axes.map((axis) => buildValueAxis(spec.fields, axis));
|
|
56
|
+
const axes = axisPlan.axes.map((axis) => buildValueAxis(spec.fields, axis, { omitName: omitYAxisName }));
|
|
55
57
|
return {
|
|
56
58
|
tooltip: {
|
|
57
59
|
trigger: 'axis',
|
|
58
60
|
formatter: (parameters) => formatChartTooltip(parameters, spec.fields, yKeys),
|
|
59
61
|
},
|
|
60
|
-
legend:
|
|
62
|
+
legend: buildSeriesLegend(seriesNames),
|
|
61
63
|
grid: {
|
|
62
64
|
left: axes.length > 1 ? 104 : 88,
|
|
63
65
|
right: axes.length > 1 ? 104 : 24,
|
|
@@ -78,17 +80,19 @@ export function buildBarChartOption(spec, bindings, viewData) {
|
|
|
78
80
|
*/
|
|
79
81
|
function buildGroupedBarOption(spec, xKey, yKey, groupKey) {
|
|
80
82
|
const line = buildLongFormatLine(spec.data, { xKey, groupKey, yKey }, spec.fields);
|
|
81
|
-
const
|
|
83
|
+
const seriesNames = line.series.map((series) => series.name);
|
|
84
|
+
const omitYAxisName = shouldOmitSingleSeriesYAxisName(seriesNames, undefined);
|
|
85
|
+
const axisPlan = buildValueAxisPlan(spec, [yKey], spec.data);
|
|
86
|
+
const axes = axisPlan.axes.map((axis) => buildValueAxis(spec.fields, axis, { omitName: omitYAxisName }));
|
|
82
87
|
return {
|
|
83
|
-
tooltip: {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
xAxis: {
|
|
87
|
-
type: 'category',
|
|
88
|
-
data: xLabels,
|
|
89
|
-
axisLabel: { interval: 0, rotate: 35 },
|
|
88
|
+
tooltip: {
|
|
89
|
+
trigger: 'axis',
|
|
90
|
+
formatter: (parameters) => formatChartTooltip(parameters, spec.fields, line.series.map(() => yKey)),
|
|
90
91
|
},
|
|
91
|
-
|
|
92
|
+
legend: buildSeriesLegend(seriesNames),
|
|
93
|
+
grid: { left: 88, right: 24, containLabel: true },
|
|
94
|
+
xAxis: buildCategoryAxis(line.xValues.map((value) => ({ [xKey]: value })), xKey, spec.fields),
|
|
95
|
+
yAxis: axes[0],
|
|
92
96
|
series: line.series.map((series) => ({
|
|
93
97
|
name: series.name,
|
|
94
98
|
type: 'bar',
|
|
@@ -1,8 +1,38 @@
|
|
|
1
1
|
import { type FieldDescriptor, type SemanticType } from '@schemacast/core';
|
|
2
|
-
import type { XAXisComponentOption } from 'echarts';
|
|
2
|
+
import type { LegendComponentOption, XAXisComponentOption } from 'echarts';
|
|
3
|
+
/**
|
|
4
|
+
* Rotate category ticks when `(label count) × (longest truncated label length)` exceeds this
|
|
5
|
+
* budget. Below the budget, labels stay upright (`rotate: 0`) with `hideOverlap: true`.
|
|
6
|
+
*/
|
|
7
|
+
export declare const CATEGORY_AXIS_ROTATE_DENSITY_BUDGET = 24;
|
|
8
|
+
/** Degrees used when {@link CATEGORY_AXIS_ROTATE_DENSITY_BUDGET} is exceeded. */
|
|
9
|
+
export declare const CATEGORY_AXIS_ROTATE_DEGREES = 35;
|
|
3
10
|
export declare function readNumericSeriesValue(value: unknown, semanticType?: SemanticType): number | null;
|
|
4
11
|
export declare function categoryValues(rows: readonly Record<string, unknown>[], categoryKey: string, fields: readonly FieldDescriptor[]): string[];
|
|
12
|
+
/**
|
|
13
|
+
* True when category labels are dense enough that unrotated ticks would collide.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* shouldRotateCategoryAxisLabels(['North American…', 'Europe']); // true
|
|
17
|
+
*/
|
|
18
|
+
export declare function shouldRotateCategoryAxisLabels(labels: readonly string[]): boolean;
|
|
5
19
|
export declare function buildCategoryAxis(rows: readonly Record<string, unknown>[], categoryKey: string, fields: readonly FieldDescriptor[]): XAXisComponentOption;
|
|
20
|
+
/**
|
|
21
|
+
* Legend chrome shared by line and bar charts: hidden for a single series, shown for two or more.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* buildSeriesLegend(['Revenue']); // { show: false }
|
|
25
|
+
*/
|
|
26
|
+
export declare function buildSeriesLegend(seriesNames: readonly string[]): LegendComponentOption;
|
|
27
|
+
/**
|
|
28
|
+
* True when a single-series y-axis name would duplicate a chart title that already names the metric.
|
|
29
|
+
* A lone series still keeps its upright y-axis name when there is no such title.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* shouldOmitSingleSeriesYAxisName(['Requests'], 'Requests'); // true
|
|
33
|
+
* shouldOmitSingleSeriesYAxisName(['Requests'], undefined); // false
|
|
34
|
+
*/
|
|
35
|
+
export declare function shouldOmitSingleSeriesYAxisName(seriesNames: readonly string[], chartTitle: string | undefined): boolean;
|
|
6
36
|
/**
|
|
7
37
|
* True when the x field is a plain number (a step, epoch or version), so the chart needs a value
|
|
8
38
|
* axis. Dates and categories keep the category axis.
|
|
@@ -2,24 +2,87 @@ import { parseNumericValue } from '@schemacast/core';
|
|
|
2
2
|
import { fieldByKey } from '../field-by-key.js';
|
|
3
3
|
import { formatFieldValue } from '../format-field-value.js';
|
|
4
4
|
const MAX_CATEGORY_AXIS_LABEL_LENGTH = 16;
|
|
5
|
+
/**
|
|
6
|
+
* Rotate category ticks when `(label count) × (longest truncated label length)` exceeds this
|
|
7
|
+
* budget. Below the budget, labels stay upright (`rotate: 0`) with `hideOverlap: true`.
|
|
8
|
+
*/
|
|
9
|
+
export const CATEGORY_AXIS_ROTATE_DENSITY_BUDGET = 24;
|
|
10
|
+
/** Degrees used when {@link CATEGORY_AXIS_ROTATE_DENSITY_BUDGET} is exceeded. */
|
|
11
|
+
export const CATEGORY_AXIS_ROTATE_DEGREES = 35;
|
|
5
12
|
export function readNumericSeriesValue(value, semanticType) {
|
|
6
13
|
return parseNumericValue(value, semanticType);
|
|
7
14
|
}
|
|
8
15
|
export function categoryValues(rows, categoryKey, fields) {
|
|
9
16
|
return rows.map((row) => formatFieldValue(row[categoryKey], fieldByKey(fields, categoryKey)));
|
|
10
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* True when category labels are dense enough that unrotated ticks would collide.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* shouldRotateCategoryAxisLabels(['North American…', 'Europe']); // true
|
|
23
|
+
*/
|
|
24
|
+
export function shouldRotateCategoryAxisLabels(labels) {
|
|
25
|
+
if (labels.length === 0) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
let longest = 0;
|
|
29
|
+
for (const label of labels) {
|
|
30
|
+
const length = Array.from(truncateCategoryAxisLabel(label)).length;
|
|
31
|
+
if (length > longest) {
|
|
32
|
+
longest = length;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return labels.length * longest > CATEGORY_AXIS_ROTATE_DENSITY_BUDGET;
|
|
36
|
+
}
|
|
11
37
|
export function buildCategoryAxis(rows, categoryKey, fields) {
|
|
38
|
+
const data = categoryValues(rows, categoryKey, fields);
|
|
39
|
+
const rotate = shouldRotateCategoryAxisLabels(data) ? CATEGORY_AXIS_ROTATE_DEGREES : 0;
|
|
12
40
|
return {
|
|
13
41
|
type: 'category',
|
|
14
|
-
data
|
|
42
|
+
data,
|
|
15
43
|
axisLabel: {
|
|
16
44
|
interval: 0,
|
|
17
|
-
hideOverlap:
|
|
18
|
-
rotate
|
|
45
|
+
hideOverlap: rotate === 0,
|
|
46
|
+
rotate,
|
|
19
47
|
formatter: (value) => truncateCategoryAxisLabel(value),
|
|
20
48
|
},
|
|
21
49
|
};
|
|
22
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* Legend chrome shared by line and bar charts: hidden for a single series, shown for two or more.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* buildSeriesLegend(['Revenue']); // { show: false }
|
|
56
|
+
*/
|
|
57
|
+
export function buildSeriesLegend(seriesNames) {
|
|
58
|
+
if (seriesNames.length <= 1) {
|
|
59
|
+
return { show: false };
|
|
60
|
+
}
|
|
61
|
+
return { show: true, data: [...seriesNames] };
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* True when a single-series y-axis name would duplicate a chart title that already names the metric.
|
|
65
|
+
* A lone series still keeps its upright y-axis name when there is no such title.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* shouldOmitSingleSeriesYAxisName(['Requests'], 'Requests'); // true
|
|
69
|
+
* shouldOmitSingleSeriesYAxisName(['Requests'], undefined); // false
|
|
70
|
+
*/
|
|
71
|
+
export function shouldOmitSingleSeriesYAxisName(seriesNames, chartTitle) {
|
|
72
|
+
if (seriesNames.length !== 1 || chartTitle === undefined) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
const seriesName = seriesNames[0];
|
|
76
|
+
if (seriesName === undefined) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
const normalizedTitle = chartTitle.trim().toLowerCase();
|
|
80
|
+
const normalizedSeries = seriesName.trim().toLowerCase();
|
|
81
|
+
if (normalizedTitle.length === 0 || normalizedSeries.length === 0) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
return normalizedTitle === normalizedSeries || normalizedTitle.includes(normalizedSeries);
|
|
85
|
+
}
|
|
23
86
|
/**
|
|
24
87
|
* True when the x field is a plain number (a step, epoch or version), so the chart needs a value
|
|
25
88
|
* axis. Dates and categories keep the category axis.
|
|
@@ -37,12 +100,14 @@ export function isNumericAxisField(field) {
|
|
|
37
100
|
* const axis = buildNumericXAxis(spec.fields, 'epoch');
|
|
38
101
|
*/
|
|
39
102
|
export function buildNumericXAxis(fields, xKey) {
|
|
103
|
+
const name = fieldByKey(fields, xKey)?.label ?? xKey;
|
|
40
104
|
return {
|
|
41
105
|
type: 'value',
|
|
42
106
|
scale: true,
|
|
43
|
-
name
|
|
107
|
+
name,
|
|
44
108
|
nameLocation: 'middle',
|
|
45
109
|
nameGap: 28,
|
|
110
|
+
nameRotate: 0,
|
|
46
111
|
};
|
|
47
112
|
}
|
|
48
113
|
/**
|
|
@@ -22,13 +22,17 @@ export interface ValueAxisPlan {
|
|
|
22
22
|
export declare function buildValueAxisPlan(spec: VisualSpec, yKeys: readonly string[], rows?: readonly Record<string, unknown>[], isLineChart?: boolean): ValueAxisPlan;
|
|
23
23
|
/**
|
|
24
24
|
* Builds a themed-ready, unit-aware y-axis shared by bar and line charts.
|
|
25
|
+
* Titles stay upright (`nameRotate: 0`). Short 1–2 character names are never rotated either.
|
|
26
|
+
* Pass `omitName` when a chart title already shows the metric (a lone series otherwise keeps its name).
|
|
25
27
|
*
|
|
26
28
|
* @example
|
|
27
29
|
* ```ts
|
|
28
30
|
* const axis = buildValueAxis(spec.fields, { fieldKey: 'error_rate', position: 'left' });
|
|
29
31
|
* ```
|
|
30
32
|
*/
|
|
31
|
-
export declare function buildValueAxis(fields: readonly FieldDescriptor[], definition: ValueAxisDefinition
|
|
33
|
+
export declare function buildValueAxis(fields: readonly FieldDescriptor[], definition: ValueAxisDefinition, options?: {
|
|
34
|
+
readonly omitName?: boolean;
|
|
35
|
+
}): YAXisComponentOption;
|
|
32
36
|
/**
|
|
33
37
|
* Formats a chart tooltip using full precision and field units.
|
|
34
38
|
*
|
|
@@ -62,21 +62,26 @@ export function buildValueAxisPlan(spec, yKeys, rows = spec.data, isLineChart =
|
|
|
62
62
|
}
|
|
63
63
|
/**
|
|
64
64
|
* Builds a themed-ready, unit-aware y-axis shared by bar and line charts.
|
|
65
|
+
* Titles stay upright (`nameRotate: 0`). Short 1–2 character names are never rotated either.
|
|
66
|
+
* Pass `omitName` when a chart title already shows the metric (a lone series otherwise keeps its name).
|
|
65
67
|
*
|
|
66
68
|
* @example
|
|
67
69
|
* ```ts
|
|
68
70
|
* const axis = buildValueAxis(spec.fields, { fieldKey: 'error_rate', position: 'left' });
|
|
69
71
|
* ```
|
|
70
72
|
*/
|
|
71
|
-
export function buildValueAxis(fields, definition) {
|
|
73
|
+
export function buildValueAxis(fields, definition, options = {}) {
|
|
72
74
|
const field = fieldByKey(fields, definition.fieldKey);
|
|
73
75
|
const label = seriesLabel(fields, definition.fieldKey);
|
|
74
76
|
const unit = fieldUnit(field);
|
|
77
|
+
const name = unit === undefined ? label : appendAxisUnit(label, unit);
|
|
78
|
+
const omitName = options.omitName === true;
|
|
75
79
|
return {
|
|
76
80
|
type: 'value',
|
|
77
|
-
|
|
81
|
+
...(omitName ? {} : { name }),
|
|
78
82
|
nameLocation: 'middle',
|
|
79
|
-
nameGap: 56,
|
|
83
|
+
nameGap: omitName ? 36 : 56,
|
|
84
|
+
nameRotate: 0,
|
|
80
85
|
position: definition.position,
|
|
81
86
|
splitNumber: 5,
|
|
82
87
|
alignTicks: definition.position === 'right',
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { buildLongFormatLine, LINE_SAMPLING_MIN_POINTS } from '@schemacast/core';
|
|
2
2
|
export { LINE_SAMPLING_MIN_POINTS };
|
|
3
3
|
import { fieldByKey } from '../field-by-key.js';
|
|
4
|
-
import { buildCategoryAxis, buildNumericXAxis, isNumericAxisField, numericSeries, numericXSeries, readNumericSeriesValue, seriesLabel, } from './chart-series-data.js';
|
|
4
|
+
import { buildCategoryAxis, buildNumericXAxis, buildSeriesLegend, isNumericAxisField, numericSeries, numericXSeries, readNumericSeriesValue, seriesLabel, shouldOmitSingleSeriesYAxisName, } from './chart-series-data.js';
|
|
5
5
|
import { buildValueAxis, buildValueAxisPlan, formatChartTooltip } from './chart-value-axes.js';
|
|
6
6
|
/**
|
|
7
7
|
* Builds a line-chart option with shared unit-aware value-axis behavior. A numeric x (a step,
|
|
@@ -23,14 +23,16 @@ export function buildLineChartOption(spec, bindings) {
|
|
|
23
23
|
return buildLongFormatLineOption(spec, xKey, yKeys[0], bindings.groupBy);
|
|
24
24
|
}
|
|
25
25
|
const hasNumericX = isNumericAxisField(fieldByKey(spec.fields, xKey));
|
|
26
|
+
const seriesNames = yKeys.map((key) => seriesLabel(spec.fields, key));
|
|
27
|
+
const omitYAxisName = shouldOmitSingleSeriesYAxisName(seriesNames, undefined);
|
|
26
28
|
const axisPlan = buildValueAxisPlan(spec, yKeys, spec.data, true);
|
|
27
|
-
const axes = axisPlan.axes.map((axis) => buildValueAxis(spec.fields, axis));
|
|
29
|
+
const axes = axisPlan.axes.map((axis) => buildValueAxis(spec.fields, axis, { omitName: omitYAxisName }));
|
|
28
30
|
return {
|
|
29
31
|
tooltip: {
|
|
30
32
|
trigger: 'axis',
|
|
31
33
|
formatter: (parameters) => formatChartTooltip(parameters, spec.fields, yKeys),
|
|
32
34
|
},
|
|
33
|
-
legend:
|
|
35
|
+
legend: buildSeriesLegend(seriesNames),
|
|
34
36
|
grid: {
|
|
35
37
|
left: axes.length > 1 ? 104 : 88,
|
|
36
38
|
right: axes.length > 1 ? 104 : 24,
|
|
@@ -63,8 +65,10 @@ function buildLongFormatLineOption(spec, xKey, yKey, groupKey) {
|
|
|
63
65
|
const line = buildLongFormatLine(spec.data, { xKey, groupKey, yKey }, spec.fields);
|
|
64
66
|
const xField = fieldByKey(spec.fields, xKey);
|
|
65
67
|
const hasNumericX = isNumericAxisField(xField);
|
|
68
|
+
const seriesNames = line.series.map((series) => series.name);
|
|
69
|
+
const omitYAxisName = shouldOmitSingleSeriesYAxisName(seriesNames, undefined);
|
|
66
70
|
const axisPlan = buildValueAxisPlan(spec, [yKey], spec.data, true);
|
|
67
|
-
const axes = axisPlan.axes.map((axis) => buildValueAxis(spec.fields, axis));
|
|
71
|
+
const axes = axisPlan.axes.map((axis) => buildValueAxis(spec.fields, axis, { omitName: omitYAxisName }));
|
|
68
72
|
const seriesYKeys = line.series.map(() => yKey);
|
|
69
73
|
const xNumbers = line.xValues.map((value) => readNumericSeriesValue(value, xField?.semanticType));
|
|
70
74
|
return {
|
|
@@ -72,7 +76,7 @@ function buildLongFormatLineOption(spec, xKey, yKey, groupKey) {
|
|
|
72
76
|
trigger: 'axis',
|
|
73
77
|
formatter: (parameters) => formatChartTooltip(parameters, spec.fields, seriesYKeys),
|
|
74
78
|
},
|
|
75
|
-
legend:
|
|
79
|
+
legend: buildSeriesLegend(seriesNames),
|
|
76
80
|
grid: { left: 88, right: 24, containLabel: true },
|
|
77
81
|
xAxis: hasNumericX
|
|
78
82
|
? buildNumericXAxis(spec.fields, xKey)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@schemacast/react",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "React renderer for schemacast VisualSpec output.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"!dist/**/*.map"
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@schemacast/core": "0.5.
|
|
36
|
+
"@schemacast/core": "0.5.2",
|
|
37
37
|
"echarts": "^5.6.0"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|