presenton/servers/nextjs/presentation-layouts/analytics/SingleChartSlideLayout.tsx
2025-07-18 20:49:06 +05:45

235 lines
No EOL
10 KiB
TypeScript

import React from 'react'
import * as z from "zod";
import { ChartContainer, ChartTooltip, ChartTooltipContent, ChartLegend, ChartLegendContent } from "@/components/ui/chart";
import { BarChart, Bar, LineChart, Line, PieChart, Pie, AreaChart, Area, ScatterChart, Scatter, XAxis, YAxis, CartesianGrid, Cell, ResponsiveContainer } from "recharts";
export const layoutId = 'single-chart-slide'
export const layoutName = 'Single Chart Slide'
export const layoutDescription = 'A layout for displaying one key metric or data trend with title and interactive chart.'
const chartDataSchema = z.object({
name: z.string().meta({ description: "Data point name" }),
value: z.number().meta({ description: "Data point value" }),
category: z.string().optional().meta({ description: "Category for grouping" }),
x: z.number().optional().meta({ description: "X coordinate for scatter plots" }),
y: z.number().optional().meta({ description: "Y coordinate for scatter plots" }),
});
const singleChartSlideSchema = z.object({
title: z.string().min(3).max(100).default('Revenue Growth Analysis').meta({
description: "Main title of the slide",
}),
description: z.string().min(10).max(300).default('This analysis reveals key performance trends across quarterly periods, identifying growth patterns and areas requiring strategic focus for sustained business success.').meta({
description: "Bottom description text",
}),
chartType: z.enum(['bar', 'line', 'pie', 'area', 'scatter']).default('bar').meta({
description: "Type of chart to display",
}),
data: z.array(chartDataSchema).min(2).max(10).default([
{ name: 'Jan', value: 4000 },
{ name: 'Feb', value: 3000 },
{ name: 'Mar', value: 2000 },
{ name: 'Apr', value: 2780 },
{ name: 'May', value: 1890 },
{ name: 'Jun', value: 2390 },
]).meta({
description: "Chart data points",
}),
dataKey: z.string().default('value').meta({
description: "Key field for chart values",
}),
categoryKey: z.string().default('name').meta({
description: "Key field for chart categories",
}),
color: z.string().default('#3b82f6').meta({
description: "Primary color for chart elements",
}),
showLegend: z.boolean().default(false).meta({
description: "Whether to show chart legend",
}),
showTooltip: z.boolean().default(true).meta({
description: "Whether to show chart tooltip",
}),
})
export const Schema = singleChartSlideSchema
export type SingleChartSlideData = z.infer<typeof singleChartSlideSchema>
interface SingleChartSlideLayoutProps {
data?: Partial<SingleChartSlideData>
}
const chartConfig = {
value: {
label: "Value",
},
name: {
label: "Name",
},
};
const CHART_COLORS = [
'#3b82f6', '#ef4444', '#10b981', '#f59e0b', '#8b5cf6',
'#06b6d4', '#84cc16', '#f97316', '#ec4899', '#6366f1'
];
const SingleChartSlideLayout: React.FC<SingleChartSlideLayoutProps> = ({ data: slideData }) => {
const chartData = slideData?.data || [];
const chartType = slideData?.chartType || 'bar';
const color = slideData?.color || '#3b82f6';
const dataKey = slideData?.dataKey || 'value';
const categoryKey = slideData?.categoryKey || 'name';
const showLegend = slideData?.showLegend || false;
const showTooltip = slideData?.showTooltip || true;
const renderChart = () => {
const commonProps = {
data: chartData,
margin: { top: 20, right: 30, left: 40, bottom: 60 },
};
switch (chartType) {
case 'bar':
return (
<BarChart {...commonProps}>
<CartesianGrid strokeDasharray="3 3" />
<XAxis dataKey={categoryKey} />
<YAxis />
{showTooltip && <ChartTooltip content={<ChartTooltipContent />} />}
{showLegend && <ChartLegend content={<ChartLegendContent />} />}
<Bar dataKey={dataKey} fill={color} radius={[4, 4, 0, 0]} />
</BarChart>
);
case 'line':
return (
<LineChart {...commonProps}>
<CartesianGrid strokeDasharray="3 3" />
<XAxis dataKey={categoryKey} />
<YAxis />
{showTooltip && <ChartTooltip content={<ChartTooltipContent />} />}
{showLegend && <ChartLegend content={<ChartLegendContent />} />}
<Line
type="monotone"
dataKey={dataKey}
stroke={color}
strokeWidth={3}
dot={{ fill: color, strokeWidth: 2, r: 4 }}
/>
</LineChart>
);
case 'area':
return (
<AreaChart {...commonProps}>
<CartesianGrid strokeDasharray="3 3" />
<XAxis dataKey={categoryKey} />
<YAxis />
{showTooltip && <ChartTooltip content={<ChartTooltipContent />} />}
{showLegend && <ChartLegend content={<ChartLegendContent />} />}
<Area
type="monotone"
dataKey={dataKey}
stroke={color}
fill={color}
fillOpacity={0.6}
/>
</AreaChart>
);
case 'pie':
return (
<PieChart margin={{ top: 20, right: 30, left: 40, bottom: 60 }}>
{showTooltip && <ChartTooltip content={<ChartTooltipContent />} />}
{showLegend && <ChartLegend content={<ChartLegendContent />} />}
<Pie
data={chartData}
cx="50%"
cy="40%"
outerRadius={70}
fill={color}
dataKey={dataKey}
label={({ name, percent }) => `${name} ${(percent * 100).toFixed(0)}%`}
>
{chartData.map((entry, index) => (
<Cell key={`cell-${index}`} fill={CHART_COLORS[index % CHART_COLORS.length]} />
))}
</Pie>
</PieChart>
);
case 'scatter':
return (
<ScatterChart {...commonProps}>
<CartesianGrid strokeDasharray="3 3" />
<XAxis dataKey="x" type="number" />
<YAxis dataKey="y" type="number" />
{showTooltip && <ChartTooltip content={<ChartTooltipContent />} />}
{showLegend && <ChartLegend content={<ChartLegendContent />} />}
<Scatter dataKey="value" fill={color} />
</ScatterChart>
);
default:
return <div>Unsupported chart type</div>;
}
};
return (
<>
{/* Import Google Fonts */}
<link
href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@400;500;600;700&family=Nunito:wght@400;500;600;700&display=swap"
rel="stylesheet"
/>
<div
className="w-full rounded-sm max-w-[1280px] shadow-md h-[720px] flex flex-col aspect-video bg-stone-100 relative z-20 mx-auto overflow-hidden"
style={{
fontFamily: 'Nunito, sans-serif'
}}
>
{/* Glass overlay background */}
<div className="absolute inset-0 bg-white/20 backdrop-blur-sm rounded-sm border border-slate-200"></div>
<div className="relative z-10 flex flex-col w-full h-full p-4 sm:p-6 lg:p-8">
{/* Header section */}
<div className="flex-shrink-0 h-16 sm:h-20 flex items-center justify-center">
<h1
className="text-3xl sm:text-3xl lg:text-4xl font-bold text-gray-900 leading-tight text-center"
style={{
fontFamily: 'Space Grotesk, sans-serif'
}}
>
{slideData?.title || 'Revenue Growth Analysis'}
</h1>
</div>
{/* Chart section */}
<div className="flex-1 w-full overflow-hidden">
<div className="bg-white/30 backdrop-blur-sm rounded-xl border border-slate-200 shadow-md p-4 sm:p-6 h-full overflow-hidden">
<ChartContainer config={chartConfig} className="h-full w-full max-h-[400px] sm:max-h-[420px] lg:max-h-[440px]">
{renderChart()}
</ChartContainer>
</div>
</div>
{/* Bottom Description section */}
<div className="flex-shrink-0 h-20 sm:h-24 flex items-center justify-center">
<p
className="text-sm sm:text-base text-center text-gray-700 leading-relaxed max-w-4xl px-4"
style={{
fontFamily: 'Nunito, sans-serif'
}}
>
{slideData?.description || 'This analysis reveals key performance trends across quarterly periods, identifying growth patterns and areas requiring strategic focus for sustained business success.'}
</p>
</div>
</div>
</div>
</>
)
}
export default SingleChartSlideLayout