echarts 环图设置背景圈、label文字和线的样式处理、legend样式处理

<template>
  <div class='screen3-left-2' :ref="refName" :class="refName"></div>
</template>
<script>
import * as echarts from 'echarts';
import $ from 'jquery';

export default {
  components: {},
  data() {
    return {
      myChart: null,
      chartData: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
      refName: 'screen3-left-2'
    }
  },
  watch: {
    chartData: {
      handler: function (val) {
        this.dealData(val);
      },
      deep: true
    },
  },
  mounted() {
    var chartDom = this.$refs[this.refName];
    this.myChart = echarts.init(chartDom);
    this.chartData && this.dealData(this.chartData);
    window.addEventListener("resize", () => {
      this.chartData && this.dealData(this.chartData);
      this.myChart.resize();
    });
  },
  computed: {
  },
  methods: {
    dealData(val) {
      const img = require('@/assets/knowyou/screen3-left-2.png');
      const hei = $('.screen3-left-2')[0].offsetHeight;
      if (val.length > 0) {
        let seriesData = [];
        for (let i = 0; i < val.length; i++) {
          seriesData.push({
            name: 'aaa' + i,
            value: val[i]
          });
        }
        this.initChart(seriesData, hei, img);
      } else {
        this.myChart.clear();
      }
    },
    initChart(seriesData = [], hei, img) {
      var option = {
        color: ['#49dff2', '#29bae9', '#079aee', '#2171d4', '#66a69b', '#95b6de', '#adf2cc', '#3a5ac3', '#b8d2c3', '#5c83ae', '#49dff2', '#29bae9', '#079aee', '#2171d4', '#66a69b', '#95b6de', '#adf2cc', '#3a5ac3', '#b8d2c3', '#5c83ae'],
        tooltip: {
          trigger: 'item',
        },
        legend: {
          icon: 'circle',
          itemWidth: 10,
          itemHeight: 10,
          itemGap: 15, // 每一个item之间的间距
          width: '400',
          top: 'center',
          left: '62%',
          orient: 'vertical',
          formatter: (name, value) => {
            let dats = name.split('-');
            return `{name|${dats[0]}}\t\t\t{percent|${dats[1]}}`
          },
          textStyle: {
            color: '#fff',
            rich: {
              name: {
                fontSize: 12,
                color: '#7f99b7',
                padding: [0, 0, 0, 0],
                width: 40
              },
              percent: {
                fontSize: 14,
                fontWeight: 400,
                color: "white",
                padding: [0, 0, 0, 0],
              },
            }
          }
        },
        series: [
          {
            type: 'pie',
            radius: ['44%', '60%'],
            center: ['50%', '50%'],
            hoverAnimation: false, // 鼠标经过的动画 
            animation: false,
            selectedMode: false,
            silent: true, // 不响应和触发鼠标事件
            tooltip: {
              show: false
            },
            itemStyle: {
              normal: {
                label: { show: false },
                labelLine: { show: false },
                color: 'rgba(255,144,0,0.4)'
              }
            },
            data: [{
              value: 0,
              z: 1,
              label: {
                show: true,
                position: 'center',
                formatter: () => {
                  return `{bg|}`
                },
                rich: {
                  bg: {
                    left: 'center',
                    width: hei * 0.32,
                    height: hei * 0.32,
                    backgroundColor: {
                      image: img
                    }
                  }
                }
              }
            }],
          },
          {
            type: 'pie',
            radius: ['44%', '60%'],
            center: ['50%', '50%'],
            avoidLabelOverlap: false,
            label: {
              formatter: (val) => {
                return `{name|${val.name}}\n${val.percent}%`
              },
              minMargin: 5,
              edgeDistance: 10,
              lineHeight: 20,
              textStyle: {
                color: '#fff',
                rich: {
                  name: {
                    fontSize: 14,
                    color: '#12bedd',
                    padding: [0, 0, 0, 0],
                    width: 40
                  },
                }
              }
            },
            labelLine: {
              length: 25,
              length2: 20,
              maxSurfaceAngle: 80,
              lineStyle: {
                color: '#6590bf',
              }
            },
            labelLayout: (params) => {
              const isLeft = params.labelRect.x < this.myChart.getWidth() / 2;
              const points = params.labelLinePoints;
              points[2][0] = isLeft ? params.labelRect.x : params.labelRect.x + params.labelRect.width;
              return params;
            },
            data: seriesData
          },
        ]
      };
      this.myChart && this.myChart.clear();
      option && this.myChart.setOption(option);
    },
  },
}
</script>
<style lang='scss' scoped>
.screen3-left-2 {
  width: 100%;
  height: 100%;
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ECharts 中,可以通过设置 `legend` 的 `textStyle` 属性来控制例(legend)中文字样式,包括颜色、字体大小、字重等。下面是一个设置饼状文字颜色的例子: ```javascript option = { legend: { textStyle: { color: '#333', // 设置文字颜色 fontSize: 14, fontWeight: 'normal' }, data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎'] }, series: [ { name: '访问来源', type: 'pie', radius: ['50%', '70%'], avoidLabelOverlap: false, label: { show: false, position: 'center' }, labelLine: { show: false }, data: [ {value: 335, name: '直接访问'}, {value: 310, name: '邮件营销'}, {value: 234, name: '联盟广告'}, {value: 135, name: '视频广告'}, {value: 1548, name: '搜索引擎'} ], itemStyle: { borderWidth: 5, borderColor: '#ffffff', }, emphasis: { itemStyle: { borderWidth: 0, shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)' } } } ] }; ``` 在上面的例子中,通过设置 `legend` 的 `textStyle` 属性来控制文字样式,其中 `color` 属性可以设置文字颜色,`fontSize` 属性可以设置文字大小,`fontWeight` 属性可以设置文字粗细。 需要注意的是,如果需要为不同的设置不同的文字样式,可以在 `legend` 的 `data` 属性中指定每个例的名称,并在 `textStyle` 属性中为每个名称设置对应的样式,如下所示: ```javascript legend: { textStyle: { color: '#333', fontSize: 14, fontWeight: 'normal', rich: { // 为每个设置不同的样式 a: { color: '#ff0000', fontSize: 16, fontWeight: 'bold' }, b: { color: '#00ff00', fontSize: 14, fontStyle: 'italic' }, c: { color: '#0000ff', fontSize: 12, fontWeight: 'normal' } } }, data: [ {name: '直接访问', textStyle: {rich: {a: {}}}}, {name: '邮件营销', textStyle: {rich: {b: {}}}}, {name: '联盟广告', textStyle: {rich: {c: {}}}}, {name: '视频广告', textStyle: {rich: {a: {}}}}, {name: '搜索引擎', textStyle: {rich: {b: {}}}} ] } ``` 在上面的例子中,通过在 `data` 属性中为每个例指定名称和样式,然后在 `textStyle` 属性中设置每个样式的具体内容。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值