Gauge.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template>
  2. <div ref="chartRef" :style="{ height, width }"></div>
  3. </template>
  4. <script lang="ts">
  5. import { defineComponent, PropType, ref, Ref, reactive, watchEffect } from 'vue';
  6. import { useECharts } from '/@/hooks/web/useECharts';
  7. import { GaugeChart } from 'echarts/charts';
  8. import { cloneDeep } from 'lodash-es';
  9. export default defineComponent({
  10. name: 'Gauge',
  11. props: {
  12. chartData: {
  13. type: Object as PropType<Object>,
  14. default: () => [],
  15. },
  16. option: {
  17. type: Object,
  18. default: () => ({}),
  19. },
  20. width: {
  21. type: String as PropType<string>,
  22. default: '100%',
  23. },
  24. height: {
  25. type: String as PropType<string>,
  26. default: 'calc(100vh - 78px)',
  27. },
  28. },
  29. setup(props) {
  30. const chartRef = ref<HTMLDivElement | null>(null);
  31. const { setOptions, echarts } = useECharts(chartRef as Ref<HTMLDivElement>);
  32. const option = reactive({
  33. series: [
  34. {
  35. type: 'gauge',
  36. progress: {
  37. show: true,
  38. width: 12,
  39. },
  40. axisLine: {
  41. lineStyle: {
  42. width: 12,
  43. },
  44. },
  45. axisTick: {
  46. show: false,
  47. },
  48. splitLine: {
  49. length: 12,
  50. lineStyle: {
  51. width: 1,
  52. color: '#999',
  53. },
  54. },
  55. axisLabel: {
  56. distance: 25,
  57. color: '#999',
  58. fontSize: 12,
  59. },
  60. anchor: {
  61. show: true,
  62. showAbove: true,
  63. size: 20,
  64. itemStyle: {
  65. borderWidth: 5,
  66. },
  67. },
  68. title: {},
  69. detail: {
  70. valueAnimation: true,
  71. fontSize: 25,
  72. formatter: '{value}%',
  73. offsetCenter: [0, '80%'],
  74. },
  75. data: [
  76. {
  77. value: 70,
  78. name: '本地磁盘',
  79. },
  80. ],
  81. },
  82. ],
  83. });
  84. watchEffect(() => {
  85. props.chartData && initCharts();
  86. });
  87. function initCharts() {
  88. echarts.use(GaugeChart);
  89. if (props.option) {
  90. Object.assign(option, cloneDeep(props.option));
  91. }
  92. option.series[0].data[0].name = props.chartData.name;
  93. option.series[0].data[0].value = props.chartData.value;
  94. setOptions(option);
  95. }
  96. return { chartRef };
  97. },
  98. });
  99. </script>