280 lines
4.8 KiB
JavaScript
280 lines
4.8 KiB
JavaScript
import React from 'react';
|
|
import {
|
|
ResponsiveContainer,
|
|
LineChart,
|
|
Line,
|
|
XAxis,
|
|
YAxis,
|
|
CartesianGrid,
|
|
Tooltip,
|
|
Legend,
|
|
PieChart,
|
|
Pie,
|
|
Cell,
|
|
BarChart,
|
|
Bar,
|
|
} from 'recharts';
|
|
|
|
|
|
// 默认颜色池
|
|
const DEFAULT_COLORS = [
|
|
'#1677ff',
|
|
'#52c41a',
|
|
'#fa8c16',
|
|
'#eb2f96',
|
|
'#722ed1',
|
|
'#13c2c2',
|
|
'#f5222d',
|
|
'#2f54eb',
|
|
];
|
|
|
|
|
|
|
|
export const UniversalLineChart = ({
|
|
data,
|
|
xKey,
|
|
series,
|
|
width = '100%',
|
|
height = 360,
|
|
showGrid = true,
|
|
showLegend = true,
|
|
showTooltip = true,
|
|
backgroundColor = '#ffffff',
|
|
smooth = true,
|
|
dot = false,
|
|
className,
|
|
}) => {
|
|
return (
|
|
<div
|
|
className={className}
|
|
style={{
|
|
width,
|
|
height,
|
|
background: backgroundColor,
|
|
borderRadius: 12,
|
|
padding: 12,
|
|
}}
|
|
>
|
|
<ResponsiveContainer width="100%" height="100%">
|
|
<LineChart data={data}>
|
|
{showGrid && (
|
|
<CartesianGrid strokeDasharray="3 3" opacity={0.25} />
|
|
)}
|
|
|
|
<XAxis dataKey={xKey} />
|
|
<YAxis />
|
|
|
|
{showTooltip && <Tooltip />}
|
|
{showLegend && <Legend />}
|
|
|
|
{series.map((item, index) => (
|
|
<Line
|
|
key={item.key}
|
|
type={smooth ? 'monotone' : 'linear'}
|
|
dataKey={item.key}
|
|
name={item.name}
|
|
stroke={item.color || DEFAULT_COLORS[index % DEFAULT_COLORS.length]}
|
|
strokeWidth={item.strokeWidth || 2}
|
|
strokeDasharray={item.dashed ? '6 6' : undefined}
|
|
dot={dot}
|
|
activeDot={{ r: 6 }}
|
|
/>
|
|
))}
|
|
</LineChart>
|
|
</ResponsiveContainer>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
|
|
|
|
export const UniversalBarChart = ({
|
|
data,
|
|
xKey,
|
|
series,
|
|
width = '100%',
|
|
height = 360,
|
|
showGrid = true,
|
|
showLegend = true,
|
|
showTooltip = true,
|
|
backgroundColor = '#ffffff',
|
|
barSize = 28,
|
|
radius = 6,
|
|
stack = false,
|
|
className,
|
|
}) => {
|
|
return (
|
|
<div
|
|
className={className}
|
|
style={{
|
|
width,
|
|
height,
|
|
background: backgroundColor,
|
|
borderRadius: 12,
|
|
padding: 12,
|
|
}}
|
|
>
|
|
<ResponsiveContainer width="100%" height="100%">
|
|
<BarChart data={data}>
|
|
{showGrid && (
|
|
<CartesianGrid strokeDasharray="3 3" opacity={0.25} />
|
|
)}
|
|
|
|
<XAxis dataKey={xKey} />
|
|
<YAxis />
|
|
|
|
{showTooltip && <Tooltip />}
|
|
{showLegend && <Legend />}
|
|
|
|
{series.map((item, index) => (
|
|
<Bar
|
|
key={item.key}
|
|
dataKey={item.key}
|
|
name={item.name}
|
|
fill={item.color || DEFAULT_COLORS[index % DEFAULT_COLORS.length]}
|
|
barSize={barSize}
|
|
radius={radius}
|
|
stackId={stack ? 'stack' : undefined}
|
|
/>
|
|
))}
|
|
</BarChart>
|
|
</ResponsiveContainer>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
|
|
|
|
export const UniversalPieChart = ({
|
|
data,
|
|
width = '100%',
|
|
height = 360,
|
|
showLegend = true,
|
|
showTooltip = true,
|
|
backgroundColor = '#ffffff',
|
|
innerRadius = 40,
|
|
outerRadius = 60,
|
|
paddingAngle = 2,
|
|
showLabel = true,
|
|
className,
|
|
style
|
|
}) => {
|
|
return (
|
|
<div
|
|
className={className}
|
|
style={{
|
|
width,
|
|
height,
|
|
background: backgroundColor,
|
|
borderRadius: 12,
|
|
padding: 12,
|
|
...style
|
|
}}
|
|
>
|
|
<ResponsiveContainer width="100%" height="100%">
|
|
<PieChart>
|
|
{showTooltip && <Tooltip />}
|
|
{showLegend && <Legend />}
|
|
|
|
<Pie
|
|
data={data}
|
|
dataKey="value"
|
|
nameKey="name"
|
|
cx="50%"
|
|
cy="50%"
|
|
innerRadius={innerRadius}
|
|
outerRadius={outerRadius}
|
|
paddingAngle={paddingAngle}
|
|
label={showLabel}
|
|
>
|
|
{data.map((entry, index) => (
|
|
<Cell
|
|
key={`cell-${index}`}
|
|
fill={entry.color || DEFAULT_COLORS[index % DEFAULT_COLORS.length]}
|
|
/>
|
|
))}
|
|
</Pie>
|
|
</PieChart>
|
|
</ResponsiveContainer>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
// =========================
|
|
// 使用示例
|
|
// =========================
|
|
|
|
export const DemoCharts = () => {
|
|
const lineData = [
|
|
{ month: '1月', uv: 400, pv: 240, amt: 240 },
|
|
{ month: '2月', uv: 300, pv: 456, amt: 300 },
|
|
{ month: '3月', uv: 500, pv: 139, amt: 200 },
|
|
{ month: '4月', uv: 278, pv: 390, amt: 278 },
|
|
{ month: '5月', uv: 189, pv: 480, amt: 189 },
|
|
];
|
|
|
|
const pieData = [
|
|
{ name: 'Vue', value: 40 },
|
|
{ name: 'React', value: 35 },
|
|
{ name: 'Angular', value: 25 },
|
|
];
|
|
|
|
return (
|
|
<div className="grid grid-cols-1 gap-6 p-6">
|
|
<UniversalLineChart
|
|
data={lineData}
|
|
xKey="month"
|
|
height={320}
|
|
smooth
|
|
dot
|
|
series={[
|
|
{
|
|
key: 'uv',
|
|
name: '访问量',
|
|
color: '#1677ff',
|
|
strokeWidth: 3,
|
|
},
|
|
{
|
|
key: 'pv',
|
|
name: '浏览量',
|
|
color: '#52c41a',
|
|
strokeWidth: 2,
|
|
},
|
|
{
|
|
key: 'amt',
|
|
name: '成交量',
|
|
color: '#fa8c16',
|
|
strokeWidth: 2,
|
|
},
|
|
]}
|
|
/>
|
|
|
|
<UniversalBarChart
|
|
data={lineData}
|
|
xKey="month"
|
|
height={320}
|
|
barSize={24}
|
|
series={[
|
|
{
|
|
key: 'uv',
|
|
name: '访问量',
|
|
color: '#1677ff',
|
|
},
|
|
{
|
|
key: 'pv',
|
|
name: '浏览量',
|
|
color: '#52c41a',
|
|
},
|
|
]}
|
|
/>
|
|
|
|
<UniversalPieChart
|
|
data={pieData}
|
|
height={360}
|
|
innerRadius={70}
|
|
outerRadius={130}
|
|
/>
|
|
</div>
|
|
);
|
|
};
|