vue-cli項目中使用echart圖表

1.安裝依賴(引入核心庫)

??????npm install echarts

2.設(shè)定容器并指定大小

????????設(shè)置寬高,否則無法顯示


3.設(shè)置配置項

? ?—初始圖表:

data(){

?????? return{

????? ?????? options:{

????????????????????//具體配置項參見https://echarts.apache.org/zh/option.html#title

? ? ? ? ? ? ? ?},

????????}

}

mounted() {

???this.initChart();

?},

methods: {

??? // 實(shí)例化echart

???initChart() {

?????let chart =this.$echart.init(document.getElementById("chart"));

?????chart.setOption(this.options);

}

}

注意:this.$echart.這里用到的是全局引入

?????? //在main.js中全局引入echart

????????import?echart?from?'echarts'

????????Vue.prototype.$echart=echart




遇到的問題:

? ? 當(dāng)數(shù)據(jù)發(fā)生改變時,圖表無變化

解決方法:

? ? 使用vue中的watch監(jiān)聽options的變化,然后重新初始化

watch:?{

????options:?{

??????handler(newVal,?oldVal)?{

????????let?chart?=?this.$echart.init(document.getElementById("chart"));

????????chart.setOption(newVal,?true);//設(shè)置為true是因?yàn)椴恢貜?fù)渲染

??????},

??????deep:?true //深度監(jiān)聽,因?yàn)閛ptions是個復(fù)雜數(shù)組對象

????}

??}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容