使用vue3+ts打開echarts的正確方式
準備工作
1. 注冊為百度地圖開發(fā)者
官網(wǎng)地址,然后在 應用管理 -> 我的應用 里,創(chuàng)建應用,創(chuàng)建好后復制 AK
2. 在根目錄的 index.html 里引入百度地圖
<head> <meta charset="UTF-8" /> <link rel="icon" type="image/svg+xml" href="/vite.svg" rel="external nofollow" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>xxx</title> <script type="text/javascript" src="https://api.map.baidu.com/api?v=3.0&ak=你復制好的AK"></script> </head>
在 head
里引入,是為了提前加載進來
3. 安裝 echarts
npm i echarts -S
封裝
1. 增加ts對百度地圖的支持
修改 .eslintrc.cjs
,加入對百度地圖的支持
module.exports = { // 其他省略 globals: { BMap: true } }
2. 全局注冊 echarts
修改 main.ts
// 引入 echarts import * as echarts from 'echarts' import themeJSON from '@/assets/weizwz.json' echarts.registerTheme('weizwz', themeJSON) const app = createApp(App) // 全局掛載 echarts app.config.globalProperties.$echarts = echarts
3. 封裝 echarts
src/components
下新建 chart
文件夾,并在其下新建 index.vue
,封裝如下
<script setup lang="ts"> import { onMounted, getCurrentInstance, defineExpose, ref } from 'vue' defineOptions({ name: 'WChart' }) // defineExpose 讓父組件可調(diào)用此方法 defineExpose({ setData }) // 組件傳參 const props = defineProps({ width: { type: String, //參數(shù)類型 default: '100%', //默認值 required: false //是否必須傳遞 }, height: { type: String, default: '10rem', required: true }, option: { type: Object, default: () => { return {} }, required: true }, // 初始化之前的工作,比如加載百度地圖相關(guān)數(shù)據(jù) initBefore: { type: Function, required: false }, // 初始化之后的工作,比如添加百度地址控件 initAfter: { type: Function, required: false } }) let chart: { setOption: (arg0: Record<string, any>) => void; resize: () => void } const wchart = ref(null) //聲明周期函數(shù),自動執(zhí)行初始化 onMounted(() => { init() // 監(jiān)控窗口大小,自動適應界面 window.addEventListener('resize', resize, false) }) //初始化函數(shù) function init() { // 基于準備好的dom,初始化echarts實例 const dom = wchart.value // 通過 internalInstance.appContext.config.globalProperties 獲取全局屬性或方法 let internalInstance = getCurrentInstance() let echarts = internalInstance?.appContext.config.globalProperties.$echarts chart = echarts.init(dom, 'weizwz') // 渲染圖表 if (props.initBefore) { props.initBefore(chart).then((data: Record<string, any>) => { setData(data) if (props.initAfter) props.initAfter(chart) }) } else { chart.setOption(props.option) if (props.initAfter) props.initAfter(chart) } } function resize() { chart.resize() } // 父組件可調(diào)用,設(shè)置動態(tài)數(shù)據(jù) function setData(option: Record<string, any>) { chart.setOption(option) } </script> <template> <div ref="wchart" :style="`width: ${props.width} ; height: ${props.height}`" /> </template> <style lang="scss" scoped></style>
使用
1. 使用 echarts 普通圖表
示例:使用玫瑰環(huán)形圖
<script setup lang="ts"> import WChart from '@comp/chart/index.vue' defineOptions({ name: 'ChartLoop' }) // 正常 echarts 參數(shù) const option = { grid: { top: '20', left: '10', right: '10', bottom: '20', containLabel: true }, series: [ { name: '人口統(tǒng)計', type: 'pie', radius: [50, 120], center: ['50%', '50%'], roseType: 'area', itemStyle: { borderRadius: 8 }, label: { formatter: '\n{c} 萬人' }, data: [ { value: 2189.31, name: '北京' }, { value: 1299.59, name: '西安' }, { value: 1004.79, name: '長沙' } ] } ] } </script> <template> <WChart width="100%" height="300px" :option="option" /> </template> <style lang="scss" scoped></style>
2. 結(jié)合百度地圖
示例:西安熱力圖
<script setup lang="ts"> import { reactive } from 'vue' import WChart from '@comp/chart/index.vue' // 注意需要引入 bmap,即 echarts 對百度地圖的支持擴展 import 'echarts/extension/bmap/bmap' // 熱力數(shù)據(jù),內(nèi)容如:{ features: [ { geometry: { coordinates: [ [ [x, y] ] ] } } ]} // 為什么這么復雜,因為是我從阿里地理數(shù)據(jù)下載的,地址 https://datav.aliyun.com/portal/school/atlas/area_selector import xianJson from '@/assets/xian.json' defineOptions({ name: 'ChartMap' }) const option = { animation: false, backgroundColor: 'transparent', bmap: { // 地圖中心點 center: [108.93957150268, 34.21690396762], zoom: 12, roam: true }, visualMap: { show: false, top: 'top', min: 0, max: 5, seriesIndex: 0, calculable: true, inRange: { color: ['blue', 'blue', 'green', 'yellow', 'red'] } }, series: [ { type: 'heatmap', coordinateSystem: 'bmap', data: reactive([] as any[]), pointSize: 5, blurSize: 6 } ] } const initBefore = () => { return new Promise((resolve) => { // 處理數(shù)據(jù) const arr = [] for (const item of xianJson.features) { const positions = item.geometry.coordinates[0][0] for (const temp of positions) { const position = temp.concat(Math.random() * 1000 + 200) arr.push(position) } } option.series[0].data = arr resolve(option) }) } const initAfter = (chart: { getModel: () => { (): any new (): any getComponent: { (arg0: string): { (): any; new (): any; getBMap: { (): any; new (): any } }; new (): any } } }) => { // 添加百度地圖插件 var bmap = chart.getModel().getComponent('bmap').getBMap() // 百度地圖樣式,需要自己去創(chuàng)建 bmap.setMapStyleV2({ styleId: 'bc05830a75e51be40a38ffc9220613bb' }) // bmap.addControl(new BMap.MapTypeControl()) } </script> <template> <WChart width="100%" height="500px" :option="option" :initBefore="initBefore" :initAfter="initAfter" /> </template> <style lang="scss" scoped></style>
實例項目
使用 vite5 + vue3 + ts,項目地址 vite-vue3-charts,預覽地址 https://weizwz.com/vite-vue3-charts
總結(jié)
到此這篇關(guān)于使用vue3+ts打開echarts的正確方式的文章就介紹到這了,更多相關(guān)vue3+ts打開echarts內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3中el-table實現(xiàn)表格合計行的示例代碼
這篇文章主要介紹了vue3中el-table實現(xiàn)表格合計行,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01vue-router后臺鑒權(quán)流程實現(xiàn)
本文主要介紹了vue-router后臺鑒權(quán)流程實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08Vue 實現(xiàn)創(chuàng)建全局組件,并且使用Vue.use() 載入方式
這篇文章主要介紹了Vue 實現(xiàn)創(chuàng)建全局組件,并且使用Vue.use() 載入方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08淺談VUE防抖與節(jié)流的最佳解決方案(函數(shù)式組件)
這篇文章主要介紹了淺談VUE防抖與節(jié)流的最佳解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-05-05