Skip to content

Commit 0d9f655

Browse files
committed
feat(ReportViewer): enhance chart rendering logic with type inference and data mapping
1 parent 1e557cb commit 0d9f655

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

packages/plugin-report/src/ReportViewer.tsx

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,47 @@ export const ReportViewer: React.FC<ReportViewerProps> = ({ schema }) => {
150150

151151
{section.type === 'chart' && section.chart && (
152152
<div className="min-h-[300px]">
153-
<SchemaRenderer schema={{ ...section.chart, data: data || section.chart.data }} />
153+
{(() => {
154+
// 1. Determine Component Type
155+
// If explicit 'type' is missing, but 'chartType' exists (e.g. "line"), infer 'chart'
156+
let type = section.chart.type;
157+
const hasChartType = !!section.chart.chartType;
158+
159+
// If no strict type but has chartType, assume 'chart' generic renderer
160+
if (!type && hasChartType) {
161+
type = 'chart';
162+
}
163+
164+
// Fallback validation: If resolved type is not registered, try 'chart'
165+
const isRegistered = type && !!ComponentRegistry.get(type);
166+
if (!isRegistered) {
167+
// Even if 'line' was somehow passed as type, fallback to 'chart'
168+
type = 'chart';
169+
}
170+
171+
// 2. Data Adapter (Report Schema -> Chart Component Schema)
172+
// The generic 'chart' component needs mapped props (xAxisKey, series)
173+
// whereas Report schema uses (xAxisField, yAxisFields)
174+
const isGenericChart = type === 'chart';
175+
const adapterProps = isGenericChart ? {
176+
xAxisKey: section.chart.xAxisKey || section.chart.xAxisField || 'name',
177+
series: section.chart.series || (section.chart.yAxisFields ? section.chart.yAxisFields.map((f: any) => ({ dataKey: f })) : []),
178+
// Ensure chartType is passed if we are using the generic renderer
179+
chartType: section.chart.chartType || 'bar',
180+
} : {};
181+
182+
// 3. Construct Safe Schema
183+
const safeSchema = {
184+
...section.chart,
185+
type,
186+
...adapterProps,
187+
data: data || section.chart.data,
188+
// Force explicit height to preventing Recharts "height(-1)" error
189+
className: section.chart.className || 'w-full h-[350px]'
190+
};
191+
192+
return <SchemaRenderer schema={safeSchema} />;
193+
})()}
154194
</div>
155195
)}
156196

0 commit comments

Comments
 (0)