vue echarts 双饼图设计 及与后端数据连接

<template>
                     <div class="echart5" ref="mychart5" id="mychart5" :style="myChartStyle5"></div>

</template>


data() {
  
  return {
        myChartStyle5: { float: "left", width: "100%", height: "450px" }, //图表样式
        isActivated:0,
        productStatus1:0,
        productStatus2:0,
        productStatus3:0,
        productStatus4:0,
        productStatus5:0,
        productStatus6:0,
        kind1:0,
        kind2:0,
        kind3:0,
        kind4:0,
        kind5:0,
        kind6:0,
        kind7:0,
  }
}, 
activated(){

     if(!this.isActivated){

        this.isActivated = true

        this.underRight();
      
        window.addEventListener("resize", () => { this.handleResize()})
     }
      window.dispatchEvent(new Event('resize'))

  },
  beforeDestroy() {

       window.removeEventListener('resize', this.handleResize());

    },


  methods: {
   handleResize(){ 
          this.myChart5 = echarts.init(this.$refs.mychart5);
          this.myChart5.resize();
         
      },
    underRight(){

     HTZXTZProductStatus().then( res=>{

         const result  = res.result
            debugger

                for(var i=0; i< result.length;i++){

                    this.productStatus1= result[0].productStatus1
                    this.productStatus2= result[0].productStatus2
                    this.productStatus3= result[0].productStatus3
                    this.productStatus4= result[0].productStatus4
                    this.productStatus5= result[0].productStatus5
                    this.productStatus6= result[0].productStatus6
                    this.kind1=result[0].kind1
                    this.kind2=result[0].kind2
                    this.kind3=result[0].kind3
                    this.kind4=result[0].kind5
                    this.kind5=result[0].kind5
                    this.kind6=result[0].kind6
                    this.kind7=result[0].kind7

                }

            const option5 = {
                      tooltip:{
                        trigger :"item",
                      },
                       label: {
                        show: true, // 显示标签
                        position: 'inside', // 在饼图内部显示标签
                        formatter: '{b}:{d}%'
                      },
                      legend: {
                           orient: 'vertical',
                            height: 200,
                            x: 'left',
                            y: 'bottom',
                            itemGap:3,
                            icon: 'circle',
                            align: 'left',
                            itemWidth:2,
                            //borderColor: black,
                            formatter: function (name) {
                               var total = 0;
                              var data;
                              option5.series.forEach(function (series) {
                                  data = series.data.find(function (item) {
                                      return item.name === name;
                                  });
                                  total += data ? data.value : 0;
                              });
                              return name +":"+ total;
                            },
                          },

                      series: [
                        {

                          name: '产品状态',
                          type: 'pie',
                          radius: ['60%', '90%'], // 设置饼图的内外半径,实现环形效果
                          data: [
                            { value: this.productStatus1, name: '未投产' },
                            { value: this.productStatus2, name: '生产中' },
                            { value: this.productStatus3, name: '待发货' },
                            { value: this.productStatus4, name: '待安装' },
                            { value: this.productStatus5, name: '质保期内' },
                            { value: this.productStatus6, name: '质保完成' },

                          ]
                        },
                        {
                          name: '产品类别',
                          type: 'pie',

                          radius: ['20%', '40%'], // 设置饼图2的内外半径,调整大小关系
                          data: [
                            { value: this.kind1, name: '多功能' },
                            { value: this.kind2, name: '门吊' },
                            { value: this.kind3, name: '桥吊' },
                            { value: this.kind4, name: 'PG件' },
                            { value: this.kind5, name: '配件' },
                            { value: this.kind6, name: '风电' },
                            { value: this.kind7, name: '改造' },


                          ]
                        }
                      ]
                    };
              const myChart5 = echarts.init(document.getElementById("mychart5"));
              myChart5.setOption(option5);


      })
 },
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Vue3 和 echarts 来实现实时更新的饼图。下面是一个简单的示例: 1. 首先确保你已经安装了 Vue3 和 echarts,可以通过以下命令进行安装: ```shell npm install vue@next npm install echarts ``` 2. 在你的Vue组件中,引入 echarts,并创建一个容器用于显示饼图。例如,你可以在 template 中添加一个 div 元素作为容器: ```html <template> <div id="chartContainer"></div> </template> ``` 3. 在组件的 script 部分,引入 echarts,并在 mounted 钩子函数中初始化饼图。你可以从后端获取实时数据,并将其传递给饼图。 ```javascript <script> import * as echarts from 'echarts'; export default { mounted() { this.initChart(); // 模拟实时数据更新 setInterval(() => { this.updateChart(); }, 1000); }, methods: { initChart() { // 初始化饼图 this.chart = echarts.init(document.getElementById('chartContainer')); // 设置饼图的配置项 const options = { series: [ { type: 'pie', data: [ { value: 335, name: '数据1' }, { value: 310, name: '数据2' }, { value: 234, name: '数据3' }, { value: 135, name: '数据4' }, { value: 1548, name: '数据5' } ] } ] }; // 使用配置项显示饼图 this.chart.setOption(options); }, updateChart() { // 模拟实时数据更新 const newData = [ { value: Math.random() * 500, name: '数据1' }, { value: Math.random() * 500, name: '数据2' }, { value: Math.random() * 500, name: '数据3' }, { value: Math.random() * 500, name: '数据4' }, { value: Math.random() * 500, name: '数据5' } ]; // 更新饼图数据 const options = { series: [ { data: newData } ] }; this.chart.setOption(options); } } }; </script> ``` 在上述代码中,我们使用 setInterval 模拟定时获取实时数据,并通过 updateChart 方法来更新饼图数据。你可以根据你的实际需求,从后端获取实时数据并更新饼图。 请注意,上述代码仅为示例,你可能需要根据你的实际需求进行适当的修改。希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值