@object-ui/plugin-charts 0.3.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/CHANGELOG.md +48 -0
- package/LICENSE +21 -0
- package/README.md +121 -0
- package/dist/AdvancedChartImpl-LUnT2ZAf.js +2320 -0
- package/dist/BarChart-CRc8MAtI.js +17766 -0
- package/dist/ChartImpl-DiqV9Evl.js +79 -0
- package/dist/index-BcjHuFVN.js +738 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +9 -0
- package/dist/index.umd.cjs +85 -0
- package/dist/src/AdvancedChartImpl.d.ts +16 -0
- package/dist/src/ChartContainerImpl.d.ts +31 -0
- package/dist/src/ChartImpl.d.ts +13 -0
- package/dist/src/index.d.ts +42 -0
- package/dist/src/index.test.d.ts +1 -0
- package/dist/src/types.d.ts +47 -0
- package/package.json +42 -0
- package/src/AdvancedChartImpl.tsx +162 -0
- package/src/ChartContainerImpl.tsx +345 -0
- package/src/ChartImpl.tsx +83 -0
- package/src/index.test.ts +128 -0
- package/src/index.tsx +201 -0
- package/src/types.ts +60 -0
- package/tsconfig.json +17 -0
- package/vite.config.ts +38 -0
package/src/index.tsx
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import React, { Suspense } from 'react';
|
|
2
|
+
import { ComponentRegistry } from '@object-ui/core';
|
|
3
|
+
import { Skeleton } from '@object-ui/components';
|
|
4
|
+
|
|
5
|
+
// Export types for external use
|
|
6
|
+
export type { BarChartSchema } from './types';
|
|
7
|
+
|
|
8
|
+
// 🚀 Lazy load the implementation files
|
|
9
|
+
// This ensures Recharts is only loaded when the component is actually rendered
|
|
10
|
+
const LazyChart = React.lazy(() => import('./ChartImpl'));
|
|
11
|
+
const LazyAdvancedChart = React.lazy(() => import('./AdvancedChartImpl'));
|
|
12
|
+
|
|
13
|
+
export interface ChartBarRendererProps {
|
|
14
|
+
schema: {
|
|
15
|
+
type: string;
|
|
16
|
+
id?: string;
|
|
17
|
+
className?: string;
|
|
18
|
+
data?: Array<Record<string, any>>;
|
|
19
|
+
dataKey?: string;
|
|
20
|
+
xAxisKey?: string;
|
|
21
|
+
height?: number;
|
|
22
|
+
color?: string;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* ChartBarRenderer - The public API for the bar chart component
|
|
28
|
+
* This wrapper handles lazy loading internally using React.Suspense
|
|
29
|
+
*/
|
|
30
|
+
export const ChartBarRenderer: React.FC<ChartBarRendererProps> = ({ schema }) => {
|
|
31
|
+
return (
|
|
32
|
+
<Suspense fallback={<Skeleton className="w-full h-[400px]" />}>
|
|
33
|
+
<LazyChart
|
|
34
|
+
data={schema.data}
|
|
35
|
+
dataKey={schema.dataKey}
|
|
36
|
+
xAxisKey={schema.xAxisKey}
|
|
37
|
+
height={schema.height}
|
|
38
|
+
className={schema.className}
|
|
39
|
+
color={schema.color}
|
|
40
|
+
/>
|
|
41
|
+
</Suspense>
|
|
42
|
+
);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// Register the component with the ComponentRegistry
|
|
46
|
+
ComponentRegistry.register(
|
|
47
|
+
'chart-bar',
|
|
48
|
+
ChartBarRenderer,
|
|
49
|
+
{
|
|
50
|
+
label: 'Bar Chart',
|
|
51
|
+
category: 'plugin',
|
|
52
|
+
inputs: [
|
|
53
|
+
{ name: 'data', type: 'array', label: 'Data', required: true },
|
|
54
|
+
{ name: 'dataKey', type: 'string', label: 'Data Key', defaultValue: 'value' },
|
|
55
|
+
{ name: 'xAxisKey', type: 'string', label: 'X-Axis Key', defaultValue: 'name' },
|
|
56
|
+
{ name: 'height', type: 'number', label: 'Height', defaultValue: 400 },
|
|
57
|
+
{ name: 'color', type: 'color', label: 'Color', defaultValue: '#8884d8' },
|
|
58
|
+
],
|
|
59
|
+
defaultProps: {
|
|
60
|
+
data: [
|
|
61
|
+
{ name: 'Jan', value: 400 },
|
|
62
|
+
{ name: 'Feb', value: 300 },
|
|
63
|
+
{ name: 'Mar', value: 600 },
|
|
64
|
+
{ name: 'Apr', value: 800 },
|
|
65
|
+
{ name: 'May', value: 500 },
|
|
66
|
+
],
|
|
67
|
+
dataKey: 'value',
|
|
68
|
+
xAxisKey: 'name',
|
|
69
|
+
height: 400,
|
|
70
|
+
color: '#8884d8',
|
|
71
|
+
},
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
// Advanced Chart Renderer with multiple chart types
|
|
76
|
+
export interface ChartRendererProps {
|
|
77
|
+
schema: {
|
|
78
|
+
type: string;
|
|
79
|
+
id?: string;
|
|
80
|
+
className?: string;
|
|
81
|
+
chartType?: 'bar' | 'line' | 'area';
|
|
82
|
+
data?: Array<Record<string, any>>;
|
|
83
|
+
config?: Record<string, any>;
|
|
84
|
+
xAxisKey?: string;
|
|
85
|
+
series?: Array<{ dataKey: string }>;
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* ChartRenderer - The public API for the advanced chart component
|
|
91
|
+
* Supports multiple chart types (bar, line, area) with full configuration
|
|
92
|
+
*/
|
|
93
|
+
export const ChartRenderer: React.FC<ChartRendererProps> = ({ schema }) => {
|
|
94
|
+
// ⚡️ Adapter: Normalize JSON schema to Recharts Props
|
|
95
|
+
const props = React.useMemo(() => {
|
|
96
|
+
// 1. Defaults
|
|
97
|
+
let series = schema.series;
|
|
98
|
+
let xAxisKey = schema.xAxisKey;
|
|
99
|
+
let config = schema.config;
|
|
100
|
+
|
|
101
|
+
// 2. Adapt Tremor/Simple format (categories -> series, index -> xAxisKey)
|
|
102
|
+
if (!xAxisKey) {
|
|
103
|
+
if ((schema as any).index) xAxisKey = (schema as any).index;
|
|
104
|
+
else if ((schema as any).category) xAxisKey = (schema as any).category; // Support Pie/Donut category
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (!series) {
|
|
108
|
+
if ((schema as any).categories) {
|
|
109
|
+
series = (schema as any).categories.map((cat: string) => ({ dataKey: cat }));
|
|
110
|
+
} else if ((schema as any).value) {
|
|
111
|
+
// Single value adapter (for Pie/Simple charts)
|
|
112
|
+
series = [{ dataKey: (schema as any).value }];
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// 3. Auto-generate config/colors if missing
|
|
117
|
+
if (!config && series) {
|
|
118
|
+
const colors = (schema as any).colors || ['hsl(var(--chart-1))', 'hsl(var(--chart-2))', 'hsl(var(--chart-3))'];
|
|
119
|
+
config = {};
|
|
120
|
+
series.forEach((s: any, idx: number) => {
|
|
121
|
+
config[s.dataKey] = { label: s.dataKey, color: colors[idx % colors.length] };
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
chartType: schema.chartType,
|
|
127
|
+
data: schema.data,
|
|
128
|
+
config,
|
|
129
|
+
xAxisKey,
|
|
130
|
+
series,
|
|
131
|
+
className: schema.className
|
|
132
|
+
};
|
|
133
|
+
}, [schema]);
|
|
134
|
+
|
|
135
|
+
return (
|
|
136
|
+
<Suspense fallback={<Skeleton className="w-full h-[400px]" />}>
|
|
137
|
+
<LazyAdvancedChart
|
|
138
|
+
// Pass adapted props
|
|
139
|
+
chartType={props.chartType}
|
|
140
|
+
data={props.data}
|
|
141
|
+
config={props.config}
|
|
142
|
+
xAxisKey={props.xAxisKey}
|
|
143
|
+
series={props.series}
|
|
144
|
+
className={props.className}
|
|
145
|
+
/>
|
|
146
|
+
</Suspense>
|
|
147
|
+
);
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
// Register the advanced chart component
|
|
151
|
+
ComponentRegistry.register(
|
|
152
|
+
'chart',
|
|
153
|
+
ChartRenderer,
|
|
154
|
+
{
|
|
155
|
+
label: 'Chart',
|
|
156
|
+
category: 'plugin',
|
|
157
|
+
inputs: [
|
|
158
|
+
{
|
|
159
|
+
name: 'chartType',
|
|
160
|
+
type: 'enum',
|
|
161
|
+
label: 'Chart Type',
|
|
162
|
+
enum: [
|
|
163
|
+
{ label: 'Bar', value: 'bar' },
|
|
164
|
+
{ label: 'Line', value: 'line' },
|
|
165
|
+
{ label: 'Area', value: 'area' }
|
|
166
|
+
],
|
|
167
|
+
defaultValue: 'bar'
|
|
168
|
+
},
|
|
169
|
+
{ name: 'data', type: 'code', label: 'Data (JSON)', required: true },
|
|
170
|
+
{ name: 'config', type: 'code', label: 'Config (JSON)' },
|
|
171
|
+
{ name: 'xAxisKey', type: 'string', label: 'X Axis Key', defaultValue: 'name' },
|
|
172
|
+
{ name: 'series', type: 'code', label: 'Series (JSON Array)', required: true },
|
|
173
|
+
{ name: 'className', type: 'string', label: 'CSS Class' }
|
|
174
|
+
],
|
|
175
|
+
defaultProps: {
|
|
176
|
+
chartType: 'bar',
|
|
177
|
+
data: [
|
|
178
|
+
{ name: 'Jan', sales: 400, revenue: 240 },
|
|
179
|
+
{ name: 'Feb', sales: 300, revenue: 139 },
|
|
180
|
+
{ name: 'Mar', sales: 600, revenue: 380 },
|
|
181
|
+
{ name: 'Apr', sales: 800, revenue: 430 },
|
|
182
|
+
{ name: 'May', sales: 500, revenue: 220 },
|
|
183
|
+
],
|
|
184
|
+
config: {
|
|
185
|
+
sales: { label: 'Sales', color: '#8884d8' },
|
|
186
|
+
revenue: { label: 'Revenue', color: '#82ca9d' }
|
|
187
|
+
},
|
|
188
|
+
xAxisKey: 'name',
|
|
189
|
+
series: [
|
|
190
|
+
{ dataKey: 'sales' },
|
|
191
|
+
{ dataKey: 'revenue' }
|
|
192
|
+
]
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
// Standard Export Protocol - for manual integration
|
|
198
|
+
export const chartComponents = {
|
|
199
|
+
'chart-bar': ChartBarRenderer,
|
|
200
|
+
'chart': ChartRenderer,
|
|
201
|
+
};
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TypeScript type definitions for @object-ui/plugin-charts
|
|
3
|
+
*
|
|
4
|
+
* These types can be imported by applications using this plugin
|
|
5
|
+
* to get full TypeScript support for chart schemas.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { BaseSchema } from '@object-ui/types';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Bar Chart component schema.
|
|
12
|
+
* Renders a bar chart using Recharts library.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import type { BarChartSchema } from '@object-ui/plugin-charts';
|
|
17
|
+
*
|
|
18
|
+
* const chartSchema: BarChartSchema = {
|
|
19
|
+
* type: 'chart-bar',
|
|
20
|
+
* data: [
|
|
21
|
+
* { name: 'Jan', value: 400 },
|
|
22
|
+
* { name: 'Feb', value: 300 }
|
|
23
|
+
* ],
|
|
24
|
+
* dataKey: 'value',
|
|
25
|
+
* xAxisKey: 'name'
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export interface BarChartSchema extends BaseSchema {
|
|
30
|
+
type: 'chart-bar';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Array of data points to display in the chart.
|
|
34
|
+
*/
|
|
35
|
+
data?: Array<Record<string, any>>;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Key in the data object for the Y-axis values.
|
|
39
|
+
* @default 'value'
|
|
40
|
+
*/
|
|
41
|
+
dataKey?: string;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Key in the data object for the X-axis labels.
|
|
45
|
+
* @default 'name'
|
|
46
|
+
*/
|
|
47
|
+
xAxisKey?: string;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Height of the chart in pixels.
|
|
51
|
+
* @default 400
|
|
52
|
+
*/
|
|
53
|
+
height?: number;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Color of the bars.
|
|
57
|
+
* @default '#8884d8'
|
|
58
|
+
*/
|
|
59
|
+
color?: string;
|
|
60
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "dist",
|
|
5
|
+
"jsx": "react-jsx",
|
|
6
|
+
"baseUrl": ".",
|
|
7
|
+
"paths": {
|
|
8
|
+
"@/*": ["src/*"]
|
|
9
|
+
},
|
|
10
|
+
"noImplicitAny": true,
|
|
11
|
+
"noEmit": false,
|
|
12
|
+
"declaration": true,
|
|
13
|
+
"composite": true,
|
|
14
|
+
"skipLibCheck": true
|
|
15
|
+
},
|
|
16
|
+
"include": ["src"]
|
|
17
|
+
}
|
package/vite.config.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { defineConfig } from 'vite';
|
|
2
|
+
import react from '@vitejs/plugin-react';
|
|
3
|
+
import dts from 'vite-plugin-dts';
|
|
4
|
+
import { resolve } from 'path';
|
|
5
|
+
|
|
6
|
+
export default defineConfig({
|
|
7
|
+
plugins: [
|
|
8
|
+
react(),
|
|
9
|
+
dts({
|
|
10
|
+
insertTypesEntry: true,
|
|
11
|
+
include: ['src'],
|
|
12
|
+
}),
|
|
13
|
+
],
|
|
14
|
+
resolve: {
|
|
15
|
+
alias: {
|
|
16
|
+
'@': resolve(__dirname, './src'),
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
build: {
|
|
20
|
+
lib: {
|
|
21
|
+
entry: resolve(__dirname, 'src/index.tsx'),
|
|
22
|
+
name: 'ObjectUIPluginCharts',
|
|
23
|
+
fileName: 'index',
|
|
24
|
+
},
|
|
25
|
+
rollupOptions: {
|
|
26
|
+
external: ['react', 'react-dom', '@object-ui/components', '@object-ui/core', '@object-ui/react'],
|
|
27
|
+
output: {
|
|
28
|
+
globals: {
|
|
29
|
+
react: 'React',
|
|
30
|
+
'react-dom': 'ReactDOM',
|
|
31
|
+
'@object-ui/components': 'ObjectUIComponents',
|
|
32
|
+
'@object-ui/core': 'ObjectUICore',
|
|
33
|
+
'@object-ui/react': 'ObjectUIReact',
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
});
|