最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用vue3+ts打開echarts的正確方式

 更新時(shí)間:2023年12月29日 11:28:54   作者:唯之為之  
這篇文章主要給大家介紹了關(guān)于使用vue3+ts打開echarts的正確方式,在Vue3中使用ECharts組件可以方便地創(chuàng)建各種數(shù)據(jù)可視化圖表,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下

準(zhǔn)備工作

1. 注冊(cè)為百度地圖開發(fā)者

官網(wǎng)地址,然后在 應(yīng)用管理 -> 我的應(yīng)用 里,創(chuàng)建應(yīng)用,創(chuàng)建好后復(fù)制 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=你復(fù)制好的AK"></script>
</head>

head 里引入,是為了提前加載進(jìn)來

3. 安裝 echarts

npm i echarts -S

封裝

1. 增加ts對(duì)百度地圖的支持

修改 .eslintrc.cjs,加入對(duì)百度地圖的支持

module.exports = {
  // 其他省略
  globals: {
    BMap: true
  }
}

2. 全局注冊(cè) 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%', //默認(rèn)值
    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ù),自動(dòng)執(zhí)行初始化
onMounted(() => {
  init()
  // 監(jiān)控窗口大小,自動(dòng)適應(yīng)界面
  window.addEventListener('resize', resize, false)
})
 
//初始化函數(shù)
function init() {
  // 基于準(zhǔn)備好的dom,初始化echarts實(shí)例
  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è)置動(dòng)態(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)計(jì)',
      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: '長(zhǎng)沙' }
      ]
    }
  ]
}
</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 對(duì)百度地圖的支持?jǐn)U展
import 'echarts/extension/bmap/bmap'
// 熱力數(shù)據(jù),內(nèi)容如:{ features: [ { geometry: { coordinates: [ [ [x, y] ] ] } } ]}
// 為什么這么復(fù)雜,因?yàn)槭俏覐陌⒗锏乩頂?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: {
    // 地圖中心點(diǎn)
    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>

實(shí)例項(xiàng)目

使用 vite5 + vue3 + ts,項(xiàng)目地址 vite-vue3-charts,預(yù)覽地址 https://weizwz.com/vite-vue3-charts

總結(jié)

到此這篇關(guān)于使用vue3+ts打開echarts的正確方式的文章就介紹到這了,更多相關(guān)vue3+ts打開echarts內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue2.0實(shí)現(xiàn)選項(xiàng)卡導(dǎo)航效果

    vue2.0實(shí)現(xiàn)選項(xiàng)卡導(dǎo)航效果

    這篇文章主要為大家詳細(xì)介紹了vue2.0實(shí)現(xiàn)選項(xiàng)卡導(dǎo)航效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • vue3+vite+ts使用monaco-editor編輯器的簡(jiǎn)單步驟

    vue3+vite+ts使用monaco-editor編輯器的簡(jiǎn)單步驟

    因?yàn)楫呍O(shè)需要用到代碼編輯器,根據(jù)調(diào)研,我選擇使用monaco-editor代碼編輯器,下面這篇文章主要給大家介紹了關(guān)于vue3+vite+ts使用monaco-editor編輯器的簡(jiǎn)單步驟,需要的朋友可以參考下
    2023-01-01
  • vue界面發(fā)送表情的實(shí)現(xiàn)代碼

    vue界面發(fā)送表情的實(shí)現(xiàn)代碼

    這篇文章主要介紹了vue界面發(fā)送表情的實(shí)現(xiàn)代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • 前端vue3樹形組件使用代碼示例

    前端vue3樹形組件使用代碼示例

    最近在開發(fā)時(shí)遇到一個(gè)問題,是在輸入框里面放一個(gè)樹形組件,這篇文章主要給大家介紹了關(guān)于前端vue3樹形組件使用的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-07-07
  • 圖文講解用vue-cli腳手架創(chuàng)建vue項(xiàng)目步驟

    圖文講解用vue-cli腳手架創(chuàng)建vue項(xiàng)目步驟

    本次小編給大家?guī)淼氖顷P(guān)于用vue-cli腳手架創(chuàng)建vue項(xiàng)目步驟講解內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。
    2019-02-02
  • v-slot和slot、slot-scope之間相互替換實(shí)例

    v-slot和slot、slot-scope之間相互替換實(shí)例

    這篇文章主要介紹了v-slot和slot、slot-scope之間相互替換實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • Vue路由懶加載與組件懶加載示例詳解

    Vue路由懶加載與組件懶加載示例詳解

    懶加載也稱為延遲加載,是一種將資源(如圖片、組件、代碼等)推遲到需要的時(shí)候再加載的策略,這篇文章主要介紹了Vue路由懶加載與組件懶加載的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2026-03-03
  • vue如何通過image-conversion實(shí)現(xiàn)圖片壓縮詳解

    vue如何通過image-conversion實(shí)現(xiàn)圖片壓縮詳解

    在Vue項(xiàng)目中上傳大圖片時(shí),可以通過image-conversion庫壓縮至指定大小,這篇文章主要介紹了vue如何通過image-conversion實(shí)現(xiàn)圖片壓縮的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-12-12
  • Vue-Router如何動(dòng)態(tài)更改當(dāng)前頁url query

    Vue-Router如何動(dòng)態(tài)更改當(dāng)前頁url query

    這篇文章主要介紹了Vue-Router如何動(dòng)態(tài)更改當(dāng)前頁url query問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • axios中cookie跨域及相關(guān)配置示例詳解

    axios中cookie跨域及相關(guān)配置示例詳解

    自從入了 Vue 之后,一直在用 axios 這個(gè)庫來做一些異步請(qǐng)求。下面這篇文章主要給大家介紹了關(guān)于axios中cookie跨域及相關(guān)配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起看看吧。
    2017-12-12

最新評(píng)論

新田县| 柘荣县| 罗江县| 福建省| 贡嘎县| 玉山县| 额尔古纳市| 高要市| 普宁市| 西吉县| 华安县| 平安县| 陇南市| 镇远县| 盐池县| 科技| 云南省| 平顺县| 淳安县| 乐陵市| 武定县| 莱西市| 深圳市| 嘉鱼县| 咸宁市| 鲁山县| 泗洪县| 将乐县| 融水| 佛冈县| 宜兴市| 哈密市| 黄山市| 农安县| 襄垣县| 宁波市| 华坪县| 肥东县| 西城区| 梅河口市| 宁南县|