@sqlrooms/vega 0.4.0 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,60 @@
1
+ import { VisualizationSpec } from 'react-vega';
2
+ /**
3
+ * A component that renders a Vega-Lite chart with SQL data and responsive sizing.
4
+ *
5
+ * The chart can be sized in multiple ways:
6
+ * - Fixed dimensions: Provide both width and height as numbers
7
+ * - Fixed width, proportional height: Provide width as number, height as 'auto'
8
+ * - Fixed height, proportional width: Provide height as number, width as 'auto'
9
+ * - Fully responsive: Leave both as 'auto' (default), chart will fill container while maintaining aspect ratio
10
+ *
11
+ * @param props - The component props
12
+ * @param {number | 'auto'} [props.width='auto'] - The chart width in pixels, or 'auto' to use container width
13
+ * @param {number | 'auto'} [props.height='auto'] - The chart height in pixels, or 'auto' to calculate from aspect ratio
14
+ * @param {number} [props.aspectRatio=3/2] - The desired width-to-height ratio when dimensions are auto-calculated
15
+ * @param {string} props.sqlQuery - The SQL query to fetch data for the chart
16
+ * @param {string | VisualizationSpec} props.spec - The Vega-Lite specification for the chart.
17
+ * Can be either a JSON string or a VisualizationSpec object.
18
+ * The data and size properties will be overridden by the component.
19
+ *
20
+ * @returns The rendered chart component
21
+ *
22
+ * @example
23
+ * // Fixed size chart
24
+ * <VegaLiteChart
25
+ * width={600}
26
+ * height={400}
27
+ * sqlQuery="SELECT category, count(*) as count FROM sales GROUP BY category"
28
+ * spec={{
29
+ * mark: 'bar',
30
+ * encoding: {
31
+ * x: {field: 'category', type: 'nominal'},
32
+ * y: {field: 'count', type: 'quantitative'}
33
+ * }
34
+ * }}
35
+ * />
36
+ *
37
+ * @example
38
+ * // Responsive chart with 16:9 aspect ratio
39
+ * <VegaLiteChart
40
+ * className="max-w-[600px]"
41
+ * aspectRatio={16/9}
42
+ * sqlQuery="SELECT date, value FROM metrics"
43
+ * spec={{
44
+ * mark: 'line',
45
+ * encoding: {
46
+ * x: {field: 'date', type: 'temporal'},
47
+ * y: {field: 'value', type: 'quantitative'}
48
+ * }
49
+ * }}
50
+ * />
51
+ */
52
+ export declare const VegaLiteChart: React.FC<{
53
+ className?: string;
54
+ width?: number | 'auto';
55
+ height?: number | 'auto';
56
+ aspectRatio?: number;
57
+ sqlQuery: string;
58
+ spec: string | VisualizationSpec;
59
+ }>;
60
+ //# sourceMappingURL=VegaLiteChart.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"VegaLiteChart.d.ts","sourceRoot":"","sources":["../src/VegaLiteChart.tsx"],"names":[],"mappings":"AAIA,OAAO,EAAW,iBAAiB,EAAC,MAAM,YAAY,CAAC;AAIvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AACH,eAAO,MAAM,aAAa,EAAE,KAAK,CAAC,EAAE,CAAC;IACnC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACxB,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACzB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,GAAG,iBAAiB,CAAC;CAClC,CAqDA,CAAC"}
@@ -0,0 +1,92 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { arrowTableToJson, useDuckDb } from '@sqlrooms/duckdb';
3
+ import { cn, useAspectRatioDimensions } from '@sqlrooms/ui';
4
+ import { safeJsonParse } from '@sqlrooms/utils';
5
+ import { useEffect, useMemo, useRef, useState } from 'react';
6
+ import { VegaLite } from 'react-vega';
7
+ const DATA_NAME = 'queryResult';
8
+ /**
9
+ * A component that renders a Vega-Lite chart with SQL data and responsive sizing.
10
+ *
11
+ * The chart can be sized in multiple ways:
12
+ * - Fixed dimensions: Provide both width and height as numbers
13
+ * - Fixed width, proportional height: Provide width as number, height as 'auto'
14
+ * - Fixed height, proportional width: Provide height as number, width as 'auto'
15
+ * - Fully responsive: Leave both as 'auto' (default), chart will fill container while maintaining aspect ratio
16
+ *
17
+ * @param props - The component props
18
+ * @param {number | 'auto'} [props.width='auto'] - The chart width in pixels, or 'auto' to use container width
19
+ * @param {number | 'auto'} [props.height='auto'] - The chart height in pixels, or 'auto' to calculate from aspect ratio
20
+ * @param {number} [props.aspectRatio=3/2] - The desired width-to-height ratio when dimensions are auto-calculated
21
+ * @param {string} props.sqlQuery - The SQL query to fetch data for the chart
22
+ * @param {string | VisualizationSpec} props.spec - The Vega-Lite specification for the chart.
23
+ * Can be either a JSON string or a VisualizationSpec object.
24
+ * The data and size properties will be overridden by the component.
25
+ *
26
+ * @returns The rendered chart component
27
+ *
28
+ * @example
29
+ * // Fixed size chart
30
+ * <VegaLiteChart
31
+ * width={600}
32
+ * height={400}
33
+ * sqlQuery="SELECT category, count(*) as count FROM sales GROUP BY category"
34
+ * spec={{
35
+ * mark: 'bar',
36
+ * encoding: {
37
+ * x: {field: 'category', type: 'nominal'},
38
+ * y: {field: 'count', type: 'quantitative'}
39
+ * }
40
+ * }}
41
+ * />
42
+ *
43
+ * @example
44
+ * // Responsive chart with 16:9 aspect ratio
45
+ * <VegaLiteChart
46
+ * className="max-w-[600px]"
47
+ * aspectRatio={16/9}
48
+ * sqlQuery="SELECT date, value FROM metrics"
49
+ * spec={{
50
+ * mark: 'line',
51
+ * encoding: {
52
+ * x: {field: 'date', type: 'temporal'},
53
+ * y: {field: 'value', type: 'quantitative'}
54
+ * }
55
+ * }}
56
+ * />
57
+ */
58
+ export const VegaLiteChart = ({ className, width = 'auto', height = 'auto', aspectRatio = 3 / 2, sqlQuery, spec, }) => {
59
+ const containerRef = useRef(null);
60
+ const dimensions = useAspectRatioDimensions({
61
+ containerRef,
62
+ width,
63
+ height,
64
+ aspectRatio,
65
+ });
66
+ const { conn } = useDuckDb();
67
+ const [data, setData] = useState();
68
+ const refinedSpec = useMemo(() => {
69
+ const parsed = typeof spec === 'string' ? safeJsonParse(spec) : spec;
70
+ if (!parsed)
71
+ return null;
72
+ return {
73
+ ...parsed,
74
+ data: { name: DATA_NAME },
75
+ width: dimensions.width,
76
+ height: dimensions.height,
77
+ autosize: {
78
+ type: 'fit',
79
+ contains: 'padding',
80
+ },
81
+ };
82
+ }, [spec, dimensions]);
83
+ useEffect(() => {
84
+ const fetchData = async () => {
85
+ const result = await conn.query(sqlQuery);
86
+ setData({ [DATA_NAME]: arrowTableToJson(result) });
87
+ };
88
+ fetchData();
89
+ }, [sqlQuery, conn]);
90
+ return (_jsx("div", { ref: containerRef, className: cn('w-full h-full flex flex-col gap-2 overflow-hidden', className), children: refinedSpec && data && _jsx(VegaLite, { spec: refinedSpec, data: data }) }));
91
+ };
92
+ //# sourceMappingURL=VegaLiteChart.js.map
@@ -0,0 +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,CA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AAI,EAAE,WAAW,EAAE,IAAI,EAAE,IAAI,GAAI,GAC/D,CACP,CAAC;AACJ,CAAC,CAAC"}
@@ -0,0 +1,50 @@
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
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,47 @@
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
@@ -0,0 +1 @@
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"}
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { VegaLiteChart } from './vega-lite-chart';
1
+ export { VegaLiteChart } from './VegaLiteChart';
2
2
  export type { VisualizationSpec } from 'react-vega';
