vue.js+Echarts開發(fā)圖表放大縮小功能實例
最近使用echarts來開發(fā)某系統(tǒng)的圖表功能,先申明我以前用的ext.js,ext.js對圖表有自己的一套組件,用起來也很方便。但是由于ext.js過于臃腫,公司決定使用echarts來開發(fā)圖表功能。當(dāng)我們使用的時候才悲催的發(fā)現(xiàn),echart繪制之后,不能隨著容器div的大小而變化。而我們所開發(fā)的圖表是需要有放大縮小功能,于是在網(wǎng)上找了很久,也沒有找到合適的答案,大部分是通過監(jiān)聽窗口大小改變事件來設(shè)置,然而并不是我們所需要的。于是自己用了一點點時間,了解了為何echarts不能重新渲染,原來是在容器div里面設(shè)置了標(biāo)記,每個div容器只能被渲染一次。知道原因之后,就容易了,就寫了一個簡單的demo。希望可以幫到有需要的同學(xué)。
html代碼:
<!doctype html>
<html>
<head>
<title>vue+chart</title>
<script src="echarts.min.js"></script>
<script src="vue.js"></script>
<style>
.button{
float:left;
width:150px;
height:60px;
color:#CC3333;
border:2px solid #CC3333;
background-color:#3399CC;
line-height:60px;
text-align:center;
font-size:36px;
}
.button:hover{
float:left;
width:150px;
height:60px;
color:#3399CC;
border:2px solid #3399CC;
background-color:#CC3333;
line-height:60px;
text-align:center;
font-size:36px;
}
.chart{
margin:0 auto;
}
#but{
width:310px;
margin:0 auto;
}
</style>
</head>
<body>
<div id="app">
<div id="panel">
<div class="chart" id="main" style="width:300px;height:300px"></div>
</div>
<div id="but">
<div id="add" class="button" @click="add">放大</div>
<div id="reduce" class="button" @click="reduce">縮小</div>
</div>
</div>
</body>
</html>
js代碼:
var vm=new Vue({
el:"#app",
data:{
size:300,
},
computed: {
style: function () {
return "width:"+this.width+"px;height:"+this.height+"px"
}
},
methods:{
add:function(){
if(this.size<900){
this.size=this.size+50;}
else{
this.size=900;
}
},
reduce:function(){
if(this.size>300){
this.size=this.size-50;}
else{
this.size=300;
}
}
}
})
var myChart = echarts.init(document.getElementById('main'));
var option = {
title: {
text: 'ECharts 入門'
},
tooltip: {},
legend: {
data:['銷量']
},
xAxis: {
data: ["襯衫","羊毛衫","雪紡衫","褲子","高跟鞋","襪子"]
},
yAxis: {},
series: [{
name: '銷量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
};
myChart.setOption(option);
// 基于準(zhǔn)備好的dom,初始化echarts實例
vm.$watch("size",function(newVal, oldVal){
var dom=document.getElementById('panel')
dom.innerHTML='<div class="chart" id="main" style="width:'+newVal+'px;height:'+newVal+'px"></div>';
var myChart = echarts.init(document.getElementById('main'));
myChart.setOption(option);
})

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談Vue render函數(shù)在ElementUi中的應(yīng)用
今天小編就為大家分享一篇淺談Vue render函數(shù)在ElementUi中的應(yīng)用,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09

