echarts带刻度的渐变色饼状图 legend自定义

echarts yekong

echarts饼状图环形颜色渐变,并且上面有刻度,右侧使用自定义html实现legend饼状图中心是总数以及标题。

echarts饼状图效果

组件代码

<template>
  <div class="echartsBodys">
    <echarts1 title="应交付项目总数" :total="total" :list="list" ref="echarts"></echarts1>
    <div class="list">
      <div class="listItem cur" @click="getChecked(index)" v-for="(item,index) in list" :key="index">
        <div class="listIteml">
          <div class="dot" :class="{disabled:!item.checked}"
               :style="'background: linear-gradient(0deg,'+item.color1+' 0%, '+item.color2+' 100%);'"></div>
          <span :class="{disabled:!item.checked}">{{ item.name }}</span>
        </div>
        <div class="num"
             :class="{disabled:!item.checked}">
          {{ item.value }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>

import echarts1 from "./echarts.vue";

export default {
  name: "title",
  data() {
    return {
      list: [
        {
          name: '未完成“保交楼”任务项目数',
          value: 80,
          color1: 'rgba(75, 98, 255, 1)',
          color2: 'rgba(78, 135, 255, 1)',
          checked: true
        },
        {
          name: '已完成“保交楼”任务项目数',
          value: 100,
          color1: '#1ef2c1',
          color2: '#26e0ee',
          checked: true
        }]
    }
  },
  computed: {
    total: function () {
      var total = 0
      this.list.forEach((type) => {
        total += type.value
      });
      return total
    }
  },
  components: {echarts1},
  methods: {
    getChecked(index) {
      this.list[index].checked = !this.list[index].checked
      this.$refs.echarts.drawEcharts()
    }
  },
}
</script>

<style lang="scss" scoped>
.list {
  margin-left: 0px;
  position: relative;
  width: 60%;
  height: calc(100% - 10px);
  //background: url("./assets/jiaobiaobg.png") no-repeat;
  //background-size: 100% 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  flex-direction: column;
  align-content: flex-start;

  .listItem {
    font-size: 14px;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    flex-wrap: nowrap;
    flex-direction: row;
    align-content: flex-start;
    width: 100%;
    margin-bottom: 15px;
    margin-top: 15px;
    height: 38px;
    background: rgba(0, 54, 96, 0.4);
    //border: 1px solid #328FFC;
    border-radius: 19px;
    box-shadow: inset 0px 0px 10px #318efb;

    .listIteml {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      flex-wrap: nowrap;
      flex-direction: row;
      align-content: flex-start;
      font-size: 13px;
      font-family: MicrosoftYaHei;
      font-weight: 400;
      color: rgba(152, 191, 251, 1);
    }

    span {
      font-size: 14px;
      font-family: MicrosoftYaHei;
      font-weight: 400;
      color: #FFFFFF;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      flex-wrap: nowrap;
      flex-direction: row;
      align-content: flex-start;
    }

    .num {
      margin-left: 10px;
      font-size: 14px;
      font-family: MicrosoftYaHei;
      font-weight: 400;
      color: #FFFFFF;
    }

    .dot {
      width: 12px;
      height: 12px;
      border-radius: 50%;
      margin-right: 10px;
      flex-shrink: 0;
      margin-left: 20px;
    }

    .dot.disabled {
      background: rgba(#999, 0.8) !important;
    }

    .disabled span, span.disabled {
      color: rgba(#999, 0.8) !important;
    }
  }

  .listItem:nth-child(4) {
    width: 68%;
  }

  .listItem:nth-child(5) {
    width: 68%;
  }
}

.echartsBodys {
  width: 100%;
  position: relative;
  height: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: nowrap;
  flex-direction: row;
  align-content: flex-start;
}
</style>

echarts代码

<template>
  <div class="echartsBody">
    <div class="echartsBody1" ref="echarts1"></div>
    <div class="echartsBody2">
      <div class="echartsTitle">{{ total }}</div>
      <div class="echartsDesc">{{ title }}</div>
    </div>
  </div>
</template>

<script>
import * as echarts from "echarts"

export default {
  name: 'echarts1',
  components: {},
  data() {
    return {}
  },
  props: {
    list: {
      type: Array,
      default() {
        return [];
      }
    },
    shadowColor: {
      type: String,
      default() {
        return 'rgba(254, 175, 87, 0.5)';
      }
    },
    title: {
      type: String,
      default() {
        return '总数';
      }
    },
    total: {
      type: String,
      default() {
        return 0;
      }
    },
  },
  mounted() {
    var that = this;
    const viewElem = document.body;
    // 监听窗口变化,重绘echarts
    const resizeObserver = new ResizeObserver(() => {
      setTimeout(() => {
        that.drawEcharts();
      }, 300)
    });
    resizeObserver.observe(viewElem);
  },
  computed: {
    colorList: function () {
      var that = this;
      var colorList = []
      that.list.forEach((type) => {
            if (type.checked) {
              var color = new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
                offset: 0,
                color: type.color1
              }, {
                offset: 1,
                color: type.color2
              }])
              colorList.push(color)
            }
          }
      );
      return colorList
    }
  },
  methods: {
    drawEcharts() {
      var that = this
      let myChart = echarts.init(this.$refs.echarts1)
      var colors = this.colorList
      var data = []
      var placeHolderStyle = {
        normal: {
          label: {
            show: false
          },
          labelLine: {
            show: false
          },
          color: 'rgba(0, 0, 0, 0)',
          borderColor: 'rgba(0, 0, 0, 0)',
          borderWidth: 0
        }
      };
      that.list.forEach((type) => {
        if (type.checked) {
          var datas = {
            ...type,
            itemStyle: {
              normal: {
                borderColor: new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
                  offset: 0,
                  color: type.color1
                }, {
                  offset: 1,
                  color: type.color2
                }]),
                shadowColor: that.shadowColor
              }
            }
          }
          data.push(datas)
        }
      });
      var option = {
        color: colors,
        tooltip: {
          trigger: 'item',
          // formatter: '{a} {b} : {c} ({d}%)'
        },
        series: [
          {
            type: 'pie',
            hoverAnimation: false,
            center: ['50%', '50%'],
            radius: ['65%', '90%'],
            labelLine: {
              normal: {
                length: 20
              }
            },
            label: {
              show: false,
              position: 'inside',
              formatter: '{d}%',
              color: '#fff',
              fontSize: 16
            },
            itemStyle: {
              normal: {
                borderColor: '#3eebc4',
                borderWidth: 0
              }
            },
            data: data
          },
          {
            type: 'pie',
            name: '中间黑色背景',
            radius: ['100%', '99%'],
            center: ['50%', '50%'],
            startAngle: 110,
            hoverAnimation: false,
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
                  offset: 0,
                  color: '#11417d'
                }, {
                  offset: 1,
                  color: '#11417d'
                }]),
              },
            },
            tooltip: {
              show: false,
            },
            label: {
              show: false
            },
            data: [50]
          },
          {
            type: 'pie',
            name: '中间背景',
            radius: ['0%', '60%'],
            center: ['50%', '50%'],
            startAngle: 110,
            hoverAnimation: false,
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 1, 1, [{
                  offset: 0,
                  color: 'rgba(75, 98, 255, 0.1)'
                }, {
                  offset: 1,
                  color: 'rgba(75, 98, 255, 0.1)'
                }]),
              },
            },
            tooltip: {
              show: false,
            },
            label: {
              show: false
            },
            data: [50]
          },
          {
            type: 'gauge',
            radius: '90%',
            clockwise: false,
            startAngle: '90',
            endAngle: '-269.9999',
            splitNumber: 20,
            z: 4,
            detail: {
              offsetCenter: [0, -20],
              formatter: ' '
            },
            pointer: {
              show: false
            },
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            splitLine: {
              show: true,
              length: 22,
              lineStyle: {
                shadowBlur: 10,
                shadowColor: 'rgba(0, 255, 255, 0)',
                shadowOffsetY: '0',
                color: '#071a39',
                width: 2
              }
            },
            axisLabel: {
              show: false
            }
          },
        ]
      }
      myChart.clear()
      myChart.resize()
      myChart.setOption(option)
    },
  }
}
</script>

<style lang="scss" scoped>
.echartsBody {
  position: relative;
  width: 40%;
  height: calc(100% - 0px);

  .echartsBody1 {
    position: relative;
    width: 100%;
    height: calc(100% - 0px);
  }
}

.echartsBody2 {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: calc(100% - 0px);
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: nowrap;
  flex-direction: column;
  align-content: flex-start;
  pointer-events: none;

  .echartsTitle {
    font-size: 32px;
    font-family: UniDream;
    font-weight: normal;
    color: #FFFFFF;
    text-shadow: 0px 0px 10px #4e85ff;
  }

  .echartsDesc {
    font-size: 16px;
    font-family: MicrosoftYaHei;
    font-weight: 400;
    color: #A4C4FF;
    width: 80px;
    text-align: center;
  }

}
</style>

喜欢