鍍金池/ 問答/HTML/ vue里面添加echsrt插件,怎么把獲取的后臺數(shù)據(jù)填充到折線圖里面

vue里面添加echsrt插件,怎么把獲取的后臺數(shù)據(jù)填充到折線圖里面

clipboard.png

clipboard.png

clipboard.png
數(shù)據(jù)出不來

clipboard.png
下面的標(biāo)注是空的,數(shù)據(jù)沒有渲染出來

回答
編輯回答
尋仙
2018年8月26日 07:53
編輯回答
澐染

我是把echsrts封裝為一個(gè)組件,通過props傳到到組件內(nèi)部,然后直接調(diào)用這個(gè)props內(nèi)的值的,具體可以看下我這個(gè)組件是怎么寫的

<template>
  <div :id="echartsId" class="myChart"></div>
</template>

<script>
export default {
  props: ['echarts'],
  data () {
    return {
      data: {
        line: {
          animation: false,
          xAxis: {
            type: 'category',
            data: this.echarts.option.title
          },
          yAxis: {
            type: 'value'
          },
          series: [{
            data: this.echarts.option.data,
            type: 'line'
          }]
        },
        bar: {
          animation: false,
          color: ['#3398DB'],
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            }
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [
            {
              type: 'category',
              data: this.echarts.option.title,
              axisTick: {
                alignWithLabel: true
              }
            }
          ],
          yAxis: [
            {
              type: 'value'
            }
          ],
          series: [
            {
              type: 'bar',
              barWidth: '60%',
              data: this.echarts.option.data
            }
          ]
        },
        pie: {
          animation: false,
          tooltip: {
            trigger: 'item',
            formatter: ': {c} (xbjbbzt%)'
          },
          legend: {
            orient: 'vertical',
            x: 'left',
            data: this.echarts.option.title
          },
          series: [
            {
              type: 'pie',
              radius: ['50%', '70%'],
              avoidLabelOverlap: false,
              label: {
                normal: {
                  show: false,
                  position: 'center'
                },
                emphasis: {
                  show: true,
                  textStyle: {
                    fontSize: '30',
                    fontWeight: 'bold'
                  }
                }
              },
              labelLine: {
                normal: {
                  show: false
                }
              },
              data: this.echarts.option.data
            }
          ]
        },
        radar: {
          animation: false,
          radar: {
            name: {
              textStyle: {
                color: '#fff',
                backgroundColor: '#999',
                borderRadius: 3,
                padding: [3, 5]
              }
            },
            indicator: this.echarts.option.title
          },
          series: [{
            type: 'radar',
            data: this.echarts.option.data
          }]
        }
      }
    }
  },
  created () {
    this.echartsId = Math.random().toString(36).substr(2)
  },
  mounted () {
    this.$nextTick(function () {
      this.colours(this.echarts.type)
    })
  },
  methods: {
    colours (types) {
      let option
      if (types === 'line') {
        option = this.data.line
      }
      if (types === 'bar') {
        option = this.data.bar
      }
      if (types === 'pie') {
        option = this.data.pie
      }
      if (types === 'radar') {
        option = this.data.radar
      }
      this.$echarts.init(document.getElementById(this.echartsId)).setOption(option)
    }
  }
}
</script>

<style lang="scss" scoped>
.myChart{
  width: 100%;
  height: 100%;
}
</style>
2017年1月7日 01:15
編輯回答
糖豆豆

此時(shí)不能用this了,this已經(jīng)不再指向vue實(shí)例了,所以找不到vue實(shí)例下的xAxis,可以console.log(this) 看看

2018年2月9日 08:55