前言
Vue項目中開發(fā)數(shù)據(jù)大屏,使用echarts圖表根據(jù)不同尺寸的屏幕進行適配
BUG:當頁面進行縮放時圖表大小沒有變化
- 使用到的方法:
resize() 調用echarts中內置的resize函數(shù)進行自適應縮放,然后添加監(jiān)控,頁面銷毀時刪掉,避免不必要內存占用 - 我們先看一下官方文檔怎么說 resize 官網(wǎng)
響應的原理:需要手動調用 resize 方法獲取正確的高寬并且刷新畫布億點小知識:可以在opts 中顯示指定圖表高寬
- 實戰(zhàn) vue3.0
1.創(chuàng)建容器
<div ref="myChart" style="width:100%;height:100%"></div>
2.創(chuàng)建echarts
先創(chuàng)建一個 echartsLine.ts 文件
import { EChartsOption } from "echarts";
const exportFuns = (): EChartsOption => {
return {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
data: [150, 230, 224, 218, 135, 147, 260],
type: 'line'
}
]
}
};
export const EchartsLineConsumption = exportFuns();
在容器組件里面應用echartsLine.ts 文件
<script lang="ts" setup>
import { ref, onMounted, markRaw } from "vue";
import * as echarts from "echarts";
import { EchartsLineConsumption } from "@/config/echartsLine";
const myChart = ref<any>(); // 獲取元素實例
const line = ref<any>(null); // 獲取echarts
onMounted(() => {
init();
});
const init = ()=>{
line.value = markRaw(echarts.init(myChart.value));
line.value.setOption(EchartsLineConsumption);
}
</script>
3.進行響應式頁面變化.resize()
onMounted(() => {
// 只要窗口大小發(fā)生像素變化就會觸發(fā)
window.addEventListener("resize", () => {
line.value.resize(); // 窗口發(fā)生改變就更新echarts
});
init();
});
4.需要進行銷毀實例優(yōu)化性能
onUnmounted(() => {
// 卸載echarts實例
line.value.dispose();
window.removeEventListener("resize", line.value);
});
以上就完成了 Vue中 echarts響應式頁面變化
接下來總結了一下echarts的一些常用知識文章來源:http://www.zghlxwxcb.cn/news/detail-482036.html
- 在餅圖中間添加文字
1.富文本 比較麻煩
2.在series的label中
type: "pie",
radius: ["47%", "57%"], // 讓餅圖中間為空心狀
label: {
show: true,
position: "center",
formatter: "中間圓心內容",
fontSize: '18px',
},
- 讓圖表占滿容器
使用grid屬性
grid: {
// 讓圖表占滿容器
left: 0,
right: 0,
bottom: 0,
containLabel: true,
},
- 自定義移入樣式
marker屬性
tooltip: {
position: "top",
show: true,
formatter: (params) => {
//只有“直接訪問”使用大標簽,其他都使用小標簽
return `$標題<br/>${params.marker}`;//marker 圖標
},
// extraCssText:'width:60px;white-space:pre-wrap'
},
- legend顯示和位置
bottom和left調整位置
circle::讓前面顯示的為圓形
itemGap:顯示之間的間距
legend: {
orient: "horizontal",
bottom: '5px',
left: "left",
icon: "circle",
itemGap: 2,
},
以上就是echarts響應式頁面變化.resize()感謝大家的閱讀
如碰到其他的問題 可以私下我 一起探討學習
如果對你有所幫助還請 點贊
收藏謝謝~!
關注收藏博客 作者會持續(xù)更新…文章來源地址http://www.zghlxwxcb.cn/news/detail-482036.html
到了這里,關于Vue中 echarts響應式頁面變化resize()的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!