在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ vue 使用echarts問題

vue 使用echarts問題

vue中使用echarts渲染不出來

<template>
  <div id="app" class="login_area">
    <div id="myChart" class="echarts"></div>
  </div>
</template>

<script>
  export default {
    name: 'second',
    data () {
      return {
          dataList:[],
          time:[],
          datas:[]
      }
    },
created: function () {
  this.$axios.get("xxxx").then(res => {
    this.dataList=res.data.result.group;
    this.dataList.sort((a,b)=>{
      return b.index - a.index
    });
    for(var value of this.dataList){
        this.time.push(value.recharge_day)
        this.datas.push(Number(value.mount))
    }
  });
},
mounted(){
  this.drawLine();
},
methods: {
  drawLine(){
    let that=this;
    console.log(that.time)
    console.log(that.datas)
    let myChart = this.$echarts.init(document.getElementById('myChart'));
    myChart.setOption({
      color: ['#90bcf3'],
      tooltip: {
        trigger: 'axis'
      },
      legend: {
        data: ['收益/元'],
        left: 20,
        top: 20,
        textStyle: {
          color: ['#90bcf3']
        }
      },
      toolbox: {
        show: false,
        feature: {
          mark: {show: true},
          dataView: {show: true, readOnly: false},
          magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
          restore: {show: true},
          saveAsImage: {show: true}
        }
      },
      calculable: true,
      xAxis: [
        {
          type: 'category',
          name: '日期',
          boundaryGap: false,
          data: that.time,
          axisLine: {
            lineStyle: {
              color: ['#90bcf3']
            }
          },
          axisLabel: {
            textStyle: {
              color: ['#000']
            }
          }
        }
      ],
      yAxis: [
        {
          type: 'value',
          axisLine: {
            lineStyle: {
              color: ['#90bcf3']
            }
          },
          axisLabel: {
            textStyle: {
              color: ['#000']
            }
          }
        }
      ],
      series: [
        {
          name: '收益/元',
          type: 'line',
          smooth: true,//平滑
          stack: '總量',
          data: that.datas
        }
      ]
    });
  }
}
  }
</script>


<style scoped>
.echarts{
  width: 700px;
  height: 300px;
}
</style>

打印數(shù)據(jù):

clipboard.png

頁面:

clipboard.png
求大佬指點哪里出問題了呢?

回答
編輯回答
幼梔

let myChart = this.$echarts.init(document.getElementById('myChart'));

設置成全局看看

2017年9月17日 11:14
編輯回答
誮惜顏

你在console中點擊箭頭查看詳情的時候是點擊的時候求的值,這個值并不是log時求的值??茨愕慕貓D,如果打印的時候數(shù)據(jù)已經(jīng)返回的話,輸出的就不應該是[__ob__: Observer],而應該是類似于(6)["2018-05-02", "2018-05-03", ..., __ob__: Observer]

這樣的寫法本身是存在問題的,因為你在mounted的時候無法保證請求已經(jīng)回來了,所以比較簡單的方式是把created里面的內(nèi)容放到mounted里面,然后在請求回來的時候調(diào)用this.drawLine;
而且就目前你展示出來的需求來看,沒有必要把數(shù)據(jù)存在data里面,畢竟存在data里面還是耗性能的:

mounted () {
    this.$axios.get("xxxx").then(res => {
        let list = res.data.result.group;
        list.sort((a,b)=>{
            return b.index - a.index
        });
        let time = []
        let datas = []
        for(var value of list){
            time.push(value.recharge_day)
            datas.push(Number(value.mount))
        }
        this.drawLine(time, datas)
    });
},

methods: {
    drawLine (time, datas) { // 替換掉里面的that.time和that.datas
        // ...
    }
}
2018年1月3日 03:00
編輯回答
拮據(jù)

可能是mounted的時候沒數(shù)據(jù),console.log 改成 console.log(JSON.stringify(this.datas) 或者加斷點再看輸出。

2017年4月14日 23:34
編輯回答
執(zhí)念

感覺vue的生命周期是同步執(zhí)行的,寫在生命周期內(nèi)的異步操作都會在mounted后執(zhí)行,其實。。drawLine寫在請求回調(diào)內(nèi)就行。

2018年8月15日 16:55
編輯回答
愛是癌

生命周期其實只是vue創(chuàng)建實例時候按順序執(zhí)行的一個一個函數(shù),所以是在macrotask里的,是當前的執(zhí)行棧,而promise.then會在請求返回的時候加入到microtask里面,所以你在mounted里的時候是還沒有數(shù)據(jù)的,自然也就無法畫圖了。

另外說一下,axios放在created或者mounted區(qū)別不大,但是有可能數(shù)據(jù)返回的時候還沒有渲染完成,microtask是在渲染完成之前的,如果卸載created里的話建議把drawline放在this.$nextTick()里面執(zhí)行,避免獲取不到$refs,當然你直接document.getElementById了也就沒什么關系了

2017年4月15日 10:50
編輯回答
孤巷

貌似從vue.nextTick也可以入手

2018年2月28日 20:15