Using Chart Libraries with Claude Code
Learn about using chart libraries with Claude Code. Includes practical code examples.
Using Chart Libraries With Claude Code
Charts are essential in dashboards and reporting screens. There are many libraries available (Recharts, Chart.js, D3.js), and Claude Code can quickly build chart components that take advantage of each library’s strengths.
Choosing a Library
> I want to display charts in a React app.
> Implement line, bar, pie, and area charts.
> Use Recharts, with responsive and dark mode support.
- Recharts: React-native, declarative, simple
- Chart.js: Lightweight, supports a wide range of charts
- D3.js: Maximum flexibility, higher learning curve
Various Charts With Recharts
// src/components/charts/SalesLineChart.tsx
import {
LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip,
Legend, ResponsiveContainer
} from 'recharts';
interface DataPoint {
date: string;
revenue: number;
orders: number;
}
export function SalesLineChart({ data }: { data: DataPoint[] }) {
return (
<div className="bg-white dark:bg-gray-800 rounded-xl p-6 shadow-sm">
<h3 className="text-lg font-semibold mb-4 dark:text-white">Sales Trend</h3>
<ResponsiveContainer width="100%" height={300}>
<LineChart data={data}>
<CartesianGrid strokeDasharray="3 3" stroke="#374151" />
<XAxis dataKey="date" stroke="#9CA3AF" />
<YAxis
yAxisId="revenue"
stroke="#3B82F6"
tickFormatter={(v) => `$${(v / 1000).toFixed(0)}k`}
/>
<YAxis yAxisId="orders" orientation="right" stroke="#10B981" />
<Tooltip
contentStyle={{
backgroundColor: '#1F2937',
border: 'none',
borderRadius: '8px',
color: '#fff',
}}
formatter={(value: number, name: string) =>
name === 'revenue' ? `$${value.toLocaleString()}` : `${value} orders`
}
/>
<Legend />
<Line yAxisId="revenue" type="monotone" dataKey="revenue" stroke="#3B82F6" name="Revenue" strokeWidth={2} dot={false} />
<Line yAxisId="orders" type="monotone" dataKey="orders" stroke="#10B981" name="Orders" strokeWidth={2} dot={false} />
</LineChart>
</ResponsiveContainer>
</div>
);
}
Bar Chart
// src/components/charts/CategoryBarChart.tsx
import { BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, Cell } from 'recharts';
const COLORS = ['#3B82F6', '#10B981', '#F59E0B', '#EF4444', '#8B5CF6'];
interface CategoryData {
name: string;
value: number;
}
export function CategoryBarChart({ data }: { data: CategoryData[] }) {
return (
<div className="bg-white dark:bg-gray-800 rounded-xl p-6 shadow-sm">
<h3 className="text-lg font-semibold mb-4 dark:text-white">Sales by Category</h3>
<ResponsiveContainer width="100%" height={300}>
<BarChart data={data} layout="vertical">
<CartesianGrid strokeDasharray="3 3" horizontal={false} />
<XAxis type="number" tickFormatter={(v) => `$${(v / 1000).toFixed(0)}k`} />
<YAxis type="category" dataKey="name" width={100} />
<Tooltip formatter={(v: number) => `$${v.toLocaleString()}`} />
<Bar dataKey="value" radius={[0, 4, 4, 0]}>
{data.map((_, index) => (
<Cell key={index} fill={COLORS[index % COLORS.length]} />
))}
</Bar>
</BarChart>
</ResponsiveContainer>
</div>
);
}
Pie Chart
// src/components/charts/DistributionPieChart.tsx
import { PieChart, Pie, Cell, Tooltip, ResponsiveContainer, Legend } from 'recharts';
const COLORS = ['#3B82F6', '#10B981', '#F59E0B', '#EF4444', '#8B5CF6', '#EC4899'];
interface SegmentData {
name: string;
value: number;
}
export function DistributionPieChart({ data, title }: { data: SegmentData[]; title: string }) {
const total = data.reduce((sum, d) => sum + d.value, 0);
return (
<div className="bg-white dark:bg-gray-800 rounded-xl p-6 shadow-sm">
<h3 className="text-lg font-semibold mb-4 dark:text-white">{title}</h3>
<ResponsiveContainer width="100%" height={300}>
<PieChart>
<Pie
data={data}
cx="50%"
cy="50%"
innerRadius={60}
outerRadius={100}
dataKey="value"
label={({ name, value }) => `${name} ${((value / total) * 100).toFixed(0)}%`}
>
{data.map((_, index) => (
<Cell key={index} fill={COLORS[index % COLORS.length]} />
))}
</Pie>
<Tooltip formatter={(v: number) => `${v.toLocaleString()} (${((v / total) * 100).toFixed(1)}%)`} />
<Legend />
</PieChart>
</ResponsiveContainer>
</div>
);
}
Assembling a Chart Dashboard
// src/app/dashboard/analytics/page.tsx
import { SalesLineChart } from '@/components/charts/SalesLineChart';
import { CategoryBarChart } from '@/components/charts/CategoryBarChart';
import { DistributionPieChart } from '@/components/charts/DistributionPieChart';
export default async function AnalyticsPage() {
const [salesData, categoryData, channelData] = await Promise.all([
fetchSalesData(),
fetchCategoryData(),
fetchChannelData(),
]);
return (
<div className="space-y-6">
<h1 className="text-2xl font-bold dark:text-white">Analytics Dashboard</h1>
<SalesLineChart data={salesData} />
<div className="grid md:grid-cols-2 gap-6">
<CategoryBarChart data={categoryData} />
<DistributionPieChart data={channelData} title="Traffic by Channel" />
</div>
</div>
);
}
Related Articles
For data visualization generally, see data visualization implementation, and for dashboards, see admin dashboard development.
You can find the API reference in the official Recharts documentation.
Free PDF: Claude Code Cheatsheet in 5 Minutes
Just enter your email and we'll send you the single-page A4 cheatsheet right away.
We handle your data with care and never send spam.
Level up your Claude Code workflow
50 battle-tested prompt templates you can copy-paste into Claude Code right now.
About the Author
Masa
Engineer obsessed with Claude Code. Runs claudecode-lab.com, a 10-language tech media with 2,000+ pages.
Related Posts
7 Deployment Checks Before You Publish a Multilingual Claude Code Article Every Day
A practical checklist for publishing daily multilingual Claude Code articles without missing locales, breaking CTAs, or shipping stale pages.
Codex Automations for Content Ops: A Daily Revenue Workflow for Claude Code Sites
Use Codex Automations to turn analytics, article updates, CTA improvements, deployment, and verification into a daily revenue workflow.
Claude Code × GCP Cloud Functions Complete Guide | Rapid Serverless Function Development
Streamline GCP Cloud Functions with Claude Code. Implement HTTP/Pub/Sub/Firestore triggers, local testing, and deployment automation with real-world code examples from Masa's experience.
Related Products
50 Battle-Tested Claude Code Prompt Templates
Copy, paste, ship. 50 production-ready prompts.
Use proven prompts for code review, refactoring, testing, documentation, debugging, architecture, and incident response.
The Complete Claude Code Setup & Configuration Guide
From install to team-ready workflow.
A practical guide to installation, CLAUDE.md, hooks, MCP servers, permissions, IDE setup, and CI/CD workflows.