echarts+圆环图+自定义tooltip+完整代码

一、完整代码

<template>
  <div>
    <div id="main1" ref="main1"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';
import { mapState } from 'vuex';

export default {
  name: 'PieChart',
  props: {
    bedtype: {
      type: String,
      required: true
    },
    data: {
      type: Array,
      required: true,
      default: () => []
    }
  },
  data() {
    return {
      chart: null
    };
  },
  computed: {
    ...mapState(['login']),
  },
  watch: {
    data: {
      handler(newData) {
        if (this.chart) {
          this.updateChart(newData);
        }
      },
      deep: true
    }
  },
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    if (this.chart) {
      this.chart.dispose();
    }
  },
  methods: {
    getMockData() {
      return [
        { value: Math.floor(Math.random() * 100), name: '浅睡', percentage: '40%', itemStyle: { color: '#2ed7ee' } },
        { value: Math.floor(Math.random() * 100), name: '深睡', percentage: '30%', itemStyle: { color: '#5b8ff9' } },
        { value: Math.floor(Math.random() * 100), name: '清醒', percentage: '30%', itemStyle: { color: '#f56c6c' } }
      ];
    },
    initChart() {
      this.chart = echarts.init(this.$refs.main1);
      const dataToUse = this.data.length ? this.data : this.getMockData();
      this.chart.setOption(this.getChartOptions(dataToUse));
    },
    updateChart(newData) {
      const dataToUse = newData.length ? newData : this.getMockData();
      this.chart.setOption(this.getChartOptions(dataToUse));
    },
    getChartOptions(data) {
      return {
        title: {
          text: '睡眠',
          subtext: '阶段',
          left: 'center',
          top: '40%',
          left: '29%',
          textAlign: 'center',
          textStyle: { fontSize: 17, color: '#ffffff' },
          subtextStyle: { fontSize: 17, color: '#ffffff' }
        },
        tooltip: {
          trigger: 'item',
          formatter: ({ marker, name, value }) => `${marker} ${name}: ${value}分钟`
        },
        label: {
          show: true,
          color: '#fff',
          position: 'outside',
          formatter: ({ data }) => `${data.percentage}`
        },
        emphasis: {
          label: {
            show: true,
            fontSize: 20,
            fontWeight: 'bold',
            color: '#fff'
          }
        },
        labelLine: {
          show: true,
          lineStyle: { color: '#fff' }
        },
        series: [
          {
            name: '',
            type: 'pie',
            radius: ['40%', '70%'],
            center: ['30%', '50%'],
            data
          }
        ]
      };
    }
  }
};
</script>

<style scoped>
#main1 {
  width: 560px;
  height: 180px;
}
</style>

 自定义tooltip:{}

tooltip:{
trigger:"item",
formatter:({marker,name,value,percentage})=>{
`${marker}${name}${value}`
}
}

marker就是小圆点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值