Vue3集成Echarts详解

一、安装

 npm install echarts --save

二、引用到Vue身上

  • 在src下新建echart文件夹

  • 在echart文件夹中新建index.js文件

index.js

 import * as echarts from "echarts";
 export default echarts;

在main.js中引入

 import echarts from "@/echarts";
 ​
 const app = createApp(App);
 app.use(echarts);
 app.mount("#app");

三、在组件中使用

一下是一个具体使用的例子

白色背景下

黑色背景下

 <template>
   <div>
     <div
       style="width: 100%; height: 40px; display: flex; justify-content: center"
     >
         <--动态切换背景颜色 --></--动态切换背景颜色>
       <a-switch @click="switchColorMethod" v-model:checked="switchColor" /><span
         style="margin-left: 10px; font-weight: bold"
         >深色模式</span
       >
     </div>
     <a-row>
       <a-col :span="12">
         <!-- 会议数据 -->
         <div>
           <div style="font-size: 24px; color: #000000; margin-bottom: 20px">
             会议统计
           </div>
           <div>
             <div id="reservation" style="width: 640px; height: 350px"></div>
           </div>
         </div>
       </a-col>
       <a-col :span="12">
         <!--  用户、评价、反馈数据-->
         <div>
           <div
             style="
               display: flex;
               justify-content: flex-end;
               font-size: 24px;
               color: #000000;
               margin-bottom: 20px;
             "
           >
             内容统计
           </div>
           <div id="threeData" style="width: 640px; height: 350px"></div>
         </div>
       </a-col>
     </a-row>
   </div>
 </template>
 ​
 <script setup>
 import { onMounted, ref } from "vue";
 import echarts from "@/echarts";
 ​
 const switchColor = ref(false);
 onMounted(() => {
   initReservation();
   initThreeData();
 });
 ​
 //动态切换背景颜色
 function switchColorMethod() {
   initReservation();
   initThreeData();
 }
 ​
 // 用户数据展示
 function initReservation() {
   var myChart = echarts.init(document.getElementById("reservation"));
   var option = {
     xAxis: {
       type: "category",
       data: [
         "星期一",
         "星期二",
         "星期三",
         "星期四",
         "星期五",
         "星期六",
         "星期日",
       ],
     },
     yAxis: {
       type: "value",
     },
     series: [
       {
         data: [120, 200, 150, 80, 70, 110, 130],
         type: "bar",
       },
     ],
   };
   if (switchColor.value) {
     myChart.dispose();
     //黑色模式
     myChart = echarts.init(document.getElementById("reservation"), "dark");
     myChart.setOption(option);
     //随着屏幕大小调节图表
     window.addEventListener("resize", () => {
       myChart.resize();
     });
   } else {
     myChart.dispose();
       //白色模式
     myChart = echarts.init(document.getElementById("reservation"));
     myChart.setOption(option);
     //随着屏幕大小调节图表
     window.addEventListener("resize", () => {
       myChart.resize();
     });
   }
 }
 ​
 //其他全部数据展示
 function initThreeData() {
   var myChart = echarts.init(document.getElementById("threeData"));
   var option = {
     title: {
       text: "其他内容",
       subtext: "实时数据",
       left: "center",
     },
     tooltip: {
       trigger: "item",
     },
     legend: {
       orient: "vertical",
       left: "left",
     },
     series: [
       {
         name: "数据来源",
         type: "pie",
         radius: "50%",
         data: [
           { value: 1048, name: "部门" },
           { value: 735, name: "公告" },
           { value: 580, name: "通知" },
           { value: 484, name: "签到" },
           { value: 300, name: "会议室" },
         ],
         emphasis: {
           itemStyle: {
             shadowBlur: 10,
             shadowOffsetX: 0,
             shadowColor: "rgba(0, 0, 0, 0.5)",
           },
         },
       },
     ],
   };
   if (switchColor.value) {
     myChart.dispose();
     myChart = echarts.init(document.getElementById("threeData"), "dark");
     myChart.setOption(option);
     //随着屏幕大小调节图表
     window.addEventListener("resize", () => {
       myChart.resize();
     });
   } else {
     myChart.dispose();
     myChart = echarts.init(document.getElementById("threeData"));
     myChart.setOption(option);
     //随着屏幕大小调节图表
     window.addEventListener("resize", () => {
       myChart.resize();
     });
   }
 }
 </script>
 ​
 <style scoped>
 </style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

six-key

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值