Using Chart Libraries:Claude Code 实战指南
了解using chart libraries:Claude Code 实战. 包含实用代码示例。
チャート库を通过 Claude Code活用する
ダッシュボードやレポート画面ではグラフ显示が不可或缺。Recharts、Chart.js、D3.jsなど多くの库がありますが、借助 Claude Code那ぞれの特性を活かしたチャート组件を快速构建可以。
库の選定
> React应用でチャートを显示したい。
> 折れ線グラフ、棒グラフ、円グラフ、エリアチャート实现。
> Rechartsを使って、响应式支持とダークモード支持で。
- Recharts: Reactネイティブ、宣言的でシンプル
- Chart.js: 軽量で幅広いチャート支持
- D3.js: 最高の柔軟性、学習コスト高
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">売上推移</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 / 10000).toFixed(0)}万`}
/>
<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}件`
}
/>
<Legend />
<Line yAxisId="revenue" type="monotone" dataKey="revenue" stroke="#3B82F6" name="売上" strokeWidth={2} dot={false} />
<Line yAxisId="orders" type="monotone" dataKey="orders" stroke="#10B981" name="注文数" strokeWidth={2} dot={false} />
</LineChart>
</ResponsiveContainer>
</div>
);
}
棒グラフ
// 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">カテゴリ別売上</h3>
<ResponsiveContainer width="100%" height={300}>
<BarChart data={data} layout="vertical">
<CartesianGrid strokeDasharray="3 3" horizontal={false} />
<XAxis type="number" tickFormatter={(v) => `¥${(v / 10000).toFixed(0)}万`} />
<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>
);
}
円グラフ
// 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>
);
}
チャートダッシュボードの組み立て
// 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">分析ダッシュボード</h1>
<SalesLineChart data={salesData} />
<div className="grid md:grid-cols-2 gap-6">
<CategoryBarChart data={categoryData} />
<DistributionPieChart data={channelData} title="チャネル別流入" />
</div>
</div>
);
}
関連文章
数据可視化全般は数据可視化の实现、ダッシュボードの构建は管理ダッシュボード开发。
Recharts的官方文档(recharts.org)でAPI参考を確授权以。
#Claude Code
#chart
#Recharts
#D3.js
#data visualization
免费
免费 PDF:5 分钟看懂 Claude Code 速查表
只需留下邮箱,我们就会立即把这份 A4 一页速查表 PDF 发送给你。
我们会严格保护你的个人信息,绝不发送垃圾邮件。
把 Claude Code 变成真正能带来结果的工作流
先领取中文说明的免费 PDF,再进入英文商品页选择合适的教材。如果你需要团队落地、流程设计或内容变现支持,也可以直接咨询。
本文作者
Masa
深度使用 Claude Code 的工程师。运营 claudecode-lab.com——一个涵盖 10 种语言、超过 2,000 页内容的科技媒体。
相关文章
Use Cases
每天发布多语言 Claude Code 文章前,要先检查的 7 件事
一份实用清单,帮助你每天发布多语言 Claude Code 文章时避免漏语言、CTA 错位和线上内容未更新。
Use Cases
Codex Automations 是什么?让 AI 在你睡觉时完成内容运营
用 Codex Automations 自动查看流量、选择主题、写文章、改善转化路径并部署网站的实用指南。
Use Cases
Claude Code × GCP Cloud Functions 完全指南 | 极速开发无服务器函数
用 Claude Code 高效开发 GCP Cloud Functions。从 HTTP/Pub/Sub/Firestore 触发器实现到本地测试、部署自动化,基于 Masa 的实战经验,附完整可运行代码示例。