使用echarts自定义仪表盘进度条

效果图:

1、项目配置echart

npm install echarts

 在main.js文件内按需引入

import * as echarts from 'echarts/core'
import { TooltipComponent } from 'echarts/components'
import { GaugeChart } from 'echarts/charts'
import { CanvasRenderer } from 'echarts/renderers'

echarts.use([TooltipComponent, GaugeChart, CanvasRenderer])

Vue.prototype.$echarts = echarts

2、仪表盘的实现

<template>
  <div id="chart"></div>
</template>

<script>
export default {
  props: ['tests'],
  data () {
    return {}
  },
  watch: {
    immediate: true,
    tests (newVal) {
      this.getLoadEcharts(newVal)
    }
  },
  mounted () {
    this.getLoadEcharts(this.tests)
  },
  methods: {
    getLoadEcharts (n) {
      const chart = this.$echarts.init(document.getElementById('chart'))

      let option = {
        'series': [
          {
            'name': '',
            'type': 'gauge',
            'radius': '100%',
            'min': 0,
            'max': 7,
            'startAngle': '210',
            'endAngle': '-30',
            // 图表的刻度分隔段数
            'splitNumber': 7,
            // 图表的轴线相关
            'axisLine': {
              'show': true,
              'lineStyle': {
                'color': [
                  [
                    0.9,
                    new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [{
                      offset: 0,
                      color: '#FFD900'
                    }, {
                      offset: 1,
                      color: '#FF8000'
                    }])
                  ],
                  [1, '#e0e0e0']
                ],
                'width': 10
              }
            },
            // 图表的刻度及样式
            'axisTick': {
              'distance': -10,//远离圆心的距离
              'lineStyle': {
                'color': '#0F1318',
                'width': 2
              },
              'length': 10,
              'splitNumber': 1
            },
            // 图表的刻度标签(20、40、60等等)
            'axisLabel': {
              'distance': 10,
              'textStyle': {
                'color': '#8e8e8e'
              }
            },
            // 图表的分割线
            'splitLine': {
              'show': false
            },
            // 图表的指针
            'pointer': {
              'show': false
            },
            // 图表的数据详情
            'detail': {
              'formatter': function () {
                return ' '
              }
            },
            // 图表的标题
            'title': {
              'offsetCenter': [0, '90%'],
              'color': '#000000',
              'fontSize': 14
            },
            'data': [{
              'name': ' ',
              'value': 31
            }]
          },
          {
            'name': '外层线',
            'type': 'gauge',
            'radius': '82%',
            'startAngle': '210',
            'endAngle': '-30',
            'center': ['50%', '50%'],
            'axisLine': {
              'lineStyle': {
                'width': 1,
                'color': [[1, '#8e8e8e']]
              }
            },
            'splitLine': {
              'length': -6,
              'lineStyle': {
                'opacity': 0
              }
            },
            'axisLabel': {
              'show': false
            },
            'axisTick': {
              'splitNumber': 1,
              'lineStyle': {
                'opacity': 0
              }
            },
            'detail': {
              'show': false
            },
            'pointer': {
              'show': false
            }
          }
        ]
      }
      let tempVal = n
      if (tempVal > this.myIvstrAbility) {
        // 最后转到最终数据的地方
        option.series[0].data[0].value = this.myIvstrAbility
        option.series[0].axisLine.lineStyle.color[0][0] = this.myIvstrAbility / 7
        // 使用刚指定的配置项和数据显示图表
        chart.setOption(option)
        return
      }
      option.series[0].data[0].value = tempVal
      option.series[0].axisLine.lineStyle.color[0][0] = tempVal / 7
      // 使用刚指定的配置项和数据显示图表。
      chart.setOption(option)
      // 此处监听浏览器的resize,重新渲染图表
      let that = this
      window.addEventListener('resize', function () {
        clearTimeout(that.resizeTimer)
        that.resizeTimer = setTimeout(() => {
          chart.resize()
        }, 1000)
      })
    }
  }
}
</script>

<style scoped>
  #chart {
    width: 200px;
    height: 200px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
</style>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值