echarts实现饼图渐变颜色

<template>
  <div>
    <Echart
      :options="options"
      id="centreLeft1Chart"
      height="193px"
      width="380px"
      style="
        border: solid rgb(83, 146, 199) 1px;
        margin: 5px;
        border-radius: 5px;
      "
    ></Echart>
  </div>
</template>

<script>
import Echart from "@/components/echart";

import * as echarts from "echarts";
export default {
  data() {
    return {
      options: {},
      data: [
        { value: 52, name: "成功" },
        { value: 28, name: "中断" },
        { value: 24, name: "失败" },
        { value: 20, name: "超时" },
      ],
    };
  },
  components: {
    Echart,
  },
  created() {
    let that = this;
    this.options = {
      tooltip: {
        //鼠标上移显示
        trigger: "item",
        formatter: "{a} <br/>{b} : {c} ({d}%)",
      },
      toolbox: {
        show: true,
      },
      calculable: true,
      legend: {
        itemHeight: 12,
        itemWidth: 12,
        orient: "horizontal",
        itemGap: 20,
        x: "57%",
        y: "center", //可设定图例在上、下、居中
        textStyle: {
          color: "#000000",
          fontSize: 12,
          rich: {
            name: {
              fontSize: 12,
              color: "#adaab0",
              padding: [30, 0, 20, 1], //上,右,下,左
            },
            num: {
              fontSize: 12,
              padding: [-30, 0, 0, -40],
            },
            nums: {
              fontSize: 12,
              padding: [-30, 0, 0, 5],
              color: "#adaab0",
            },
            unit: {
              fontSize: 12,
              padding: [-30, 0, 0, 5],
              color: "#0048FF",
            },
          },
        },
        formatter: function (labelName) {
          const count = that.arrCount(that.data);
          const val = that.data.filter((item) => {
            return item.name === labelName;
          });

          return [
            `{name|${labelName}}\n`,
            `{num|${(val[0].value / count).toFixed(2) * 100}%}{nums||}{unit|${
              val[0].value
            }}`,
          ].join("\t\t");
        },
      },

      graphic: [
        //饼图中间数字和文字显示
        {
          type: "text",
          left: "20%",
          top: "70px",
          z: 2,
          zlevel: 100,
          style: {
            text: "已完成数",
            fill: "#bbbbbb", //颜色
            font: "bold 10px Microsoft YaHei", //字体
          },
        },
        {
          type: "text",
          left: "21%",
          top: "90px",
          z: 2,
          zlevel: 100,
          style: {
            text: this.data.reduce((n, i) => {
              //循环数据得到总数
              return (n += i.value);
            }, 0),
            fill: "#000000",
            font: "bold 15px Microsoft YaHei",
          },
        },
      ],
      series: [
        {
          //外圈数据
          name: "完成数",
          type: "pie",
          radius: [55, 80],
          roseType: "area",
          center: ["25%", "45%"],
          itemStyle: {
            normal: {
              color: (list) => {
                // 注意 !!!!! 这里的数组一定要和实际的类目长度相等或大于,不然会缺少颜色报错
                var colorList = [
                  {
                    colorStart: "#488BFF",
                    colorEnd: "#9abffd",
                  },
                  {
                    colorStart: "#e2def9",
                    colorEnd: "#9889e6",
                  },
                  {
                    colorStart: "#FFC069",
                    colorEnd: "#df7d66",
                  },
                  {
                    colorStart: " #d1dcf8",
                    colorEnd: "#f2f0fe",
                  },
                ];
                return new echarts.graphic.LinearGradient(1, 0, 0, 0, [
                  {
                    //左、下、右、上
                    offset: 0,
                    color: colorList[list.dataIndex]["colorStart"],
                  },
                  {
                    offset: 1,
                    color: colorList[list.dataIndex]["colorEnd"],
                  },
                ]);
              },
            },
          },

          label: {
            normal: {
              position: "inner", //不显示指示线文字
              show: false, //不显示指示线文字
            },
          },
          data: that.data,
        },
        {
          //内圈数据
          name: "完成数",
          type: "pie",
          radius: [55, 65],
          roseType: "area",
          center: ["25%", "45%"],
          itemStyle: {
            normal: {
              color: (list) => {
                // 注意 !!!!! 这里的数组一定要和实际的类目长度相等或大于,不然会缺少颜色报错
                var colorList = [
                  {
                    colorStart: "#9abffd",
                    colorEnd: "#d0d9f6",
                  },
                  {
                    colorStart: "#f8f5fd",
                    colorEnd: "#bdb4eb",
                  },
                  {
                    colorStart: "#faf0ef",
                    colorEnd: "#ebb1a5",
                  },
                  {
                    colorStart: " #f8f5fd",
                    colorEnd: "#f2f3f8",
                  },
                ];
                return new echarts.graphic.LinearGradient(1, 0, 0, 0, [
                  {
                    //左、下、右、上
                    offset: 0,
                    color: colorList[list.dataIndex]["colorStart"],
                  },
                  {
                    offset: 1,
                    color: colorList[list.dataIndex]["colorEnd"],
                  },
                ]);
              },
            },
          },

          label: {
            normal: {
              position: "inner", //不显示指示线文字
              show: false, //不显示指示线文字
            },
          },
          data: that.data,
        },
      ],
    };
  },
  methods: {
    arrCount(arr) {
      let count = 0;
      arr.forEach((item) => {
        count = count + item.value;
      });
      return count;
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 echarts 提供的渐变色功能来实现饼图颜色渐变效果。下面是一个使用 Vue 和 echarts 实现饼图颜色渐变的示例代码: ```vue <template> <div> <div ref="chart" style="width: 600px; height: 400px;"></div> </div> </template> <script> import echarts from 'echarts' export default { mounted() { this.renderChart() }, methods: { renderChart() { const chart = echarts.init(this.$refs.chart) const data = [ { value: 335, name: '数据1' }, { value: 310, name: '数据2' }, { value: 234, name: '数据3' }, { value: 135, name: '数据4' }, { value: 1548, name: '数据5' } ] const colors = ['#FF5733', '#FFB233', '#FFE333', '#B9FF33', '#33FFA8'] // 创建渐变色数组 const gradientColors = [] for (let i = 0; i < colors.length; i++) { gradientColors.push( { offset: i / (colors.length - 1), color: colors[i] } ) } const option = { series: [ { type: 'pie', data: data, itemStyle: { // 设置渐变色 color: new echarts.graphic.LinearGradient(0, 0, 1, 1, gradientColors) } } ] } chart.setOption(option) } } } </script> <style> </style> ``` 在这个示例代码中,我们首先引入 echarts 库,然后在 `mounted` 钩子函数中初始化图表,并在 `renderChart` 方法中设置饼图的数据和渐变色。我们使用 `LinearGradient` 类来创建渐变色,通过设置不同的偏移量和颜色值来定义渐变效果。 你可以根据需要修改 `data` 数组和 `colors` 数组来调整饼图的数据和颜色。注意,渐变色的个数应该与饼图数据的个数一致,以保证每个扇形区域都有对应的渐变色。 希望这个示例对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值