vue3-admin-element框架引入echars图表

        在写后台管理系统时首页部分有时需要引入echars图表来进行一些数据的可视化操作,那么在框架中怎么进行echars图表的引入呢?本文通过一个小示例来进行说明。

一、下载echars插件包

        首先在vue3-admin-element框架中需要先安装echars依赖,可以使用如下命令:

         如果你是npm安装:


npm install echarts --save

          如果你是cnpm安装:


cnpm install echarts --save

二、使用echars

     在使用时我们第一步只是下载了echars插件,我们还需要进行引入,在引入时如下所示:

<script>
	import echarts from 'echarts'; 
</script>

    如果以上引入方式报错可以换用下面这句:

import * as echarts from "echarts";

三、创建HIML结构和CSS

<template>
  <div>
    <div class="top">
      <div class="box">
        <div id="container" style="width: 100%;height: 100%;">
        </div>
      </div>
      <div class="box">
        <div id="one" style="width: 100%;height: 100%;"></div>
      </div>
    </div>
    <div class="bottom">
      <div id="myEcharts" style="width: 1100px;height:400px;"></div>
    </div>
  </div>
</template>
<style scoped>
.title {
  width: 98%;
  margin-left: 1%;
  height: 50px;
  background-color: #fff;
  margin-bottom: 20px;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.title p {
  margin-left: 20px;
}

.top,
.bottom {
  width: 98%;
  margin-left: 1%;
  display: flex;
  justify-content: space-around;
  margin-bottom: 14px;
}

.bottom {
  background-color: #fff;
}

.box {
  width: 49%;
  height: 260px;
  background-color: #fff;
}</style>

四、书写js代码

       这里是示例,你可以去echars官网去选择想要的图表并且进行相应配置项的修改,将代码移植到你的代码中。

export default {
  name: "echartsBox",
  setup() {
    /// 声明定义一下echart
    let echart = echarts;

    onMounted(() => {
      initChart();
      initparChart();
      initright();
    });

    onUnmounted(() => {
      echart.dispose;
    });

    // 基础配置一下Echarts
    function initChart() {
      let chart = echart.init(document.getElementById("myEcharts"));
      // 把配置和数据放这里
      chart.setOption({
        xAxis: {
          type: "category",
          data: [
            "一月",
            "二月",
            "三月",
            "四月",
            "五月",
            "六月",
            "七月",
            "八月",
            "九月",
            "十月",
            "十一月",
            "十二月"
          ]
        },
        tooltip: {
          trigger: "axis"
        },
        yAxis: {
          type: "value"
        },
        series: [
          {
            data: [
              820,
              932,
              901,
              934,
              1290,
              1330,
              1320,
              801,
              102,
              230,
              4321,
              4129
            ],
            type: "line",
            smooth: true
          }
        ]
      });


      window.onresize = function () {
        //自适应大小
        chart.resize();
      };

    }
    function initparChart() {
      //初始化
      let myChart = echarts.init(document.getElementById('container'));
      myChart.setOption({
        legend: {
          // 图例
          data: ["企业老板", "参加工作", "个体户", "自由职业", "其他"],
          right: "10%",
          top: "10%",
          orient: "vertical"
        },
        title: {
          // 设置饼图标题,位置设为顶部居中
          text: "用户分类",
          top: "0%",
          left: "left"
        },
        series: [
          {
            type: "pie",
            data: [
              {
                value: 40,
                name: "企业老板"
              },
              {
                value: 100,
                name: "参加工作"
              },
              {
                value: 166,
                name: "个体户"
              },
              {
                value: 201,
                name: "自由职业"
              },
              {
                value: 147,
                name: "其他"
              }
            ]
          }
        ]
      });
      window.onresize = function () {
        //自适应大小
        myChart.resize();
      };

    }
    function initright() {
      var youChart = echarts.init(document.getElementById('one'));
      youChart.setOption({
        legend: {
          // 图例
          data: ["按摩足疗", "洗浴", "ktv", "酒吧", "游泳馆","健身中心",'茶舍',"游戏中心"],
          right: "10%",
          top: "10%",
          orient: "vertical"
        },
        title: {
          text: "娱乐门店入驻",
          top: "0%",
          left: "left"
        },
        series: [
          {
            type: "pie",
            data: [
              {
                value: 40,
                name: "按摩足疗"
              },
              {
                value: 100,
                name: "洗浴"
              },
              {
                value: 166,
                name: "ktv"
              },
              {
                value: 201,
                name: "酒吧"
              },
              {
                value: 147,
                name: "游泳馆"
              },{
                value:23,
                name:"健身中心"
              },
              {
                value:45,
                name:"茶舍"
              },              {
                value:4,
                name:"游戏中心"
              }
            ]
          }
        ]
      });
    }
    window.onresize = function () {w
        //自适应大小
        youChart.resize();
      };
    return { initChart, initparChart,initright };
  }
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值