阅行客电子档案
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
5.7 KiB

  1. <template>
  2. <div id="mainOther" :style="{height:height,width:width}" style="margin-top: -20px;" />
  3. </template>
  4. <script>
  5. import * as echarts from 'echarts'
  6. import resize from '@/views/dashboard/mixins/resize'
  7. export default {
  8. name: 'EchartsComponent',
  9. mixins: [resize],
  10. props: {
  11. memoryTotal: {
  12. type: Number,
  13. require: true,
  14. default: function() {
  15. return 0
  16. }
  17. },
  18. memoryFree: {
  19. type: Number,
  20. require: true,
  21. default: function() {
  22. return 0
  23. }
  24. },
  25. width: {
  26. type: String,
  27. default: '50%'
  28. },
  29. height: {
  30. type: String,
  31. default: '100%'
  32. }
  33. },
  34. watch: {
  35. 'memoryTotal': {
  36. handler(val) {
  37. setTimeout(() => {
  38. this.initChart()
  39. }, 100)
  40. },
  41. immediate: true,
  42. deep: true
  43. },
  44. 'memoryFree': {
  45. handler(val) {
  46. setTimeout(() => {
  47. this.initChart()
  48. }, 100)
  49. },
  50. immediate: true,
  51. deep: true
  52. }
  53. },
  54. mounted() {
  55. this.initChart()
  56. // window.addEventListener('resize', this.__resizeHandler)
  57. },
  58. methods: {
  59. initChart() {
  60. const chartDom = document.getElementById('mainOther')
  61. const myChart = echarts.init(chartDom)
  62. const dataValue = this.memoryTotal === 0 ? 0 : ((this.memoryTotal - this.memoryFree) / this.memoryTotal * 100).toFixed(0)
  63. const axisLineHandler = (value) => {
  64. const ratio = Number(value / 100)
  65. if (ratio <= 0.7) {
  66. return [
  67. [ratio, '#56BC91'], // 进度色
  68. [1, 'rgba(216, 216, 216, 1)'] // 背景色
  69. ]
  70. } else if (ratio <= 0.9) {
  71. return [
  72. [ratio, '#FF973E'], // 进度色
  73. [1, 'rgba(216, 216, 216, 1)'] // 背景色
  74. ]
  75. } else if (ratio <= 1) {
  76. return [
  77. [ratio, '#fd666d'], // 进度色
  78. [1, 'rgba(216, 216, 216, 1)'] // 背景色
  79. ]
  80. }
  81. }
  82. const itemStyleHandler = (value) => {
  83. if (value <= 70) {
  84. return `#56BC91`
  85. } else if (value <= 90) {
  86. return `#FF973E`
  87. } else if (value <= 100) {
  88. return `#fd666d`
  89. }
  90. return ''
  91. }
  92. // const dataNameHandler = (value) => {
  93. // if (value <= 60) {
  94. // return value + '°C'
  95. // } else if (value <= 80) {
  96. // return value + '°C'
  97. // } else if (value <= 100) {
  98. // return value + '°C'
  99. // }
  100. // return ''
  101. // }
  102. const option = {
  103. series: [
  104. {
  105. type: 'gauge',
  106. center: ['40%', '38%'],
  107. radius: 60,
  108. startAngle: 200,
  109. endAngle: -20,
  110. min: 0,
  111. max: 100,
  112. splitNumber: 10,
  113. itemStyle: {
  114. color: itemStyleHandler(dataValue)
  115. },
  116. progress: {
  117. show: true,
  118. width: 20
  119. },
  120. pointer: {
  121. show: false
  122. },
  123. axisLine: {
  124. lineStyle: {
  125. width: 14,
  126. color: axisLineHandler(dataValue)
  127. }
  128. },
  129. axisTick: {
  130. show: false,
  131. distance: -35,
  132. splitNumber: 5,
  133. lineStyle: {
  134. width: 2,
  135. color: 'auto'
  136. }
  137. },
  138. splitLine: {
  139. show: false,
  140. distance: -42,
  141. length: 14,
  142. lineStyle: {
  143. width: 3,
  144. color: 'auto'
  145. }
  146. },
  147. axisLabel: {
  148. show: false,
  149. distance: -40,
  150. color: 'inherit',
  151. fontSize: 14
  152. },
  153. anchor: {
  154. show: false
  155. },
  156. title: {
  157. show: true,
  158. offsetCenter: [0, '50%'],
  159. fontSize: 14,
  160. color: '#000'
  161. },
  162. detail: {
  163. valueAnimation: true,
  164. width: '60%',
  165. lineHeight: 40,
  166. borderRadius: 8,
  167. offsetCenter: [0, '0'],
  168. fontSize: 26,
  169. fontWeight: 'bolder',
  170. formatter: '{value} %',
  171. color: 'auto'
  172. },
  173. data: [
  174. {
  175. value: dataValue,
  176. name: ((this.memoryTotal - this.memoryFree) / 1024).toFixed(2) + 'G' + ' / ' + (this.memoryTotal / 1024).toFixed(0) + 'G'
  177. }
  178. ]
  179. },
  180. // 最外侧
  181. {
  182. type: 'gauge',
  183. radius: 66, // 半径
  184. center: ['40%', '38%'], // 位置
  185. min: 0,
  186. max: 100,
  187. startAngle: 200,
  188. endAngle: -20,
  189. axisLine: {
  190. show: false,
  191. lineStyle: {
  192. width: 1,
  193. color: '#999'
  194. }
  195. },
  196. axisTick: {
  197. show: true,
  198. splitNumber: 8,
  199. length: 2,
  200. lineStyle: {
  201. width: 1,
  202. color: '#999'
  203. }
  204. },
  205. splitLine: {
  206. show: false
  207. },
  208. axisLabel: {
  209. show: false
  210. },
  211. pointer: {
  212. show: false
  213. },
  214. detail: {
  215. show: false
  216. }
  217. }
  218. ]
  219. }
  220. if (option) {
  221. myChart.setOption(option)
  222. }
  223. }
  224. }
  225. }
  226. </script>
  227. <style scoped>
  228. </style>