@sqlrooms/vega 0.5.1 → 0.7.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 ADDED
@@ -0,0 +1,117 @@
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
+ ## Dependencies
111
+
112
+ This package depends on:
113
+
114
+ - `@sqlrooms/duckdb` - For SQL query execution
115
+ - `@sqlrooms/ui` - For UI utilities
116
+ - `@sqlrooms/utils` - For utility functions
117
+ - `react-vega` - For rendering Vega-Lite visualizations
package/dist/index.d.ts CHANGED
@@ -1,3 +1,7 @@
1
+ /**
2
+ * {@include ../README.md}
3
+ * @packageDocumentation
4
+ */
1
5
  export { VegaLiteChart } from './VegaLiteChart';
2
6
  export type { VisualizationSpec } from 'react-vega';
3
7
  //# sourceMappingURL=index.d.ts.map
@@ -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,YAAY,EAAC,iBAAiB,EAAC,MAAM,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -1,2 +1,6 @@
1
+ /**
2
+ * {@include ../README.md}
3
+ * @packageDocumentation
4
+ */
1
5
  export { VegaLiteChart } from './VegaLiteChart';
2
6
  //# 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":["export {VegaLiteChart} from './VegaLiteChart';\nexport type {VisualizationSpec} from 'react-vega';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC","sourcesContent":["/**\n * {@include ../README.md}\n * @packageDocumentation\n */\nexport {VegaLiteChart} from './VegaLiteChart';\nexport type {VisualizationSpec} from 'react-vega';\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sqlrooms/vega",
3
- "version": "0.5.1",
3
+ "version": "0.7.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "module": "dist/index.js",
@@ -19,9 +19,9 @@
19
19
  "access": "public"
20
20
  },
21
21
  "dependencies": {
22
- "@sqlrooms/duckdb": "0.5.1",
23
- "@sqlrooms/ui": "0.5.1",
24
- "@sqlrooms/utils": "0.5.1",
22
+ "@sqlrooms/duckdb": "0.7.0",
23
+ "@sqlrooms/ui": "0.7.0",
24
+ "@sqlrooms/utils": "0.7.0",
25
25
  "react-vega": "^7.6.0",
26
26
  "vega": "^5.31.0",
27
27
  "vega-lite": "^5.23.0"
@@ -36,5 +36,5 @@
36
36
  "lint": "eslint .",
37
37
  "typedoc": "typedoc"
38
38
  },
39
- "gitHead": "5e41362f1552ee1f8a661c487fe7cdb606cc6d23"
39
+ "gitHead": "8be65f051c588d3a963f721322429657913b6c63"
40
40
  }
