66 * LICENSE file in the root directory of this source tree.
77 */
88
9- import React , { Suspense } from 'react' ;
109import { ComponentRegistry } from '@object-ui/core' ;
11- import { Skeleton } from '@object-ui/components ' ;
12- import type { ChartConfig } from './ChartContainerImpl' ;
10+ import { ChartBarRenderer , ChartRenderer } from './ChartRenderer ' ;
11+ import './ObjectChart' ; // Import for side-effects (registration of object-chart)
1312
1413// Export types for external use
1514export type { BarChartSchema } from './types' ;
16-
17- // 🚀 Lazy load the implementation files
18- // This ensures Recharts is only loaded when the component is actually rendered
19- const LazyChart = React . lazy ( ( ) => import ( './ChartImpl' ) ) ;
20- const LazyAdvancedChart = React . lazy ( ( ) => import ( './AdvancedChartImpl' ) ) ;
21-
22- export interface ChartBarRendererProps {
23- schema : {
24- type : string ;
25- id ?: string ;
26- className ?: string ;
27- data ?: Array < Record < string , any > > ;
28- dataKey ?: string ;
29- xAxisKey ?: string ;
30- height ?: number ;
31- color ?: string ;
32- } ;
33- }
34-
35- /**
36- * ChartBarRenderer - The public API for the bar chart component
37- * This wrapper handles lazy loading internally using React.Suspense
38- */
39- export const ChartBarRenderer : React . FC < ChartBarRendererProps > = ( { schema } ) => {
40- return (
41- < Suspense fallback = { < Skeleton className = "w-full h-[400px]" /> } >
42- < LazyChart
43- data = { schema . data }
44- dataKey = { schema . dataKey }
45- xAxisKey = { schema . xAxisKey }
46- height = { schema . height }
47- className = { schema . className }
48- color = { schema . color }
49- />
50- </ Suspense >
51- ) ;
52- } ;
15+ export { ChartBarRenderer , ChartRenderer } ;
5316
5417// Register the component with the ComponentRegistry
5518ComponentRegistry . register (
@@ -82,82 +45,6 @@ ComponentRegistry.register(
8245 }
8346) ;
8447
85- // Advanced Chart Renderer with multiple chart types
86- export interface ChartRendererProps {
87- schema : {
88- type : string ;
89- id ?: string ;
90- className ?: string ;
91- chartType ?: 'bar' | 'line' | 'area' ;
92- data ?: Array < Record < string , any > > ;
93- config ?: Record < string , any > ;
94- xAxisKey ?: string ;
95- series ?: Array < { dataKey : string } > ;
96- } ;
97- }
98-
99- /**
100- * ChartRenderer - The public API for the advanced chart component
101- * Supports multiple chart types (bar, line, area) with full configuration
102- */
103- export const ChartRenderer : React . FC < ChartRendererProps > = ( { schema } ) => {
104- // ⚡️ Adapter: Normalize JSON schema to Recharts Props
105- const props = React . useMemo ( ( ) => {
106- // 1. Defaults
107- let series = schema . series ;
108- let xAxisKey = schema . xAxisKey ;
109- let config = schema . config ;
110-
111- // 2. Adapt Tremor/Simple format (categories -> series, index -> xAxisKey)
112- if ( ! xAxisKey ) {
113- if ( ( schema as any ) . index ) xAxisKey = ( schema as any ) . index ;
114- else if ( ( schema as any ) . category ) xAxisKey = ( schema as any ) . category ; // Support Pie/Donut category
115- }
116-
117- if ( ! series ) {
118- if ( ( schema as any ) . categories ) {
119- series = ( schema as any ) . categories . map ( ( cat : string ) => ( { dataKey : cat } ) ) ;
120- } else if ( ( schema as any ) . value ) {
121- // Single value adapter (for Pie/Simple charts)
122- series = [ { dataKey : ( schema as any ) . value } ] ;
123- }
124- }
125-
126- // 3. Auto-generate config/colors if missing
127- if ( ! config && series ) {
128- const colors = ( schema as any ) . colors || [ 'hsl(var(--chart-1))' , 'hsl(var(--chart-2))' , 'hsl(var(--chart-3))' ] ;
129- const newConfig : ChartConfig = { } ;
130- series . forEach ( ( s : any , idx : number ) => {
131- newConfig [ s . dataKey ] = { label : s . dataKey , color : colors [ idx % colors . length ] } ;
132- } ) ;
133- config = newConfig ;
134- }
135-
136- return {
137- chartType : schema . chartType ,
138- data : schema . data ,
139- config,
140- xAxisKey,
141- series,
142- className : schema . className
143- } ;
144- } , [ schema ] ) ;
145-
146- return (
147- < Suspense fallback = { < Skeleton className = "w-full h-[400px]" /> } >
148- < LazyAdvancedChart
149- // Pass adapted props
150- chartType = { props . chartType }
151- data = { props . data }
152- config = { props . config }
153- xAxisKey = { props . xAxisKey }
154- series = { props . series }
155- className = { props . className }
156- />
157- </ Suspense >
158- ) ;
159- } ;
160-
16148// Register the advanced chart component
16249ComponentRegistry . register (
16350 'chart' ,
@@ -210,16 +97,10 @@ ComponentRegistry.register(
21097 }
21198) ;
21299
213- // Standard Export Protocol - for manual integration
214- export const chartComponents = {
215- 'bar-chart' : ChartBarRenderer ,
216- 'chart' : ChartRenderer ,
217- } ;
218-
219100// Alias for CRM App compatibility
220101ComponentRegistry . register (
221102 'chart:bar' ,
222- ChartRenderer , // Use the smart renderer to handle Tremor-like props
103+ ChartRenderer ,
223104 {
224105 namespace : 'plugin-charts' ,
225106 label : 'Bar Chart (Alias)' ,
@@ -228,7 +109,6 @@ ComponentRegistry.register(
228109 }
229110) ;
230111
231- // Register specific chart type aliases
232112ComponentRegistry . register (
233113 'pie-chart' ,
234114 ChartRenderer ,
0 commit comments