P.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <template>
  2. <div class="container1">
  3. <div class="databox">
  4. <el-scrollbar :style="{ height: Height + 'px' }">
  5. <div class="box" v-show="!addStatus">
  6. <div
  7. style="
  8. display: flex;
  9. align-items: center;
  10. justify-content: space-between;
  11. "
  12. >
  13. <div style="display: flex; align-items: center">
  14. <div class="bg"></div>
  15. 控制图绘制
  16. </div>
  17. <el-button
  18. type="primary"
  19. v-print="'#print'"
  20. style="margin-left: 10px; height: 25px"
  21. >打 印</el-button
  22. >
  23. </div>
  24. <div class="info">
  25. <div id="print">
  26. <div ref="chartRef" style="width: 100%; height: 400px"></div>
  27. </div>
  28. </div>
  29. </div>
  30. </el-scrollbar>
  31. </div>
  32. </div>
  33. </template>
  34. <script setup>
  35. import { ref, onMounted, watch } from "vue";
  36. import * as echarts from "echarts";
  37. import { PCompute } from "@/api/analysis";
  38. // 假设的数据
  39. var chartData = ref([]);
  40. const param = {
  41. x: {
  42. defectCount: 30,
  43. sampleSize: 500,
  44. },
  45. x2: {
  46. defectCount: 50,
  47. sampleSize: 600,
  48. },
  49. };
  50. const getTableData = async (resultData) => {
  51. // 转换函数
  52. const transformData = (data) => {
  53. const result = [];
  54. let sampleId = 1;
  55. // 遍历原始数据,排除 total 属性
  56. for (const key in data) {
  57. if (key !== "total") {
  58. const item = data[key];
  59. result.push({
  60. sampleId: sampleId.toString(),
  61. value: item.p,
  62. ewma: item.cl,
  63. ucl: item.ucl,
  64. lcl: item.lcl,
  65. });
  66. sampleId++;
  67. }
  68. }
  69. return result;
  70. };
  71. // 转换后的数据
  72. const formattedData = ref(transformData(resultData));
  73. chartData.value = formattedData.value;
  74. };
  75. const chartRef = ref(null);
  76. const chartInstance = ref(null);
  77. const prepareData = () => {
  78. let sampleIds = chartData.value.map((item) => item.sampleId);
  79. let values = chartData.value.map((item) => item.value);
  80. let ewmaValues = chartData.value.map((item) => item.ewma);
  81. let uclValues = chartData.value.map((item) => item.ucl);
  82. let lclValues = chartData.value.map((item) => item.lcl);
  83. // 保留四位小数
  84. values = values.map((num) => parseFloat(num.toFixed(4)));
  85. ewmaValues = ewmaValues.map((num) => parseFloat(num.toFixed(4)));
  86. uclValues = uclValues.map((num) => parseFloat(num.toFixed(4)));
  87. lclValues = lclValues.map((num) => parseFloat(num.toFixed(4)));
  88. return {
  89. sampleIds,
  90. values,
  91. ewmaValues,
  92. uclValues,
  93. lclValues,
  94. };
  95. };
  96. const initChart = (chartDom) => {
  97. const myChart = echarts.init(chartDom);
  98. const { sampleIds, values, ewmaValues, uclValues, lclValues } = prepareData();
  99. const option = {
  100. title: {
  101. text: "P控制图",
  102. left: "center",
  103. },
  104. tooltip: {
  105. trigger: "axis",
  106. },
  107. xAxis: {
  108. type: "category",
  109. boundaryGap: true, // 不留白,从原点开始
  110. data: sampleIds,
  111. },
  112. yAxis: {
  113. type: "value",
  114. // name: "EWMA值",
  115. },
  116. series: [
  117. {
  118. name: "数值",
  119. type: "line",
  120. // step: "start",
  121. symbol: "circle", //将小圆点改成实心 不写symbol默认空心
  122. symbolSize: 8,
  123. data: values.map((value, index) => ({
  124. value,
  125. itemStyle: {
  126. color:
  127. value > uclValues[index] || value < lclValues[index]
  128. ? "red"
  129. : "blue",
  130. },
  131. })),
  132. },
  133. {
  134. name: "均值",
  135. type: "line",
  136. showSymbol: false,
  137. data: ewmaValues,
  138. lineStyle: {
  139. color: "green",
  140. },
  141. },
  142. // {
  143. // name: "均值",
  144. // type: "line",
  145. // data: Array(sampleIds.length).fill(meanValue),
  146. // showSymbol: false,
  147. // lineStyle: {
  148. // color: "green",
  149. // type: "solid",
  150. // },
  151. // },
  152. {
  153. name: "UCL",
  154. type: "line",
  155. data: uclValues,
  156. showSymbol: false,
  157. lineStyle: {
  158. color: "red",
  159. type: "dashed",
  160. },
  161. },
  162. {
  163. name: "LCL",
  164. type: "line",
  165. data: lclValues,
  166. showSymbol: false,
  167. lineStyle: {
  168. color: "red",
  169. type: "dashed",
  170. },
  171. },
  172. ],
  173. color: ["blue", "green", "red", "red", "black"],
  174. };
  175. myChart.setOption(option);
  176. chartInstance.value = myChart;
  177. };
  178. const resizeChart = () => {
  179. if (chartInstance.value) {
  180. chartInstance.value.resize();
  181. }
  182. };
  183. onMounted(() => {
  184. window.addEventListener("resize", resizeChart);
  185. });
  186. watch(chartData, () => {
  187. if (chartInstance.value) {
  188. const { sampleIds, values, ewmaValues, uclValues, lclValues, meanValue } =
  189. prepareData();
  190. const option = {
  191. title: [
  192. {
  193. text: `P控制图`,
  194. left: "40%",
  195. },
  196. ],
  197. xAxis: {
  198. data: sampleIds,
  199. },
  200. series: [
  201. {
  202. data: values.map((value, index) => ({
  203. value,
  204. itemStyle: {
  205. color:
  206. value > uclValues[index] || value < lclValues[index]
  207. ? "red"
  208. : "blue",
  209. },
  210. })),
  211. },
  212. {
  213. data: ewmaValues,
  214. },
  215. {
  216. data: uclValues,
  217. },
  218. {
  219. data: lclValues,
  220. },
  221. {
  222. data: Array(sampleIds.length).fill(meanValue),
  223. },
  224. ],
  225. };
  226. chartInstance.value.setOption(option);
  227. }
  228. });
  229. const init = (data) => {
  230. if (data) {
  231. nextTick(async () => {
  232. await getTableData(data);
  233. await initChart(chartRef.value);
  234. });
  235. }
  236. };
  237. // 暴露 init 方法
  238. defineExpose({
  239. init,
  240. });
  241. </script>
  242. <style lang="scss" scoped>
  243. /* 样式可以根据需要进行调整 */
  244. .container1 {
  245. width: 100%;
  246. height: 100%;
  247. display: flex;
  248. background-color: white;
  249. .infobox {
  250. width: 200px;
  251. .header {
  252. height: 120px;
  253. border-bottom: 2px solid #00000010;
  254. padding: 20px;
  255. }
  256. .body {
  257. padding: 20px;
  258. }
  259. }
  260. .databox {
  261. flex: 1;
  262. border-left: 2px solid #00000010;
  263. .box {
  264. height: 710px;
  265. padding: 5px 20px;
  266. display: flex;
  267. flex-direction: column;
  268. .illustrate {
  269. padding: 20px 60px;
  270. }
  271. .tableTitle {
  272. text-align: center;
  273. margin: 10px 0;
  274. padding-right: 40px;
  275. }
  276. .header {
  277. margin-top: 20px;
  278. //margin-left: 100px;
  279. display: flex;
  280. width: 100%;
  281. height: auto;
  282. }
  283. //.title {
  284. // height: 50px;
  285. // display: flex;
  286. // align-items: center;
  287. // margin-bottom: 10px;
  288. // justify-content: space-between;
  289. // .btns {
  290. // display: flex;
  291. // align-items: center;
  292. // .btn {
  293. // height: 24px;
  294. // font-size: 14px;
  295. // margin: 0 5px;
  296. // }
  297. // }
  298. //}
  299. .info {
  300. margin-top: 20px;
  301. flex: 1;
  302. height: 300px;
  303. }
  304. }
  305. }
  306. }
  307. </style>