2020-05-07 折线图数据正确,渲染不正确问题

项目中把折线图抽成了公用组件。所以在用的时候单独赋值

子组件

<template>

  <div class="lineChart">

    <div style="width:100%;height:500px;" :id="echarts" class="echarts" ref="echarts"></div>

  </div>

</template>

<script>

// 引入echarts

import echarts from 'echarts'

export default {

  name: 'lineChart',

  props: {

    // 接收父组件传递过来的信息

    nameData: {

      default: () => []

    },

    timeData: {

      default: () => []

    },

    chartData: {

      default: () => []

    }

  },

  data () {

    return {

    }

  },

  computed: {

    echarts () {

      return 'echarts' + Math.random() * 100000

    }

  },

  mounted: function () {

    const vm = this

    vm.$nextTick(() => {

      vm.drawChart()

    })

  },

  watch: { // 父传子延迟问题 导致第一次点击没有刷新

    timeData (newVal, oldVal) {

      if (newVal) {

        this.drawChart()

      }

    }

  },

  created: () => { },

  methods: {

    drawChart () {

      // 基于准备好的dom,初始化echarts实例

      var myChart = echarts.init(document.getElementById(this.echarts))

      myChart.setOption({

        tooltip: { // 提示框取默认值,即鼠标移动到柱状图会显示内容

          trigger: 'axis',

          axisPointer: {

            type: 'cross',

            label: {

              backgroundColor: '#6a7985' // 移上去的时候显示的颜色

            }

          }

        },

        legend: {

          y: '95%',

          data: this.nameData, // 名字

          bottom: 'bottom'

        },

        // toolbox: { // 下载的箭头

        //   feature: {

        //     saveAsImage: {}

        //   }

        // },

        grid: { // 设置canvas构造内容的边距

          left: '3%',

          right: '4%',

          bottom: '10%',

          containLabel: true

        },

        xAxis: [

          {

            type: 'category',

            // boundaryGap: false,

            data: this.timeData // x数据

          }

        ],

        yAxis: [

          {

            type: 'value'

          }

        ],

        series: this.chartData // 折线图数据

      })

      // 自适应

      window.addEventListener('resize', function () {

        myChart.resize()

      })

    }

  }

}

</script>

<style lang="less" scoped>

.lineChart {

}

</style>


在父组件中引用的时候

后台返回数据是这样的


然后在data中

赋值的时候直接循环

但是出现了一个问题,后台返回的数据,没错,而且循环进去的数据也没错,渲染的时候数据展示 就是不正确


找了半天,原来是在最开始没有清空导致的,加上这样就好了


©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容