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>
  • 16
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue3 是一个流行的 JavaScript 框架,而 ECharts 是一个基于 JavaScript 的数据可视化库,可用于创建各种图表和图形。Vue3 和 ECharts 可以很好地集成,因为它们都是基于 JavaScript 的。你可以使用 Vue3 的组件化开发来创建包含 ECharts 图表的交互式应用程序。以下是一些使用 Vue3 和 ECharts 的示例: 1. 在 Vue3 中使用 ECharts 进行数据可视化 使用 Vue3 和 ECharts 可以很容易地创建数据可视化应用程序。你可以将 ECharts 图表作为 Vue3 组件添加到你的应用程序中,然后使用 Vue3 的数据绑定和事件处理程序来实现动态交互。例如,你可以创建一个柱状图示例,当用户点击柱状图时,可以显示更多信息。 2. 使用 Vue3 和 ECharts 创建仪表板 使用 Vue3 和 ECharts 可以创建仪表板应用程序,在仪表板中显示各种图表和指标。你可以通过将 ECharts 图表作为 Vue3 组件添加到仪表板中来实现这一点。然后,你可以使用 Vue3 的路由和状态管理来实现导航和数据管理。 3. 将 ECharts 图表作为 Vue3 组件进行重用 你可以将 ECharts 图表作为可重用的 Vue3 组件进行封装,以便在多个应用程序中重复使用。然后,你可以使用 Vue3 的插槽和属性来自定义和配置每个实例的外观和行为。 总之,Vue3 和 ECharts 是两个非常强大的 JavaScript 工具,可以很好地结合使用,以创建出色的数据可视化应用程序和仪表板。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

six-key

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

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

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

打赏作者

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

抵扣说明:

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

余额充值