EWMA.vue 6.9 KB

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