vue中使用echerts饼状图

1.在页面中按需加载

let echarts = require("echarts/lib/echarts");
// 引入饼状图组件
require("echarts/lib/chart/pie");
// 引入提示框和title组件
require("echarts/lib/component/tooltip");
require("echarts/lib/component/title");

2.创建容器

<div class="pie">
     <!-- 为 ECharts 准备一个具备大小(宽高)的 DOM -->
     <div id="main1" style="float:left;width:493px;height: 530px"></div>
</div>

3.在js里写

 mounted() {
    this.initData();
  }
   methods: {
    //初始化数据
    initData() {
      // 基于准备好的dom,初始化echarts实例
      var myChart = echarts.init(document.getElementById("main1"));
      window.onresize = myChart.resize;
      // 绘制图表
      myChart.setOption({
        series: [
          {
            name: "部门报销比例",
            type: "pie",
            radius: ["50%", "70%"],
            avoidLabelOverlap: false,
            label: {
              normal: {
                show: false,
                position: "center",
                //{b}是name {c}是value
                formatter: ["{a|{b}}", "{b|{c}元}"].join("\n"),
                //在 rich 里面,可以自定义富文本样式。{a|}是标签
                rich: {
                  a: {
                    fontSize: 40,
                    lineHeight: 30
                  },
                  b: {
                    fontSize: 21,
                    lineHeight: 60,
                    height: 60
                  }
                }
              },
              emphasis: {
                show: true,
                textStyle: {
                  fontSize: "30",
                  fontWeight: "bold"
                }
              }
            },
            labelLine: {
              normal: {
                show: false
              }
            },
            data: this.data,
            color: ["#f9c270", "#40c4f4", "#999", "pink", "blue", "red"]
          }
        ]
      });

      //设置默认选中高亮部分
      myChart.dispatchAction({
        type: "highlight",
        seriesIndex: 0,
        dataIndex: 0
      });

      myChart.on("mouseover", function(e) {
        console.log(e);
        //当检测到鼠标悬停事件,取消默认选中高亮
        myChart.dispatchAction({
          type: "downplay",
          seriesIndex: 0,
          dataIndex: 0
        });
        //高亮显示悬停的那块
        myChart.dispatchAction({
          type: "highlight",
          seriesIndex: 0,
          dataIndex: e.dataIndex
        });
      });

      //检测鼠标移出后显示之前默认高亮的那块
      myChart.on("mouseout", function(e) {
        myChart.dispatchAction({
          type: "downplay",
          seriesIndex: 0,
          dataIndex: e.dataIndex
        });
        myChart.dispatchAction({
          type: "highlight",
          seriesIndex: 0,
          dataIndex: 0
        });
      });
    }
  }
``


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3使用echarts饼状图可以通过以下步骤实现: 1. 首先,确保你已经安装了echartsvue-echarts插件。可以通过npm或yarn进行安装。 2. 在你的Vue组件,引入echartsvue-echarts插件。 ```javascript import * as echarts from 'echarts'; import ECharts from 'vue-echarts'; ``` 3. 在template使用ECharts组件,并设置图表的类型为饼状图。 ```html <template> <div> <e-charts :options="chartOptions" :auto-resize="true"></e-charts> </div> </template> ``` 4. 在data定义chartOptions对象,并设置饼状图的配置选项,如数据和样式。 ```javascript data() { return { chartOptions: { series: [ { type: 'pie', data: [ { value: 335, name: 'Category 1' }, { value: 310, name: 'Category 2' }, { value: 234, name: 'Category 3' }, { value: 135, name: 'Category 4' }, { value: 1548, name: 'Category 5' }, ], }, ], }, }; }, ``` 5. 在mounted生命周期钩子,初始化echarts实例,并将其绑定到ECharts组件上。 ```javascript mounted() { this.$nextTick(() => { const chart = echarts.init(this.$refs.chart); this.$refs.chart.echarts = chart; }); }, ``` 6. 最后,在需要的地方使用ECharts组件。 ```html <template> <div> <e-charts :options="chartOptions" :auto-resize="true" ref="chart"></e-charts> </div> </template> ``` 通过以上步骤,你就可以在Vue3使用echarts饼状图了。记得在引用组件的时候添加v-if判断来解决刷新不及时的问题。<span class="em">1</span> #### 引用[.reference_title] - *1* [vue3+echarts实现炫酷的百分比饼图,echarts组件封装](https://blog.csdn.net/xu20166424/article/details/128558713)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值