3
3
  //# 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,mBAAmB,CAAC;AAChD,YAAY,EAAC,iBAAiB,EAAC,MAAM,YAAY,CAAC"}
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"}
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- export { VegaLiteChart } from './vega-lite-chart';
1
+ export { VegaLiteChart } from './VegaLiteChart';
2
2
  //# 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,mBAAmB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAC,MAAM,iBAAiB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sqlrooms/vega",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "module": "dist/index.js",
@@ -19,8 +19,9 @@
19
19
  "access": "public"
20
20
  },
21
21
  "dependencies": {
22
- "@sqlrooms/duckdb": "0.4.0",
23
- "@sqlrooms/utils": "0.4.0",
22
+ "@sqlrooms/duckdb": "0.4.2",
23
+ "@sqlrooms/ui": "0.4.2",
24
+ "@sqlrooms/utils": "0.4.2",
24
25
  "react-vega": "^7.6.0",
25
26
  "vega": "^5.31.0",
26
27
  "vega-lite": "^5.23.0"
@@ -35,5 +36,5 @@
35
36
  "lint": "eslint .",
36
37
  "typedoc": "typedoc"
37
38
  },
38
- "gitHead": "fbc92d77cf3a70bdbef67bb0d7cf9342ed3e81a1"
39
+ "gitHead": "c71b75c7c6f391e0760803bcac674b8a59368922"
39
40
  }