【vue3或3.2中使用echarts】展示饼图

一、安装echarts

npm i echarts 或者 npm install echarts --save

安装成功

二、在页面引入

import * as echarts from "echarts";

三、在vue中写入HTML代码

例如:

 <div class="canvas-pie">
          <div id="oil-discount"></div>
          <div class="line"></div>
          <div id="oil-pay"></div>
  </div>

注意:在style中设置宽高

 #oil-discount{
            width: 254px;
            height: 298px;
          }
          #oil-pay{
            width: 254px;
            height: 298px;
  }

四、在script里面获取DOM初始化echarts

例如:

<script setup>
import { ref, reactive, toRefs, watch, onMounted, getCurrentInstance } from "vue";
import * as echarts from "echarts";

onMounted(()=>{
  changeBar()
  changePay()
})
// 基本柱形图
function changeBar(){
  const myChart = echarts.init(document.getElementById("oil-discount"));
  const data = [{
          value: 335,
          name: '直接访问'
        },
        {
          value: 234,
          name: '联盟广告'
        },
        {
          value: 985,
          name: '搜索引擎'
        },{
          value: 138,
          name: '满减代理'
        },
        {
          value: 225,
          name: '限时代理'
        }, 
        {
          value: 558,
          name: '固定代理'
        },
        {
          value: 360,
          name:''
        }]
  myChart.setOption({
           color: [
            "#96DDF2",
            "#3436C7",
            "#63DCA6",
            "#5B93FF",
            "#F899C9 ",
            "#FF8F6B",
            "#FFD66B",
            "#FF7723",
            "#00FF7F",
            "#FF0000",
          ],
          tooltip: { trigger: "item" },
          legend: {
            show: window.innerWidth > 1680,
            bottom: "0",
            left: "center",
            itemWidth: 15,
            itemHeight: 12,
          },
          graphic: [
            {
              type: "text",
              left: "center",
              top: "28%",
              style: {
                fill: "#333333",
                text: 50 + "元",
                font: 'normal bolder 1em "Microsoft YaHei"',
                textAlign: "center",
              },
            },
            {
              type: "text",
              left: "center",
              top: "38%",
              style: {
                fill: "#333333",
                text: "实收金额",
                font: '14px bolder "Microsoft YaHei"',
                textAlign: "center",
                textVerticalAlign: "middle",
              },
            },
          ],
          series: [
            {
              name: "实收金额",
              type: "pie",
              center: ["50%", "35%"],
              radius: ["35%", "60%"],
              avoidLabelOverlap: false,
              label: { show: false },
              data: data,
            },
          ],
      }) 
}
function changePay(){
    const myChart = echarts.init(document.getElementById("oil-pay"));
    const data = [{
          value: 335,
          name: '直接访问'
        },
        {
          value: 234,
          name: '联盟广告'
        },
        {
          value: 985,
          name: '搜索引擎'
        },{
          value: 138,
          name: '满减代理'
        },
        {
          value: 225,
          name: '限时代理'
        }, 
        {
          value: 558,
          name: '固定代理'
        },
        {
          value: 360,
          name:''
        }]
    myChart.setOption({
           color: [
            "#1890FF",
            "#2FC25B",
            "#FACC14",
            "#223273",
            "#8543E0",
            "#13C2C2",
            "#3436C7",
            "#FF7723",
            "#00FF7F",
            "#FF0000",
          ],
          tooltip: { trigger: "item" },
          legend: {
            show: window.innerWidth > 1680,
            bottom: "0",
            left: "center",
            itemWidth: 15,
            itemHeight: 12,
          },
          graphic: [
            {
              type: "text",
              left: "center",
              top: "28%",
              style: {
                fill: "#333333",
                text: 50 + "元",
                font: 'normal bolder 1em "Microsoft YaHei"',
                textAlign: "center",
              },
            },
            {
              type: "text",
              left: "center",
              top: "38%",
              style: {
                fill: "#333333",
                text: "展业页面",
                font: '14px bolder "Microsoft YaHei"',
                textAlign: "center",
                textVerticalAlign: "middle",
              },
            },
          ],
          series: [
            {
              name: "展业页面",
              type: "pie",
              center: ["50%", "35%"],
              radius: ["35%", "60%"],
              avoidLabelOverlap: false,
              label: { show: false },
              data: data,
            },
          ],
      }) 
}
</script>

五、效果页面

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值