日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > vue >内容正文

vue

vue3使用echarts

發(fā)布時(shí)間:2023/12/10 vue 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 vue3使用echarts 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本地安裝Echarts

npm install echarts --save有淘寶鏡像的可以選擇 (安裝速度快) cnpm install echarts --save

新建一個(gè)echarts.js文件

// 引入 echarts 核心模塊,核心模塊提供了 echarts 使用必須要的接口。 import * as echarts from "echarts/core";/** 引入柱狀圖and折線(xiàn)圖圖表,圖表后綴都為 Chart */ import { BarChart, LineChart } from "echarts/charts";// 引入提示框,標(biāo)題,直角坐標(biāo)系,數(shù)據(jù)集,內(nèi)置數(shù)據(jù)轉(zhuǎn)換器組件,組件后綴都為 Component import {TitleComponent,TooltipComponent,GridComponent,DatasetComponent,TransformComponent, } from "echarts/components";// 標(biāo)簽自動(dòng)布局,全局過(guò)渡動(dòng)畫(huà)等特性 import { LabelLayout, UniversalTransition } from "echarts/features";// 引入 Canvas 渲染器,注意引入 CanvasRenderer 或者 SVGRenderer 是必須的一步 import { CanvasRenderer } from "echarts/renderers";// 注冊(cè)必須的組件 echarts.use([TitleComponent,TooltipComponent,GridComponent,DatasetComponent,TransformComponent,BarChart,LabelLayout,UniversalTransition,CanvasRenderer,LineChart, ]);// 導(dǎo)出 export default echarts;

創(chuàng)建好的js文件全局引入到main.js內(nèi)

import { createApp } from 'vue' import App from './App.vue' import router from "./router/index"import echarts from './utils/echarts';const app = createApp(App)app.config.globalProperties.$echarts = echartsapp.use(router) app.use(pinia)app.mount('#app')

這里需要注意:(組件名大寫(xiě)首字母)
按需引入時(shí):import { LineChart } from “echarts/charts”?

<script setup>邏輯內(nèi)使用echart

因?yàn)閟etup中沒(méi)有this,而且這時(shí)候還沒(méi)有渲染.所以,在setup中,也可以使用provide/inject把echarts引入進(jìn)來(lái).? 在根組件里引入echarts

// App.vue文件內(nèi)插入生產(chǎn)者provide <script setup> import * as echarts from "echarts"; import { provide } from "vue"; provide("echarts", echarts); </script><template><router-view></router-view> </template><style> body {margin: 0px; } </style>

在需要的頁(yè)面中inject

<template><div><div id="main"></div><div id="maychar"></div></div> </template><script lang="js"> import { defineComponent, onMounted, inject } from "vue"; // 主要 export default defineComponent({setup() {onMounted(() => {change();changetype();});let echarts = inject("echarts"); // 主要// 基本柱形圖const change = () => {const chartBox = echarts.init(document.getElementById("main")); // 主要const option = {xAxis: {data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],},yAxis: {},series: [{type: "bar",data: [23, 24, 18, 25, 27, 28, 25],},],};chartBox.setOption(option);// 根據(jù)頁(yè)面大小自動(dòng)響應(yīng)圖表大小window.addEventListener("resize", function () {chartBox.resize();});};// 折線(xiàn)圖const changetype = () => {// 獲取組件實(shí)例const machart = echarts.init(document.getElementById("maychar"));// 設(shè)置配置項(xiàng)const option = {xAxis: {data: ["A", "B", "C", "D", "E"],},yAxis: {},series: [{data: [10, 22, 28, 43, 49],type: "line",stack: "x",},{data: [5, 4, 3, 5, 10],type: "line",stack: "x",},],};// 復(fù)制machart.setOption(option);// 根據(jù)頁(yè)面大小自動(dòng)響應(yīng)圖表大小window.addEventListener("resize", function () {machart.resize();});};return {changetype,};}, }); </script><style lang="scss" scoped> #main {min-width: 31.25rem;min-height: 31.25rem;// max-height: 500px; }#maychar {max-height: 500px;// max-height: 400px;height: 500px; } </style>

達(dá)到統(tǒng)一管理引入的echarts效果

總結(jié)

以上是生活随笔為你收集整理的vue3使用echarts的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。