一言不合直接上代码吧!

App.vue的代码:

<template>
  <div id="app">
    <h1>this is App page</h1>
    <Eharts :echartObj="echartObj"></Eharts>
  </div>
  
</template>

<script>
import Eharts from './components/echarts'

export default {
  name: 'App',
  data() {
    return {
      echartObj:{
        title: {
            text: 'ECharts 示例'
        },
        tooltip: {},
        xAxis: {
            data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
        },
        yAxis: {},
        series: [{
            name: '销量',
            type: 'bar',
            data: [5, 20, 36, 10, 10, 20]
        }]
      }
    }
  },
  components: {
    Eharts
  }
}
</script>

<style>

</style>

echarts.vue的代码:

<template>
    <div class="echarts">
        <div id="echart"></div>
    </div>
</template>

<script>
import Echarts from 'echarts'
export default {
    data() {
        return{
            myChart: {}
        }
    },
    props: {
        echartObj: {
            type: Object,
            default: {}
        }
    },
    created() {
        this.$nextTick(()=> {
            this.loadEchart()
        })
    },
    mounted(){
        let _this = this;
        window.onresize = function() {
            _this.myChart.resize()
        }
    },
    methods: {
        loadEchart() {
            this.myChart = Echarts.init(document.getElementById("echart"));
            this.myChart.setOption({
                title: {
                    text: this.echartObj.title.text
                },
                tooltip: {},
                xAxis: {
                    data: this.echartObj.xAxis.data
                },
                yAxis: {},
                series: this.echartObj.series
            })
        }
    }
}
</script>

<style>
#echart {
    width: 50%;
    height: 300px;
}
</style>

效果图:

Logo

前往低代码交流专区

更多推荐