gauge.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <script setup lang="ts">
  2. import * as echarts from 'echarts'
  3. import { GaugeChart } from 'echarts/charts'
  4. import { LabelLayout, UniversalTransition } from 'echarts/features'
  5. import { CanvasRenderer } from 'echarts/renderers'
  6. import {
  7. onUnmounted,
  8. onMounted, ref, watch
  9. } from 'vue'
  10. echarts.use([ GaugeChart, LabelLayout, UniversalTransition, CanvasRenderer ])
  11. const props = defineProps<{
  12. value: number,
  13. gears: string
  14. }>()
  15. const chartDom = ref()
  16. const chart = ref(null as any)
  17. const option = ref({
  18. series: {
  19. name: 'Pressure',
  20. type: 'gauge',
  21. itemStyle: {
  22. color: '#FFFFFF'
  23. },
  24. startAngle: 180,
  25. max: 60,
  26. endAngle: 0,
  27. axisLine: {
  28. lineStyle: {
  29. width: 1
  30. }
  31. },
  32. axisTick: {
  33. distance: 0,
  34. length: 10,
  35. lineStyle: {
  36. color: '#FFFFFF'
  37. }
  38. },
  39. splitLine: {
  40. length: 15,
  41. distance: 0,
  42. lineStyle: {
  43. color: '#FFFFFF'
  44. }
  45. },
  46. axisLabel: {
  47. distance: 8,
  48. color: '#FFFFFF'
  49. },
  50. progress: {
  51. show: true
  52. },
  53. radius: '190%',
  54. center: [ '50%', '98%' ],
  55. detail: {
  56. offsetCenter: [ 0, -25 ],
  57. valueAnimation: true,
  58. formatter: (value: number) => `{value|${value.toFixed(0)}}{unit|km/h}\n{num|${props.gears}}`,
  59. rich: {
  60. value: {
  61. fontSize: 20,
  62. fontWeight: 'bolder',
  63. color: '#FFFFFF'
  64. },
  65. unit: {
  66. fontSize: 20,
  67. color: '#FFFFFF',
  68. padding: [ 0, 0, 0, 10 ]
  69. },
  70. num: {
  71. fontSize: 20,
  72. color: '#FFFFFF',
  73. padding: [ 0, 0, 0, 10 ]
  74. }
  75. }
  76. },
  77. pointer: {
  78. show: false
  79. },
  80. data: [ 0 ]
  81. }
  82. })
  83. function resetChart() {
  84. if (!chart.value) return
  85. chart.value.resize()
  86. }
  87. watch(() => props.value, (v: number) => {
  88. if (!chart.value) return
  89. if (v >= 60) v = 60
  90. if (v <= 0) v = 0
  91. option.value.series.data[0] = v
  92. chart.value.setOption(option.value)
  93. })
  94. watch(() => props.gears, () => {
  95. if (!chart.value) return
  96. chart.value.setOption(option.value)
  97. })
  98. onMounted(() => {
  99. chart.value = echarts.init(chartDom.value)
  100. chart.value.setOption(option.value)
  101. window.addEventListener('resize', resetChart, false)
  102. })
  103. onUnmounted(() => {
  104. window.removeEventListener('resize', resetChart, false)
  105. })
  106. </script>
  107. <template>
  108. <div
  109. id="charts"
  110. ref="chartDom"
  111. />
  112. </template>
  113. <style scoped>
  114. #charts {
  115. width: 100%;
  116. height: 100%;
  117. }
  118. </style>