Echarts饼图-实现资源对比+旋转动画

本次实现的是一个饼图,外圈两个圆,转动效果 + 资源对比显示。

思路:写三个div,让外圈两个旋转起来,如果有多个,可以增加div即可,颜色和圆大小都可以修改,根据自己需求来定,通过定时器可以控制旋转的快慢,通过resize属性来监听图形的改变而变化。

实现效果静态图片:

整体代码如下:

<template>
  <div class="container">
    <div class="pie">
      <div id="pieChart"></div>
      <div id="innerMargin"></div>
      <div id="outerMargin"></div>
    </div>
  </div>
</template>
   
  <script>
import * as echarts from "echarts";
export default {
  name: "",
  props: {},
  data() {
    return {
      data: { value: 50 }, // 数据
    };
  },
  mounted() {
    this.pieChart();
    this.innerMargin();
    this.outerMargin();
  },
  methods: {
    /**内圆 */
    pieChart() {
      const myChart = echarts.init(document.getElementById("pieChart"));
      let color = {
        type: "linear",
        x: 0,
        y: 0,
        x2: 0,
        y2: 1,
        colorStops: [
          {
            offset: 0,
            color: "#25ABD6", // 0% 处的颜色
          },
          {
            offset: 0.17,
            color: "#2494D0", // 100% 处的颜色
          },
          {
            offset: 0.9,
            color: "#29C0D8", // 100% 处的颜色
          },
          {
            offset: 1,
            color: "#27B3D5", // 100% 处的颜色
          },
        ],
        global: false, // 缺省为 false
      };
      const option = { series: [] };
      for (let i = 0; i < 3; i++) {
        let series = {
          name: "第一个圆环",
          type: "pie",
          clockWise: false,
          radius: [28, 38],
          hoverAnimation: false,
          itemStyle: {
            borderRadius: 60,
            normal: {
              label: {
                show: false,
              },
              labelLine: {
                show: false,
              },
            },
          },
          center: ["12%", "50%"],
          data: [
            {
              value: 10,
              label: {
                normal: {
                  rich: {
                    a: {
                      color: "#BBFFFF",
                      align: "center",
                      fontSize: 17,
                      fontWeight: "bold",
                    },
                    b: {
                      color: "#BBFFFF",
                      align: "center",
                      fontSize: 12,
                      padding: [0, 0, 5, 0],
                    },
                  },
                  formatter: function (params) {
                    return "{b|虚拟资源}" + "\n{a|" + params.value + "}";
                  },
                  position: "center",
                  show: true,
                  textStyle: {
                    fontSize: "14",
                    fontWeight: "normal",
                    color: "#fff",
                  },
                },
              },
              itemStyle: {
                borderWidth: 1,
                borderRadius: "50%",
                color: color,
              },
            },
            {
              value: 90,
              itemStyle: {
                normal: {
                  color: "#072c6d",
                  borderWidth: 1,
                  borderRadius: "50%",
                },
              },
            },
          ],
        };
        if (i == 0) {
          series.data[0].value = this.data.value; //  第一个内圆
        }
        series.data[1].value = 100 - series.data[0].value;
        option.series.push(series);
      }
      myChart.setOption(option);
    },

    //**内边框 */
    innerMargin() {
      const myChart = echarts.init(document.getElementById("innerMargin"));
      const option = { series: [] };
      for (let i = 0; i < 3; i++) {
        let series = {
          name: "内圈小狐线",
          type: "pie",
          radius: ["46", "48"],
          center: ["12%", "50%"],
          startAngle: 90, //默认90,起始角度,支持范围[0, 360]
          hoverAnimation: false,
          itemStyle: {
            borderRadius: 40,
            normal: {
              label: {
                show: false,
              },
              labelLine: {
                show: false,
              },
            },
          },
          data: [
            {
              value: 10,
              itemStyle: {
                normal: {
                  color: "#4682B4",
                  borderWidth: 1,
                },
              },
            },
            {
              value: 5,
              itemStyle: {
                normal: {
                  color: "#072c6d",
                  borderWidth: 3,
                  borderRadius: "25%",
                },
              },
            },
            {
              value: 10,
              itemStyle: {
                normal: {
                  color: "#4682B4",
                  borderWidth: 1,
                },
              },
            },
            {
              value: 5,
              itemStyle: {
                normal: {
                  color: "#072c6d",
                  borderWidth: 3,
                  borderRadius: "25%",
                },
              },
            },
            {
              value: 10,
              itemStyle: {
                normal: {
                  color: "#4682B4",
                  borderWidth: 1,
                },
              },
            },
            {
              value: 5,
              itemStyle: {
                normal: {
                  color: "#072c6d",
                  borderWidth: 3,
                  borderRadius: "25%",
                },
              },
            },
            {
              value: 10,
              itemStyle: {
                normal: {
                  color: "#4682B4",
                  borderWidth: 1,
                },
              },
            },
            {
              value: 5,
              itemStyle: {
                normal: {
                  color: "#072c6d",
                  borderWidth: 3,
                  borderRadius: "25%",
                },
              },
            },
          ],
        };
        option.series.push(series);
      }
      // 用定时器让他旋转起来
      setInterval(() => {
        for (let i = 0; i < option.series.length; i++) {
          const element = option.series[i];
          element.startAngle = element.startAngle - 5;
        }
        myChart.setOption(option);
      }, 100);
      window.addEventListener("resize", () => {
        if (myChart) {
          myChart.resize();
        }
      });
    },
    //**外边框 */
    outerMargin() {
      const myChart = echarts.init(document.getElementById("outerMargin"));
      const option = { series: [] };
      for (let i = 0; i < 3; i++) {
        let series = {
          name: "最外圈小狐线",
          type: "pie",
          radius: ["50", "52"],
          center: ["12%", "50%"],
          startAngle: 90, //默认90,起始角度,支持范围[0, 360]
          hoverAnimation: false,
          itemStyle: {
            borderRadius: 40,
            normal: {
              label: {
                show: false,
              },
              labelLine: {
                show: false,
              },
            },
          },
          data: [
            {
              value: 10,
              itemStyle: {
                normal: {
                  color: "#4682B4",
                  borderWidth: 1,
                },
              },
            },
          ],
        };
        option.series.push(series);
      }
      // 用定时器让他旋转起来
      setInterval(() => {
        for (let i = 0; i < option.series.length; i++) {
          const element = option.series[i];
          element.startAngle = element.startAngle - 5;
        }
        myChart.setOption(option);
      }, 100);
      window.addEventListener("resize", () => {
        if (myChart) {
          myChart.resize();
        }
      });
    },
  },
};
</script>
   
  <style  scoped>
