@outsidedata/dolex 0.1.12 → 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/dist/src/analysis/rules.d.ts.map +1 -1
- package/dist/src/analysis/rules.js +8 -2
- package/dist/src/analysis/rules.js.map +1 -1
- package/dist/src/connectors/csv.d.ts.map +1 -1
- package/dist/src/connectors/csv.js +31 -1
- package/dist/src/connectors/csv.js.map +1 -1
- package/dist/src/connectors/dsl-compiler.d.ts +4 -0
- package/dist/src/connectors/dsl-compiler.d.ts.map +1 -1
- package/dist/src/connectors/dsl-compiler.js +51 -18
- package/dist/src/connectors/dsl-compiler.js.map +1 -1
- package/dist/src/connectors/dsl-validator.js +6 -1
- package/dist/src/connectors/dsl-validator.js.map +1 -1
- package/dist/src/connectors/js-aggregation.d.ts.map +1 -1
- package/dist/src/connectors/js-aggregation.js +18 -12
- package/dist/src/connectors/js-aggregation.js.map +1 -1
- package/dist/src/mcp/index.js +63 -29
- package/dist/src/mcp/index.js.map +1 -1
- package/dist/src/mcp/spec-store.d.ts +2 -1
- package/dist/src/mcp/spec-store.d.ts.map +1 -1
- package/dist/src/mcp/spec-store.js +4 -2
- package/dist/src/mcp/spec-store.js.map +1 -1
- package/dist/src/mcp/tools/dsl-schemas.d.ts +52 -51
- package/dist/src/mcp/tools/dsl-schemas.d.ts.map +1 -1
- package/dist/src/mcp/tools/dsl-schemas.js +1 -1
- package/dist/src/mcp/tools/dsl-schemas.js.map +1 -1
- package/dist/src/mcp/tools/list-patterns.js +2 -2
- package/dist/src/mcp/tools/list-patterns.js.map +1 -1
- package/dist/src/mcp/tools/query-source.d.ts +16 -16
- package/dist/src/mcp/tools/refine.d.ts +124 -15
- package/dist/src/mcp/tools/refine.d.ts.map +1 -1
- package/dist/src/mcp/tools/refine.js +335 -253
- package/dist/src/mcp/tools/refine.js.map +1 -1
- package/dist/src/mcp/tools/shared.d.ts +15 -2
- package/dist/src/mcp/tools/shared.d.ts.map +1 -1
- package/dist/src/mcp/tools/shared.js +64 -6
- package/dist/src/mcp/tools/shared.js.map +1 -1
- package/dist/src/mcp/tools/visualize-from-source.d.ts +52 -76
- package/dist/src/mcp/tools/visualize-from-source.d.ts.map +1 -1
- package/dist/src/mcp/tools/visualize-from-source.js +20 -5
- package/dist/src/mcp/tools/visualize-from-source.js.map +1 -1
- package/dist/src/mcp/tools/visualize.d.ts +45 -100
- package/dist/src/mcp/tools/visualize.d.ts.map +1 -1
- package/dist/src/mcp/tools/visualize.js +19 -19
- package/dist/src/mcp/tools/visualize.js.map +1 -1
- package/dist/src/patterns/definitions/comparison/grouped-bar.js +5 -5
- package/dist/src/patterns/definitions/comparison/grouped-bar.js.map +1 -1
- package/dist/src/patterns/definitions/composition/waffle.d.ts.map +1 -1
- package/dist/src/patterns/definitions/composition/waffle.js +17 -3
- package/dist/src/patterns/definitions/composition/waffle.js.map +1 -1
- package/dist/src/patterns/definitions/distribution/strip-plot.d.ts.map +1 -1
- package/dist/src/patterns/definitions/distribution/strip-plot.js +12 -3
- package/dist/src/patterns/definitions/distribution/strip-plot.js.map +1 -1
- package/dist/src/patterns/definitions/time/sparkline-grid.js +2 -2
- package/dist/src/patterns/definitions/time/sparkline-grid.js.map +1 -1
- package/dist/src/patterns/selector.d.ts.map +1 -1
- package/dist/src/patterns/selector.js +46 -1
- package/dist/src/patterns/selector.js.map +1 -1
- package/dist/src/renderers/d3/comparison/bar.d.ts.map +1 -1
- package/dist/src/renderers/d3/comparison/bar.js +24 -1
- package/dist/src/renderers/d3/comparison/bar.js.map +1 -1
- package/dist/src/renderers/d3/comparison/grouped-bar.d.ts.map +1 -1
- package/dist/src/renderers/d3/comparison/grouped-bar.js +9 -4
- package/dist/src/renderers/d3/comparison/grouped-bar.js.map +1 -1
- package/dist/src/renderers/d3/composition/donut.d.ts.map +1 -1
- package/dist/src/renderers/d3/composition/donut.js +12 -0
- package/dist/src/renderers/d3/composition/donut.js.map +1 -1
- package/dist/src/renderers/d3/composition/stacked-bar.d.ts.map +1 -1
- package/dist/src/renderers/d3/composition/stacked-bar.js +4 -0
- package/dist/src/renderers/d3/composition/stacked-bar.js.map +1 -1
- package/dist/src/renderers/d3/composition/treemap.js +2 -1
- package/dist/src/renderers/d3/composition/treemap.js.map +1 -1
- package/dist/src/renderers/d3/distribution/beeswarm.d.ts.map +1 -1
- package/dist/src/renderers/d3/distribution/beeswarm.js +8 -0
- package/dist/src/renderers/d3/distribution/beeswarm.js.map +1 -1
- package/dist/src/renderers/d3/distribution/histogram.js +36 -2
- package/dist/src/renderers/d3/distribution/histogram.js.map +1 -1
- package/dist/src/renderers/d3/flow/funnel.d.ts.map +1 -1
- package/dist/src/renderers/d3/flow/funnel.js +4 -0
- package/dist/src/renderers/d3/flow/funnel.js.map +1 -1
- package/dist/src/renderers/d3/flow/sankey-layout.js +20 -3
- package/dist/src/renderers/d3/flow/sankey-layout.js.map +1 -1
- package/dist/src/renderers/d3/heatmap.d.ts.map +1 -1
- package/dist/src/renderers/d3/heatmap.js +18 -3
- package/dist/src/renderers/d3/heatmap.js.map +1 -1
- package/dist/src/renderers/d3/radar.d.ts.map +1 -1
- package/dist/src/renderers/d3/radar.js +3 -1
- package/dist/src/renderers/d3/radar.js.map +1 -1
- package/dist/src/renderers/d3/relationship.d.ts.map +1 -1
- package/dist/src/renderers/d3/relationship.js +29 -7
- package/dist/src/renderers/d3/relationship.js.map +1 -1
- package/dist/src/renderers/d3/shared.js +7 -7
- package/dist/src/renderers/d3/shared.js.map +1 -1
- package/dist/src/renderers/d3/time/area.js +21 -7
- package/dist/src/renderers/d3/time/area.js.map +1 -1
- package/dist/src/renderers/d3/time/calendar-heatmap.js +6 -0
- package/dist/src/renderers/d3/time/calendar-heatmap.js.map +1 -1
- package/dist/src/renderers/d3/time/horizon-chart.d.ts.map +1 -1
- package/dist/src/renderers/d3/time/horizon-chart.js +10 -0
- package/dist/src/renderers/d3/time/horizon-chart.js.map +1 -1
- package/dist/src/renderers/d3/time/line.d.ts.map +1 -1
- package/dist/src/renderers/d3/time/line.js +27 -7
- package/dist/src/renderers/d3/time/line.js.map +1 -1
- package/dist/src/renderers/d3/time/small-multiples.js +8 -0
- package/dist/src/renderers/d3/time/small-multiples.js.map +1 -1
- package/dist/src/renderers/d3/time/sparkline-grid.d.ts.map +1 -1
- package/dist/src/renderers/d3/time/sparkline-grid.js +5 -2
- package/dist/src/renderers/d3/time/sparkline-grid.js.map +1 -1
- package/dist/src/renderers/d3/time/stream-graph.js +6 -0
- package/dist/src/renderers/d3/time/stream-graph.js.map +1 -1
- package/dist/src/renderers/html/_generated/bundles.d.ts +39 -39
- package/dist/src/renderers/html/_generated/bundles.d.ts.map +1 -1
- package/dist/src/renderers/html/_generated/bundles.js +39 -39
- package/dist/src/renderers/html/_generated/bundles.js.map +1 -1
- package/dist/src/renderers/html/template.js +2 -2
- package/dist/src/renderers/html/template.js.map +1 -1
- package/dist/src/types.d.ts +30 -2
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/types.js.map +1 -1
- package/package.json +2 -1
- package/src/analysis/rules.ts +10 -2
- package/src/connectors/csv.ts +31 -1
- package/src/connectors/dsl-compiler.ts +56 -20
- package/src/connectors/dsl-validator.ts +6 -1
- package/src/connectors/js-aggregation.ts +16 -11
- package/src/mcp/index.ts +63 -29
- package/src/mcp/spec-store.ts +5 -1
- package/src/mcp/tools/dsl-schemas.ts +1 -1
- package/src/mcp/tools/list-patterns.ts +2 -2
- package/src/mcp/tools/refine.ts +333 -258
- package/src/mcp/tools/shared.ts +70 -7
- package/src/mcp/tools/visualize-from-source.ts +20 -5
- package/src/mcp/tools/visualize.ts +22 -22
- package/src/patterns/definitions/comparison/grouped-bar.ts +5 -5
- package/src/patterns/definitions/composition/waffle.ts +17 -3
- package/src/patterns/definitions/distribution/strip-plot.ts +12 -3
- package/src/patterns/definitions/time/sparkline-grid.ts +2 -2
- package/src/patterns/selector.ts +47 -1
- package/src/renderers/d3/comparison/bar.ts +24 -1
- package/src/renderers/d3/comparison/grouped-bar.ts +9 -4
- package/src/renderers/d3/composition/donut.ts +14 -0
- package/src/renderers/d3/composition/stacked-bar.ts +5 -0
- package/src/renderers/d3/composition/treemap.ts +2 -1
- package/src/renderers/d3/distribution/beeswarm.ts +12 -0
- package/src/renderers/d3/distribution/histogram.ts +44 -2
- package/src/renderers/d3/flow/funnel.ts +8 -0
- package/src/renderers/d3/flow/sankey-layout.ts +23 -3
- package/src/renderers/d3/heatmap.ts +20 -2
- package/src/renderers/d3/radar.ts +4 -1
- package/src/renderers/d3/relationship.ts +31 -6
- package/src/renderers/d3/shared.ts +7 -7
- package/src/renderers/d3/time/area.ts +22 -7
- package/src/renderers/d3/time/calendar-heatmap.ts +5 -0
- package/src/renderers/d3/time/horizon-chart.ts +11 -0
- package/src/renderers/d3/time/line.ts +24 -9
- package/src/renderers/d3/time/small-multiples.ts +6 -0
- package/src/renderers/d3/time/sparkline-grid.ts +5 -2
- package/src/renderers/d3/time/stream-graph.ts +5 -0
- package/src/renderers/html/_generated/bundles.ts +39 -39
- package/src/renderers/html/template.ts +2 -2
- package/src/types.ts +18 -2
- package/dist/src/mcp/tools/connect-data.d.ts +0 -14
- package/dist/src/mcp/tools/connect-data.d.ts.map +0 -1
- package/dist/src/mcp/tools/connect-data.js +0 -80
- package/dist/src/mcp/tools/connect-data.js.map +0 -1
- package/src/mcp/tools/connect-data.ts +0 -94
package/src/mcp/tools/shared.ts
CHANGED
|
@@ -67,9 +67,23 @@ export function inferColumns(data: Record<string, any>[]): DataColumn[] {
|
|
|
67
67
|
values.every(v => /^\d{4}[-/]/.test(String(v)));
|
|
68
68
|
const isId = /^id$|_id$|Id$/i.test(key) && uniqueValues.length > data.length * 0.5;
|
|
69
69
|
|
|
70
|
+
const YEAR_NAME_PATTERN = /\byear\b|\bcohort\b|\bfiscal\b|\bfy\b|\bsemester\b|\bvintage\b|\bclass_of\b|\bgraduating\b|\bperiod\b|\bseason\b/i;
|
|
71
|
+
|
|
72
|
+
const looksLikeYear = isNumeric &&
|
|
73
|
+
!isDate &&
|
|
74
|
+
YEAR_NAME_PATTERN.test(key) &&
|
|
75
|
+
values.length > 0 &&
|
|
76
|
+
values.every(v => {
|
|
77
|
+
const n = Number(v);
|
|
78
|
+
return Number.isInteger(n) && n >= 1900 && n <= 2100;
|
|
79
|
+
}) &&
|
|
80
|
+
uniqueValues.length <= 50 &&
|
|
81
|
+
uniqueValues.length > 1 &&
|
|
82
|
+
(Math.max(...values.map(Number)) - Math.min(...values.map(Number))) < 200;
|
|
83
|
+
|
|
70
84
|
let type: DataColumn['type'] = 'categorical';
|
|
71
85
|
if (isId) type = 'id';
|
|
72
|
-
else if (isDate) type = 'date';
|
|
86
|
+
else if (isDate || looksLikeYear) type = 'date';
|
|
73
87
|
else if (isNumeric) type = 'numeric';
|
|
74
88
|
|
|
75
89
|
return {
|
|
@@ -87,9 +101,11 @@ export function inferColumns(data: Record<string, any>[]): DataColumn[] {
|
|
|
87
101
|
|
|
88
102
|
export function applyColorPreferences(
|
|
89
103
|
spec: VisualizationSpec,
|
|
90
|
-
prefs?:
|
|
91
|
-
|
|
92
|
-
|
|
104
|
+
prefs?: { palette?: string; highlight?: { values: any[]; color?: string | string[]; mutedColor?: string; mutedOpacity?: number }; colorField?: string },
|
|
105
|
+
data?: Record<string, any>[],
|
|
106
|
+
): { notes: string[] } {
|
|
107
|
+
const notes: string[] = [];
|
|
108
|
+
if (!prefs) return { notes };
|
|
93
109
|
if (!spec.encoding.color) spec.encoding.color = {};
|
|
94
110
|
|
|
95
111
|
if (prefs.palette) {
|
|
@@ -107,6 +123,53 @@ export function applyColorPreferences(
|
|
|
107
123
|
spec.encoding.color.field = prefs.colorField;
|
|
108
124
|
if (!spec.encoding.color.type) spec.encoding.color.type = 'nominal';
|
|
109
125
|
}
|
|
126
|
+
|
|
127
|
+
// Auto-infer color field if palette or highlight was set but no field exists
|
|
128
|
+
if ((prefs.palette || prefs.highlight) && !spec.encoding.color?.field) {
|
|
129
|
+
let inferred: string | null = null;
|
|
130
|
+
|
|
131
|
+
// Strategy 1: use nominal axis
|
|
132
|
+
if (spec.encoding.x?.type === 'nominal') inferred = spec.encoding.x.field;
|
|
133
|
+
else if (spec.encoding.y?.type === 'nominal') inferred = spec.encoding.y.field;
|
|
134
|
+
|
|
135
|
+
// Strategy 2: find a suitable categorical column from data
|
|
136
|
+
if (!inferred && data && data.length > 0) {
|
|
137
|
+
const keys = Object.keys(data[0]);
|
|
138
|
+
for (const key of keys) {
|
|
139
|
+
if (/^id$|_id$|Id$/i.test(key)) continue;
|
|
140
|
+
const vals = data.map(d => d[key]).filter(v => v != null);
|
|
141
|
+
const isNum = vals.every(v => typeof v === 'number' || !isNaN(Number(v)));
|
|
142
|
+
if (isNum) continue;
|
|
143
|
+
const unique = new Set(vals.map(String));
|
|
144
|
+
if (unique.size <= 20) {
|
|
145
|
+
inferred = key;
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (inferred) {
|
|
152
|
+
spec.encoding.color!.field = inferred;
|
|
153
|
+
if (!spec.encoding.color!.type) spec.encoding.color!.type = 'nominal';
|
|
154
|
+
notes.push(`Color field auto-detected as '${inferred}'. Use colorField to override.`);
|
|
155
|
+
} else if (prefs.palette) {
|
|
156
|
+
notes.push(`Palette '${prefs.palette}' ignored — no categorical column found with ≤20 unique values. Use colorField to specify.`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Validate highlight values against data
|
|
161
|
+
if (prefs.highlight && spec.encoding.color?.field && data) {
|
|
162
|
+
const field = spec.encoding.color.field;
|
|
163
|
+
const dataValues = new Set(data.map(d => String(d[field]).toLowerCase()));
|
|
164
|
+
const unmatched = prefs.highlight.values.filter(
|
|
165
|
+
(v: any) => !dataValues.has(String(v).toLowerCase())
|
|
166
|
+
);
|
|
167
|
+
if (unmatched.length > 0) {
|
|
168
|
+
notes.push(`Highlight: ${unmatched.map((v: any) => `'${v}'`).join(', ')} not found in data for field '${field}'.`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return { notes };
|
|
110
173
|
}
|
|
111
174
|
|
|
112
175
|
// ─── TIME BUCKET HELPERS ────────────────────────────────────────────────────
|
|
@@ -118,7 +181,7 @@ function getTimeBucketedFields(groupBy?: DslGroupByField[]): string[] {
|
|
|
118
181
|
.map(g => g.field);
|
|
119
182
|
}
|
|
120
183
|
|
|
121
|
-
function enhanceIntentForTimeBucket(intent: string, groupBy?: DslGroupByField[]): string {
|
|
184
|
+
export function enhanceIntentForTimeBucket(intent: string, groupBy?: DslGroupByField[]): string {
|
|
122
185
|
const bucketedFields = getTimeBucketedFields(groupBy);
|
|
123
186
|
if (bucketedFields.length > 0 && !/time.series|trend|over.time/i.test(intent)) {
|
|
124
187
|
return intent + ' (time series trend)';
|
|
@@ -126,7 +189,7 @@ function enhanceIntentForTimeBucket(intent: string, groupBy?: DslGroupByField[])
|
|
|
126
189
|
return intent;
|
|
127
190
|
}
|
|
128
191
|
|
|
129
|
-
function applyTimeBucketColumnTypes(columns: DataColumn[], groupBy?: DslGroupByField[]): void {
|
|
192
|
+
export function applyTimeBucketColumnTypes(columns: DataColumn[], groupBy?: DslGroupByField[]): void {
|
|
130
193
|
const bucketedFields = getTimeBucketedFields(groupBy);
|
|
131
194
|
for (const col of columns) {
|
|
132
195
|
if (bucketedFields.some(f => col.name.includes(f) || f.includes(col.name))) {
|
|
@@ -206,7 +269,7 @@ export async function executeDashboardViews(
|
|
|
206
269
|
if (view.config) {
|
|
207
270
|
spec.config = { ...spec.config, ...view.config };
|
|
208
271
|
}
|
|
209
|
-
applyColorPreferences(spec, view.colorPreferences);
|
|
272
|
+
applyColorPreferences(spec, view.colorPreferences, data);
|
|
210
273
|
|
|
211
274
|
viewData.push({ viewId: view.id, data, spec });
|
|
212
275
|
viewReasonings.push({ viewId: view.id, pattern: spec.pattern, reasoning });
|
|
@@ -8,14 +8,13 @@
|
|
|
8
8
|
|
|
9
9
|
import { z } from 'zod';
|
|
10
10
|
import type { VisualizeInput, VisualizeOutput } from '../../types.js';
|
|
11
|
-
import { dslQuerySchema } from './dsl-schemas.js';
|
|
11
|
+
import { dslQuerySchema, ALL_PALETTE_NAMES } from './dsl-schemas.js';
|
|
12
12
|
import {
|
|
13
|
-
colorPreferencesSchema,
|
|
14
13
|
columnsSchema,
|
|
15
14
|
dataShapeHintsSchema,
|
|
16
15
|
handleVisualizeCore,
|
|
17
16
|
} from './visualize.js';
|
|
18
|
-
import { errorResponse } from './shared.js';
|
|
17
|
+
import { errorResponse, inferColumns, applyTimeBucketColumnTypes, enhanceIntentForTimeBucket } from './shared.js';
|
|
19
18
|
import { logOperation, extractDslStructure } from './operation-log.js';
|
|
20
19
|
|
|
21
20
|
export const visualizeFromSourceInputSchema = z.object({
|
|
@@ -29,7 +28,15 @@ export const visualizeFromSourceInputSchema = z.object({
|
|
|
29
28
|
title: z.string().optional().describe('Chart title — set upfront to avoid a refine round-trip'),
|
|
30
29
|
subtitle: z.string().optional().describe('Chart subtitle — set upfront to avoid a refine round-trip'),
|
|
31
30
|
includeDataTable: z.boolean().optional().describe('Whether to add a companion sortable data table with linked highlighting below the chart. Default: true'),
|
|
32
|
-
|
|
31
|
+
palette: z.enum(ALL_PALETTE_NAMES).optional()
|
|
32
|
+
.describe('Named palette: categorical, blue, green, purple, warm, blueRed, etc.'),
|
|
33
|
+
highlight: z.object({
|
|
34
|
+
values: z.array(z.union([z.string(), z.number()])).describe('Values to emphasize'),
|
|
35
|
+
color: z.union([z.string(), z.array(z.string())]).optional(),
|
|
36
|
+
mutedColor: z.string().optional(),
|
|
37
|
+
mutedOpacity: z.number().optional(),
|
|
38
|
+
}).optional(),
|
|
39
|
+
colorField: z.string().optional().describe('Which data field to base colors on'),
|
|
33
40
|
maxAlternativeChartTypes: z.number().optional().describe('Max alternative chart type recommendations to return (default: 2, set 0 for none)'),
|
|
34
41
|
geoLevel: z.enum(['country', 'subdivision']).optional()
|
|
35
42
|
.describe('Geographic level: "country" (each row = a nation) or "subdivision" (each row = a state/province). Auto-detected if omitted.'),
|
|
@@ -69,7 +76,15 @@ export function handleVisualizeFromSource(
|
|
|
69
76
|
return errorResponse(result.error);
|
|
70
77
|
}
|
|
71
78
|
|
|
72
|
-
|
|
79
|
+
// Enrich column types and intent with time bucket context (matches dashboard behavior)
|
|
80
|
+
if (!args.columns && args.query.groupBy) {
|
|
81
|
+
const columns = inferColumns(result.rows);
|
|
82
|
+
applyTimeBucketColumnTypes(columns, args.query.groupBy);
|
|
83
|
+
args = { ...args, columns: columns as any };
|
|
84
|
+
}
|
|
85
|
+
const enrichedIntent = enhanceIntentForTimeBucket(args.intent, args.query.groupBy);
|
|
86
|
+
|
|
87
|
+
return core(result.rows, { ...args, intent: enrichedIntent }, { truncated: result.truncated, totalSourceRows: result.totalRows }, {
|
|
73
88
|
dslStructure: extractDslStructure(args.query),
|
|
74
89
|
sourceType,
|
|
75
90
|
});
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
import { z } from 'zod';
|
|
13
13
|
import type { VisualizeInput, VisualizeOutput, DataColumn, VisualizationSpec } from '../../types.js';
|
|
14
14
|
import { isCompoundSpec } from '../../types.js';
|
|
15
|
+
import { ALL_PALETTE_NAMES } from './dsl-schemas.js';
|
|
15
16
|
import { buildChartHtml, isHtmlPatternSupported } from '../../renderers/html/index.js';
|
|
16
17
|
import { shouldCompound, buildCompoundSpec } from '../../renderers/html/compound.js';
|
|
17
18
|
import { buildCompoundHtml } from '../../renderers/html/builders/compound.js';
|
|
@@ -21,23 +22,6 @@ import { errorResponse, inferColumns, applyColorPreferences } from './shared.js'
|
|
|
21
22
|
import { logOperation } from './operation-log.js';
|
|
22
23
|
import type { OperationMeta } from './operation-log.js';
|
|
23
24
|
|
|
24
|
-
const ALL_PALETTE_NAMES = [
|
|
25
|
-
'categorical', 'blue', 'green', 'purple', 'warm',
|
|
26
|
-
'blueRed', 'greenPurple', 'tealOrange', 'redGreen',
|
|
27
|
-
'traffic-light', 'profit-loss', 'temperature',
|
|
28
|
-
] as const;
|
|
29
|
-
|
|
30
|
-
export const colorPreferencesSchema = z.object({
|
|
31
|
-
palette: z.enum(ALL_PALETTE_NAMES).optional().describe('Named palette: categorical, blue, green, purple, warm, blueRed, greenPurple, tealOrange, redGreen, traffic-light, profit-loss, temperature'),
|
|
32
|
-
highlight: z.object({
|
|
33
|
-
values: z.array(z.any()).describe('Values to emphasize — all others become muted gray'),
|
|
34
|
-
color: z.union([z.string(), z.array(z.string())]).optional().describe('Color(s) for highlighted values. Single CSS color or array matching values length.'),
|
|
35
|
-
mutedColor: z.string().optional().describe('Color for non-highlighted items (default: #6b7280)'),
|
|
36
|
-
mutedOpacity: z.number().optional().describe('Opacity for muted items (default: 1.0, lower for stronger de-emphasis)'),
|
|
37
|
-
}).optional(),
|
|
38
|
-
colorField: z.string().optional().describe('Which data field to base colors on'),
|
|
39
|
-
}).optional().describe('Color preferences applied to the visualization');
|
|
40
|
-
|
|
41
25
|
export const columnsSchema = z.array(z.object({
|
|
42
26
|
name: z.string(),
|
|
43
27
|
type: z.enum(['numeric', 'categorical', 'date', 'id', 'text']),
|
|
@@ -69,7 +53,15 @@ export const visualizeInputSchema = z.object({
|
|
|
69
53
|
title: z.string().optional().describe('Chart title — set upfront to avoid a refine round-trip'),
|
|
70
54
|
subtitle: z.string().optional().describe('Chart subtitle — set upfront to avoid a refine round-trip'),
|
|
71
55
|
includeDataTable: z.boolean().optional().describe('Whether to add a companion sortable data table with linked highlighting below the chart. Default: true'),
|
|
72
|
-
|
|
56
|
+
palette: z.enum(ALL_PALETTE_NAMES).optional()
|
|
57
|
+
.describe('Named palette: categorical, blue, green, purple, warm, blueRed, etc.'),
|
|
58
|
+
highlight: z.object({
|
|
59
|
+
values: z.array(z.union([z.string(), z.number()])).describe('Values to emphasize'),
|
|
60
|
+
color: z.union([z.string(), z.array(z.string())]).optional(),
|
|
61
|
+
mutedColor: z.string().optional(),
|
|
62
|
+
mutedOpacity: z.number().optional(),
|
|
63
|
+
}).optional(),
|
|
64
|
+
colorField: z.string().optional().describe('Which data field to base colors on'),
|
|
73
65
|
maxAlternativeChartTypes: z.number().optional().describe('Max alternative chart type recommendations to return (default: 2, set 0 for none)'),
|
|
74
66
|
geoLevel: z.enum(['country', 'subdivision']).optional()
|
|
75
67
|
.describe('Geographic level: "country" (each row = a nation) or "subdivision" (each row = a state/province). Auto-detected if omitted.'),
|
|
@@ -93,12 +85,15 @@ export function handleVisualizeCore(
|
|
|
93
85
|
title?: string;
|
|
94
86
|
subtitle?: string;
|
|
95
87
|
includeDataTable?: boolean;
|
|
96
|
-
|
|
88
|
+
palette?: string;
|
|
89
|
+
highlight?: { values: any[]; color?: string | string[]; mutedColor?: string; mutedOpacity?: number };
|
|
90
|
+
colorField?: string;
|
|
97
91
|
maxAlternativeChartTypes?: number;
|
|
98
92
|
geoLevel?: 'country' | 'subdivision';
|
|
99
93
|
geoRegion?: string;
|
|
100
94
|
}, queryMeta?: { truncated?: boolean; totalSourceRows?: number }, extraMeta?: Partial<OperationMeta>) => {
|
|
101
95
|
const start = Date.now();
|
|
96
|
+
const notes: string[] = [];
|
|
102
97
|
const columns = (args.columns as DataColumn[]) || inferColumns(data);
|
|
103
98
|
const maxAlternativeChartTypes = args.maxAlternativeChartTypes ?? 2;
|
|
104
99
|
|
|
@@ -116,8 +111,12 @@ export function handleVisualizeCore(
|
|
|
116
111
|
|
|
117
112
|
const alternatives = result.alternatives.slice(0, maxAlternativeChartTypes);
|
|
118
113
|
|
|
119
|
-
|
|
120
|
-
|
|
114
|
+
const colorPrefs = (args.palette || args.highlight || args.colorField)
|
|
115
|
+
? { palette: args.palette, highlight: args.highlight, colorField: args.colorField }
|
|
116
|
+
: undefined;
|
|
117
|
+
if (colorPrefs) {
|
|
118
|
+
const colorResult = applyColorPreferences(result.recommended.spec, colorPrefs, data);
|
|
119
|
+
notes.push(...colorResult.notes);
|
|
121
120
|
}
|
|
122
121
|
|
|
123
122
|
const spec = result.recommended.spec;
|
|
@@ -139,10 +138,11 @@ export function handleVisualizeCore(
|
|
|
139
138
|
for (const alt of alternatives) {
|
|
140
139
|
alternativesMap.set(alt.pattern, alt.spec);
|
|
141
140
|
}
|
|
142
|
-
const specId = specStore.save(finalSpec, columns, alternativesMap);
|
|
141
|
+
const specId = specStore.save(finalSpec, columns, alternativesMap, data);
|
|
143
142
|
|
|
144
143
|
const compactResponse: Record<string, any> = {
|
|
145
144
|
specId,
|
|
145
|
+
...(notes.length > 0 ? { notes } : {}),
|
|
146
146
|
recommended: {
|
|
147
147
|
pattern: result.recommended.pattern,
|
|
148
148
|
title: spec.title,
|
|
@@ -93,7 +93,7 @@ export const groupedBarPattern: VisualizationPattern = {
|
|
|
93
93
|
generateSpec: (data, columns, options) => {
|
|
94
94
|
const categoryCol = columns[0];
|
|
95
95
|
const seriesCol = columns.length > 2 ? columns[1] : undefined;
|
|
96
|
-
const valueCol = columns.length > 2 ? columns[2] : columns.length > 1 ? columns[1] :
|
|
96
|
+
const valueCol = columns.length > 2 ? columns[2] : columns.length > 1 ? columns[1] : undefined;
|
|
97
97
|
|
|
98
98
|
const spec: VisualizationSpec = {
|
|
99
99
|
pattern: 'grouped-bar',
|
|
@@ -101,15 +101,15 @@ export const groupedBarPattern: VisualizationPattern = {
|
|
|
101
101
|
data,
|
|
102
102
|
encoding: {
|
|
103
103
|
x: { field: categoryCol, type: 'nominal', title: categoryCol },
|
|
104
|
-
y: { field: valueCol, type: 'quantitative', title: valueCol },
|
|
104
|
+
y: valueCol ? { field: valueCol, type: 'quantitative', title: valueCol } : undefined,
|
|
105
105
|
color: seriesCol
|
|
106
106
|
? { field: seriesCol, type: 'nominal', scale: {} }
|
|
107
|
-
:
|
|
107
|
+
: undefined,
|
|
108
108
|
},
|
|
109
109
|
config: {
|
|
110
110
|
categoryField: categoryCol,
|
|
111
|
-
seriesField: seriesCol,
|
|
112
|
-
valueField: valueCol,
|
|
111
|
+
seriesField: seriesCol ?? null,
|
|
112
|
+
valueField: valueCol ?? null,
|
|
113
113
|
orientation: options?.orientation ?? 'vertical',
|
|
114
114
|
},
|
|
115
115
|
};
|
|
@@ -33,8 +33,8 @@ export const wafflePattern: VisualizationPattern = {
|
|
|
33
33
|
|
|
34
34
|
selectionRules: [
|
|
35
35
|
{
|
|
36
|
-
condition: 'Part-to-whole with few categories
|
|
37
|
-
weight:
|
|
36
|
+
condition: 'Part-to-whole with few categories',
|
|
37
|
+
weight: 40,
|
|
38
38
|
matches: (ctx) => {
|
|
39
39
|
return (
|
|
40
40
|
ctx.dataShape.categoryCount >= 2 &&
|
|
@@ -52,13 +52,27 @@ export const wafflePattern: VisualizationPattern = {
|
|
|
52
52
|
);
|
|
53
53
|
},
|
|
54
54
|
},
|
|
55
|
+
{
|
|
56
|
+
condition: 'Composition intent — proportion, breakdown, split',
|
|
57
|
+
weight: 40,
|
|
58
|
+
matches: (ctx) => {
|
|
59
|
+
return /\b(proportion|share|part.of|breakdown|split|how\s+much\s+of)\b/i.test(ctx.intent);
|
|
60
|
+
},
|
|
61
|
+
},
|
|
55
62
|
{
|
|
56
63
|
condition: 'Very few categories (2-4) — waffle is clearest',
|
|
57
|
-
weight:
|
|
64
|
+
weight: 15,
|
|
58
65
|
matches: (ctx) => {
|
|
59
66
|
return ctx.dataShape.categoryCount >= 2 && ctx.dataShape.categoryCount <= 4;
|
|
60
67
|
},
|
|
61
68
|
},
|
|
69
|
+
{
|
|
70
|
+
condition: 'Penalize for comparison intent — bar chart is better',
|
|
71
|
+
weight: -25,
|
|
72
|
+
matches: (ctx) => {
|
|
73
|
+
return /(compar|rank|top|bottom|highest|lowest|bar\s*chart)/i.test(ctx.intent);
|
|
74
|
+
},
|
|
75
|
+
},
|
|
62
76
|
{
|
|
63
77
|
condition: 'Penalize for too many categories — colors become indistinguishable',
|
|
64
78
|
weight: -40,
|
|
@@ -30,18 +30,19 @@ export const stripPlotPattern: VisualizationPattern = {
|
|
|
30
30
|
selectionRules: [
|
|
31
31
|
{
|
|
32
32
|
condition: 'Very small dataset — strip plot shows every point clearly',
|
|
33
|
-
weight:
|
|
33
|
+
weight: 35,
|
|
34
34
|
matches: (ctx) => {
|
|
35
35
|
return (
|
|
36
36
|
ctx.dataShape.rowCount >= 3 &&
|
|
37
37
|
ctx.dataShape.rowCount <= 50 &&
|
|
38
|
-
ctx.dataShape.numericColumnCount >= 1
|
|
38
|
+
ctx.dataShape.numericColumnCount >= 1 &&
|
|
39
|
+
!ctx.dataShape.hasTimeSeries
|
|
39
40
|
);
|
|
40
41
|
},
|
|
41
42
|
},
|
|
42
43
|
{
|
|
43
44
|
condition: 'Distribution intent with small data — too few points for histogram/violin',
|
|
44
|
-
weight:
|
|
45
|
+
weight: 65,
|
|
45
46
|
matches: (ctx) => {
|
|
46
47
|
return (
|
|
47
48
|
/\b(distribut|spread|point|individual)\b/i.test(ctx.intent) &&
|
|
@@ -58,6 +59,14 @@ export const stripPlotPattern: VisualizationPattern = {
|
|
|
58
59
|
return avgPerGroup < 15 && avgPerGroup >= 3;
|
|
59
60
|
},
|
|
60
61
|
},
|
|
62
|
+
{
|
|
63
|
+
condition: 'Penalize for time/comparison/composition signals — strip-plot is wrong',
|
|
64
|
+
weight: -30,
|
|
65
|
+
matches: (ctx) => {
|
|
66
|
+
return ctx.dataShape.hasTimeSeries ||
|
|
67
|
+
/(compar|rank|trend|over\s*time|composition|proportion)/i.test(ctx.intent);
|
|
68
|
+
},
|
|
69
|
+
},
|
|
61
70
|
{
|
|
62
71
|
condition: 'Penalize for large datasets — too many overlapping points',
|
|
63
72
|
weight: -50,
|
|
@@ -123,8 +123,8 @@ export const sparklineGridPattern: VisualizationPattern = {
|
|
|
123
123
|
seriesField: seriesCol,
|
|
124
124
|
gridCols,
|
|
125
125
|
seriesMeta,
|
|
126
|
-
cellWidth: options?.cellWidth ??
|
|
127
|
-
cellHeight: options?.cellHeight ??
|
|
126
|
+
cellWidth: options?.cellWidth ?? undefined,
|
|
127
|
+
cellHeight: options?.cellHeight ?? undefined,
|
|
128
128
|
curveType: options?.curveType ?? 'monotone',
|
|
129
129
|
showSeriesLabel: options?.showSeriesLabel ?? true,
|
|
130
130
|
showLatestValue: options?.showLatestValue ?? true,
|
package/src/patterns/selector.ts
CHANGED
|
@@ -89,6 +89,21 @@ export function selectPattern(
|
|
|
89
89
|
candidates = candidates.filter((p) => !options.excludePatterns!.includes(p.id));
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
+
// Pre-filter by dataRequirements (structural compatibility check).
|
|
93
|
+
// Skip when forcePattern is set — the user explicitly wants that pattern.
|
|
94
|
+
if (!options?.forcePattern) {
|
|
95
|
+
const compatible = registry.getCompatible({
|
|
96
|
+
rowCount: ctx.dataShape.rowCount,
|
|
97
|
+
numericColumnCount: ctx.dataShape.numericColumnCount,
|
|
98
|
+
categoricalColumnCount: ctx.dataShape.categoricalColumnCount,
|
|
99
|
+
dateColumnCount: ctx.dataShape.dateColumnCount,
|
|
100
|
+
hasHierarchy: ctx.dataShape.hasHierarchy,
|
|
101
|
+
hasTimeSeries: ctx.dataShape.hasTimeSeries,
|
|
102
|
+
});
|
|
103
|
+
const compatibleIds = new Set(compatible.map((p) => p.id));
|
|
104
|
+
candidates = candidates.filter((p) => compatibleIds.has(p.id));
|
|
105
|
+
}
|
|
106
|
+
|
|
92
107
|
// Score every candidate
|
|
93
108
|
const scored = candidates
|
|
94
109
|
.map((pattern) => scorePattern(pattern, ctx, intentResult))
|
|
@@ -210,6 +225,23 @@ function scorePattern(
|
|
|
210
225
|
});
|
|
211
226
|
}
|
|
212
227
|
|
|
228
|
+
// Pattern name matching: if the user's intent mentions a pattern by name, boost it strongly
|
|
229
|
+
if (ctx.intent) {
|
|
230
|
+
const intentLower = ctx.intent.toLowerCase();
|
|
231
|
+
const idAsWords = pattern.id.replace(/-/g, ' ');
|
|
232
|
+
const nameLower = pattern.name.toLowerCase();
|
|
233
|
+
const nameRegex = new RegExp(`\\b${idAsWords.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`, 'i');
|
|
234
|
+
const nameRegex2 = new RegExp(`\\b${nameLower.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\b`, 'i');
|
|
235
|
+
if (nameRegex.test(intentLower) || nameRegex2.test(intentLower)) {
|
|
236
|
+
const boost = 100;
|
|
237
|
+
totalScore += boost;
|
|
238
|
+
matchedRules.push({
|
|
239
|
+
condition: `Intent mentions pattern name "${pattern.name}"`,
|
|
240
|
+
weight: boost,
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
213
245
|
// Build human-readable reasoning
|
|
214
246
|
const reasoning = buildReasoning(pattern, matchedRules, totalScore);
|
|
215
247
|
|
|
@@ -340,6 +372,7 @@ function selectColumnsForPattern(
|
|
|
340
372
|
case 'time': {
|
|
341
373
|
if (dateCols.length > 0) result.push(dateCols[0].name);
|
|
342
374
|
if (numericCols.length > 0) result.push(numericCols[0].name);
|
|
375
|
+
if (dateCols.length === 0 && numericCols.length > 1) result.push(numericCols[1].name);
|
|
343
376
|
if (categoricalCols.length > 0) result.push(categoricalCols[0].name);
|
|
344
377
|
break;
|
|
345
378
|
}
|
|
@@ -415,6 +448,16 @@ function selectColumnsForSpecificPattern(
|
|
|
415
448
|
numericCols: DataColumn[]
|
|
416
449
|
): string[] | null {
|
|
417
450
|
switch (pattern.id) {
|
|
451
|
+
// ── Scatter: 2 numerics + optional categorical for color ────────────
|
|
452
|
+
case 'scatter': {
|
|
453
|
+
if (numericCols.length >= 2) {
|
|
454
|
+
const result = [numericCols[0].name, numericCols[1].name];
|
|
455
|
+
if (categoricalCols.length > 0) result.push(categoricalCols[0].name);
|
|
456
|
+
return result;
|
|
457
|
+
}
|
|
458
|
+
return null;
|
|
459
|
+
}
|
|
460
|
+
|
|
418
461
|
// ── Heatmap: 2 categoricals + 1 numeric ──────────────────────────────
|
|
419
462
|
case 'heatmap': {
|
|
420
463
|
const catCandidates = getCategoricalCandidates(columns);
|
|
@@ -449,12 +492,15 @@ function selectColumnsForSpecificPattern(
|
|
|
449
492
|
return null;
|
|
450
493
|
}
|
|
451
494
|
|
|
452
|
-
// ── Grouped bar: 2 categoricals + 1 numeric
|
|
495
|
+
// ── Grouped bar: 2 categoricals + 1 numeric, OR 1 categorical + N numerics (wide format)
|
|
453
496
|
case 'grouped-bar': {
|
|
454
497
|
if (categoricalCols.length >= 2 && numericCols.length >= 1) {
|
|
455
498
|
const sorted = sortByCardinality(categoricalCols);
|
|
456
499
|
return [sorted[0].name, sorted[1].name, numericCols[0].name];
|
|
457
500
|
}
|
|
501
|
+
if (categoricalCols.length >= 1 && numericCols.length >= 2) {
|
|
502
|
+
return [categoricalCols[0].name];
|
|
503
|
+
}
|
|
458
504
|
return null;
|
|
459
505
|
}
|
|
460
506
|
|
|
@@ -32,7 +32,30 @@ export function renderBar(container: HTMLElement, spec: VisualizationSpec): void
|
|
|
32
32
|
const { config, encoding, data } = spec;
|
|
33
33
|
const isHorizontal = config.orientation === 'horizontal';
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
// Filter out rows with NaN y-values
|
|
36
|
+
const yFieldName = encoding.y?.field;
|
|
37
|
+
let sortedData = yFieldName
|
|
38
|
+
? [...data].filter((d) => !isNaN(Number(d[yFieldName])))
|
|
39
|
+
: [...data];
|
|
40
|
+
|
|
41
|
+
// Aggregate duplicate categories: if the same x-value appears multiple times, sum y-values
|
|
42
|
+
if (encoding.x?.field && yFieldName) {
|
|
43
|
+
const xKey = encoding.x.field;
|
|
44
|
+
const categorySet = new Set(sortedData.map((d) => d[xKey]));
|
|
45
|
+
if (categorySet.size < sortedData.length) {
|
|
46
|
+
const aggregated = new Map<any, Record<string, any>>();
|
|
47
|
+
for (const row of sortedData) {
|
|
48
|
+
const key = row[xKey];
|
|
49
|
+
if (aggregated.has(key)) {
|
|
50
|
+
const existing = aggregated.get(key)!;
|
|
51
|
+
existing[yFieldName] = Number(existing[yFieldName]) + Number(row[yFieldName]);
|
|
52
|
+
} else {
|
|
53
|
+
aggregated.set(key, { ...row });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
sortedData = [...aggregated.values()];
|
|
57
|
+
}
|
|
58
|
+
}
|
|
36
59
|
|
|
37
60
|
// Sort by value
|
|
38
61
|
if (config.sortBy === 'value' && encoding.y) {
|
|
@@ -34,13 +34,14 @@ export function renderGroupedBar(container: HTMLElement, spec: VisualizationSpec
|
|
|
34
34
|
let isHorizontal = config.orientation === 'horizontal';
|
|
35
35
|
|
|
36
36
|
const categoryField = config.categoryField || encoding.x?.field;
|
|
37
|
-
const seriesField = config.seriesField || encoding.color?.field;
|
|
37
|
+
const seriesField = config.seriesField || encoding.color?.field || null;
|
|
38
38
|
const valueField = config.valueField || encoding.y?.field;
|
|
39
39
|
|
|
40
|
-
if (!categoryField
|
|
40
|
+
if (!categoryField) return;
|
|
41
41
|
|
|
42
42
|
let series: string[];
|
|
43
43
|
let pivoted: Record<string, Record<string, number>>;
|
|
44
|
+
let isWideFormat = false;
|
|
44
45
|
|
|
45
46
|
if (seriesField) {
|
|
46
47
|
series = [...new Set(data.map((d: any) => String(d[seriesField])))];
|
|
@@ -54,7 +55,8 @@ export function renderGroupedBar(container: HTMLElement, spec: VisualizationSpec
|
|
|
54
55
|
const numericFields = Object.keys(data[0] || {}).filter(
|
|
55
56
|
(k) => k !== categoryField && typeof data[0][k] === 'number'
|
|
56
57
|
);
|
|
57
|
-
|
|
58
|
+
isWideFormat = numericFields.length > 1;
|
|
59
|
+
series = numericFields.length > 1 ? numericFields : valueField ? [valueField] : numericFields;
|
|
58
60
|
pivoted = {};
|
|
59
61
|
for (const d of data) {
|
|
60
62
|
const cat = String(d[categoryField]);
|
|
@@ -113,7 +115,10 @@ export function renderGroupedBar(container: HTMLElement, spec: VisualizationSpec
|
|
|
113
115
|
|
|
114
116
|
const showLegend = containerHeight > 250 && containerWidth > 350;
|
|
115
117
|
|
|
116
|
-
|
|
118
|
+
let colorScale = buildColorScale(encoding.color, data, seriesField || valueField);
|
|
119
|
+
if (isWideFormat) {
|
|
120
|
+
colorScale = d3.scaleOrdinal().domain(series).range(d3.schemeTableau10 || colorScale.range());
|
|
121
|
+
}
|
|
117
122
|
|
|
118
123
|
container.style.display = 'flex';
|
|
119
124
|
container.style.flexDirection = 'column';
|
|
@@ -57,6 +57,8 @@ export function renderDonut(container: HTMLElement, spec: VisualizationSpec): vo
|
|
|
57
57
|
const minVisible = maxVal * 0.02;
|
|
58
58
|
const clampVal = (v: number) => (v > 0 && v < minVisible ? minVisible : v);
|
|
59
59
|
|
|
60
|
+
const negativeCount = data.filter((d: any) => Number(d[valueField]) < 0).length;
|
|
61
|
+
|
|
60
62
|
const items = data
|
|
61
63
|
.filter((d: any) => Number(d[valueField]) > 0)
|
|
62
64
|
.map((d: any) => ({
|
|
@@ -223,4 +225,16 @@ export function renderDonut(container: HTMLElement, spec: VisualizationSpec): vo
|
|
|
223
225
|
.attr('font-family', 'Inter, system-ui, sans-serif')
|
|
224
226
|
.text(centerLabel);
|
|
225
227
|
}
|
|
228
|
+
|
|
229
|
+
if (negativeCount > 0) {
|
|
230
|
+
svg.append('text')
|
|
231
|
+
.attr('x', wrapperWidth / 2)
|
|
232
|
+
.attr('y', wrapperHeight - 6)
|
|
233
|
+
.attr('text-anchor', 'middle')
|
|
234
|
+
.attr('fill', TEXT_MUTED)
|
|
235
|
+
.attr('font-size', '10px')
|
|
236
|
+
.attr('font-style', 'italic')
|
|
237
|
+
.attr('font-family', 'Inter, system-ui, sans-serif')
|
|
238
|
+
.text(`${negativeCount} item${negativeCount > 1 ? 's' : ''} with negative values excluded`);
|
|
239
|
+
}
|
|
226
240
|
}
|
|
@@ -33,6 +33,11 @@ export function renderStackedBar(container: HTMLElement, spec: VisualizationSpec
|
|
|
33
33
|
const valueField = config.valueField || encoding.y?.field;
|
|
34
34
|
const isNormalized = config.normalized || false;
|
|
35
35
|
|
|
36
|
+
if (!categoryField || !seriesField || !valueField || !data || data.length === 0) {
|
|
37
|
+
container.innerHTML = `<div style="display:flex;align-items:center;justify-content:center;height:100%;color:#9ca3af;font-size:14px;font-family:Inter,system-ui,sans-serif;background:#0f1117;border-radius:8px;">Stacked bar requires category, series, and value fields</div>`;
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
36
41
|
// Get unique categories and series
|
|
37
42
|
let categories = [...new Set(data.map((d) => d[categoryField]))];
|
|
38
43
|
const series = [...new Set(data.map((d) => d[seriesField]))];
|
|
@@ -245,6 +245,7 @@ function buildHierarchy(
|
|
|
245
245
|
const clampVal = (v: number) => (v > 0 && v < minVisible ? minVisible : v);
|
|
246
246
|
|
|
247
247
|
if (parentField) {
|
|
248
|
+
const childField = config.childField || categoryField;
|
|
248
249
|
const parents = [...new Set(data.map((d) => d[parentField]))];
|
|
249
250
|
const hierarchy = {
|
|
250
251
|
name: 'root',
|
|
@@ -253,7 +254,7 @@ function buildHierarchy(
|
|
|
253
254
|
children: data
|
|
254
255
|
.filter((d) => d[parentField] === p)
|
|
255
256
|
.map((d) => ({
|
|
256
|
-
name: d[
|
|
257
|
+
name: d[childField] || d[categoryField] || d[parentField],
|
|
257
258
|
value: clampVal(Number(d[valueField]) || 0),
|
|
258
259
|
_data: d,
|
|
259
260
|
})),
|
|
@@ -73,7 +73,19 @@ function dodgeBeeswarm(xPositions: number[], spacing: number, maxOffset: number)
|
|
|
73
73
|
|
|
74
74
|
export function renderBeeswarm(container: HTMLElement, spec: VisualizationSpec): void {
|
|
75
75
|
const { config, encoding, data } = spec;
|
|
76
|
+
|
|
77
|
+
if (!data || data.length === 0) {
|
|
78
|
+
container.innerHTML = `<div style="display:flex;align-items:center;justify-content:center;height:100%;color:#9ca3af;font-size:14px;font-family:Inter,system-ui,sans-serif;background:#0f1117;border-radius:8px;">No data for beeswarm</div>`;
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
76
82
|
const valueField = config.valueField || encoding.x?.field || Object.keys(data[0])[0];
|
|
83
|
+
|
|
84
|
+
if (!valueField) {
|
|
85
|
+
container.innerHTML = `<div style="display:flex;align-items:center;justify-content:center;height:100%;color:#9ca3af;font-size:14px;font-family:Inter,system-ui,sans-serif;background:#0f1117;border-radius:8px;">Beeswarm requires a numeric value field</div>`;
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
77
89
|
const categoryField = config.categoryField || encoding.y?.field || null;
|
|
78
90
|
const baseRadius = config.dotRadius ?? 4;
|
|
79
91
|
const opacity = config.opacity ?? 0.7;
|