echarts实例:圆环内衬图

记录下工作中使用echarts做出的特殊组件

js 复制代码
import { defineComponent } from 'vue'

import { COLOR_LIGHT_ORG,} from './createChart'


let props = {
  propData: {
    type: Array,
    default: () => [100, 80, 50],
  },
  legendData: {
    type: Array,
    default: () => [
      { name: '0%-60%', desc: '20天22小时25分钟' },
      { name: '60%-80%', desc: '10天15小时35分钟' },
      { name: '80%以上', desc: '5天15小时35分钟' },
    ],
  },
  barWidth: {
    type: Number,
    default: 20,
  },
  barColorArray: {
    type: Array,
    default: () => ['#FDC13C', '#32FFE1', '#E365FF', COLOR_LIGHT_ORG],
  },
  chartExtraOption: {
    type: Object,
    default() {
      return {}
    },
  },
}


export default defineComponent({
  props,
  data() {
    return {}
  },
  created() {},
  mounted() {
    this.init()
    this.$watch(
      () => this.$props, // 监听整个 props 对象
      () => {
        this.init()
      },
      { deep: true, immediate: false },
    )
  },
  beforeDestroy() {
    this.chart?.dispose?.()
  },
  methods: {
    init() {
      const option = this.getChartOption()
      const dom = this.$refs.chart
      if (!this.chart) {
        this.chart = echarts.init(dom, null, {
          renderer: 'canvas',
        })
      }
      this.chart.setOption(option, true)
    },
    getChartOption() {
      const filtered = this.legendData.map((item, index) => {
        return [item.name, this.propData[index]]
      })
      const maxValue = Math.max(...this.propData)
      return {
        color: this.barColorArray,
        angleAxis: {
          clockwise: false,
          axisLine: {
            show: false,
          },
          splitLine: {
            show: false,
          },
          axisLabel: {
            show: false,
          },
          axisTick: {
            show: false,
          },
          min: 0,
          max: maxValue * 1.4,
        },
        radiusAxis: {
          type: 'category',
          data: _.map(this.legendData, 'name'),
          z: 10,
          inverse: true,
          axisLine: {
            show: false,
          },
          axisLabel: {
            show: false,
          },
          axisTick: {
            show: false,
          },
        },
        polar: {
          radius: '100%',
        },
        series: [
          {
            type: 'bar',
            barWidth: this.barWidth,
            stack: 'base',
            data: filtered,
            coordinateSystem: 'polar',
            name: '1',
            colorBy: 'data',
          },
          {
            type: 'bar',
            stack: 'base',
            barWidth: this.barWidth,
            data: this.legendData.map((row) => [row.name, maxValue * 1.25]),
            coordinateSystem: 'polar',
            name: '半透衬底',
            itemStyle: {
              color: '#ffffff08',
            },
          },
        ],
      }
    },
  },
})
相关推荐
B站_计算机毕业设计之家6 天前
电影知识图谱推荐问答系统 | Python Django系统 Neo4j MySQL Echarts 协同过滤 大数据 人工智能 毕业设计源码(建议收藏)✅
人工智能·python·机器学习·django·毕业设计·echarts·知识图谱
吴声子夜歌13 天前
RxJava——Subscriber
android·echarts·rxjava
小白探索世界欧耶!~14 天前
Vue2项目引入sortablejs实现表格行拖曳排序
前端·javascript·vue.js·经验分享·elementui·html·echarts
吴声子夜歌17 天前
RxJava——调度器Scheduler
android·echarts·rxjava
吴声子夜歌17 天前
RxJava——并行编程
android·echarts·rxjava
吴声子夜歌20 天前
RxJava——FlowableProcessor详解
android·echarts·rxjava
B站_计算机毕业设计之家20 天前
电影市场预测分析系统 | Python Django Echarts 票房可视化分析 大数据 人工智能 毕业设计源码(建议收藏)✅
大数据·数据库·python·机器学习·django·毕业设计·echarts
吴声子夜歌20 天前
RxJava——Subject详解
android·echarts·rxjava
吴声子夜歌21 天前
RxJava——被观察者
android·echarts·rxjava