LockerChange.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <template>
  2. <div>
  3. <!-- <span style="margin: 10px">统计区间:</span>-->
  4. <!-- <el-date-picker-->
  5. <!-- style="margin: 10px"-->
  6. <!-- v-model="value2"-->
  7. <!-- type="daterange"-->
  8. <!-- align="right"-->
  9. <!-- unlink-panels-->
  10. <!-- range-separator="至"-->
  11. <!-- start-placeholder="开始日期"-->
  12. <!-- end-placeholder="结束日期"-->
  13. <!-- :picker-options="pickerOptions" >-->
  14. <!-- </el-date-picker>-->
  15. <div ref="barChart" style="width: 700px; height: 400px;margin-top: 3%"></div> <!-- 这里指定了柱状图的宽高 -->
  16. </div>
  17. </template>
  18. <script>
  19. import { getMaterialsChangeStatistics } from '@/api/mes/statisticians'
  20. import * as echarts from 'echarts'
  21. export default {
  22. name: 'warehouseEquipmentUsageStatus',
  23. props: {
  24. startTime:{
  25. type: String,
  26. default: ''
  27. },
  28. endTime:{
  29. type: String,
  30. default: ''
  31. }
  32. },
  33. data() {
  34. return {
  35. cabinetData: [], // 存储请求到的柜子数据
  36. chartInstance: null,
  37. value2: this.getDefaultDateRange(),
  38. queryParams:{
  39. startTime: null,
  40. endTime: null,
  41. },
  42. pickerOptions: {
  43. shortcuts: [{
  44. text: '最近一周',
  45. onClick(picker) {
  46. const end = new Date();
  47. const start = new Date();
  48. start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
  49. picker.$emit('pick', [start, end]);
  50. }
  51. }, {
  52. text: '最近一个月',
  53. onClick(picker) {
  54. const end = new Date();
  55. const start = new Date();
  56. start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
  57. picker.$emit('pick', [start, end]);
  58. }
  59. }, {
  60. text: '最近三个月',
  61. onClick(picker) {
  62. const end = new Date();
  63. const start = new Date();
  64. start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
  65. picker.$emit('pick', [start, end]);
  66. }
  67. }]
  68. },
  69. }
  70. },
  71. computed: {
  72. dateRange() {
  73. return {
  74. startTime: this.startTime,
  75. endTime: this.endTime
  76. };
  77. }
  78. },
  79. watch: {
  80. // value2(val) {
  81. // if(val){
  82. // this.getList()
  83. // }
  84. // },
  85. dateRange(newVal, oldVal) {
  86. if (newVal.startTime && newVal.endTime) {
  87. this.queryParams.startTime = newVal.startTime;
  88. this.queryParams.endTime = newVal.endTime;
  89. this.getList();
  90. }
  91. }
  92. },
  93. mounted() {
  94. // this.getList()
  95. },
  96. methods: {
  97. getDefaultDateRange() {
  98. const today = new Date();
  99. const oneMonthAgo = new Date(today.getFullYear(), today.getMonth() - 1, today.getDate());
  100. return [oneMonthAgo, today];
  101. },
  102. formatDate(date) {
  103. const year = date.getFullYear().toString().padStart(2, "0");
  104. const month = (date.getMonth() + 1).toString().padStart(2, "0");
  105. const day = date.getDate().toString().padStart(2, "0");
  106. const hours = date.getHours().toString().padStart(2, "0");
  107. const minutes = date.getMinutes().toString().padStart(2, "0");
  108. const seconds = date.getSeconds().toString().padStart(2, "0");
  109. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  110. },
  111. // 请求数据
  112. getList() {
  113. // if (Array.isArray(this.value2) && this.value2.length === 2) {
  114. // this.queryParams.startTime = this.formatDate(this.value2[0])
  115. // this.queryParams.endTime = this.formatDate(this.value2[1])
  116. // }
  117. getMaterialsChangeStatistics(this.queryParams).then(response => {
  118. console.log(response, '物资柜状态统计--更换次数')
  119. this.cabinetData = response.data // 获取数据后保存
  120. this.renderChart() // 数据获取完后渲染图表
  121. })
  122. },
  123. // 渲染柱状图
  124. renderChart() {
  125. if (this.cabinetData.length === 0) return
  126. // 初始化图表实例
  127. this.chartInstance = echarts.init(this.$refs.barChart)
  128. // 提取 X 轴和 Y 轴的数据
  129. const materialTypeName = this.cabinetData.map(item => item.materialsTypeName)
  130. const allCount = this.cabinetData.map(item => item.allCount)
  131. const expireCount=this.cabinetData.map(item=>item.expireCount)
  132. const badCount=this.cabinetData.map(item=>item.badCount)
  133. // 配置图表的选项
  134. const options = {
  135. title: {
  136. text: '物资更换统计',
  137. left: 'center',
  138. top: '0%'
  139. },
  140. tooltip: {
  141. trigger: 'axis',
  142. axisPointer: {
  143. type: 'cross',
  144. crossStyle: {
  145. color: '#999'
  146. }
  147. }
  148. },
  149. toolbox: {
  150. feature: {
  151. dataView: { show: true, readOnly: false },
  152. magicType: { show: true, type: ['line', 'bar'] },
  153. restore: { show: true },
  154. saveAsImage: { show: true }
  155. }
  156. },
  157. legend: {
  158. data: ['正常更换次数','过期更换次数','损坏更换次数'],
  159. top: '5%', // 调整图例距离顶部的距离,确保图例在标题下方
  160. left: 'center', // 图例居中显示
  161. },
  162. xAxis: [
  163. {
  164. type: 'category',
  165. data: materialTypeName,
  166. axisPointer: {
  167. type: 'shadow'
  168. }
  169. }
  170. ],
  171. yAxis: [
  172. {
  173. type: 'value',
  174. name: '次数',
  175. axisLabel: {
  176. formatter: '{value} ' // 修改单位为 次
  177. },
  178. splitLine: {
  179. show: false // 隐藏纵向的网格线
  180. }
  181. }
  182. ],
  183. series: [
  184. {
  185. name: '正常更换次数', // 修改为适当的名称
  186. type: 'bar',
  187. stack: 'Ad', // 启用堆叠效果
  188. barWidth: '25%',
  189. tooltip: {
  190. valueFormatter: function (value) {
  191. return value + ' 次'; // 修改单位为 次
  192. }
  193. },
  194. data: allCount,
  195. itemStyle: {
  196. color: '#5470c6', // 设置柱子的颜色为蓝色(你可以根据需要修改)
  197. }
  198. },
  199. {
  200. name: '过期更换次数',
  201. type: 'bar',
  202. stack: 'Ad',
  203. emphasis: {
  204. focus: 'series'
  205. },
  206. data: expireCount,
  207. itemStyle: {
  208. color: '#91cc75', // 设置柱子的颜色为蓝色(你可以根据需要修改)
  209. }
  210. },
  211. {
  212. name: '损坏更换次数',
  213. type: 'bar',
  214. stack: 'Ad',
  215. emphasis: {
  216. focus: 'series'
  217. },
  218. data: badCount,
  219. itemStyle: {
  220. color: '#fac858', // 设置柱子的颜色为蓝色(你可以根据需要修改)
  221. }
  222. }
  223. ]
  224. };
  225. // 设置图表的配置项
  226. this.chartInstance.setOption(options)
  227. }
  228. },
  229. beforeDestroy() {
  230. if (this.chartInstance) {
  231. this.chartInstance.dispose() // 销毁图表实例,避免内存泄漏
  232. }
  233. }
  234. }
  235. </script>
  236. <style scoped lang="scss">
  237. /* 如果需要更改图表的样式,可以在这里添加 */
  238. </style>