echarts 仪表盘根据点击的刻度重新设置值

在这里插入图片描述

1 更具点击获取的坐标 event.offsetY , event.offsetX
2 通过中心点坐标差,获取角的度数,然后取180度的占比,最后✖️总值=刻度值.
3 然后在赋值给data
例子 : 角的度数是30度 30/180*30 = 5 则刻度值指向5
角度度数怎么求? (Math.atan2(y - event.offsetY, x - event.offsetX) * 180) / Math.PI在这里插入图片描述

<!--
 * @Author:  215683509@qq.com
 * @Date: 2024-05-08 14:07:53
 * @LastEditors: 215683509@qq.com
 * @LastEditTime: 2024-05-30 15:27:25
 * @Description: 测试
-->
<template>
  <div class="index-view">
    <div @touchend="handleTouchEnd" id="main"></div>
  </div>
</template>
<script>
import { mapGetters, mapMutations } from 'vuex'
import Header from '@/components/Header'
import Empty from './empty.vue'
import { getImgUrl } from '@/utils/auth.js'
import * as echarts from 'echarts'

export default {
  components: {
    Header,
    Empty,
  },
  data() {
    return {
      dataValue: 0,
      data1: 0,
    }
  },
  mounted() {
    this.initChart()

    // 假设你已经有了一个echarts实例
    // 监听ECharts的touch事件
    // 监听touchmove事件
    let myChart = echarts.init(document.getElementById('main'))
    myChart.getZr().on('click', (event) => {
      this.setChart(event)
    })
  },
  computed: {
    option() {
      return {
        series: [
          {
            type: 'gauge',
            center: ['50%', '60%'],
            startAngle: 200,
            endAngle: -20,
            min: 0,
            max: 30,
            splitNumber: 12,
            itemStyle: {
              color: '#FFAB91',
            },
            progress: {
              show: true,
              width: 30,
            },
            pointer: {
              show: false,
            },
            axisLine: {
              lineStyle: {
                width: 30,
              },
            },
            axisTick: {
              distance: -45,
              splitNumber: 5,
              lineStyle: {
                width: 2,
                color: '#999',
              },
            },
            splitLine: {
              distance: -52,
              length: 14,
              lineStyle: {
                width: 3,
                color: '#999',
              },
            },
            axisLabel: {
              distance: -20,
              color: '#999',
              fontSize: 20,
            },
            anchor: {
              show: false,
            },
            title: {
              show: false,
            },
            // detail: {formatter:'{value}'},
            data: [{ value: this.dataValue, name: 'Status' }],
            detail: {
              valueAnimation: true,
              width: '60%',
              lineHeight: 40,
              borderRadius: 8,
              offsetCenter: [0, '-15%'],
              fontSize: 60,
              fontWeight: 'bolder',
              formatter: '{value} °C',
              color: 'inherit',
            },
          },
          {
            type: 'gauge',
            center: ['50%', '60%'],
            startAngle: 200,
            endAngle: -20,
            min: 0,
            max: 30,
            itemStyle: {
              color: '#FD7347',
            },
            progress: {
              show: true,
              width: 8,
            },
            pointer: {
              show: false,
            },
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              show: false,
            },
            axisLabel: {
              show: false,
            },
            detail: {
              show: false,
            },
            data: [
              {
                value: this.data1,
              },
            ],
          },
        ],
      }
    },
  },
  methods: {
    setChart(event) {
      let x = 200
      let y = 295
      // 计算角度
      let currentAngle =
        (Math.atan2(y - event.offsetY, x - event.offsetX) * 180) / Math.PI
     //求值
      this.dataValue = ((currentAngle / 180) * 30).toFixed(0)
      this.data1 = ((currentAngle / 180) * 30).toFixed(0)
      //渲染
      this.initChart()
    },
    initChart() {
      var chartDom = document.getElementById('main')
      var myChart = echarts.init(chartDom)
      var option
      option = this.option
      option && myChart.setOption(option)
    },
  },
}
</script>
<style scoped>
.index-view {
  height: 500px;
  background-color: bisque;
}
#main {
  height: 400px;
  width: 400px;
  border: 1px solid red;
}
</style>
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值