vue.js+Echarts開發圖表放大縮小功能執行個體,vue.jsecharts

來源:互聯網
上載者:User

vue.js+Echarts開發圖表放大縮小功能執行個體,vue.jsecharts

最近使用echarts來開發某系統的圖表功能,先申明我以前用的ext.js,ext.js對圖表有自己的一套組件,用起來也很方便。但是由於ext.js過於臃腫,公司決定使用echarts來開發圖表功能。當我們使用的時候才悲催的發現,echart繪製之後,不能隨著容器div的大小而變化。而我們所開發的圖表是需要有放大縮小功能,於是在網上找了很久,也沒有找到合適的答案,大部分是通過監聽視窗大小改變事件來設定,然而並不是我們所需要的。於是自己用了一點點時間,瞭解了為何echarts不能重新渲染,原來是在容器div裡面設定了標記,每個div容器只能被渲染一次。知道原因之後,就容易了,就寫了一個簡單的demo。希望可以幫到有需要的同學。

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);      // 基於準備好的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);    })

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.