@sqlrooms/vega 0.6.0 → 0.8.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/README.md +206 -0
- package/dist/VegaChartTool.d.ts +33 -0
- package/dist/VegaChartTool.d.ts.map +1 -0
- package/dist/VegaChartTool.js +56 -0
- package/dist/VegaChartTool.js.map +1 -0
- package/dist/VegaChartToolComponent.d.ts +13 -0
- package/dist/VegaChartToolComponent.d.ts.map +1 -0
- package/dist/VegaChartToolComponent.js +12 -0
- package/dist/VegaChartToolComponent.js.map +1 -0
- package/dist/VegaChartToolResult.d.ts +14 -0
- package/dist/VegaChartToolResult.d.ts.map +1 -0
- package/dist/VegaChartToolResult.js +13 -0
- package/dist/VegaChartToolResult.js.map +1 -0
- package/dist/VegaLiteChart.js +1 -1
- package/dist/VegaLiteChart.js.map +1 -1
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/package.json +6 -5
package/README.md
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
A package that provides Vega-Lite visualization components for the SQLRooms framework, allowing you to create interactive data visualizations directly from SQL queries.
|
|
2
|
+
|
|
3
|
+
## About Vega-Lite
|
|
4
|
+
|
|
5
|
+
[Vega-Lite](https://vega.github.io/vega-lite/) is a high-level grammar of interactive graphics. It provides a concise, declarative JSON syntax to create an expressive range of visualizations for data analysis and presentation.
|
|
6
|
+
|
|
7
|
+
Vega-Lite specifications describe visualizations as encoding mappings from data to properties of graphical marks (e.g., points or bars). The Vega-Lite compiler automatically produces visualization components including axes, legends, and scales. This approach allows for concise specifications while giving users control to customize various parts of a visualization.
|
|
8
|
+
|
|
9
|
+
This package integrates Vega-Lite with SQLRooms, allowing you to easily create data visualizations directly from SQL queries.
|
|
10
|
+
|
|
11
|
+
## Components
|
|
12
|
+
|
|
13
|
+
### VegaLiteChart
|
|
14
|
+
|
|
15
|
+
A React component that renders a Vega-Lite chart with SQL data and responsive sizing.
|
|
16
|
+
|
|
17
|
+
#### Features
|
|
18
|
+
|
|
19
|
+
- **SQL-Powered**: Directly use SQL queries to fetch data for your visualizations
|
|
20
|
+
- **Responsive Design**: Multiple sizing options to fit any layout
|
|
21
|
+
- **Aspect Ratio Control**: Maintain visual consistency with customizable aspect ratios
|
|
22
|
+
- **Integration with DuckDB**: Seamlessly works with the SQLRooms DuckDB integration
|
|
23
|
+
|
|
24
|
+
#### Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npm install @sqlrooms/vega
|
|
28
|
+
# or
|
|
29
|
+
yarn add @sqlrooms/vega
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
#### Usage
|
|
33
|
+
|
|
34
|
+
```tsx
|
|
35
|
+
import {VegaLiteChart} from '@sqlrooms/vega';
|
|
36
|
+
|
|
37
|
+
// Basic usage
|
|
38
|
+
function MyChart() {
|
|
39
|
+
return (
|
|
40
|
+
<VegaLiteChart
|
|
41
|
+
sqlQuery="SELECT category, count(*) as count FROM sales GROUP BY category"
|
|
42
|
+
spec={{
|
|
43
|
+
mark: 'bar',
|
|
44
|
+
encoding: {
|
|
45
|
+
x: {field: 'category', type: 'nominal'},
|
|
46
|
+
y: {field: 'count', type: 'quantitative'},
|
|
47
|
+
},
|
|
48
|
+
}}
|
|
49
|
+
/>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
#### Props
|
|
55
|
+
|
|
56
|
+
| Prop | Type | Default | Description |
|
|
57
|
+
| ------------- | ----------------------------- | ----------- | --------------------------------------------------------------------- |
|
|
58
|
+
| `sqlQuery` | `string` | (required) | The SQL query to fetch data for the chart |
|
|
59
|
+
| `spec` | `string \| VisualizationSpec` | (required) | The Vega-Lite specification for the chart |
|
|
60
|
+
| `width` | `number \| 'auto'` | `'auto'` | The chart width in pixels, or 'auto' to use container width |
|
|
61
|
+
| `height` | `number \| 'auto'` | `'auto'` | The chart height in pixels, or 'auto' to calculate from aspect ratio |
|
|
62
|
+
| `aspectRatio` | `number` | `3/2` | The desired width-to-height ratio when dimensions are auto-calculated |
|
|
63
|
+
| `className` | `string` | `undefined` | Additional CSS classes to apply to the container |
|
|
64
|
+
|
|
65
|
+
#### Sizing Options
|
|
66
|
+
|
|
67
|
+
The chart can be sized in multiple ways:
|
|
68
|
+
|
|
69
|
+
- **Fixed dimensions**: Provide both width and height as numbers
|
|
70
|
+
- **Fixed width, proportional height**: Provide width as number, height as 'auto'
|
|
71
|
+
- **Fixed height, proportional width**: Provide height as number, width as 'auto'
|
|
72
|
+
- **Fully responsive**: Leave both as 'auto' (default), chart will fill container while maintaining aspect ratio
|
|
73
|
+
|
|
74
|
+
#### Examples
|
|
75
|
+
|
|
76
|
+
**Fixed size chart:**
|
|
77
|
+
|
|
78
|
+
```tsx
|
|
79
|
+
<VegaLiteChart
|
|
80
|
+
width={600}
|
|
81
|
+
height={400}
|
|
82
|
+
sqlQuery="SELECT category, count(*) as count FROM sales GROUP BY category"
|
|
83
|
+
spec={{
|
|
84
|
+
mark: 'bar',
|
|
85
|
+
encoding: {
|
|
86
|
+
x: {field: 'category', type: 'nominal'},
|
|
87
|
+
y: {field: 'count', type: 'quantitative'},
|
|
88
|
+
},
|
|
89
|
+
}}
|
|
90
|
+
/>
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
**Responsive chart with 16:9 aspect ratio:**
|
|
94
|
+
|
|
95
|
+
```tsx
|
|
96
|
+
<VegaLiteChart
|
|
97
|
+
className="max-w-[600px]"
|
|
98
|
+
aspectRatio={16 / 9}
|
|
99
|
+
sqlQuery="SELECT date, value FROM metrics"
|
|
100
|
+
spec={{
|
|
101
|
+
mark: 'line',
|
|
102
|
+
encoding: {
|
|
103
|
+
x: {field: 'date', type: 'temporal'},
|
|
104
|
+
y: {field: 'value', type: 'quantitative'},
|
|
105
|
+
},
|
|
106
|
+
}}
|
|
107
|
+
/>
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### VegaChartTool
|
|
111
|
+
|
|
112
|
+
A tool for creating Vega-Lite charts in AI-assisted workflows, designed to work with the [SQLRooms AI assistant framework](/api/ai/).
|
|
113
|
+
|
|
114
|
+
#### Features
|
|
115
|
+
|
|
116
|
+
- **AI Integration**: Allows AI assistants to create data visualizations
|
|
117
|
+
- **SQL-Powered**: Uses SQL queries to fetch data for visualizations
|
|
118
|
+
- **Declarative Specs**: Uses Vega-Lite's declarative JSON syntax for chart creation
|
|
119
|
+
- **Responsive Design**: Charts automatically adapt to container size
|
|
120
|
+
|
|
121
|
+
#### Usage
|
|
122
|
+
|
|
123
|
+
```tsx
|
|
124
|
+
import {createVegaChartTool} from '@sqlrooms/vega';
|
|
125
|
+
import {createAiSlice} from '@sqlrooms/ai';
|
|
126
|
+
import {createProjectStore} from '@sqlrooms/project-builder';
|
|
127
|
+
|
|
128
|
+
// Create a project store with the VegaChartTool configured
|
|
129
|
+
export const {projectStore, useProjectStore} = createProjectStore(
|
|
130
|
+
(set, get, store) => ({
|
|
131
|
+
// Other slices and state...
|
|
132
|
+
|
|
133
|
+
// AI slice with custom tools
|
|
134
|
+
...createAiSlice({
|
|
135
|
+
getApiKey: (modelProvider: string) => {
|
|
136
|
+
return get()?.apiKeys[modelProvider] || '';
|
|
137
|
+
},
|
|
138
|
+
// Configure custom tools at store creation time
|
|
139
|
+
customTools: {
|
|
140
|
+
// Add the VegaChart tool
|
|
141
|
+
chart: createVegaChartTool({
|
|
142
|
+
// Optional custom description
|
|
143
|
+
description:
|
|
144
|
+
'Create data visualizations using Vega-Lite and SQL queries',
|
|
145
|
+
}),
|
|
146
|
+
|
|
147
|
+
// Other custom tools...
|
|
148
|
+
},
|
|
149
|
+
})(set, get, store),
|
|
150
|
+
|
|
151
|
+
// Other state and methods...
|
|
152
|
+
}),
|
|
153
|
+
);
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
#### Tool Parameters
|
|
157
|
+
|
|
158
|
+
The VegaChartTool accepts the following parameters:
|
|
159
|
+
|
|
160
|
+
| Parameter | Type | Description |
|
|
161
|
+
| -------------- | -------- | ----------------------------------------------------------- |
|
|
162
|
+
| `sqlQuery` | `string` | The SQL query to fetch data for the chart |
|
|
163
|
+
| `vegaLiteSpec` | `string` | The Vega-Lite specification as a JSON string |
|
|
164
|
+
| `reasoning` | `string` | Optional explanation of the chart's purpose or significance |
|
|
165
|
+
|
|
166
|
+
#### Example AI Assistant Usage
|
|
167
|
+
|
|
168
|
+
When the AI assistant uses the VegaChartTool, it will generate a response like this:
|
|
169
|
+
|
|
170
|
+
```json
|
|
171
|
+
{
|
|
172
|
+
"sqlQuery": "SELECT product_category, SUM(sales) as total_sales FROM sales GROUP BY product_category ORDER BY total_sales DESC LIMIT 10",
|
|
173
|
+
"vegaLiteSpec": "{\"mark\": \"bar\", \"encoding\": {\"x\": {\"field\": \"product_category\", \"type\": \"nominal\", \"sort\": \"-y\"}, \"y\": {\"field\": \"total_sales\", \"type\": \"quantitative\", \"title\": \"Total Sales\"}, \"color\": {\"field\": \"product_category\", \"type\": \"nominal\", \"legend\": null}}}",
|
|
174
|
+
"reasoning": "This chart visualizes the top 10 product categories by total sales to identify the best-performing categories."
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
In a conversation with the AI assistant, it might look like this:
|
|
179
|
+
|
|
180
|
+
```
|
|
181
|
+
User: Can you show me a chart of our top 10 product categories by sales?
|
|
182
|
+
|
|
183
|
+
AI: I'll create a visualization of your top 10 product categories by sales.
|
|
184
|
+
|
|
185
|
+
[Chart: Bar chart showing product categories on the x-axis and total sales on the y-axis]
|
|
186
|
+
|
|
187
|
+
This chart displays your top 10 product categories ranked by total sales. As you can see,
|
|
188
|
+
Electronics leads with the highest sales, followed by Furniture and Clothing. This visualization
|
|
189
|
+
helps identify which product categories are driving the most revenue for your business.
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
The tool will:
|
|
193
|
+
|
|
194
|
+
1. Execute the SQL query to fetch the data
|
|
195
|
+
2. Apply the Vega-Lite specification to create the visualization
|
|
196
|
+
3. Render the chart in the UI with responsive sizing
|
|
197
|
+
|
|
198
|
+
## Dependencies
|
|
199
|
+
|
|
200
|
+
This package depends on:
|
|
201
|
+
|
|
202
|
+
- `@sqlrooms/duckdb` - For SQL query execution
|
|
203
|
+
- `@sqlrooms/ui` - For UI utilities
|
|
204
|
+
- `@sqlrooms/utils` - For utility functions
|
|
205
|
+
- `@sqlrooms/ai` - For AI assistant integration
|
|
206
|
+
- `react-vega` - For rendering Vega-Lite visualizations
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { AiSliceTool } from '@sqlrooms/ai';
|
|
3
|
+
/**
|
|
4
|
+
* Zod schema for the VegaChart tool parameters
|
|
5
|
+
*/
|
|
6
|
+
export declare const VegaChartToolParameters: z.ZodObject<{
|
|
7
|
+
sqlQuery: z.ZodString;
|
|
8
|
+
vegaLiteSpec: z.ZodString;
|
|
9
|
+
reasoning: z.ZodString;
|
|
10
|
+
}, "strip", z.ZodTypeAny, {
|
|
11
|
+
sqlQuery: string;
|
|
12
|
+
vegaLiteSpec: string;
|
|
13
|
+
reasoning: string;
|
|
14
|
+
}, {
|
|
15
|
+
sqlQuery: string;
|
|
16
|
+
vegaLiteSpec: string;
|
|
17
|
+
reasoning: string;
|
|
18
|
+
}>;
|
|
19
|
+
export type VegaChartToolParameters = z.infer<typeof VegaChartToolParameters>;
|
|
20
|
+
/**
|
|
21
|
+
* Default description for the VegaChart tool
|
|
22
|
+
*/
|
|
23
|
+
export declare const DEFAULT_VEGA_CHART_DESCRIPTION = "A tool for creating VegaLite charts based on the schema of the SQL query result from the \"query\" tool.\nIn the response:\n- omit the data from the vegaLiteSpec\n- provide an sql query in sqlQuery instead.";
|
|
24
|
+
/**
|
|
25
|
+
* Creates a VegaLite chart visualization tool for AI assistants
|
|
26
|
+
* @param options - Configuration options for the VegaChart tool
|
|
27
|
+
* @param options.description - Custom description for the tool (defaults to a standard description)
|
|
28
|
+
* @returns A tool that can be used with the AI assistant
|
|
29
|
+
*/
|
|
30
|
+
export declare function createVegaChartTool({ description, }?: {
|
|
31
|
+
description?: string;
|
|
32
|
+
}): AiSliceTool;
|
|
33
|
+
//# sourceMappingURL=VegaChartTool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VegaChartTool.d.ts","sourceRoot":"","sources":["../src/VegaChartTool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,CAAC,EAAC,MAAM,KAAK,CAAC;AAEtB,OAAO,EAAC,WAAW,EAAC,MAAM,cAAc,CAAC;AAEzC;;GAEG;AACH,eAAO,MAAM,uBAAuB;;;;;;;;;;;;EAIlC,CAAC;AAEH,MAAM,MAAM,uBAAuB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,uBAAuB,CAAC,CAAC;AAE9E;;GAEG;AACH,eAAO,MAAM,8BAA8B,mNAGC,CAAC;AAE7C;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,EAClC,WAA4C,GAC7C,GAAE;IACD,WAAW,CAAC,EAAE,MAAM,CAAC;CACjB,GAAG,WAAW,CA8BnB"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { VegaChartToolResult } from './VegaChartToolResult';
|
|
3
|
+
/**
|
|
4
|
+
* Zod schema for the VegaChart tool parameters
|
|
5
|
+
*/
|
|
6
|
+
export const VegaChartToolParameters = z.object({
|
|
7
|
+
sqlQuery: z.string(),
|
|
8
|
+
vegaLiteSpec: z.string(),
|
|
9
|
+
reasoning: z.string(),
|
|
10
|
+
});
|
|
11
|
+
/**
|
|
12
|
+
* Default description for the VegaChart tool
|
|
13
|
+
*/
|
|
14
|
+
export const DEFAULT_VEGA_CHART_DESCRIPTION = `A tool for creating VegaLite charts based on the schema of the SQL query result from the "query" tool.
|
|
15
|
+
In the response:
|
|
16
|
+
- omit the data from the vegaLiteSpec
|
|
17
|
+
- provide an sql query in sqlQuery instead.`;
|
|
18
|
+
/**
|
|
19
|
+
* Creates a VegaLite chart visualization tool for AI assistants
|
|
20
|
+
* @param options - Configuration options for the VegaChart tool
|
|
21
|
+
* @param options.description - Custom description for the tool (defaults to a standard description)
|
|
22
|
+
* @returns A tool that can be used with the AI assistant
|
|
23
|
+
*/
|
|
24
|
+
export function createVegaChartTool({ description = DEFAULT_VEGA_CHART_DESCRIPTION, } = {}) {
|
|
25
|
+
return {
|
|
26
|
+
description,
|
|
27
|
+
parameters: VegaChartToolParameters,
|
|
28
|
+
execute: async ({ sqlQuery, vegaLiteSpec }) => {
|
|
29
|
+
try {
|
|
30
|
+
const parsedVegaLiteSpec = JSON.parse(vegaLiteSpec);
|
|
31
|
+
// data object of the vegaLiteSpec and sqlQuery
|
|
32
|
+
// it is not used yet, but we can use it to create a JSON editor for user to edit the vegaLiteSpec so that chart can be updated
|
|
33
|
+
return {
|
|
34
|
+
llmResult: {
|
|
35
|
+
success: true,
|
|
36
|
+
details: 'Chart created successfully.',
|
|
37
|
+
},
|
|
38
|
+
additionalData: {
|
|
39
|
+
sqlQuery,
|
|
40
|
+
vegaLiteSpec: parsedVegaLiteSpec,
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
return {
|
|
46
|
+
llmResult: {
|
|
47
|
+
success: false,
|
|
48
|
+
details: `Not a valid JSON object: ${error}`,
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
component: VegaChartToolResult,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=VegaChartTool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VegaChartTool.js","sourceRoot":"","sources":["../src/VegaChartTool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,CAAC,EAAC,MAAM,KAAK,CAAC;AACtB,OAAO,EAAC,mBAAmB,EAAC,MAAM,uBAAuB,CAAC;AAG1D;;GAEG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,CAAC,MAAM,CAAC;IAC9C,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE;IACpB,YAAY,EAAE,CAAC,CAAC,MAAM,EAAE;IACxB,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;CACtB,CAAC,CAAC;AAIH;;GAEG;AACH,MAAM,CAAC,MAAM,8BAA8B,GAAG;;;4CAGF,CAAC;AAE7C;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CAAC,EAClC,WAAW,GAAG,8BAA8B,MAG1C,EAAE;IACJ,OAAO;QACL,WAAW;QACX,UAAU,EAAE,uBAAuB;QACnC,OAAO,EAAE,KAAK,EAAE,EAAC,QAAQ,EAAE,YAAY,EAA0B,EAAE,EAAE;YACnE,IAAI,CAAC;gBACH,MAAM,kBAAkB,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;gBACpD,+CAA+C;gBAC/C,+HAA+H;gBAC/H,OAAO;oBACL,SAAS,EAAE;wBACT,OAAO,EAAE,IAAI;wBACb,OAAO,EAAE,6BAA6B;qBACvC;oBACD,cAAc,EAAE;wBACd,QAAQ;wBACR,YAAY,EAAE,kBAAkB;qBACjC;iBACF,CAAC;YACJ,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,OAAO;oBACL,SAAS,EAAE;wBACT,OAAO,EAAE,KAAK;wBACd,OAAO,EAAE,4BAA4B,KAAK,EAAE;qBAC7C;iBACF,CAAC;YACJ,CAAC;QACH,CAAC;QACD,SAAS,EAAE,mBAAmB;KAC/B,CAAC;AACJ,CAAC","sourcesContent":["import {z} from 'zod';\nimport {VegaChartToolResult} from './VegaChartToolResult';\nimport {AiSliceTool} from '@sqlrooms/ai';\n\n/**\n * Zod schema for the VegaChart tool parameters\n */\nexport const VegaChartToolParameters = z.object({\n sqlQuery: z.string(),\n vegaLiteSpec: z.string(),\n reasoning: z.string(),\n});\n\nexport type VegaChartToolParameters = z.infer<typeof VegaChartToolParameters>;\n\n/**\n * Default description for the VegaChart tool\n */\nexport const DEFAULT_VEGA_CHART_DESCRIPTION = `A tool for creating VegaLite charts based on the schema of the SQL query result from the \"query\" tool.\nIn the response:\n- omit the data from the vegaLiteSpec\n- provide an sql query in sqlQuery instead.`;\n\n/**\n * Creates a VegaLite chart visualization tool for AI assistants\n * @param options - Configuration options for the VegaChart tool\n * @param options.description - Custom description for the tool (defaults to a standard description)\n * @returns A tool that can be used with the AI assistant\n */\nexport function createVegaChartTool({\n description = DEFAULT_VEGA_CHART_DESCRIPTION,\n}: {\n description?: string;\n} = {}): AiSliceTool {\n return {\n description,\n parameters: VegaChartToolParameters,\n execute: async ({sqlQuery, vegaLiteSpec}: VegaChartToolParameters) => {\n try {\n const parsedVegaLiteSpec = JSON.parse(vegaLiteSpec);\n // data object of the vegaLiteSpec and sqlQuery\n // it is not used yet, but we can use it to create a JSON editor for user to edit the vegaLiteSpec so that chart can be updated\n return {\n llmResult: {\n success: true,\n details: 'Chart created successfully.',\n },\n additionalData: {\n sqlQuery,\n vegaLiteSpec: parsedVegaLiteSpec,\n },\n };\n } catch (error) {\n return {\n llmResult: {\n success: false,\n details: `Not a valid JSON object: ${error}`,\n },\n };\n }\n },\n component: VegaChartToolResult,\n };\n}\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
type VegaChartToolComponentProps = {
|
|
2
|
+
reasoning: string;
|
|
3
|
+
sqlQuery: string;
|
|
4
|
+
vegaLiteSpec: string;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Renders a chart tool call with visualization using Vega-Lite
|
|
8
|
+
* @param {VegaChartToolComponentProps} props - The component props
|
|
9
|
+
* @returns {JSX.Element} The rendered chart tool call
|
|
10
|
+
*/
|
|
11
|
+
export declare function VegaChartToolComponent({ sqlQuery, vegaLiteSpec, }: VegaChartToolComponentProps): import("react/jsx-runtime").JSX.Element;
|
|
12
|
+
export {};
|
|
13
|
+
//# sourceMappingURL=VegaChartToolComponent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VegaChartToolComponent.d.ts","sourceRoot":"","sources":["../src/VegaChartToolComponent.tsx"],"names":[],"mappings":"AAGA,KAAK,2BAA2B,GAAG;IACjC,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,EACrC,QAAQ,EACR,YAAY,GACb,EAAE,2BAA2B,2CA2B7B"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { Suspense } from 'react';
|
|
3
|
+
import { VegaLiteChart } from './VegaLiteChart';
|
|
4
|
+
/**
|
|
5
|
+
* Renders a chart tool call with visualization using Vega-Lite
|
|
6
|
+
* @param {VegaChartToolComponentProps} props - The component props
|
|
7
|
+
* @returns {JSX.Element} The rendered chart tool call
|
|
8
|
+
*/
|
|
9
|
+
export function VegaChartToolComponent({ sqlQuery, vegaLiteSpec, }) {
|
|
10
|
+
return (_jsx(_Fragment, { children: vegaLiteSpec && (_jsxs("div", { className: "flex flex-col gap-2", children: [_jsxs("div", { className: "rounded-md border border-gray-200 bg-gray-50 p-2", children: [_jsx("h3", { className: "mb-1 font-medium", children: "Query" }), _jsx("pre", { className: "whitespace-pre-wrap text-sm", children: sqlQuery })] }), _jsx(Suspense, { fallback: _jsx("div", { className: "flex h-full w-full items-center justify-center", children: _jsx("div", { className: "h-4 w-4 animate-spin rounded-full border-2 border-gray-300 border-t-blue-600" }) }), children: _jsx(VegaLiteChart, { className: "max-w-[600px]", aspectRatio: 16 / 9, sqlQuery: sqlQuery, spec: vegaLiteSpec }) })] })) }));
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=VegaChartToolComponent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VegaChartToolComponent.js","sourceRoot":"","sources":["../src/VegaChartToolComponent.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC;AAQ9C;;;;GAIG;AACH,MAAM,UAAU,sBAAsB,CAAC,EACrC,QAAQ,EACR,YAAY,GACgB;IAC5B,OAAO,CACL,4BACG,YAAY,IAAI,CACf,eAAK,SAAS,EAAC,qBAAqB,aAClC,eAAK,SAAS,EAAC,kDAAkD,aAC/D,aAAI,SAAS,EAAC,kBAAkB,sBAAW,EAC3C,cAAK,SAAS,EAAC,6BAA6B,YAAE,QAAQ,GAAO,IACzD,EACN,KAAC,QAAQ,IACP,QAAQ,EACN,cAAK,SAAS,EAAC,gDAAgD,YAC7D,cAAK,SAAS,EAAC,8EAA8E,GAAO,GAChG,YAGR,KAAC,aAAa,IACZ,SAAS,EAAC,eAAe,EACzB,WAAW,EAAE,EAAE,GAAG,CAAC,EACnB,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,YAAY,GAClB,GACO,IACP,CACP,GACA,CACJ,CAAC;AACJ,CAAC","sourcesContent":["import {Suspense} from 'react';\nimport {VegaLiteChart} from './VegaLiteChart';\n\ntype VegaChartToolComponentProps = {\n reasoning: string;\n sqlQuery: string;\n vegaLiteSpec: string;\n};\n\n/**\n * Renders a chart tool call with visualization using Vega-Lite\n * @param {VegaChartToolComponentProps} props - The component props\n * @returns {JSX.Element} The rendered chart tool call\n */\nexport function VegaChartToolComponent({\n sqlQuery,\n vegaLiteSpec,\n}: VegaChartToolComponentProps) {\n return (\n <>\n {vegaLiteSpec && (\n <div className=\"flex flex-col gap-2\">\n <div className=\"rounded-md border border-gray-200 bg-gray-50 p-2\">\n <h3 className=\"mb-1 font-medium\">Query</h3>\n <pre className=\"whitespace-pre-wrap text-sm\">{sqlQuery}</pre>\n </div>\n <Suspense\n fallback={\n <div className=\"flex h-full w-full items-center justify-center\">\n <div className=\"h-4 w-4 animate-spin rounded-full border-2 border-gray-300 border-t-blue-600\"></div>\n </div>\n }\n >\n <VegaLiteChart\n className=\"max-w-[600px]\"\n aspectRatio={16 / 9}\n sqlQuery={sqlQuery}\n spec={vegaLiteSpec}\n />\n </Suspense>\n </div>\n )}\n </>\n );\n}\n"]}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { VisualizationSpec } from 'react-vega';
|
|
2
|
+
type VegaChartToolResultProps = {
|
|
3
|
+
reasoning: string;
|
|
4
|
+
sqlQuery: string;
|
|
5
|
+
vegaLiteSpec: VisualizationSpec;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Renders a chart tool call with visualization using Vega-Lite
|
|
9
|
+
* @param {VegaChartToolResultProps} props - The component props
|
|
10
|
+
* @returns {JSX.Element} The rendered chart tool call
|
|
11
|
+
*/
|
|
12
|
+
export declare function VegaChartToolResult({ sqlQuery, vegaLiteSpec, }: VegaChartToolResultProps): import("react/jsx-runtime").JSX.Element;
|
|
13
|
+
export {};
|
|
14
|
+
//# sourceMappingURL=VegaChartToolResult.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VegaChartToolResult.d.ts","sourceRoot":"","sources":["../src/VegaChartToolResult.tsx"],"names":[],"mappings":"AAGA,OAAO,EAAC,iBAAiB,EAAC,MAAM,YAAY,CAAC;AAE7C,KAAK,wBAAwB,GAAG;IAC9B,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,iBAAiB,CAAC;CACjC,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,EAClC,QAAQ,EACR,YAAY,GACb,EAAE,wBAAwB,2CAwB1B"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { Suspense } from 'react';
|
|
3
|
+
import { VegaLiteChart } from './VegaLiteChart';
|
|
4
|
+
import { QueryToolResult } from '@sqlrooms/ai';
|
|
5
|
+
/**
|
|
6
|
+
* Renders a chart tool call with visualization using Vega-Lite
|
|
7
|
+
* @param {VegaChartToolResultProps} props - The component props
|
|
8
|
+
* @returns {JSX.Element} The rendered chart tool call
|
|
9
|
+
*/
|
|
10
|
+
export function VegaChartToolResult({ sqlQuery, vegaLiteSpec, }) {
|
|
11
|
+
return (_jsx(_Fragment, { children: vegaLiteSpec && (_jsxs("div", { className: "flex flex-col gap-2", children: [_jsx(QueryToolResult, { title: "Query", sqlQuery: sqlQuery }), _jsx(Suspense, { fallback: _jsx("div", { className: "flex h-full w-full items-center justify-center", children: _jsx("div", { className: "h-4 w-4 animate-spin rounded-full border-2 border-gray-300 border-t-blue-600" }) }), children: _jsx(VegaLiteChart, { className: "max-w-[600px]", aspectRatio: 16 / 9, sqlQuery: sqlQuery, spec: vegaLiteSpec }) })] })) }));
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=VegaChartToolResult.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VegaChartToolResult.js","sourceRoot":"","sources":["../src/VegaChartToolResult.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAC,eAAe,EAAC,MAAM,cAAc,CAAC;AAS7C;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,EAClC,QAAQ,EACR,YAAY,GACa;IACzB,OAAO,CACL,4BACG,YAAY,IAAI,CACf,eAAK,SAAS,EAAC,qBAAqB,aAClC,KAAC,eAAe,IAAC,KAAK,EAAC,OAAO,EAAC,QAAQ,EAAE,QAAQ,GAAI,EACrD,KAAC,QAAQ,IACP,QAAQ,EACN,cAAK,SAAS,EAAC,gDAAgD,YAC7D,cAAK,SAAS,EAAC,8EAA8E,GAAO,GAChG,YAGR,KAAC,aAAa,IACZ,SAAS,EAAC,eAAe,EACzB,WAAW,EAAE,EAAE,GAAG,CAAC,EACnB,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,YAAY,GAClB,GACO,IACP,CACP,GACA,CACJ,CAAC;AACJ,CAAC","sourcesContent":["import {Suspense} from 'react';\nimport {VegaLiteChart} from './VegaLiteChart';\nimport {QueryToolResult} from '@sqlrooms/ai';\nimport {VisualizationSpec} from 'react-vega';\n\ntype VegaChartToolResultProps = {\n reasoning: string;\n sqlQuery: string;\n vegaLiteSpec: VisualizationSpec;\n};\n\n/**\n * Renders a chart tool call with visualization using Vega-Lite\n * @param {VegaChartToolResultProps} props - The component props\n * @returns {JSX.Element} The rendered chart tool call\n */\nexport function VegaChartToolResult({\n sqlQuery,\n vegaLiteSpec,\n}: VegaChartToolResultProps) {\n return (\n <>\n {vegaLiteSpec && (\n <div className=\"flex flex-col gap-2\">\n <QueryToolResult title=\"Query\" sqlQuery={sqlQuery} />\n <Suspense\n fallback={\n <div className=\"flex h-full w-full items-center justify-center\">\n <div className=\"h-4 w-4 animate-spin rounded-full border-2 border-gray-300 border-t-blue-600\"></div>\n </div>\n }\n >\n <VegaLiteChart\n className=\"max-w-[600px]\"\n aspectRatio={16 / 9}\n sqlQuery={sqlQuery}\n spec={vegaLiteSpec}\n />\n </Suspense>\n </div>\n )}\n </>\n );\n}\n"]}
|
package/dist/VegaLiteChart.js
CHANGED
|
@@ -87,6 +87,6 @@ export const VegaLiteChart = ({ className, width = 'auto', height = 'auto', aspe
|
|
|
87
87
|
};
|
|
88
88
|
fetchData();
|
|
89
89
|
}, [sqlQuery, conn]);
|
|
90
|
-
return (_jsx("div", { ref: containerRef, className: cn('
|
|
90
|
+
return (_jsx("div", { ref: containerRef, className: cn('flex h-full w-full flex-col gap-2 overflow-hidden', className), children: refinedSpec && data && _jsx(VegaLite, { spec: refinedSpec, data: data }) }));
|
|
91
91
|
};
|
|
92
92
|
//# sourceMappingURL=VegaLiteChart.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"VegaLiteChart.js","sourceRoot":"","sources":["../src/VegaLiteChart.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAC,gBAAgB,EAAE,SAAS,EAAC,MAAM,kBAAkB,CAAC;AAC7D,OAAO,EAAC,EAAE,EAAE,wBAAwB,EAAC,MAAM,cAAc,CAAC;AAC1D,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAC,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAC,MAAM,OAAO,CAAC;AAC3D,OAAO,EAAC,QAAQ,EAAoB,MAAM,YAAY,CAAC;AAEvD,MAAM,SAAS,GAAG,aAAa,CAAC;AAEhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AACH,MAAM,CAAC,MAAM,aAAa,GAOrB,CAAC,EACJ,SAAS,EACT,KAAK,GAAG,MAAM,EACd,MAAM,GAAG,MAAM,EACf,WAAW,GAAG,CAAC,GAAG,CAAC,EACnB,QAAQ,EACR,IAAI,GACL,EAAE,EAAE;IACH,MAAM,YAAY,GAAG,MAAM,CAAiB,IAAI,CAAC,CAAC;IAClD,MAAM,UAAU,GAAG,wBAAwB,CAAC;QAC1C,YAAY;QACZ,KAAK;QACL,MAAM;QACN,WAAW;KACZ,CAAC,CAAC;IAEH,MAAM,EAAC,IAAI,EAAC,GAAG,SAAS,EAAE,CAAC;IAC3B,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,QAAQ,EAA2B,CAAC;IAE5D,MAAM,WAAW,GAAG,OAAO,CAAC,GAAG,EAAE;QAC/B,MAAM,MAAM,GAAG,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;QACrE,IAAI,CAAC,MAAM;YAAE,OAAO,IAAI,CAAC;QACzB,OAAO;YACL,GAAG,MAAM;YACT,IAAI,EAAE,EAAC,IAAI,EAAE,SAAS,EAAC;YACvB,KAAK,EAAE,UAAU,CAAC,KAAK;YACvB,MAAM,EAAE,UAAU,CAAC,MAAM;YACzB,QAAQ,EAAE;gBACR,IAAI,EAAE,KAAK;gBACX,QAAQ,EAAE,SAAS;aACpB;SACmB,CAAC;IACzB,CAAC,EAAE,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC;IAEvB,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,SAAS,GAAG,KAAK,IAAI,EAAE;YAC3B,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;YAC1C,OAAO,CAAC,EAAC,CAAC,SAAS,CAAC,EAAE,gBAAgB,CAAC,MAAM,CAAC,EAAC,CAAC,CAAC;QACnD,CAAC,CAAC;QACF,SAAS,EAAE,CAAC;IACd,CAAC,EAAE,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC,CAAC;IAErB,OAAO,CACL,cACE,GAAG,EAAE,YAAY,EACjB,SAAS,EAAE,EAAE,CACX,mDAAmD,EACnD,SAAS,CACV,YAEA,WAAW,IAAI,IAAI,IAAI,KAAC,QAAQ,IAAC,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,IAAI,GAAI,GAC/D,CACP,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import {arrowTableToJson, useDuckDb} from '@sqlrooms/duckdb';\nimport {cn, useAspectRatioDimensions} from '@sqlrooms/ui';\nimport {safeJsonParse} from '@sqlrooms/utils';\nimport {useEffect, useMemo, useRef, useState} from 'react';\nimport {VegaLite, VisualizationSpec} from 'react-vega';\n\nconst DATA_NAME = 'queryResult';\n\n/**\n * A component that renders a Vega-Lite chart with SQL data and responsive sizing.\n *\n * The chart can be sized in multiple ways:\n * - Fixed dimensions: Provide both width and height as numbers\n * - Fixed width, proportional height: Provide width as number, height as 'auto'\n * - Fixed height, proportional width: Provide height as number, width as 'auto'\n * - Fully responsive: Leave both as 'auto' (default), chart will fill container while maintaining aspect ratio\n *\n * @param props - The component props\n * @param {number | 'auto'} [props.width='auto'] - The chart width in pixels, or 'auto' to use container width\n * @param {number | 'auto'} [props.height='auto'] - The chart height in pixels, or 'auto' to calculate from aspect ratio\n * @param {number} [props.aspectRatio=3/2] - The desired width-to-height ratio when dimensions are auto-calculated\n * @param {string} props.sqlQuery - The SQL query to fetch data for the chart\n * @param {string | VisualizationSpec} props.spec - The Vega-Lite specification for the chart.\n * Can be either a JSON string or a VisualizationSpec object.\n * The data and size properties will be overridden by the component.\n *\n * @returns The rendered chart component\n *\n * @example\n * // Fixed size chart\n * <VegaLiteChart\n * width={600}\n * height={400}\n * sqlQuery=\"SELECT category, count(*) as count FROM sales GROUP BY category\"\n * spec={{\n * mark: 'bar',\n * encoding: {\n * x: {field: 'category', type: 'nominal'},\n * y: {field: 'count', type: 'quantitative'}\n * }\n * }}\n * />\n *\n * @example\n * // Responsive chart with 16:9 aspect ratio\n * <VegaLiteChart\n * className=\"max-w-[600px]\"\n * aspectRatio={16/9}\n * sqlQuery=\"SELECT date, value FROM metrics\"\n * spec={{\n * mark: 'line',\n * encoding: {\n * x: {field: 'date', type: 'temporal'},\n * y: {field: 'value', type: 'quantitative'}\n * }\n * }}\n * />\n */\nexport const VegaLiteChart: React.FC<{\n className?: string;\n width?: number | 'auto';\n height?: number | 'auto';\n aspectRatio?: number;\n sqlQuery: string;\n spec: string | VisualizationSpec;\n}> = ({\n className,\n width = 'auto',\n height = 'auto',\n aspectRatio = 3 / 2,\n sqlQuery,\n spec,\n}) => {\n const containerRef = useRef<HTMLDivElement>(null);\n const dimensions = useAspectRatioDimensions({\n containerRef,\n width,\n height,\n aspectRatio,\n });\n\n const {conn} = useDuckDb();\n const [data, setData] = useState<Record<string, unknown>>();\n\n const refinedSpec = useMemo(() => {\n const parsed = typeof spec === 'string' ? safeJsonParse(spec) : spec;\n if (!parsed) return null;\n return {\n ...parsed,\n data: {name: DATA_NAME},\n width: dimensions.width,\n height: dimensions.height,\n autosize: {\n type: 'fit',\n contains: 'padding',\n },\n } as VisualizationSpec;\n }, [spec, dimensions]);\n\n useEffect(() => {\n const fetchData = async () => {\n const result = await conn.query(sqlQuery);\n setData({[DATA_NAME]: arrowTableToJson(result)});\n };\n fetchData();\n }, [sqlQuery, conn]);\n\n return (\n <div\n ref={containerRef}\n className={cn(\n '
|
|
1
|
+
{"version":3,"file":"VegaLiteChart.js","sourceRoot":"","sources":["../src/VegaLiteChart.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAC,gBAAgB,EAAE,SAAS,EAAC,MAAM,kBAAkB,CAAC;AAC7D,OAAO,EAAC,EAAE,EAAE,wBAAwB,EAAC,MAAM,cAAc,CAAC;AAC1D,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAC,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAC,MAAM,OAAO,CAAC;AAC3D,OAAO,EAAC,QAAQ,EAAoB,MAAM,YAAY,CAAC;AAEvD,MAAM,SAAS,GAAG,aAAa,CAAC;AAEhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AACH,MAAM,CAAC,MAAM,aAAa,GAOrB,CAAC,EACJ,SAAS,EACT,KAAK,GAAG,MAAM,EACd,MAAM,GAAG,MAAM,EACf,WAAW,GAAG,CAAC,GAAG,CAAC,EACnB,QAAQ,EACR,IAAI,GACL,EAAE,EAAE;IACH,MAAM,YAAY,GAAG,MAAM,CAAiB,IAAI,CAAC,CAAC;IAClD,MAAM,UAAU,GAAG,wBAAwB,CAAC;QAC1C,YAAY;QACZ,KAAK;QACL,MAAM;QACN,WAAW;KACZ,CAAC,CAAC;IAEH,MAAM,EAAC,IAAI,EAAC,GAAG,SAAS,EAAE,CAAC;IAC3B,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,QAAQ,EAA2B,CAAC;IAE5D,MAAM,WAAW,GAAG,OAAO,CAAC,GAAG,EAAE;QAC/B,MAAM,MAAM,GAAG,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;QACrE,IAAI,CAAC,MAAM;YAAE,OAAO,IAAI,CAAC;QACzB,OAAO;YACL,GAAG,MAAM;YACT,IAAI,EAAE,EAAC,IAAI,EAAE,SAAS,EAAC;YACvB,KAAK,EAAE,UAAU,CAAC,KAAK;YACvB,MAAM,EAAE,UAAU,CAAC,MAAM;YACzB,QAAQ,EAAE;gBACR,IAAI,EAAE,KAAK;gBACX,QAAQ,EAAE,SAAS;aACpB;SACmB,CAAC;IACzB,CAAC,EAAE,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC;IAEvB,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,SAAS,GAAG,KAAK,IAAI,EAAE;YAC3B,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;YAC1C,OAAO,CAAC,EAAC,CAAC,SAAS,CAAC,EAAE,gBAAgB,CAAC,MAAM,CAAC,EAAC,CAAC,CAAC;QACnD,CAAC,CAAC;QACF,SAAS,EAAE,CAAC;IACd,CAAC,EAAE,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC,CAAC;IAErB,OAAO,CACL,cACE,GAAG,EAAE,YAAY,EACjB,SAAS,EAAE,EAAE,CACX,mDAAmD,EACnD,SAAS,CACV,YAEA,WAAW,IAAI,IAAI,IAAI,KAAC,QAAQ,IAAC,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,IAAI,GAAI,GAC/D,CACP,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import {arrowTableToJson, useDuckDb} from '@sqlrooms/duckdb';\nimport {cn, useAspectRatioDimensions} from '@sqlrooms/ui';\nimport {safeJsonParse} from '@sqlrooms/utils';\nimport {useEffect, useMemo, useRef, useState} from 'react';\nimport {VegaLite, VisualizationSpec} from 'react-vega';\n\nconst DATA_NAME = 'queryResult';\n\n/**\n * A component that renders a Vega-Lite chart with SQL data and responsive sizing.\n *\n * The chart can be sized in multiple ways:\n * - Fixed dimensions: Provide both width and height as numbers\n * - Fixed width, proportional height: Provide width as number, height as 'auto'\n * - Fixed height, proportional width: Provide height as number, width as 'auto'\n * - Fully responsive: Leave both as 'auto' (default), chart will fill container while maintaining aspect ratio\n *\n * @param props - The component props\n * @param {number | 'auto'} [props.width='auto'] - The chart width in pixels, or 'auto' to use container width\n * @param {number | 'auto'} [props.height='auto'] - The chart height in pixels, or 'auto' to calculate from aspect ratio\n * @param {number} [props.aspectRatio=3/2] - The desired width-to-height ratio when dimensions are auto-calculated\n * @param {string} props.sqlQuery - The SQL query to fetch data for the chart\n * @param {string | VisualizationSpec} props.spec - The Vega-Lite specification for the chart.\n * Can be either a JSON string or a VisualizationSpec object.\n * The data and size properties will be overridden by the component.\n *\n * @returns The rendered chart component\n *\n * @example\n * // Fixed size chart\n * <VegaLiteChart\n * width={600}\n * height={400}\n * sqlQuery=\"SELECT category, count(*) as count FROM sales GROUP BY category\"\n * spec={{\n * mark: 'bar',\n * encoding: {\n * x: {field: 'category', type: 'nominal'},\n * y: {field: 'count', type: 'quantitative'}\n * }\n * }}\n * />\n *\n * @example\n * // Responsive chart with 16:9 aspect ratio\n * <VegaLiteChart\n * className=\"max-w-[600px]\"\n * aspectRatio={16/9}\n * sqlQuery=\"SELECT date, value FROM metrics\"\n * spec={{\n * mark: 'line',\n * encoding: {\n * x: {field: 'date', type: 'temporal'},\n * y: {field: 'value', type: 'quantitative'}\n * }\n * }}\n * />\n */\nexport const VegaLiteChart: React.FC<{\n className?: string;\n width?: number | 'auto';\n height?: number | 'auto';\n aspectRatio?: number;\n sqlQuery: string;\n spec: string | VisualizationSpec;\n}> = ({\n className,\n width = 'auto',\n height = 'auto',\n aspectRatio = 3 / 2,\n sqlQuery,\n spec,\n}) => {\n const containerRef = useRef<HTMLDivElement>(null);\n const dimensions = useAspectRatioDimensions({\n containerRef,\n width,\n height,\n aspectRatio,\n });\n\n const {conn} = useDuckDb();\n const [data, setData] = useState<Record<string, unknown>>();\n\n const refinedSpec = useMemo(() => {\n const parsed = typeof spec === 'string' ? safeJsonParse(spec) : spec;\n if (!parsed) return null;\n return {\n ...parsed,\n data: {name: DATA_NAME},\n width: dimensions.width,\n height: dimensions.height,\n autosize: {\n type: 'fit',\n contains: 'padding',\n },\n } as VisualizationSpec;\n }, [spec, dimensions]);\n\n useEffect(() => {\n const fetchData = async () => {\n const result = await conn.query(sqlQuery);\n setData({[DATA_NAME]: arrowTableToJson(result)});\n };\n fetchData();\n }, [sqlQuery, conn]);\n\n return (\n <div\n ref={containerRef}\n className={cn(\n 'flex h-full w-full flex-col gap-2 overflow-hidden',\n className,\n )}\n >\n {refinedSpec && data && <VegaLite spec={refinedSpec} data={data} />}\n </div>\n );\n};\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* {@include ../README.md}
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
1
5
|
export { VegaLiteChart } from './VegaLiteChart';
|
|
6
|
+
export { VegaChartToolResult as VegaChartToolResult } from './VegaChartToolResult';
|
|
2
7
|
export type { VisualizationSpec } from 'react-vega';
|
|
8
|
+
export { createVegaChartTool, VegaChartToolParameters, DEFAULT_VEGA_CHART_DESCRIPTION, } from './VegaChartTool';
|
|
9
|
+
export type { VegaChartToolParameters as VegaChartToolParametersType } from './VegaChartTool';
|
|
3
10
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC;AAC9C,YAAY,EAAC,iBAAiB,EAAC,MAAM,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAC,mBAAmB,IAAI,mBAAmB,EAAC,MAAM,uBAAuB,CAAC;AACjF,YAAY,EAAC,iBAAiB,EAAC,MAAM,YAAY,CAAC;AAClD,OAAO,EACL,mBAAmB,EACnB,uBAAuB,EACvB,8BAA8B,GAC/B,MAAM,iBAAiB,CAAC;AACzB,YAAY,EAAC,uBAAuB,IAAI,2BAA2B,EAAC,MAAM,iBAAiB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* {@include ../README.md}
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
1
5
|
export { VegaLiteChart } from './VegaLiteChart';
|
|
6
|
+
export { VegaChartToolResult as VegaChartToolResult } from './VegaChartToolResult';
|
|
7
|
+
export { createVegaChartTool, VegaChartToolParameters, DEFAULT_VEGA_CHART_DESCRIPTION, } from './VegaChartTool';
|
|
2
8
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC","sourcesContent":["
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAC,mBAAmB,IAAI,mBAAmB,EAAC,MAAM,uBAAuB,CAAC;AAEjF,OAAO,EACL,mBAAmB,EACnB,uBAAuB,EACvB,8BAA8B,GAC/B,MAAM,iBAAiB,CAAC","sourcesContent":["/**\n * {@include ../README.md}\n * @packageDocumentation\n */\nexport {VegaLiteChart} from './VegaLiteChart';\nexport {VegaChartToolResult as VegaChartToolResult} from './VegaChartToolResult';\nexport type {VisualizationSpec} from 'react-vega';\nexport {\n createVegaChartTool,\n VegaChartToolParameters,\n DEFAULT_VEGA_CHART_DESCRIPTION,\n} from './VegaChartTool';\nexport type {VegaChartToolParameters as VegaChartToolParametersType} from './VegaChartTool';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sqlrooms/vega",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -19,9 +19,10 @@
|
|
|
19
19
|
"access": "public"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@sqlrooms/
|
|
23
|
-
"@sqlrooms/
|
|
24
|
-
"@sqlrooms/
|
|
22
|
+
"@sqlrooms/ai": "0.8.0",
|
|
23
|
+
"@sqlrooms/duckdb": "0.8.0",
|
|
24
|
+
"@sqlrooms/ui": "0.8.0",
|
|
25
|
+
"@sqlrooms/utils": "0.8.0",
|
|
25
26
|
"react-vega": "^7.6.0",
|
|
26
27
|
"vega": "^5.31.0",
|
|
27
28
|
"vega-lite": "^5.23.0"
|
|
@@ -36,5 +37,5 @@
|
|
|
36
37
|
"lint": "eslint .",
|
|
37
38
|
"typedoc": "typedoc"
|
|
38
39
|
},
|
|
39
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "99b46a96ab900e6b005bcd30cfbfe7b3c9d51f8d"
|
|
40
41
|
}
|