U.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 { UCompute } from "@/api/analysis";
  38. // 假设的数据
  39. var chartData = ref([]);
  40. const param = {
  41. x: {
  42. defectCount: 30,
  43. sampleSize: 100,
  44. },
  45. x2: {
  46. defectCount: 50,
  47. sampleSize: 200,
  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.u,
  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: "U控制图",
  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: "UCL",
  144. type: "line",
  145. data: uclValues,
  146. showSymbol: false,
  147. lineStyle: {
  148. color: "red",
  149. type: "dashed",
  150. },
  151. },
  152. {
  153. name: "LCL",
  154. type: "line",
  155. data: lclValues,
  156. showSymbol: false,
  157. lineStyle: {
  158. color: "red",
  159. type: "dashed",
  160. },
  161. },
  162. ],
  163. color: ["blue", "green", "red", "red", "black"],
  164. };
  165. myChart.setOption(option);
  166. chartInstance.value = myChart;
  167. };
  168. const resizeChart = () => {
  169. if (chartInstance.value) {
  170. chartInstance.value.resize();
  171. }
  172. };
  173. onMounted(() => {
  174. window.addEventListener("resize", resizeChart);
  175. });
  176. // watch(chartData, () => {
  177. // if (chartInstance.value) {
  178. // const { sampleIds, values, ewmaValues, uclValues, lclValues, meanValue } =
  179. // prepareData();
  180. // const option = {
  181. // title: [
  182. // {
  183. // text: `U控制图`,
  184. // left: "40%",
  185. // },
  186. // ],
  187. // xAxis: {
  188. // data: sampleIds,
  189. // },
  190. // series: [
  191. // {
  192. // data: values.map((value, index) => ({
  193. // value,
  194. // itemStyle: {
  195. // color:
  196. // value > uclValues[index] || value < lclValues[index]
  197. // ? "red"
  198. // : "blue",
  199. // },
  200. // })),
  201. // },
  202. // {
  203. // data: ewmaValues,
  204. // },
  205. // {
  206. // data: uclValues,
  207. // },
  208. // {
  209. // data: lclValues,
  210. // },
  211. // {
  212. // data: Array(sampleIds.length).fill(meanValue),
  213. // },
  214. // ],
  215. // };
  216. // chartInstance.value.setOption(option);
  217. // }
  218. // });
  219. const init = (data) => {
  220. if (data) {
  221. nextTick(async () => {
  222. await getTableData(data);
  223. await initChart(chartRef.value);
  224. });
  225. }
  226. };
  227. // 暴露 init 方法
  228. defineExpose({
  229. init,
  230. });
  231. </script>
  232. <style lang="scss" scoped>
  233. /* 样式可以根据需要进行调整 */
  234. .container1 {
  235. width: 100%;
  236. height: 100%;
  237. display: flex;
  238. background-color: white;
  239. .infobox {
  240. width: 200px;
  241. .header {
  242. height: 120px;
  243. border-bottom: 2px solid #00000010;
  244. padding: 20px;
  245. }
  246. .body {
  247. padding: 20px;
  248. }
  249. }
  250. .databox {
  251. flex: 1;
  252. border-left: 2px solid #00000010;
  253. .box {
  254. height: 710px;
  255. padding: 5px 20px;
  256. display: flex;
  257. flex-direction: column;
  258. .illustrate {
  259. padding: 20px 60px;
  260. }
  261. .tableTitle {
  262. text-align: center;
  263. margin: 10px 0;
  264. padding-right: 40px;
  265. }
  266. .header {
  267. margin-top: 20px;
  268. //margin-left: 100px;
  269. display: flex;
  270. width: 100%;
  271. height: auto;
  272. }
  273. //.title {
  274. // height: 50px;
  275. // display: flex;
  276. // align-items: center;
  277. // margin-bottom: 10px;
  278. // justify-content: space-between;
  279. // .btns {
  280. // display: flex;
  281. // align-items: center;
  282. // .btn {
  283. // height: 24px;
  284. // font-size: 14px;
  285. // margin: 0 5px;
  286. // }
  287. // }
  288. //}
  289. .info {
  290. margin-top: 20px;
  291. flex: 1;
  292. height: 300px;
  293. }
  294. }
  295. }
  296. }
  297. </style>