vue中Echarts圖表寬度沒占滿的問題
vue Echarts圖表寬度沒占滿
顯示效果:

經(jīng)過測試,當(dāng)把寬度寫成固定px的時候,就能正確渲染。
解決方法
1、寬高寫成固定px,針對寬度不需要自適應(yīng)的大屏。
2、當(dāng)寬度需要自適應(yīng),父元素需要設(shè)置寬高,可以使用$nextTick 當(dāng)元素寬度發(fā)生改變,只需要等待DOM渲染完成在渲染圖表,這樣就不會出現(xiàn)問題了。
相似案例:
當(dāng)點(diǎn)擊按鈕使用v-if進(jìn)行圖表切換,同樣會使出現(xiàn)寬高不占滿情況。
mounted() {
this.$nextTick( () => {
this.drawBar();
})
}
vue Echarts圖表寬度自適應(yīng),親測有效
實(shí)現(xiàn)寬度自適應(yīng)語句
//實(shí)現(xiàn)自適應(yīng)部分
?window.onresize = () => {
? ? ?// 基于準(zhǔn)備好的dom,初始化echarts實(shí)例
? ? let myChart = this.$echarts.init(document.getElementById('newEcharts'));
? ? myChart.resize();
};代碼使用如下:
<template>
? <el-row>
? ? ?<el-col :xs="24" :sm="24" :md="24" :lg="24">
? ? ? ?<div id="newEcharts" style="width:100%;height:400px;padding-top:40px"></div>
? ? ?</el-col>
? </el-row>
</template>
<script>
? ?export default {
? ? data() {
? ? ? return {
? ? ? ? newVisible: false,
? ? ? ? newDialogFormVisible: false,
? ? ? ?
? ? ? };
? ? },
? ? mounted () {
? ? ? this.initEcharts()
? ? ? //實(shí)現(xiàn)自適應(yīng)部分
? ? ? window.onresize = () => {
? ? ? // 基于準(zhǔn)備好的dom,初始化echarts實(shí)例
? ? ? ? let myChart = this.$echarts.init(document.getElementById('newEcharts'));
? ? ? ? myChart.resize();
? ? ? ? };
? ? },
? ? methods: {
? ? ? // 創(chuàng)建方法
? ? ? initEcharts() {
? ? ? ? var echarts = require('echarts');
?
? ? ? ? // 基于準(zhǔn)備好的dom,初始化echarts實(shí)例
? ? ? ? const myChart = this.$echarts.init(document.getElementById('newEcharts'));
? ? ? ? // 繪制圖表
? ? ? ? ?const option = {
? ? ? ? ? ? title: {
? ? ? ? ? ? ? ? text: 'ECharts 入門示例'
? ? ? ? ? ? },
? ? ? ? ? ? tooltip: {},
? ? ? ? ? ? xAxis: {
? ? ? ? ? ? ? ? data: ['襯衫', '羊毛衫', '雪紡衫', '褲子', '高跟鞋', '襪子']
? ? ? ? ? ? },
? ? ? ? ? ? yAxis: {},
? ? ? ? ? ? series: [{
? ? ? ? ? ? ? ? name: '銷量',
? ? ? ? ? ? ? ? type: 'bar',
? ? ? ? ? ? ? ? data: [5, 20, 36, 10, 10, 20]
? ? ? ? ? ? }]
? ? ? ? };
? ? ? ? myChart.setOption(option)
? ? ? },
? ? },
? }
</script>效果如下:


以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue2.x中h函數(shù)(createElement)與vue3中的h函數(shù)詳解
h函數(shù)本質(zhì)就是createElement(),h函數(shù)其實(shí)是createVNode的語法糖,返回的就是一個Js普通對象,下面這篇文章主要給大家介紹了關(guān)于vue2.x中h函數(shù)(createElement)與vue3中h函數(shù)的相關(guān)資料,需要的朋友可以參考下2022-12-12
在Vue環(huán)境下利用worker運(yùn)行interval計時器的步驟
這篇文章主要介紹了在Vue環(huán)境下利用worker運(yùn)行interval計時器的步驟,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08
基于Vue3和Element Plus實(shí)現(xiàn)自動導(dǎo)入功能
在 Vue 3 項(xiàng)目中,結(jié)合 Element Plus 實(shí)現(xiàn)自動導(dǎo)入可以顯著減少代碼量,提升開發(fā)效率,Element Plus 提供了官方的自動導(dǎo)入插件 unplugin-vue-components 和 unplugin-auto-import,以下是如何配置和使用的詳細(xì)步驟,需要的朋友可以參考下2025-03-03
vue使用Print.js打印頁面樣式不出現(xiàn)的解決
這篇文章主要介紹了vue使用Print.js打印頁面樣式不出現(xiàn)的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
如何使用Vuex+Vue.js構(gòu)建單頁應(yīng)用
這篇文章主要教大家如何使用Vuex+Vue.js構(gòu)建單頁應(yīng)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10

