xuchang-new/src/components/Common/TodayFaultTotal/index.jsx

161 lines
3.5 KiB
React
Raw Normal View History

2023-12-13 16:59:58 +08:00
import cls from "./index.module.css";
import GraphBase from "../GraphBase";
import ReactECharts from "echarts-for-react";
2023-12-14 13:57:49 +08:00
import { useSelector } from "react-redux";
import { useState } from "react";
2023-12-14 11:06:19 +08:00
function preHandleStatisticData(data, legend) {
const obj = {};
data.forEach((item) => {
obj[item.name] = {};
item.data.forEach((d) => {
obj[item.name][d.checkType] = d.checkNum;
});
});
const series = Array(legend.length)
.fill(1)
.map((_) => ({
name: "",
type: "bar",
stack: "true",
barWidth: 12,
emphasis: {
focus: "series",
},
data: [],
}));
legend.forEach((item, index) => {
series[index].name = item;
data.forEach((d) => {
series[index].data.push(obj[d.name][item] || 0);
});
});
return series;
}
2023-11-09 13:36:21 +08:00
2023-12-14 13:57:49 +08:00
function getOptions(series, isra, currentStatistic) {
return {
2023-12-14 11:06:19 +08:00
color: ["#2760FF", "#8167F6", "#5B9BFF", "#99D66C", "#FFD160", "#FF8A40"],
2023-12-13 16:59:58 +08:00
grid: { top: 42, right: 12, bottom: 20, left: 48 },
legend: {
top: 10,
padding: 5,
itemWidth: 12,
itemHeight: 12,
itemGap: 12,
height: 12,
textStyle: {
color: "#DFF1FE",
fontSize: 12,
},
2023-12-15 15:20:20 +08:00
data: isra.checkType,
2023-12-13 16:59:58 +08:00
},
xAxis: {
type: "category",
2023-12-14 11:06:19 +08:00
data: currentStatistic.map((item) => item.name),
2023-12-13 16:59:58 +08:00
axisLabel: {
color: "#fff",
fontSize: 12,
},
axisTick: { show: false },
axisLine: {
lineStyle: {
width: 1,
color: "#213259",
},
},
},
yAxis: {
name: "单位/个",
nameTextStyle: {
color: "#fff",
fontSize: 10,
align: "right",
},
type: "value",
axisLabel: {
color: "#fff",
fontSize: 12,
formatter: "{value}",
},
axisLine: {
show: true,
lineStyle: {
color: "#213259",
},
},
splitLine: {
lineStyle: {
color: "#213259a0",
},
},
},
2023-12-14 11:06:19 +08:00
series,
2023-12-13 16:59:58 +08:00
tooltip: {
trigger: "axis",
},
};
2023-12-14 13:57:49 +08:00
}
function FaultTotal(props) {
const [currentSelect, setCurrentSelect] = useState("日");
const isra = useSelector((state) => state.isra);
const currentStatistic =
currentSelect == "日"
? isra.dayStatistic
: currentSelect == "周"
? isra.weekStatistic
: currentSelect == "月"
? isra.monthStatistic
: currentSelect == "月"
? isra.yearStatistic
: [];
2023-12-15 15:20:20 +08:00
const series = preHandleStatisticData(currentStatistic, isra.checkType);
2023-12-14 13:57:49 +08:00
const options = getOptions(series, isra, currentStatistic);
2023-12-13 16:59:58 +08:00
function handleDateChange(v) {
2023-12-14 11:06:19 +08:00
setCurrentSelect(v);
2023-12-13 16:59:58 +08:00
}
// 根据使用的页面决定背景的大小
const bgSize =
props.page == "home" ? ["middle", "short"] : ["middle", "long"];
return (
<GraphBase
icon="battery"
title="产线缺陷统计"
dateOptions={["日", "周", "月", "年"]}
2023-12-14 11:24:03 +08:00
defaultSelect={currentSelect}
2023-12-13 16:59:58 +08:00
onDateChange={handleDateChange}
size={bgSize}
style={{ width: "600px" }}
>
<div className={cls.chart}>
2023-12-14 11:24:03 +08:00
{currentStatistic.length != 0 && (
<ReactECharts option={options} style={{ height: "100%" }} />
)}
{currentStatistic.length == 0 && (
<p
style={{
paddingTop: "72px",
color: "#fff6",
textAlign: "center",
fontSize: "24px",
}}
>
暂无数据
</p>
)}
2023-12-13 16:59:58 +08:00
</div>
</GraphBase>
);
2023-11-09 13:36:21 +08:00
}
export default FaultTotal;