工单派发执行接口详情
This commit is contained in:
335
src/components/recharts.tsx
Normal file
335
src/components/recharts.tsx
Normal file
@@ -0,0 +1,335 @@
|
||||
import React from 'react';
|
||||
import {
|
||||
ResponsiveContainer,
|
||||
LineChart,
|
||||
Line,
|
||||
XAxis,
|
||||
YAxis,
|
||||
CartesianGrid,
|
||||
Tooltip,
|
||||
Legend,
|
||||
PieChart,
|
||||
Pie,
|
||||
Cell,
|
||||
BarChart,
|
||||
Bar,
|
||||
} from 'recharts';
|
||||
|
||||
// =========================
|
||||
// 通用类型定义
|
||||
// =========================
|
||||
|
||||
export interface SeriesConfig {
|
||||
key: string;
|
||||
name: string;
|
||||
color?: string;
|
||||
strokeWidth?: number;
|
||||
}
|
||||
|
||||
export interface BaseChartProps {
|
||||
width?: number | string;
|
||||
height?: number;
|
||||
showGrid?: boolean;
|
||||
showLegend?: boolean;
|
||||
showTooltip?: boolean;
|
||||
backgroundColor?: string;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
// 默认颜色池
|
||||
const DEFAULT_COLORS = [
|
||||
'#1677ff',
|
||||
'#52c41a',
|
||||
'#fa8c16',
|
||||
'#eb2f96',
|
||||
'#722ed1',
|
||||
'#13c2c2',
|
||||
'#f5222d',
|
||||
'#2f54eb',
|
||||
];
|
||||
|
||||
// =========================
|
||||
// 折线图组件
|
||||
// =========================
|
||||
|
||||
export interface UniversalLineChartProps extends BaseChartProps {
|
||||
data: Record<string, any>[];
|
||||
xKey: string;
|
||||
series: SeriesConfig[];
|
||||
smooth?: boolean;
|
||||
dot?: boolean;
|
||||
xAxisLabel?: string;
|
||||
yAxisLabel?: string;
|
||||
}
|
||||
|
||||
export const UniversalLineChart: React.FC<UniversalLineChartProps> = ({
|
||||
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}
|
||||
dot={dot}
|
||||
activeDot={{ r: 6 }}
|
||||
/>
|
||||
))}
|
||||
</LineChart>
|
||||
</ResponsiveContainer>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
// =========================
|
||||
// 柱状图组件
|
||||
// =========================
|
||||
|
||||
export interface UniversalBarChartProps extends BaseChartProps {
|
||||
data: Record<string, any>[];
|
||||
xKey: string;
|
||||
series: SeriesConfig[];
|
||||
barSize?: number;
|
||||
radius?: number | [number, number, number, number];
|
||||
stack?: boolean;
|
||||
}
|
||||
|
||||
export const UniversalBarChart: React.FC<UniversalBarChartProps> = ({
|
||||
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 interface PieDataItem {
|
||||
name: string;
|
||||
value: number;
|
||||
color?: string;
|
||||
}
|
||||
|
||||
export interface UniversalPieChartProps extends BaseChartProps {
|
||||
data: PieDataItem[];
|
||||
innerRadius?: number;
|
||||
outerRadius?: number;
|
||||
paddingAngle?: number;
|
||||
showLabel?: boolean;
|
||||
}
|
||||
|
||||
export const UniversalPieChart: React.FC<UniversalPieChartProps> = ({
|
||||
data,
|
||||
width = '100%',
|
||||
height = 360,
|
||||
showLegend = true,
|
||||
showTooltip = true,
|
||||
backgroundColor = '#ffffff',
|
||||
innerRadius = 60,
|
||||
outerRadius = 120,
|
||||
paddingAngle = 2,
|
||||
showLabel = true,
|
||||
className,
|
||||
}) => {
|
||||
return (
|
||||
<div
|
||||
className={className}
|
||||
style={{
|
||||
width,
|
||||
height,
|
||||
background: backgroundColor,
|
||||
borderRadius: 12,
|
||||
padding: 12,
|
||||
}}
|
||||
>
|
||||
<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>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user