vue3+ts使用Echarts的實(shí)例詳解
安裝
npm install echarts --save
引入
import * as echarts from 'echarts';
(一般項(xiàng)目中大致會(huì)用到三種圖表:柱形圖、折線圖、餅圖。所以本文在舉例說(shuō)明中主要以這三種圖表為例。)
echarts.setOption()配置項(xiàng)常用屬性說(shuō)明(查看完整版:Echarts-Documentation)
內(nèi)容格式器formatter
使用tips
echarts初始化時(shí),必須給其綁定的元素設(shè)置寬高,否則echarts會(huì)初始化失敗。
舉例
<template> <div> <div ref="barChart" :style="{ width: '400px', height: '300px' }"></div> <div ref="pieChart" :style="{width:'500px',height:'300px'}"></div> </div> </template> <script setup lang="ts"> import * as echarts from 'echarts'; import { onMounted, ref } from 'vue'; const barChart = ref<HTMLElement>(); const myChart1 = ref<any>(); // 繪制柱形圖 function initBarEcharts() { myChart1.value = echarts.init(barChart.value!); myChart1.value.setOption({ title: { text: '學(xué)習(xí)輸出', x: 'center' }, tooltip: { trigger: 'item' }, legend: { data: ['輸出量', '輸入量'], orient: 'vertical', right: 60, top: 20 }, xAxis: { data: ['六月', '七月', '八月', '九月', '十月'] }, yAxis: {}, color: ['#c38bef', '#6dbcf7'], series: [ { name: '輸出量', type: 'line', data: [2, 9, 6, 3, 1], smooth: true, label: { show: true, position: 'top' } }, { name: '輸入量', type: 'bar', data: [9, 18, 12, 9, 6], barWidth: '20', label: { show: true, position: 'top' } } ] }); } //繪制餅圖 const pieChart = ref<HTMLElement>(); const myChart2 = ref<any>(); function initPieEcharts(){ myChart2.value=echarts.init(pieChart.value!) myChart2.value.setOption({ title:{ text:'文章分類', x:'center' }, tooltip:{ trigger:'item', formatter:':{c}(vvxyksv9kd%)' }, legend:{ type:'scroll', orient:'vertical', left:0, top:30, height:150, data:['html','css','javascript','typescript','vue2','vue3','react','angular','uniapp','taro','vite','webpack','node','others'] }, color:['#61adf2','#56dae8','#efa49e','#7ea1ed','#5ae05a','#f2d2a2','#5881e8','#63d6c0','#edc29f','#5b97d3','#3eceb3','#6a96ed','#426ed1','#65d18b'], series:[ { name:'文章分類數(shù)量', type:'pie', radius:'30%', center:['60%','50%'], data:[ {name:'html',value:10}, {name:'css',value:12}, {name:'javascript',value:20}, {name:'typescript',value:15}, {name:'vue2',value:13}, {name:'vue3',value:12}, {name:'react',value:3}, {name:'angular',value:2}, {name:'uniapp',value:12}, {name:'taro',value:5}, {name:'vite',value:2}, {name:'webpack',value:3}, {name:'node',value:9}, {name:'others',value:10}, ], emphasis:{ itemStyle:{ shadowBlur:10, shadowOffsetX:0, shadowColor:'rgba(0,0,0,0.5)' } }, label:{ show:true, position:'outside', formatter:':{c}(vvxyksv9kd%)' } } ] }) } onMounted(() => { initBarEcharts(); initPieEcharts() }); </script>
界面展示
到此這篇關(guān)于vue3+ts使用Echarts的文章就介紹到這了,更多相關(guān)vue3 ts使用Echarts內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue踩坑記錄之echarts動(dòng)態(tài)數(shù)據(jù)刷新問(wèn)題
- 解決Vue中使用Echarts出現(xiàn)There?is?a?chart?instance?already?initialized?on?the?dom的警告問(wèn)題
- Vue+ECharts+高德地圖API實(shí)現(xiàn)天氣預(yù)報(bào)數(shù)據(jù)可視化的教程
- Vue?Echarts報(bào)錯(cuò)Initialize?failed:?invalid?dom解決方法
- vue+echarts繪制省份地圖并添加自定義標(biāo)注方式
- vue中如何使用echarts和echarts-gl實(shí)現(xiàn)3D餅圖環(huán)形餅圖
- vue?echarts移動(dòng)端踩坑解決記錄
相關(guān)文章
Vue3?<script?setup?lang=“ts“>?的基本使用
<script setup>?是在單文件組件 (SFC) 中使用?composition api?的編譯時(shí)語(yǔ)法糖,本文主要講解<script setup>?與?TypeScript?的基本使用,感興趣的朋友跟隨小編一起看看吧2022-12-12解決Vue不能檢測(cè)數(shù)組或?qū)ο笞儎?dòng)的問(wèn)題
下面小編就為大家分享一篇解決Vue不能檢測(cè)數(shù)組或?qū)ο笞儎?dòng)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02vue和thymeleaf相結(jié)合的注意事項(xiàng)詳解
這篇文章主要介紹了vue和thymeleaf相結(jié)合的注意事項(xiàng)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11使用Vue-Awesome-Swiper實(shí)現(xiàn)旋轉(zhuǎn)疊加輪播效果&平移輪播效果
這篇文章主要介紹了用Vue-Awesome-Swiper實(shí)現(xiàn)旋轉(zhuǎn)疊加輪播效果&平移輪播效果,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值需要的朋友可以參考下2019-08-08vue+openlayer5獲取當(dāng)前鼠標(biāo)滑過(guò)的坐標(biāo)實(shí)現(xiàn)方法
在vue項(xiàng)目中怎么獲取當(dāng)前鼠標(biāo)劃過(guò)的坐標(biāo)呢?下面通過(guò)本文給大家分享實(shí)現(xiàn)步驟,感興趣的朋友跟隨小編一起看看吧2021-11-11Vue數(shù)據(jù)驅(qū)動(dòng)模擬實(shí)現(xiàn)5
這篇文章主要介紹了Vue數(shù)據(jù)驅(qū)動(dòng)模擬實(shí)現(xiàn)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Vue cli+mui 區(qū)域滾動(dòng)的實(shí)例代碼
下面小編就為大家分享一篇Vue cli+mui 區(qū)域滾動(dòng)的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01