封裝一個公用Echarts圖表組件的3種模板代碼示例
一、安裝echarts
npm install echarts --save
二、在需要的頁面引入
import * as echarts from "echarts"
三、創(chuàng)建組件
1、模板1:vue2+javascript
<template> <div> <div class="echart_size" :id="id"></div> </div> </template> <script> import * as echarts from 'echarts' export default { props: { // 接收的參數(shù) id: { type: String, default: '' }, datas: { type: Array, default: () => [] } }, data() { return { // 變量 } }, created() { this.$nextTick(() => { this.barBtn() }) }, methods: { barBtn() { // 實例化對象 let myCharts = echarts.init(document.getElementById(this.id)) // 指定配置項和數(shù)據(jù) let option = { // 某圖表 } // 把配置項給實例對象 myCharts.setOption(option) // 讓圖表跟隨屏幕自動的去適應(yīng) window.addEventListener('resize', function () { myCharts.resize() }) } } } </script> <style lang="scss" scoped> .echart_size{ width: 500px; height: 500px; } </style>
2、模板2:vue3+javascript
vue3中,有的圖表調(diào)用不到,初始化echarts時使用 shallowRef
const myCharts = shallowRef()
<template> <div class="echart_size" :id="props.id"></div> </template> <script setup> import { reactive, ref, nextTick, onMounted } from 'vue' import * as echarts from 'echarts' const props = defineProps({ id: { type: String, required: true }, datas:{ type: Array, required: true } }) let person=reactive({ // 初始變量 }) onMounted(()=>{ GetEchar() }) const GetEchar = () => { const myCharts = ref() nextTick(() => { myCharts.value = echarts.init(document.getElementById(props.id)) let option = { // 某圖表 }; myCharts.value.setOption(option) // 讓圖表跟隨屏幕自動的去適應(yīng) window.addEventListener('resize', function () { myCharts.value.resize() }) }) } </script> <style lang="scss" scoped> .echart_size { width: 100%; height: 100%; } </style>
3、模板3:vue3+typescript
<template> <div class="echart_size" :id="props.id"></div> </template> <script lang="ts" setup> import { reactive, ref, nextTick, onMounted } from 'vue' import * as echarts from 'echarts' let person: any = reactive({ // 初始變量 }) type Props = { id: string } const props = withDefaults(defineProps<Props>(), {}) onMounted(()=>{ GetEchar() }) const GetEchar = () => { const myChart = ref<HTMLElement>() const myCharts = ref<any>() nextTick(() => { const chartDom = document.getElementById(props.id)! myCharts.value = echarts.init(chartDom) let option = { }; myCharts.value.setOption(option) // 讓圖表跟隨屏幕自動的去適應(yīng) window.addEventListener('resize', function () { myCharts.value.resize() }) }) } </script> <style lang="scss" scoped> .echart_size { width: 500px; height: 500px; } </style>
四、頁面調(diào)用
1、vue2
<template> <div> <EchartModule v-if="data&&data.length>0" :id="'myEchart'" :datas="data" /> </div> </template> <script> import EchartModule from '@/components/echartModule' export default { components: {EchartModule}, data(){ return{ data: [ { value: 0, label: '測試1' }, { value: 1, label: '測試2' } ] } } } </script>
2、vue3+js
<template> <EchartModule v-if="data&&data.length>0" :id="'myEchart'" :datas="data" /> </template> <script setup> import { reactive } from 'vue' import EchartModule from '@/components/echartModule' let person=reactive({ data:[ { value: 0, label: '測試1' }, { value: 1, label: '測試2' } ] }) </script>
3、vue3+ts
// vue3+ts <template> <EchartModule v-if="data&&data.length>0" :id="'myEchart'" :datas="data" /> </template> <script lang="ts" setup> import { reactive } from 'vue' import EchartModule from '@/components/echartModule' let person:any=reactive({ data:[ { value: 0, label: '測試1' }, { value: 1, label: '測試2' } ] }) </script>
五、Echarts 常用的相關(guān)事件
1、鼠標單擊/左鍵事件
//vue2 myCharts.on('click', function(e) {}) // vue3 myCharts.value.on('click', function(e) {})
2、鼠標移入/進入事件
//vue2 myCharts.on('mouseover', function(e) {}) // vue3 myCharts.value.on('mouseover', function(e) {})
3、鼠標移出/離開事件
//vue2 myCharts.on('mouseout', function(e) {}) // vue3 myCharts.value.on('mouseout', function(e) {})
4、讓圖表跟隨屏幕去自適應(yīng)
window.addEventListener('resize', function () { // vue2 myCharts.resize() // vue3 myCharts.value.resize() })
5、輪播動畫效果
需要配置tooltip參數(shù)使用,顯示tooltip提示框的輪播動畫
// vue2 myChart.currentIndex = -1; setInterval(function () { var dataLen = option.series[0].data.length; // 取消之前高亮的圖形 myChart.dispatchAction({ type: 'downplay', seriesIndex: 0, dataIndex: myChart.currentIndex }); myChart.currentIndex = (myChart.currentIndex + 1) % dataLen; // 高亮當前圖形 myChart.dispatchAction({ type: 'highlight', seriesIndex: 0, dataIndex: myChart.currentIndex }); // 顯示 tooltip myChart.dispatchAction({ type: 'showTip', seriesIndex: 0, dataIndex: myChart.currentIndex }); }, 2000);
6、dispatchAction 行為事件
具體用法參考 echarts 下的 action:https://echarts.apache.org/zh/api.html#action
(1)highlight:高亮指定的數(shù)據(jù)圖形
myCharts.currentIndex = -1 myCharts.dispatchAction({ type: 'highlight', seriesIndex: 0, dataIndex: myCharts.currentIndex });
(2)downplay:取消高亮指定的數(shù)據(jù)圖形
(3)select:選中指定的數(shù)據(jù)
(4)unselect:取消選中指定的數(shù)據(jù)
(5)toggleSelect:切換選中狀態(tài)
(6)tooltip - showTip:顯示提示框
(7)tooltip - hideTip:隱藏提示框
(8)dataZoom - dataZoom:數(shù)據(jù)區(qū)域縮放
(9)geo - geoSelect:選中指定的地圖區(qū)域
(10)geo - geoUnSelect:取消選中指定的地圖區(qū)域
(11)geo - geoToggleSelect:切換指定的地圖區(qū)域選中狀態(tài)
六、Echarts 常用的相關(guān)配置
1、tooltip 提示框
(1)tooltip的公共屬性配置
tooltip: { position:'right', padding: [5,8], textStyle:{ color: '#eee', fontSize: 13 }, backgroundColor: "rgba(13,5,30,.5)", extraCssText:'z-index:1', // 層級 axisPointer: {} }
(2)trigger 類型為 item
tooltip: { trigger: 'item', formatter: function(param) { let resultTooltip = "<div style='border:1px solid rgba(255,255,255,.2);padding:5px;border-radius:3px;'>" + "<div>" + param.name + "</div>" + "<div style='padding-top:5px; font-size:10px;color:#999;'>" + "<span style='display: inline-block; width: 10px; height:10px; border-radius: 50%;background-color: " + param.color + ";'></span>" + "<span style=''> " + param.seriesName + ":</span>" + "<span style='font-size:16px;color:" + param.color + "'>" + param.value + "</span></span>" + "</div>"; return resultTooltip } }
(3)trigger 類型為 axis
tooltip: { trigger: 'axis', formatter: function(param) { let resultTooltip = "<div style='border:1px solid rgba(255,255,255,.2);padding:5px;border-radius:3px;'>" + "<div>" + param[0].name + "</div>" + "<div style='padding-top:5px; font-size:10px;color:#999;'>" + "<span style='display: inline-block; width: 10px; height:10px; border-radius: 50%;background: " + param[0].color + ";'></span>" + "<span style=''> " + param[0].seriesName + ":</span>" + "<span style='font-size:16px;color:" + param[0].color + "'>" + param[0].value + "</span></span>" + "</div>"; return resultTooltip } }
2、axisPointer 坐標指示器
(1)type 類型為 shadow
axisPointer: { type: 'shadow', label: { show: true, backgroundColor: 'transparent' }, shadowStyle: { color: { type: 'linear', x: 0, y: 0, x2: 0, y2: 1, colorStops: [ { offset: 0, color: 'rgba(100, 101, 171, 0)' }, { offset: 0.5, color: 'rgba(100, 101, 171, 0.2)' }, { offset: 0.999999, color: 'rgba(100, 101, 171, 1)' }, { offset: 1, color: 'rgba(100, 101, 171, 1)' }, ], global: false } } }
(2)type 類型為 line
axisPointer: { type: 'line', label: { show: true, backgroundColor: 'transparent' }, lineStyle: { color: { type: 'linear', x: 0, y: 0, x2: 0, y2: 1, colorStops: [ { offset: 0, color: 'rgba(100, 101, 171, 0)' }, { offset: 0.5, color: 'rgba(100, 101, 171, 0.2)' }, { offset: 0.999999, color: 'rgba(100, 101, 171, 1)' }, { offset: 1, color: 'rgba(100, 101, 171, 1)' } ], global: false }, type: 'solid', width: 10 } }
3、漸變處理
(1)線性漸變區(qū)域 LinearGradient
// 前四個分參數(shù)分別代表右,下,左,上,數(shù)值0-1 color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [ { offset: 0, color: 'blue' }, { offset: 1, color: 'red' } ])
(2)徑向漸變區(qū)域 RadialGradient
// 前三個分參數(shù)分別代表圓心(x,y),半徑(數(shù)值0-1) color: new echarts.graphic.RadialGradient(0.5, 0.5, 0.8, [ { offset: 0, color: 'blue' }, { offset: 1, color: 'red' } ])
(3)線性漸變區(qū)域 colorStops-linear
// x,y,x2,y2數(shù)值同LinearGradient前四個參數(shù)分別代表右,下,左,上,數(shù)值0-1 color: { type: 'linear', x: 0, y: 0, x2: 0, y2: 1, colorStops: [ { offset: 0, color: 'blue' }, { offset: 1, color: 'red' } ], global: false // 缺省為 false }
(4)徑向漸變區(qū)域 colorStops-radial
// x 0.5 y 0.5 代表圓心,r 代表半徑 color: { type: 'radial', x: 0.5, y: 0.5, r: 0.9, colorStops: [ { offset: 0, color: 'blue' }, { offset: 1, color: 'red' } ], global: false // 缺省為 false }
(5)紋理填充 color-image
let url= "../../static/bg_icon.png" color: { image: url, // 支持為 HTMLImageElement, HTMLCanvasElement,不支持路徑字符串 repeat: 'repeat' // 是否平鋪,可以是 'repeat-x', 'repeat-y', 'no-repeat' }
4、label中文字通過rich自定義樣式設(shè)置
label: { show: true, formatter: `\n{d|vvxyksv9kd} {unit|${props.unit}}`, rich: { d: { fontSize: 26, fontWeight: 600, color: "#000" }, unit: { fontSize: 20, fontWeight: 600, color: "#3C3B3B" } }, color: "#000" }
總結(jié)
到此這篇關(guān)于封裝一個公用Echarts圖表組件的3種模板的文章就介紹到這了,更多相關(guān)封裝公用Echarts圖表組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript檢測瀏覽器cookie是否已經(jīng)啟動的方法
這篇文章主要介紹了JavaScript檢測瀏覽器cookie是否已經(jīng)啟動的方法,實例分析了javascript操作cookie的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-02-02js中常用的4種模糊查詢詳解(includes()、indexOf()、search()、match())
這篇文章主要給大家介紹了關(guān)于js中常用的4種模糊查詢(includes()、indexOf()、search()、match())的相關(guān)資料,搜索可以使我們更快的找到某一個關(guān)鍵詞或者某一個商品,所以模糊查詢和下拉匹配也成了前端必備的一個小技能,需要的朋友可以參考下2023-11-11js 優(yōu)化次數(shù)過多的循環(huán) 考慮到性能問題
IE沒有我們想象中笨,它知道總的循環(huán)次數(shù)還是一千萬次。因此,得把這一百個十萬次循環(huán)分開執(zhí)行。雖然Javascript是單線程的,但也可以通過setTimeout或setInterval模擬多線程。2011-03-03