Giới thiệu Recharts
Recharts là một thư viện vẽ biểu đồ (charting library) được xây dựng dựa trên D3.js và React components. Nó cung cấp:- 📊 16+ loại biểu đồ - Line, Bar, Pie, Area, Radar, Scatter, Funnel…
- ⚛️ React-native - Viết component thay vì configuration objects
- 🎨 Highly Customizable - Dễ tùy chỉnh colors, fonts, animations
- 📱 Responsive - Tự động responsive trên tất cả thiết bị
- 🎯 Interactive - Tooltips, legends, crosshair, active dot…
- 💾 Lightweight - ~60kb gzip
- ♿ Accessible - Support ARIA labels
Cài đặt
Dependencies
Các loại Biểu đồ (Chart Types)
Ví dụ Cơ Bản
1. Line Chart - Biểu đồ Đường
2. Bar Chart - Biểu đồ Cột
3. Pie Chart - Biểu đồ Tròn
4. Area Chart - Biểu đồ Diện tích
5. Radar Chart - Biểu đồ Radar
6. Composed Chart - Biểu đồ Hỗn hợp
Components Chính
Chart Containers
Axis Components
Grid & Reference
Interactive Components
Advanced Customization
Custom Tooltip
Custom Label
Theme Customization
Responsive Design
Mobile-friendly Charts
Performance Tips
1
Use ResponsiveContainer
Tự động responsive mà không cần media queries.
2
Lazy load charts
Dùng React.lazy + Suspense để lazy load charts.
3
Memo chart components
Wrap chart components với React.memo để tránh re-renders.
4
Optimize data
Chỉ pass cần thiết data vào chart component.
5
Debounce resize
Debounce window resize events để tránh quá nhiều re-renders.
6
Use server-side rendering
Recharts hỗ trợ SSR, tận dụng Next.js server components.
Real-world Example - Dashboard
Alternatives Comparison
Tài nguyên
- Official Website: https://recharts.org
- GitHub: https://github.com/recharts/recharts
- Documentation: https://recharts.org/en-US/
- API Reference: https://recharts.org/en-US/api
- Examples: https://recharts.org/en-US/examples