echarts的使用

1. 普通版

其实主要就是option1,option1就是画的图

echats不能响应刷新,要想实时刷新监听刷新的值重新调用一下方法即可

html
<div class="echart" style="width: 100%;height: calc(100% - 130px)" ref="main1"></div>

 
js
import * as echarts from "echarts";

 mounted() {
    this.initDayEcharts();
},

initDayEcharts() {
      const option1 = {
        legend: {
          textStyle: {
            color: '#ffffff'
          },
          y: 'bottom',
        },
        grid: {
          left: '5%',
          right: '5%',
          top: '10%',
          bottom: '15%',
          containLabel: true
        },
        xAxis: {
          type: 'value',
          axisLabel: {
            textStyle: {
              color: '#ffffff'
            }
          },
        },
        yAxis: {
          type: 'category',
          data: this.inAndOutWarehouseToday.name,
          axisLabel: {
            textStyle: {
              color: '#ffffff'
            }
          },
        },
        series: [
          {
            name: '入库数',
            type: 'bar',
            stack: 'total',
            label: {
              show: true
            },
            emphasis: {
              focus: 'series'
            },
            color: '#4F89FC',
            //动态条形图宽度
            barWidth: 20 ,
            data: this.inAndOutWarehouseToday.inNum
          },
          {
            name: '出库数',
            type: 'bar',
            stack: 'total',
            label: {
              show: true
            },
            emphasis: {
              focus: 'series'
            },
            color: '#A5DC65',
            barWidth: 20,
            data: this.inAndOutWarehouseToday.outNum
          }
        ]
      };

      // 获取DOM元素
      const main1 = echarts.init(this.$refs.main1);
      // 设置图表配置项和数据
      main1.setOption(option1);

    },

2. 可跳转路径版+赋值版

注意样式要给宽度,不然看不到

<div id="roadElectricity"  style="flex: 1;height: 400px;width: 500px"></div>


js
  mounted() {
    this.getRouteElectromechanical()
  },
 methods: {
a    sync getRouteElectromechanical(params) {
      let queryParams = Object.assign({}, {name: '机电设施路段机电统计'}, params)
      let {data: data} = await commonListData(queryParams)
      console.log("data===",data)

      let res = data || []
      let roadElectricity = this.$echarts.init(document.getElementById('roadElectricity'))
      let  option = {
        series: [
          {
            type: 'pie',
            data: [
              {
                value: 0,
                name: '服务区监控'
              },
              {
                value: 0,
                name: '路段监控系统'
              },
              {
                value: 0,
                name: '站级监控系统'
              },
            ],
            radius: '50%'
          }
        ]
      };
      if (res.length > 0) {
        console.log("机电设施路段机电统计=", res)
        option.series[0].data = res.map(item => Object.assign({}, {value: item.count, name: item.asset_type_name}))
      }

      roadElectricity.setOption(option)

      roadElectricity.on("click", (params) => {
        console.log('params=', params)
        this.$router.push({
          path: '/account/asset/account/search/assetDetail',
          query: Object.assign({}, {
                assetTypeId: 1037 ,
                assetTypeName: params.name
              }
          )
        })
      })
    },

}

this.$echarts是全局定义的,都差不多

main.js

import * as echarts from 'echarts'

Vue.prototype.$echarts = echarts

2. 的一句代码解析

for (let i = 0; i < reoption.series[0].data = res.map(item => Object.assign({}, {value: item.count, name: item.asset_type_name}))

非简写

for (let i = 0; i < res.length; i++) {
  let item = res[i];
  let newItem = {value: item.count, name: item.asset_type_name};
  option.series[0].data.push(newItem);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值