.container {
  background: #ddd;
  height: 100vh;
  padding: 30px;
}
.pie {
  position: relative;
}
#pieChart {
  width: 400px;
  height: 96px;
}

#innerMargin {
  top: 0px;
  width: 400px;
  height: 96px;
  position: absolute;
}
#outerMargin {
  top: 0px;
  width: 400px;
  height: 96px;
  position: absolute;
}
</style>

首先,你需要在 React 项目中安装 echarts: ``` npm install echarts --save ``` 然后,在需要使用饼图的组件中引入 echarts,并在组件的生命周期函数 `componentDidMount` 中初始化 echarts 实例,并使用数据渲染饼图。 例如,以下是一个简单的饼图组件的代码: ``` import React, { Component } from 'react'; import echarts from 'echarts'; class PieChart extends Component { componentDidMount() { this.initChart(); } initChart = () => { const { data } = this.props; const chart = echarts.init(this.chartRef); chart.setOption({ tooltip: { trigger: 'item', formatter: '{a} <br/>{b}: {c} ({d}%)', }, series: [ { name: '访问来源', type: 'pie', radius: ['50%', '70%'], avoidLabelOverlap: false, label: { show: false, position: 'center', }, emphasis: { label: { show: true, fontSize: '30', fontWeight: 'bold', }, }, labelLine: { show: false, }, data, }, ], }); }; render() { return ( <div ref={(ref) => { this.chartRef = ref; }} style={{ width: '100%', height: '300px' }} /> ); } } export default PieChart; ``` 在上面的代码中,我们使用 `componentDidMount` 函数初始化 echarts 实例,并使用传递进来的数据渲染饼图。注意,我们在组件的 `render` 函数中返回一个 `div` 元素,这个元素的 `ref` 属性绑定了一个回调函数,用来获取这个元素的引用,以便后续使用 echarts 来渲染图表。 在父组件中,我们可以使用以下代码来渲染这个饼图组件: ``` import React, { Component } from 'react'; import PieChart from './PieChart'; class App extends Component { state = { data: [ { value: 335, name: '直接访问' }, { value: 310, name: '邮件营销' }, { value: 234, name: '联盟广告' }, { value: 135, name: '视频广告' }, { value: 1548, name: '搜索引擎' }, ], }; render() { const { data } = this.state; return ( <div> <PieChart data={data} /> </div> ); } } export default App; ``` 在父组件中,我们传递一个 `data` 属性给饼图组件,这个属性包含了用于渲染饼图的数据。最终,我们可以在页面中看到一个简单的饼图
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值