vue项目中使用echarts

1、安装

npm install echarts --save

下方是我微信公众号的二维码,可以扫码关注以下,后期博文推送主要在公众号上面,有什么问题也可以通过公众号跟我发消息哦~
在这里插入图片描述

2、使用
在需要用到的页面引入

import echarts from 'echarts'

以下分别举例饼图、折线图、柱状图

一、例1(饼图):
在这里插入图片描述

 <!-- 图表 -->
 <template>
 
    <div>
      <div style="height:400px;" ref="chart"></div>
    </div>
    
</template>

<script>
import echarts from 'echarts'
export default {
  mounted() {
    this.chartInit()
  },
  methods: {
    chartInit() {
      const myChart = echarts.init(this.$refs.chart);
      const colors = ['#6CCCEB', '#FF8F4B', '#FFEF83', '#6FE59C'];   // 定义饼块的颜色系列
      let option = {
        title: {
          text:  'xxx统计图',  // 标题
          left: 'center'      // 设置标题居中
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          orient: 'vertical',
          right: 80,     // 离右侧的距离(备注的饼图饼块的名称离右侧的距离 如上图右侧展示)
          top: 20,       // 离上方的距离
          data: ['3000元以下', '3000-4000元', '4000-5000元', '5000元以上']
        },
        series: [
          {
            name: '薪资',   //  鼠标放在每个饼块上显示的饼块类别
            type: 'pie',   
            radius: '55%',
            center: ['50%', '50%'],    // 饼图位置
            data: [
              {
                value: 20,     // 用来算饼块占比
                name: '3000元以下',
                itemStyle: {    // 饼块的颜色
                  color: colors[0]
                },
              },
              {
                value: 30, name: '3000-4000元',
                itemStyle: {
                  color: colors[1]
                },
              },
              {
                value: 20,
                name: '4000-5000元',
                itemStyle: {
                  color: colors[2]
                },              
              },
              {
                value: 30,
                name: '5000元以上',
                itemStyle: {
                  color: colors[3]
                },              
              },
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)',   // 鼠标放在饼块时的阴影
              }
            },
          }
        ]
      };
      myChart.setOption(option);

    },
  }
}
</script>

一、例2(柱状图):
在这里插入图片描述

<template>
    <!-- 柱状图 -->
    <div>
      <div style="height:400px;" ref="chart"></div>
    </div>
</template>

<script>
import echarts from 'echarts'
export default {
  mounted() {
    this.chartInit()
  },
  methods: {
    chartInit() {
      const myChart = echarts.init(this.$refs.chart);
      let option = {
        title: {
          text: 'xxx统计',
          left: 'center'
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['浏览数', '申请数'],
          right: 120,
        },
        calculable: true,
        xAxis: [
          {
            type: 'category',
            data: ['周一', '周二', '周三', '周四', '周五', '周六'],
          }
        ],
        yAxis: [
          {
            type: 'value',
            splitLine: {
              show: false  // 去掉y轴默认的横线
            }
          }
        ],
        series: [
          {
            name: '浏览数',
            type: 'bar',
            barWidth: 20,  // 柱状图 宽度20px
            data: [8, 5, 0, 3, 1, 7],
            label: {  //标记数量
              normal: {
                show: true,
                position: 'top'
              }
            },
            itemStyle: {   //  设置柱状图颜色
              color: '#FF624A'
            },
          },
          {
            name: '申请数',
            type: 'bar',
            barWidth: 20, // 柱状图 宽度
            data: [5, 1, 6, 4, 0, 2],
            label: {  //标记数量
              normal: {
                show: true,
                position: 'top'
              }
            },
            itemStyle: {
              color: '#6FE59B'
            },
          }
        ]
      };
      myChart.setOption(option);

    },

  }
}
</script>

一、例2(折线图):
在这里插入图片描述

<template>
    <!-- 折线图 -->
    <div>
      <div style="height:400px;" ref="chart"></div>
    </div>
</template>

<script>
import echarts from 'echarts'
export default {
  mounted() {
    this.chartInit()
  },
  methods: {
    chartInit() {
      const myChart = echarts.init(this.$refs.chart);
      let option = {
        title: {  //标题
          text: 'xxx报表',
          left: 'center'
        },
        xAxis: {
          type: 'category',
          data: ['第1周', '第2周', '第3周', '第4周'],

        },
        yAxis: {
          type: 'value',
          splitLine: {
            show: false  // 去掉y轴默认的横线
          }
        },
        series: [
          {
            data: [0, 30, 23, 41],
            type: 'line',
            smooth: true,    // 是否是平滑线条
            itemStyle: {
              borderWidth: 2,       // // 折线与x轴虚线相交处的小点颜色线大小
              borderColor: '#fe472b',    // 折线与x轴虚线相交处的小点颜色
              color: '#fe472b'    // 折线颜色
            },
            label: {  //标记数量
              normal: {
                show: true,
                position: 'top'
              }
            },
            markLine: { // x轴虚线
              symbol: ['none', 'none'],
              label: { show: false },
              lineStyle: {   // 虚线颜色
                color: '#ccc',
              },
              data: [
                { xAxis: 0 },  // x轴虚线位置
                { xAxis: 1 },
                { xAxis: 2 },
                { xAxis: 3 }
              ]
            },
          }
        ]
      };
      myChart.setOption(option);

    },
   
    }
  }
}
</script>


