@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
|
@@ -1,293 +1,349 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* MCP Tool: refine_visualization
|
|
3
|
-
* Takes a current visualization spec and a refinement request,
|
|
4
|
-
* returns an updated spec.
|
|
5
|
-
*
|
|
6
|
-
* Supports both atomic VisualizationSpec and CompoundVisualizationSpec.
|
|
7
|
-
* Accepts specId from a previous visualize/refine call.
|
|
8
|
-
*/
|
|
9
1
|
import { z } from 'zod';
|
|
10
2
|
import { isCompoundSpec } from '../../types.js';
|
|
11
3
|
import { buildChartHtml, isHtmlPatternSupported } from '../../renderers/html/index.js';
|
|
12
4
|
import { buildCompoundHtml } from '../../renderers/html/builders/compound.js';
|
|
13
5
|
import { specStore } from '../spec-store.js';
|
|
14
|
-
import { errorResponse,
|
|
6
|
+
import { errorResponse, htmlResponse } from './shared.js';
|
|
15
7
|
import { logOperation } from './operation-log.js';
|
|
8
|
+
import { selectPattern } from '../../patterns/selector.js';
|
|
9
|
+
import { ALL_PALETTE_NAMES } from './dsl-schemas.js';
|
|
10
|
+
const FLIPPABLE_PATTERNS = new Set([
|
|
11
|
+
'bar', 'grouped-bar', 'stacked-bar', 'lollipop', 'bullet',
|
|
12
|
+
'line', 'area', 'histogram', 'scatter', 'connected-scatter',
|
|
13
|
+
]);
|
|
14
|
+
const FLOW_PATTERNS = new Set(['sankey', 'alluvial', 'chord', 'funnel']);
|
|
15
|
+
const FORMAT_MAP = {
|
|
16
|
+
'percent': '.1%',
|
|
17
|
+
'dollar': '$,.0f',
|
|
18
|
+
'integer': ',.0f',
|
|
19
|
+
'decimal': ',.2f',
|
|
20
|
+
'compact': '.2s',
|
|
21
|
+
};
|
|
16
22
|
export const refineInputSchema = z.object({
|
|
17
23
|
specId: z.string().describe('Spec ID from a previous visualize or refine call'),
|
|
18
|
-
|
|
19
|
-
|
|
24
|
+
sort: z.object({
|
|
25
|
+
field: z.string().optional().describe('Column to sort by. If omitted, sorts by primary measure axis.'),
|
|
26
|
+
direction: z.enum(['asc', 'desc']),
|
|
27
|
+
}).nullable().optional(),
|
|
28
|
+
limit: z.number().optional().describe('Top N rows'),
|
|
29
|
+
filter: z.array(z.object({
|
|
30
|
+
field: z.string(),
|
|
31
|
+
op: z.enum(['in', 'not_in', 'gt', 'gte', 'lt', 'lte', '=', '!='])
|
|
32
|
+
.default('in')
|
|
33
|
+
.transform(op => op === '=' ? 'in' : op === '!=' ? 'not_in' : op),
|
|
34
|
+
values: z.array(z.union([z.string(), z.number()])),
|
|
35
|
+
})).optional().describe('Data filters. Pass empty array [] to clear filters.'),
|
|
36
|
+
flip: z.boolean().optional().describe('Swap x/y axes (Cartesian charts only)'),
|
|
37
|
+
title: z.string().optional(),
|
|
38
|
+
subtitle: z.string().optional(),
|
|
39
|
+
xLabel: z.string().optional().describe('X-axis label'),
|
|
40
|
+
yLabel: z.string().optional().describe('Y-axis label'),
|
|
41
|
+
palette: z.enum(ALL_PALETTE_NAMES).optional()
|
|
42
|
+
.describe('Named palette: categorical, blue, green, purple, warm, blueRed, etc.'),
|
|
43
|
+
highlight: z.object({
|
|
44
|
+
values: z.array(z.union([z.string(), z.number()])).describe('Values to emphasize'),
|
|
45
|
+
color: z.union([z.string(), z.array(z.string())]).optional(),
|
|
46
|
+
mutedColor: z.string().optional(),
|
|
47
|
+
mutedOpacity: z.number().optional(),
|
|
48
|
+
}).nullable().optional()
|
|
49
|
+
.transform(v => v && v.values?.length === 0 ? null : v)
|
|
50
|
+
.describe('Highlight specific values. Pass null to clear.'),
|
|
51
|
+
colorField: z.string().optional().describe('Which data field drives color'),
|
|
52
|
+
flowColorBy: z.enum(['source', 'target']).optional()
|
|
53
|
+
.describe('Color flow charts by source or target node'),
|
|
54
|
+
format: z.enum(['percent', 'dollar', 'integer', 'decimal', 'compact']).optional()
|
|
55
|
+
.describe('Value axis number format'),
|
|
56
|
+
switchPattern: z.string().optional()
|
|
57
|
+
.describe('Switch to a different chart type by pattern ID'),
|
|
58
|
+
removeTable: z.boolean().optional().describe('Remove the data table from a compound chart'),
|
|
59
|
+
layout: z.enum(['rows', 'columns']).optional().describe('Compound layout'),
|
|
60
|
+
hideColumns: z.array(z.string()).optional().describe('Hide columns from the data table'),
|
|
20
61
|
});
|
|
21
|
-
|
|
22
|
-
/** Map natural language color terms to valid ColorPaletteName values */
|
|
23
|
-
const PALETTE_LOOKUP = [
|
|
24
|
-
[/\bblue\s*red\b|\bblue[-_]?red\b/, 'blueRed', 'blueRed diverging palette'],
|
|
25
|
-
[/\bgreen\s*purple\b|\bgreen[-_]?purple\b/, 'greenPurple', 'greenPurple diverging palette'],
|
|
26
|
-
[/\bteal\s*orange\b|\bteal[-_]?orange\b/, 'tealOrange', 'tealOrange diverging palette'],
|
|
27
|
-
[/\bred\s*green\b|\bred[-_]?green\b/, 'redGreen', 'redGreen diverging palette'],
|
|
28
|
-
[/\btraffic\s*light\b|\bstoplight\b/, 'traffic-light', 'traffic-light palette'],
|
|
29
|
-
[/\bprofit[-\s]*loss\b/, 'profit-loss', 'profit-loss palette'],
|
|
30
|
-
[/\btemperature\b/, 'temperature', 'temperature palette'],
|
|
31
|
-
[/\bwarm\b|\borange\b/, 'warm', 'warm palette'],
|
|
32
|
-
[/\bpurple\b|\bviolet\b/, 'purple', 'purple palette'],
|
|
33
|
-
[/\bgreen\b/, 'green', 'green palette'],
|
|
34
|
-
[/\bblue\b|\bsequential\b|\bcool\b/, 'blue', 'blue palette'],
|
|
35
|
-
[/\bdiverging\b/, 'blueRed', 'blueRed diverging palette'],
|
|
36
|
-
[/\bcategorical\b|\bdistinct\b|\bqualitative\b/, 'categorical', 'categorical palette'],
|
|
37
|
-
];
|
|
38
|
-
function matchPalette(text) {
|
|
39
|
-
const lower = text.toLowerCase();
|
|
40
|
-
for (const [regex, palette, label] of PALETTE_LOOKUP) {
|
|
41
|
-
if (regex.test(lower))
|
|
42
|
-
return [palette, label];
|
|
43
|
-
}
|
|
44
|
-
return null;
|
|
45
|
-
}
|
|
46
|
-
// ─── HIGHLIGHT PARSING ──────────────────────────────────────────────────────
|
|
47
|
-
/** Parse highlight values from a refinement string */
|
|
48
|
-
function parseHighlight(refinement, lower) {
|
|
49
|
-
// "highlight X and Y" or "highlight X, Y, Z"
|
|
50
|
-
const highlightMatch = lower.match(/highlight\s+(.+?)(?:\s+in\s+(#[0-9a-f]{3,8}|\w+))?$/);
|
|
51
|
-
if (!highlightMatch)
|
|
52
|
-
return null;
|
|
53
|
-
const rawValues = highlightMatch[1]
|
|
54
|
-
// Remove trailing "in <color>" that may have been greedily captured
|
|
55
|
-
.replace(/\s+in\s+#[0-9a-f]{3,8}$/i, '')
|
|
56
|
-
.replace(/\s+in\s+\w+$/i, '');
|
|
57
|
-
// Split on " and ", commas, or " & "
|
|
58
|
-
const values = rawValues
|
|
59
|
-
.split(/\s*(?:,\s*|\s+and\s+|\s*&\s*)\s*/)
|
|
60
|
-
.map(v => v.trim())
|
|
61
|
-
.filter(Boolean)
|
|
62
|
-
// Restore original casing from the refinement string
|
|
63
|
-
.map(v => {
|
|
64
|
-
const idx = refinement.toLowerCase().indexOf(v);
|
|
65
|
-
return idx >= 0 ? refinement.slice(idx, idx + v.length) : v;
|
|
66
|
-
});
|
|
67
|
-
if (values.length === 0)
|
|
68
|
-
return null;
|
|
69
|
-
const result = { values };
|
|
70
|
-
if (highlightMatch[2])
|
|
71
|
-
result.color = highlightMatch[2];
|
|
72
|
-
return result;
|
|
73
|
-
}
|
|
74
|
-
/** Parse muted opacity from refinement */
|
|
75
|
-
function parseMutedOpacity(lower) {
|
|
76
|
-
const match = lower.match(/mute\w*\s+(?:to\s+)?(\d+)%\s*opacity/);
|
|
77
|
-
if (match)
|
|
78
|
-
return parseInt(match[1], 10) / 100;
|
|
79
|
-
const match2 = lower.match(/opacity\s+(\d+)%/);
|
|
80
|
-
if (match2)
|
|
81
|
-
return parseInt(match2[1], 10) / 100;
|
|
82
|
-
const match3 = lower.match(/mute\w*\s+(?:to\s+)?(?:0?\.\d+)/);
|
|
83
|
-
if (match3) {
|
|
84
|
-
const val = parseFloat(match3[0].match(/\d+\.\d+/)[0]);
|
|
85
|
-
return val > 0 && val <= 1 ? val : null;
|
|
86
|
-
}
|
|
87
|
-
return null;
|
|
88
|
-
}
|
|
89
|
-
// ─── APPLY REFINEMENT ───────────────────────────────────────────────────────
|
|
90
|
-
/** Apply refinements to an atomic visualization spec */
|
|
91
|
-
function applyRefinement(spec, refinement) {
|
|
62
|
+
function applyAtomicParams(spec, args, stored) {
|
|
92
63
|
const changes = [];
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
64
|
+
const notes = [];
|
|
65
|
+
if (args.filter !== undefined) {
|
|
66
|
+
if (args.filter.length === 0) {
|
|
67
|
+
if (stored.originalData) {
|
|
68
|
+
spec.data = [...stored.originalData];
|
|
69
|
+
changes.push('Cleared all filters');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
for (const f of args.filter) {
|
|
74
|
+
const before = spec.data.length;
|
|
75
|
+
const normalizeStr = (v) => String(v).toLowerCase();
|
|
76
|
+
if (f.op === 'in') {
|
|
77
|
+
const valSet = new Set(f.values.map(normalizeStr));
|
|
78
|
+
spec.data = spec.data.filter(d => valSet.has(normalizeStr(d[f.field])));
|
|
79
|
+
}
|
|
80
|
+
else if (f.op === 'not_in') {
|
|
81
|
+
const valSet = new Set(f.values.map(normalizeStr));
|
|
82
|
+
spec.data = spec.data.filter(d => !valSet.has(normalizeStr(d[f.field])));
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
const threshold = Number(f.values[0]);
|
|
86
|
+
spec.data = spec.data.filter(d => {
|
|
87
|
+
const v = Number(d[f.field]);
|
|
88
|
+
if (f.op === 'gt')
|
|
89
|
+
return v > threshold;
|
|
90
|
+
if (f.op === 'gte')
|
|
91
|
+
return v >= threshold;
|
|
92
|
+
if (f.op === 'lt')
|
|
93
|
+
return v < threshold;
|
|
94
|
+
if (f.op === 'lte')
|
|
95
|
+
return v <= threshold;
|
|
96
|
+
return true;
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
if (spec.data.length === 0) {
|
|
100
|
+
spec.data = stored.originalData ? [...stored.originalData] : spec.data;
|
|
101
|
+
notes.push(`Filter on '${f.field}' removed all rows — filter was not applied.`);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
changes.push(`Filtered ${f.field}: ${before} → ${spec.data.length} rows`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
102
108
|
}
|
|
103
|
-
|
|
104
|
-
if (
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
+
if (args.sort !== undefined) {
|
|
110
|
+
if (args.sort === null) {
|
|
111
|
+
delete spec.config.sortBy;
|
|
112
|
+
delete spec.config.sortOrder;
|
|
113
|
+
changes.push('Cleared sort');
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
const dir = args.sort.direction;
|
|
117
|
+
const sortOrder = dir === 'desc' ? 'descending' : 'ascending';
|
|
118
|
+
if (args.sort.field) {
|
|
119
|
+
const field = args.sort.field;
|
|
120
|
+
if (spec.encoding.y && field === spec.encoding.y.field) {
|
|
121
|
+
spec.config.sortBy = 'value';
|
|
122
|
+
}
|
|
123
|
+
else if (spec.encoding.x && field === spec.encoding.x.field) {
|
|
124
|
+
spec.config.sortBy = 'category';
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
const keys = spec.data.length > 0 ? Object.keys(spec.data[0]) : [];
|
|
128
|
+
if (keys.includes(field)) {
|
|
129
|
+
spec.data.sort((a, b) => {
|
|
130
|
+
const av = a[field], bv = b[field];
|
|
131
|
+
if (av == null)
|
|
132
|
+
return 1;
|
|
133
|
+
if (bv == null)
|
|
134
|
+
return -1;
|
|
135
|
+
return dir === 'desc'
|
|
136
|
+
? (typeof av === 'number' ? bv - av : String(bv).localeCompare(String(av)))
|
|
137
|
+
: (typeof av === 'number' ? av - bv : String(av).localeCompare(String(bv)));
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
notes.push(`Sort field '${field}' not found. Available: ${keys.join(', ')}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
spec.config.sortOrder = sortOrder;
|
|
145
|
+
changes.push(`Sorted by ${field} ${dir}`);
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
spec.config.sortBy = 'value';
|
|
149
|
+
spec.config.sortOrder = sortOrder;
|
|
150
|
+
changes.push(`Sorted by value ${dir}`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
109
153
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const limit = parseInt(limitMatch[1], 10);
|
|
114
|
-
updated.data = updated.data.slice(0, limit);
|
|
115
|
-
changes.push(`Limited data to top ${limit} rows`);
|
|
154
|
+
if (args.limit !== undefined) {
|
|
155
|
+
spec.data = spec.data.slice(0, args.limit);
|
|
156
|
+
changes.push(`Limited to top ${args.limit} rows`);
|
|
116
157
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
158
|
+
if (args.colorField !== undefined) {
|
|
159
|
+
if (!spec.encoding.color)
|
|
160
|
+
spec.encoding.color = {};
|
|
161
|
+
spec.encoding.color.field = args.colorField;
|
|
162
|
+
if (!spec.encoding.color.type)
|
|
163
|
+
spec.encoding.color.type = 'nominal';
|
|
164
|
+
changes.push(`Set color field to '${args.colorField}'`);
|
|
125
165
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
changes.push(`
|
|
166
|
+
if (args.palette !== undefined) {
|
|
167
|
+
if (!spec.encoding.color)
|
|
168
|
+
spec.encoding.color = {};
|
|
169
|
+
spec.encoding.color.palette = args.palette;
|
|
170
|
+
changes.push(`Applied ${args.palette} palette`);
|
|
131
171
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
if (!updated.encoding.color)
|
|
139
|
-
updated.encoding.color = {};
|
|
140
|
-
updated.encoding.color.palette = paletteResult[0];
|
|
141
|
-
changes.push(`Applied ${paletteResult[1]}`);
|
|
172
|
+
if (args.highlight !== undefined) {
|
|
173
|
+
if (args.highlight === null) {
|
|
174
|
+
if (spec.encoding.color) {
|
|
175
|
+
delete spec.encoding.color.highlight;
|
|
176
|
+
}
|
|
177
|
+
changes.push('Cleared highlight');
|
|
142
178
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
179
|
+
else {
|
|
180
|
+
if (!spec.encoding.color)
|
|
181
|
+
spec.encoding.color = {};
|
|
182
|
+
const colorField = spec.encoding.color.field;
|
|
183
|
+
if (colorField && spec.data.length > 0) {
|
|
184
|
+
const normalize = (v) => String(v).trim().toLowerCase();
|
|
185
|
+
const dataVals = new Map();
|
|
186
|
+
for (const d of spec.data) {
|
|
187
|
+
const raw = String(d[colorField]);
|
|
188
|
+
dataVals.set(normalize(raw), raw);
|
|
189
|
+
}
|
|
190
|
+
const matched = [];
|
|
191
|
+
const unmatched = [];
|
|
192
|
+
for (const v of args.highlight.values) {
|
|
193
|
+
const found = dataVals.get(normalize(v));
|
|
194
|
+
if (found !== undefined) {
|
|
195
|
+
matched.push(found);
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
unmatched.push(v);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
if (unmatched.length > 0) {
|
|
202
|
+
notes.push(`Highlight: ${unmatched.map(v => `'${v}'`).join(', ')} not found in '${colorField}'.`);
|
|
203
|
+
}
|
|
204
|
+
if (matched.length > 0) {
|
|
205
|
+
spec.encoding.color.highlight = {
|
|
206
|
+
values: matched,
|
|
207
|
+
...(args.highlight.color != null ? { color: args.highlight.color } : {}),
|
|
208
|
+
...(args.highlight.mutedColor != null ? { mutedColor: args.highlight.mutedColor } : {}),
|
|
209
|
+
...(args.highlight.mutedOpacity != null ? { mutedOpacity: args.highlight.mutedOpacity } : {}),
|
|
159
210
|
};
|
|
160
|
-
changes.push(`Highlighted
|
|
211
|
+
changes.push(`Highlighted: ${matched.join(', ')}`);
|
|
161
212
|
}
|
|
162
|
-
// If no values match data, skip highlight (it's likely descriptive text like "highlight differences")
|
|
163
213
|
}
|
|
164
214
|
else {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
...(highlight.color ? { color: highlight.color } : {}),
|
|
215
|
+
spec.encoding.color.highlight = {
|
|
216
|
+
values: args.highlight.values,
|
|
217
|
+
...(args.highlight.color != null ? { color: args.highlight.color } : {}),
|
|
218
|
+
...(args.highlight.mutedColor != null ? { mutedColor: args.highlight.mutedColor } : {}),
|
|
219
|
+
...(args.highlight.mutedOpacity != null ? { mutedOpacity: args.highlight.mutedOpacity } : {}),
|
|
171
220
|
};
|
|
172
|
-
changes.push(`Highlighted
|
|
221
|
+
changes.push(`Highlighted: ${args.highlight.values.join(', ')}`);
|
|
173
222
|
}
|
|
174
223
|
}
|
|
175
224
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
// Set opacity on existing highlight or create a placeholder
|
|
184
|
-
updated.encoding.color.highlight = { values: [], mutedOpacity: opacity };
|
|
185
|
-
}
|
|
186
|
-
else {
|
|
187
|
-
updated.encoding.color.highlight.mutedOpacity = opacity;
|
|
225
|
+
if (args.flip === true) {
|
|
226
|
+
if (FLIPPABLE_PATTERNS.has(spec.pattern)) {
|
|
227
|
+
const { x, y } = spec.encoding;
|
|
228
|
+
if (x && y) {
|
|
229
|
+
spec.encoding.x = y;
|
|
230
|
+
spec.encoding.y = x;
|
|
231
|
+
changes.push('Flipped axes');
|
|
188
232
|
}
|
|
189
|
-
changes.push(`Set muted opacity to ${opacity}`);
|
|
190
233
|
}
|
|
234
|
+
else {
|
|
235
|
+
notes.push(`Flip ignored — ${spec.pattern} charts don't have swappable axes.`);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (args.title !== undefined) {
|
|
239
|
+
spec.title = args.title;
|
|
240
|
+
changes.push(`Title: "${args.title}"`);
|
|
241
|
+
}
|
|
242
|
+
if (args.subtitle !== undefined) {
|
|
243
|
+
spec.config.subtitle = args.subtitle;
|
|
244
|
+
changes.push(`Subtitle: "${args.subtitle}"`);
|
|
245
|
+
}
|
|
246
|
+
if (args.xLabel !== undefined) {
|
|
247
|
+
if (spec.encoding.x)
|
|
248
|
+
spec.encoding.x.title = args.xLabel;
|
|
249
|
+
changes.push(`X-axis label: "${args.xLabel}"`);
|
|
191
250
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
changes.push(`Set colorBy to "${colorByMatch[1]}"`);
|
|
251
|
+
if (args.yLabel !== undefined) {
|
|
252
|
+
if (spec.encoding.y)
|
|
253
|
+
spec.encoding.y.title = args.yLabel;
|
|
254
|
+
changes.push(`Y-axis label: "${args.yLabel}"`);
|
|
197
255
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
if (
|
|
201
|
-
|
|
202
|
-
|
|
256
|
+
if (args.format !== undefined) {
|
|
257
|
+
const fmt = FORMAT_MAP[args.format];
|
|
258
|
+
if (fmt) {
|
|
259
|
+
if (spec.pattern === 'heatmap') {
|
|
260
|
+
if (!spec.encoding.color)
|
|
261
|
+
spec.encoding.color = {};
|
|
262
|
+
spec.encoding.color.format = fmt;
|
|
263
|
+
}
|
|
264
|
+
else if (spec.encoding.y) {
|
|
265
|
+
spec.encoding.y.format = fmt;
|
|
266
|
+
}
|
|
267
|
+
changes.push(`Applied ${args.format} format`);
|
|
203
268
|
}
|
|
204
269
|
}
|
|
205
|
-
if (
|
|
206
|
-
|
|
207
|
-
|
|
270
|
+
if (args.flowColorBy !== undefined) {
|
|
271
|
+
if (FLOW_PATTERNS.has(spec.pattern)) {
|
|
272
|
+
spec.config.colorBy = args.flowColorBy;
|
|
273
|
+
changes.push(`Set flow colorBy to '${args.flowColorBy}'`);
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
notes.push(`flowColorBy ignored — only applies to flow patterns (sankey, alluvial, chord, funnel).`);
|
|
277
|
+
}
|
|
208
278
|
}
|
|
209
|
-
return { spec
|
|
279
|
+
return { spec, changes, notes };
|
|
210
280
|
}
|
|
211
|
-
|
|
212
|
-
function applyCompoundRefinement(spec, refinement) {
|
|
281
|
+
function applyCompoundParams(spec, args, stored) {
|
|
213
282
|
const changes = [];
|
|
214
|
-
const
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
if (lower.includes('remove') && lower.includes('table')) {
|
|
218
|
-
const chartView = updated.views.find(v => v.type === 'chart');
|
|
283
|
+
const notes = [];
|
|
284
|
+
if (args.removeTable === true) {
|
|
285
|
+
const chartView = spec.views.find(v => v.type === 'chart');
|
|
219
286
|
if (chartView?.chart) {
|
|
220
|
-
// Unwrap compound to atomic spec
|
|
221
287
|
const atomicSpec = {
|
|
222
288
|
...chartView.chart,
|
|
223
|
-
data:
|
|
289
|
+
data: spec.data,
|
|
290
|
+
};
|
|
291
|
+
const subResult = applyAtomicParams(atomicSpec, { ...args, removeTable: undefined }, stored);
|
|
292
|
+
return {
|
|
293
|
+
spec: subResult.spec,
|
|
294
|
+
changes: ['Removed table, returned atomic chart', ...subResult.changes],
|
|
295
|
+
notes: subResult.notes,
|
|
224
296
|
};
|
|
225
|
-
return { spec: atomicSpec, changes: ['Removed table, returned atomic chart spec'] };
|
|
226
297
|
}
|
|
227
298
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
changes.push('Changed layout to side-by-side columns');
|
|
232
|
-
}
|
|
233
|
-
else if (lower.includes('stack') || lower.includes('rows') || lower.includes('below') || lower.includes('above')) {
|
|
234
|
-
updated.layout.type = 'rows';
|
|
235
|
-
changes.push('Changed layout to stacked rows');
|
|
299
|
+
if (args.layout !== undefined) {
|
|
300
|
+
spec.layout.type = args.layout;
|
|
301
|
+
changes.push(`Layout: ${args.layout}`);
|
|
236
302
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
if (highlightFieldMatch) {
|
|
240
|
-
const field = highlightFieldMatch[1];
|
|
241
|
-
updated.interactions = [{ type: 'highlight', field }];
|
|
242
|
-
changes.push(`Changed highlight interaction field to "${field}"`);
|
|
243
|
-
}
|
|
244
|
-
// ── Table column visibility ──
|
|
245
|
-
const hideMatch = lower.match(/hide\s+(?:the\s+)?(\w+)\s+column/);
|
|
246
|
-
if (hideMatch) {
|
|
247
|
-
const colName = hideMatch[1].toLowerCase();
|
|
248
|
-
const tableView = updated.views.find(v => v.type === 'table');
|
|
303
|
+
if (args.hideColumns !== undefined && args.hideColumns.length > 0) {
|
|
304
|
+
const tableView = spec.views.find(v => v.type === 'table');
|
|
249
305
|
if (tableView?.table?.columns) {
|
|
250
|
-
|
|
251
|
-
|
|
306
|
+
const hideSet = new Set(args.hideColumns.map(c => c.toLowerCase()));
|
|
307
|
+
tableView.table.columns = tableView.table.columns.filter(c => !hideSet.has(c.field.toLowerCase()) && !hideSet.has((c.title || '').toLowerCase()));
|
|
308
|
+
changes.push(`Hidden columns: ${args.hideColumns.join(', ')}`);
|
|
252
309
|
}
|
|
253
310
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
updated.title = titleMatch[1].trim();
|
|
258
|
-
// Also update chart view title
|
|
259
|
-
const chartView = updated.views.find(v => v.type === 'chart');
|
|
311
|
+
if (args.title !== undefined) {
|
|
312
|
+
spec.title = args.title;
|
|
313
|
+
const chartView = spec.views.find(v => v.type === 'chart');
|
|
260
314
|
if (chartView?.chart)
|
|
261
|
-
chartView.chart.title =
|
|
262
|
-
changes.push(`
|
|
315
|
+
chartView.chart.title = args.title;
|
|
316
|
+
changes.push(`Title: "${args.title}"`);
|
|
263
317
|
}
|
|
264
|
-
|
|
265
|
-
if (
|
|
266
|
-
const
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
318
|
+
const chartView = spec.views.find(v => v.type === 'chart');
|
|
319
|
+
if (chartView?.chart) {
|
|
320
|
+
const chartSpec = {
|
|
321
|
+
...chartView.chart,
|
|
322
|
+
data: spec.data,
|
|
323
|
+
};
|
|
324
|
+
const atomicArgs = { ...args, removeTable: undefined, layout: undefined, hideColumns: undefined, title: undefined };
|
|
325
|
+
const hasAtomicArgs = atomicArgs.sort !== undefined || atomicArgs.limit !== undefined ||
|
|
326
|
+
atomicArgs.filter !== undefined || atomicArgs.flip !== undefined ||
|
|
327
|
+
atomicArgs.palette !== undefined || atomicArgs.highlight !== undefined ||
|
|
328
|
+
atomicArgs.colorField !== undefined || atomicArgs.format !== undefined ||
|
|
329
|
+
atomicArgs.flowColorBy !== undefined || atomicArgs.subtitle !== undefined ||
|
|
330
|
+
atomicArgs.xLabel !== undefined || atomicArgs.yLabel !== undefined;
|
|
331
|
+
if (hasAtomicArgs) {
|
|
332
|
+
const subResult = applyAtomicParams(chartSpec, atomicArgs, stored);
|
|
333
|
+
const refinedChart = subResult.spec;
|
|
275
334
|
chartView.chart = {
|
|
276
335
|
pattern: refinedChart.pattern,
|
|
277
336
|
title: refinedChart.title,
|
|
278
337
|
encoding: refinedChart.encoding,
|
|
279
338
|
config: refinedChart.config,
|
|
280
339
|
};
|
|
281
|
-
|
|
282
|
-
changes.push(...
|
|
340
|
+
spec.data = refinedChart.data;
|
|
341
|
+
changes.push(...subResult.changes);
|
|
342
|
+
notes.push(...subResult.notes);
|
|
283
343
|
}
|
|
284
344
|
}
|
|
285
|
-
|
|
286
|
-
changes.push(`Refinement noted: "${refinement}" — manual adjustment may be needed`);
|
|
287
|
-
}
|
|
288
|
-
return { spec: updated, changes };
|
|
345
|
+
return { spec, changes, notes };
|
|
289
346
|
}
|
|
290
|
-
// ─── BUILD HTML FOR OUTPUT ──────────────────────────────────────────────────
|
|
291
347
|
function buildOutputHtml(spec) {
|
|
292
348
|
if (isCompoundSpec(spec)) {
|
|
293
349
|
return buildCompoundHtml(spec);
|
|
@@ -297,31 +353,57 @@ function buildOutputHtml(spec) {
|
|
|
297
353
|
}
|
|
298
354
|
return undefined;
|
|
299
355
|
}
|
|
300
|
-
// ─── HANDLER ────────────────────────────────────────────────────────────────
|
|
301
356
|
export function handleRefine() {
|
|
302
357
|
return async (args) => {
|
|
303
358
|
const start = Date.now();
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
359
|
+
const switchNotes = [];
|
|
360
|
+
const stored = specStore.get(args.specId);
|
|
361
|
+
if (!stored) {
|
|
362
|
+
return errorResponse(`Spec "${args.specId}" not found. It may have expired. Re-run visualize to get a new specId.`);
|
|
363
|
+
}
|
|
364
|
+
let spec = JSON.parse(JSON.stringify(stored.spec));
|
|
365
|
+
if (args.switchPattern) {
|
|
366
|
+
const altSpec = stored.alternatives.get(args.switchPattern);
|
|
367
|
+
if (altSpec) {
|
|
368
|
+
spec = JSON.parse(JSON.stringify(altSpec));
|
|
369
|
+
}
|
|
370
|
+
else {
|
|
371
|
+
try {
|
|
372
|
+
const data = isCompoundSpec(spec)
|
|
373
|
+
? spec.data
|
|
374
|
+
: spec.data;
|
|
375
|
+
const result = selectPattern(data, stored.columns, '', { forcePattern: args.switchPattern });
|
|
376
|
+
spec = result.recommended.spec;
|
|
377
|
+
for (const alt of result.alternatives) {
|
|
378
|
+
stored.alternatives.set(alt.pattern.id, alt.spec);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
catch {
|
|
382
|
+
switchNotes.push(`Pattern '${args.switchPattern}' cannot render this data shape. Available: ${[...stored.alternatives.keys()].join(', ')}`);
|
|
383
|
+
}
|
|
309
384
|
}
|
|
310
|
-
|
|
385
|
+
}
|
|
386
|
+
let result;
|
|
387
|
+
if (isCompoundSpec(spec)) {
|
|
388
|
+
result = applyCompoundParams(spec, args, stored);
|
|
311
389
|
}
|
|
312
390
|
else {
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
}
|
|
317
|
-
spec = stored.spec;
|
|
391
|
+
result = applyAtomicParams(spec, args, stored);
|
|
392
|
+
}
|
|
393
|
+
if (args.switchPattern && !switchNotes.length) {
|
|
394
|
+
result.changes.unshift(`Switched to ${args.switchPattern}`);
|
|
318
395
|
}
|
|
319
|
-
|
|
320
|
-
? applyCompoundRefinement(spec, args.refinement)
|
|
321
|
-
: applyRefinement(spec, args.refinement);
|
|
396
|
+
result.notes.push(...switchNotes);
|
|
322
397
|
const outputHtml = buildOutputHtml(result.spec);
|
|
323
398
|
const newSpecId = specStore.updateSpec(args.specId, result.spec);
|
|
324
|
-
const body = {
|
|
399
|
+
const body = {
|
|
400
|
+
specId: newSpecId,
|
|
401
|
+
changes: result.changes,
|
|
402
|
+
alternatives: [...stored.alternatives.keys()],
|
|
403
|
+
};
|
|
404
|
+
if (result.notes.length > 0) {
|
|
405
|
+
body.notes = result.notes;
|
|
406
|
+
}
|
|
325
407
|
const pattern = isCompoundSpec(result.spec)
|
|
326
408
|
? result.spec.views?.find((v) => v.type === 'chart')?.chart?.pattern
|
|
327
409
|
: result.spec.pattern;
|
|
@@ -339,7 +421,7 @@ export function handleRefine() {
|
|
|
339
421
|
if (outputHtml) {
|
|
340
422
|
return htmlResponse(body, outputHtml);
|
|
341
423
|
}
|
|
342
|
-
return
|
|
424
|
+
return { content: [{ type: 'text', text: JSON.stringify(body, null, 2) }] };
|
|
343
425
|
};
|
|
344
426
|
}
|
|
345
427
|
//# sourceMappingURL=refine.js.map
|