A comprehensive, reusable data visualization component library built with React, TypeScript, and Recharts for the CurrentDao energy trading platform.
- Multiple Chart Types: Line, Bar, Pie, and Area charts
- Interactive Features: Zoom, pan, tooltips, and click handlers
- Responsive Design: Adapts to different screen sizes automatically
- Accessibility: Full screen reader support with ARIA labels
- Export Functionality: Export charts as PNG or SVG
- Animation: Smooth transitions and animations
- Customizable Themes: Built-in themes for energy trading data
- Performance Optimized: Handles large datasets efficiently
The chart components are part of the CurrentDao frontend project. All required dependencies are already installed:
npm install recharts html2canvas date-fns framer-motionimport { LineChart } from '@/components/charts';
import { ChartData } from '@/types/charts';
const data: ChartData[] = [
{
name: 'Energy Price',
data: [
{ x: 'Jan', y: 100 },
{ x: 'Feb', y: 120 },
{ x: 'Mar', y: 110 },
],
},
];
export default function MyChart() {
return (
<LineChart
data={data}
title="Energy Trading Prices"
description="Monthly energy price trends"
showTooltip={true}
showLegend={true}
animation={true}
/>
);
}Perfect for showing trends over time, energy prices, and market data.
<LineChart
data={data}
title="Energy Price Trends"
strokeWidth={2}
curveType="monotone"
showArea={false}
gradient={true}
dot={true}
/>Props:
data: ChartData[] - The data to displaystrokeWidth: number - Line thickness (default: 2)curveType: string - Curve type ('linear', 'basis', 'monotone', etc.)showArea: boolean - Show area under the linegradient: boolean - Use gradient filldot: boolean - Show data points
Ideal for comparing values across categories, energy consumption by source, or trading volumes.
<BarChart
data={data}
title="Energy Consumption by Source"
layout="vertical"
barSize={40}
radius={4}
stackId="stack1"
/>Props:
data: ChartData[] - The data to displaylayout: 'vertical' | 'horizontal' - Bar orientationbarSize: number - Width/thickness of barsradius: number | [number, number, number, number] - Border radiusstackId: string - Stack bars with same ID
Great for showing proportions, market share, or energy source distribution.
<PieChart
data={pieData}
title="Energy Source Distribution"
innerRadius={60}
outerRadius={100}
showLabels={true}
paddingAngle={2}
/>Props:
data: PieChartData[] - The data to displayinnerRadius: number - Inner radius for donut chartsouterRadius: number - Outer radiusshowLabels: boolean - Show percentage labelspaddingAngle: number - Space between slices
Perfect for showing cumulative values, total energy production, or stacked metrics.
<AreaChart
data={data}
title="Cumulative Energy Production"
gradient={true}
stackId="stack1"
strokeWidth={2}
/>Props:
data: ChartData[] - The data to displaygradient: boolean - Use gradient fillstackId: string - Stack areas with same IDstrokeWidth: number - Line thickness
All chart components inherit these common props from ChartConfig:
interface ChartConfig {
width?: number;
height?: number;
margin?: {
top?: number;
right?: number;
bottom?: number;
left?: number;
};
theme?: ChartTheme;
animation?: boolean;
responsive?: boolean;
showTooltip?: boolean;
showLegend?: boolean;
showGrid?: boolean;
showXAxis?: boolean;
showYAxis?: boolean;
zoomEnabled?: boolean;
panEnabled?: boolean;
exportEnabled?: boolean;
}interface ChartData {
name: string;
data: ChartDataPoint[];
color?: string;
}
interface ChartDataPoint {
x: string | number | Date;
y: number;
label?: string;
color?: string;
}interface PieChartData {
name: string;
value: number;
color?: string;
}The library includes specialized helpers for energy trading data:
import {
processEnergyTradingData,
processMarketTrendData,
processUserAnalyticsData,
energyTradingTheme
} from '@/components/charts';
// Process raw energy trading data
const chartData = processEnergyTradingData(rawEnergyData);
// Use energy trading theme
<LineChart
data={chartData}
theme={energyTradingTheme}
title="Energy Trading Dashboard"
/>Create custom themes for your charts:
const customTheme: ChartTheme = {
backgroundColor: '#ffffff',
gridColor: '#e5e7eb',
textColor: '#374151',
colors: ['#3b82f6', '#10b981', '#f59e0b', '#ef4444'],
};
<LineChart data={data} theme={customTheme} />const handleDataPointClick = (data: any) => {
console.log('Clicked data:', data);
// Navigate to detail view, show modal, etc.
};
<LineChart
data={data}
onDataPointClick={handleDataPointClick}
/><LineChart
data={data}
exportEnabled={true}
showControls={true}
/>Users can export charts as PNG or SVG using the built-in controls.
All chart components include comprehensive accessibility features:
- ARIA Labels: Proper labeling for screen readers
- Keyboard Navigation: Full keyboard support
- Screen Reader Announcements: Dynamic announcements for interactions
- High Contrast: Compatible with high contrast modes
- Focus Management: Proper focus handling
<LineChart
data={data}
title="Energy Trading Prices"
description="Monthly energy price trends for solar and wind"
ariaLabel="Energy trading price chart"
/>Charts automatically adapt to different screen sizes:
- Mobile: Optimized layout and font sizes
- Tablet: Balanced layout for medium screens
- Desktop: Full-featured layout for large screens
<LineChart
data={data}
responsive={true}
// Automatically adjusts based on container size
/>- Virtual Scrolling: Handles large datasets efficiently
- Debounced Updates: Optimized resize and interaction handling
- Lazy Loading: Components load only when needed
- Memoization: Prevents unnecessary re-renders
import { useChart, useChartData } from '@/components/charts';
function CustomChart() {
const {
chartRef,
dimensions,
interactionState,
handlers,
} = useChart({
responsive: true,
zoomEnabled: true,
exportEnabled: true,
});
const { data, loading, error, fetchData } = useChartData();
// Custom implementation
}const CustomTooltip = ({ active, payload, label }) => {
if (!active || !payload) return null;
return (
<div className="custom-tooltip">
{/* Custom tooltip content */}
</div>
);
};
<LineChart
data={data}
tooltip={<CustomTooltip />}
/>The chart library includes comprehensive unit tests:
npm test -- charts.test.tsxTests cover:
- Component rendering
- Data handling
- User interactions
- Accessibility features
- Export functionality
- Responsive behavior
- Data Validation: Always validate chart data before rendering
- Error Handling: Provide fallback UI for error states
- Loading States: Show loading indicators during data fetch
- Performance: Use pagination or virtualization for large datasets
- Accessibility: Always provide meaningful descriptions and labels
- Chart Not Rendering: Check data format and ensure Recharts is properly imported
- Export Not Working: Verify html2canvas is installed and properly imported
- Responsive Issues: Ensure container has proper dimensions
- Performance Issues: Use data pagination or virtualization for large datasets
Enable debug mode to see detailed logging:
<LineChart
data={data}
debug={true}
/>When adding new chart features:
- Follow the existing component patterns
- Add comprehensive TypeScript types
- Include accessibility features
- Write unit tests
- Update documentation
This chart component library is part of the CurrentDao project and follows the same MIT license.