**

实际应用:echarts 数据通过调接口获取

**
在这里插入图片描述

<template>
  <div class="page">
    <p>{{eName}}统计数据</p>
    <div class="search">
      <ul class="search-list">
        <li :class="{'white': true, 'active': isActive==index, 'white-color':isActive==index }" v-for="(item,index) in searchList" :key="index" @click="handleClick(item,index)">{{item.title}}</li>
      </ul>
      <el-button type="primary" size="small"  @click="handleExport">下载</el-button>
    </div>

    <!-- 图表 -->
    <div class="charts" v-if="!show">
      <div style="height:400px;" ref="chart"></div>
    </div>
    <!-- 统计表格 -->
    <div class="data">
      <el-table :data="tableData" style="width: 100%"  id="exportTable">
        <el-table-column type="index" label="序号" width="60" align="center"></el-table-column>
        <el-table-column prop="keyName" :label="this.title" align="center"></el-table-column>
        <el-table-column prop="value" label="总数" align="center"></el-table-column>
        <el-table-column prop="result" label="百分比" align="center"></el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import echarts from 'echarts'  // 图表
import { countOfApplyResumeGroupBy } from '@/apis/apis'
import FileSaver from 'file-saver'  // 下载表格数据
import XLSX from 'xlsx'  // 下载表格数据
export default {
  data() {
    return {
      isActive: 0, 
      searchList: [
        { title: "性别", tag: "sex" },
        { title: "年龄", tag: "age" },
        { title: "学历", tag: "edu" },
        { title: "工作经验", tag: "work_age" },
      ],
      title: '性别',
      groupType: '',        // title(性别) 对应的英文(传给后台)
      pieData: [],          //饼图数据
      menuList: [],         // 饼图右上方标注
      colors: ['#6CCCEB', '#FFEF83', '#6FE59C', '#FF8F4B', '#FDBC3C', '#B9C5DD', '#6089D9', '#30BEEC'],   // 饼图每块的颜色
      show: false,   //是否显示图表
      tableData: [],  // 统计表格数据
      id: '',
      eName: '',
    }
  },
  created() {
    this.id = this.$route.query.id;
    this.eName= this.$route.query.name;
    this.getList();
  },
  methods: {
    //获取饼图数据
    getList() {
      let params = {
        id: this.id,
        groupType: this.groupType || 'sex'  // 刚开始传sex,和title对应
      }
      countOfApplyResumeGroupBy({ data: params }).then(res => {
        if (res.code == 0) {
          this.pieData = [];   //  清空饼图数据
          if (res.data.length == 0) {   // 如果没有数据,饼图部分隐藏
            this.show = true
            return;
          }
          this.menuList = res.data.map(element => element.keyName);  //饼图上方右侧标注
          res.data.forEach((element, index) => {
            this.tableData = res.data;
            this.pieData.push({
              value: element.value,
              name: element.keyName,
              itemStyle: { color: this.colors[index] }
            })
          });
          this.chartInit()    // 渲染饼图
        }
      })
    },
    //饼图
    chartInit() {
      const myChart = echarts.init(this.$refs.chart);
      let option = {
        title: {
          text: this.title + '统计图',
          left: 'center'
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          orient: 'vertical',
          right: 80,
          top: 20,
          // data: ['3000元以下', '3000-4000元', '4000-5000元', '5000元以上'],
          data: this.menuList,
        },
        series: [
          {
            name: this.title,
            type: 'pie',
            radius: '55%',
            center: ['50%', '50%'],
            // data: [
            //   {
            //     value: 20,
            //     name: '3000元以下',
            //     itemStyle: {  color: colors[0]  // 饼块的颜色 },
            //   },
            // ],
            data: this.pieData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)',
              }
            },
          }
        ]
      };
      myChart.setOption(option);
    },
    //点击切换类目: 性别、年龄等
    handleClick(item, index) {
      this.isActive = index;
      this.title = item.title;
      this.groupType = item.tag;
      this.getList();  // 获取数据,渲染饼图
    },

    //下载
    handleExport() {
      this.$confirm('该操作将导出统计数据,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let exportTable = XLSX.utils.table_to_book(document.querySelector('#exportTable'))
        let exportTableOut = XLSX.write(exportTable, { bookType: 'xlsx', bookSST: true, type: 'array' })
        try {
          FileSaver.saveAs(new Blob([exportTableOut], { type: 'application/octet-stream' }), `${this.title}统计.xlsx`)
        } catch (e) { if (typeof console !== 'undefined') console.log(e, exportTableOut) }
        return exportTableOut;
      }).catch(() => { });
    },
  }
}
</script>

<style lang="less" scoped>
.page {
  .search {
    color: #333;
    .search-list {
      li {
        width: 100px;
        line-height: 34px;
        text-align: center;
        font-size: 14px;
        display: inline-block;
        margin-right: 20px;
      }
      .active {
        background-color: #ff6750;
      }
    }
  }
  .data {
    padding: 20px 40px 50px 40px;
  }
}
</style>
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值