@@ -1,50 +0,0 @@
1
- /**
2
- * Represents the dimensions of a chart
3
- * @interface ChartDimensions
4
- * @property {number} width - The width of the chart in pixels
5
- * @property {number} height - The height of the chart in pixels
6
- */
7
- export interface ChartDimensions {
8
- width: number;
9
- height: number;
10
- }
11
- /**
12
- * Props for the useChartDimensions hook
13
- * @interface UseChartDimensionsProps
14
- * @property {number | 'auto'} width - The explicitly provided width, or 'auto' for container-based width
15
- * @property {number | 'auto'} height - The explicitly provided height, or 'auto' for aspect ratio-based height
16
- * @property {number} aspectRatio - The desired width-to-height ratio when dimensions are auto-calculated
17
- * @property {React.RefObject<HTMLElement>} containerRef - Reference to the container element
18
- */
19
- export interface UseChartDimensionsProps {
20
- width: number | 'auto';
21
- height: number | 'auto';
22
- aspectRatio: number;
23
- containerRef: React.RefObject<HTMLElement>;
24
- }
25
- /**
26
- * A hook that calculates chart dimensions based on provided values and container size
27
- *
28
- * This hook handles various combinations of width/height specifications:
29
- * - If both width and height are provided, uses those exact dimensions
30
- * - If only width is provided, calculates height using the aspect ratio
31
- * - If only height is provided, calculates width using the aspect ratio
32
- * - If both are 'auto', uses container width and calculates height using the aspect ratio
33
- *
34
- * @param {UseChartDimensionsProps} props - The input parameters for dimension calculation
35
- * @returns {ChartDimensions} The calculated width and height
36
- *
37
- * @example
38
- * ```tsx
39
- * const containerRef = useRef<HTMLDivElement>(null);
40
- * const {width, height} = useChartDimensions({
41
- * width: 'auto',
42
- * height: 'auto',
43
- * aspectRatio: 16/9,
44
- * containerRef
45
- * });
46
- * // Returns dimensions based on container size
47
- * ```
48
- */
49
- export declare function useChartDimensions({ width, height, aspectRatio, containerRef, }: UseChartDimensionsProps): ChartDimensions;
50
- //# sourceMappingURL=useChartDimensions.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useChartDimensions.d.ts","sourceRoot":"","sources":["../../src/hooks/useChartDimensions.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,MAAM,WAAW,eAAe;IAC9B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,uBAAuB;IACtC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;IACvB,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC;IACxB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,KAAK,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC;CAC5C;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,kBAAkB,CAAC,EACjC,KAAK,EACL,MAAM,EACN,WAAW,EACX,YAAY,GACb,EAAE,uBAAuB,GAAG,eAAe,CAoB3C"}
@@ -1,47 +0,0 @@
1
- import { useMemo } from 'react';
2
- import { useResizeObserver } from 'usehooks-ts';
3
- /**
4
- * A hook that calculates chart dimensions based on provided values and container size
5
- *
6
- * This hook handles various combinations of width/height specifications:
7
- * - If both width and height are provided, uses those exact dimensions
8
- * - If only width is provided, calculates height using the aspect ratio
9
- * - If only height is provided, calculates width using the aspect ratio
10
- * - If both are 'auto', uses container width and calculates height using the aspect ratio
11
- *
12
- * @param {UseChartDimensionsProps} props - The input parameters for dimension calculation
13
- * @returns {ChartDimensions} The calculated width and height
14
- *
15
- * @example
16
- * ```tsx
17
- * const containerRef = useRef<HTMLDivElement>(null);
18
- * const {width, height} = useChartDimensions({
19
- * width: 'auto',
20
- * height: 'auto',
21
- * aspectRatio: 16/9,
22
- * containerRef
23
- * });
24
- * // Returns dimensions based on container size
25
- * ```
26
- */
27
- export function useChartDimensions({ width, height, aspectRatio, containerRef, }) {
28
- const { width: containerWidth = 0 } = useResizeObserver({
29
- ref: containerRef,
30
- box: 'border-box',
31
- });
32
- return useMemo(() => {
33
- if (width !== 'auto' && height !== 'auto') {
34
- return { width, height };
35
- }
36
- if (width !== 'auto') {
37
- return { width, height: width / aspectRatio };
38
- }
39
- if (height !== 'auto') {
40
- return { width: height * aspectRatio, height };
41
- }
42
- const finalWidth = containerWidth;
43
- const finalHeight = finalWidth / aspectRatio;
44
- return { width: finalWidth, height: finalHeight };
45
- }, [containerWidth, aspectRatio, width, height]);
46
- }
47
- //# sourceMappingURL=useChartDimensions.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useChartDimensions.js","sourceRoot":"","sources":["../../src/hooks/useChartDimensions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAC,MAAM,OAAO,CAAC;AAC9B,OAAO,EAAC,iBAAiB,EAAC,MAAM,aAAa,CAAC;AA4B9C;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,kBAAkB,CAAC,EACjC,KAAK,EACL,MAAM,EACN,WAAW,EACX,YAAY,GACY;IACxB,MAAM,EAAC,KAAK,EAAE,cAAc,GAAG,CAAC,EAAC,GAAG,iBAAiB,CAAC;QACpD,GAAG,EAAE,YAAY;QACjB,GAAG,EAAE,YAAY;KAClB,CAAC,CAAC;IAEH,OAAO,OAAO,CAAC,GAAG,EAAE;QAClB,IAAI,KAAK,KAAK,MAAM,IAAI,MAAM,KAAK,MAAM,EAAE,CAAC;YAC1C,OAAO,EAAC,KAAK,EAAE,MAAM,EAAC,CAAC;QACzB,CAAC;QACD,IAAI,KAAK,KAAK,MAAM,EAAE,CAAC;YACrB,OAAO,EAAC,KAAK,EAAE,MAAM,EAAE,KAAK,GAAG,WAAW,EAAC,CAAC;QAC9C,CAAC;QACD,IAAI,MAAM,KAAK,MAAM,EAAE,CAAC;YACtB,OAAO,EAAC,KAAK,EAAE,MAAM,GAAG,WAAW,EAAE,MAAM,EAAC,CAAC;QAC/C,CAAC;QACD,MAAM,UAAU,GAAG,cAAc,CAAC;QAClC,MAAM,WAAW,GAAG,UAAU,GAAG,WAAW,CAAC;QAC7C,OAAO,EAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,WAAW,EAAC,CAAC;IAClD,CAAC,EAAE,CAAC,cAAc,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC;AACnD,CAAC"}
@@ -1,16 +0,0 @@
1
- import { VisualizationSpec } from 'react-vega';
2
- /**
3
- * A component that renders a Vega-Lite chart.
4
- * @see Vega Lite Chart - https://vega.github.io/vega-lite/
5
- * @param props - The props for the component.
6
- * @returns The component.
7
- * @example
8
- * <VegaLiteChart sqlQuery="SELECT * FROM my_table" spec={spec} />
9
- */
10
- export declare const VegaLiteChart: React.FC<{
11
- width?: number;
12
- height?: number;
13
- sqlQuery: string;
14
- spec: string | VisualizationSpec;
15
- }>;
16
- //# sourceMappingURL=vega-lite-chart.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"vega-lite-chart.d.ts","sourceRoot":"","sources":["../src/vega-lite-chart.tsx"],"names":[],"mappings":"AAGA,OAAO,EAAW,iBAAiB,EAAC,MAAM,YAAY,CAAC;AAIvD;;;;;;;GAOG;AACH,eAAO,MAAM,aAAa,EAAE,KAAK,CAAC,EAAE,CAAC;IACnC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,GAAG,iBAAiB,CAAC;CAClC,CAyBA,CAAC"}
@@ -1,38 +0,0 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
2
- import { safeJsonParse } from '@sqlrooms/utils';
3
- import { arrowTableToJson, useDuckDb } from '@sqlrooms/duckdb';
4
- import { useEffect, useMemo, useState } from 'react';
5
- import { VegaLite } from 'react-vega';
6
- const DATA_NAME = 'queryResult';
7
- /**
8
- * A component that renders a Vega-Lite chart.
9
- * @see Vega Lite Chart - https://vega.github.io/vega-lite/
10
- * @param props - The props for the component.
11
- * @returns The component.
12
- * @example
13
- * <VegaLiteChart sqlQuery="SELECT * FROM my_table" spec={spec} />
14
- */
15
- export const VegaLiteChart = ({ width, height, sqlQuery, spec }) => {
16
- const { conn } = useDuckDb();
17
- const [data, setData] = useState();
18
- const refinedSpec = useMemo(() => {
19
- const parsed = typeof spec === 'string' ? safeJsonParse(spec) : spec;
20
- if (!parsed)
21
- return null;
22
- return {
23
- ...parsed,
24
- data: { name: DATA_NAME },
25
- };
26
- }, [spec]);
27
- useEffect(() => {
28
- const fetchData = async () => {
29
- const result = await conn.query(sqlQuery);
30
- setData({ [DATA_NAME]: arrowTableToJson(result) });
31
- };
32
- fetchData();
33
- }, [sqlQuery, conn]);
34
- if (!refinedSpec || !data)
35
- return null;
36
- return (_jsx("div", { className: "w-full flex flex-col gap-2 overflow-hidden", children: _jsx(VegaLite, { spec: refinedSpec, data: data, width: width, height: height }) }));
37
- };
38
- //# sourceMappingURL=vega-lite-chart.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"vega-lite-chart.js","sourceRoot":"","sources":["../src/vega-lite-chart.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAC,gBAA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