<template>
  <div>
    <div class="echart" style="width:100%;height:150px" ref="chart"></div>
  </div>
</template>

<script>
var echarts = require('echarts')
// 引入柱状图组件
require('echarts/lib/chart/bar')
require('echarts/lib/chart/pie')
require('echarts/lib/chart/line')
// 引入提示框和title组件
require('echarts/lib/component/tooltip')
require('echarts/lib/component/title')
require('echarts/lib/component/graphic')
require('echarts/lib/component/toolbox')
require('echarts/lib/component/legend')



export default {

    data() {
        return {
            option:{
                color: ['#3398DB'],
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                        type: 'line'        // 默认为直线，可选为：'line' | 'shadow'
                    }
                },
                grid: {
                    left: '0%',
                    // right: '4%',
                    bottom: '0%',
                    top:'12%',
                    containLabel: true
                },
                xAxis: [
                    {
                        type: 'category',
                        data: this.xdata,
                        show:false,
                        axisTick: {
                            alignWithLabel: true
                        }
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: this.echartTitle,
                        type: this.echartType,
                        barWidth: '40%',
                        data: this.ydata
                    }
                ]
}
        }
    },
   
    props:{
        xdata:{
            default(){
                return []
            }
        },
        ydata:{
            default(){
                return []
            }
        },
        echartType:{
            default(){
                return 'bar'
            }
        },
        echartTitle:{
            default(){
                return ''
            }
        },
    },
    methods: {
         //获取echart的值
        getEchartData() {
            console.log('echart')
            const chart = this.$refs.chart
            if (chart) {
                const myChart = echarts.init(document.getElementsByClassName('echart'))
                mychart.forEach(element => {
                    
                });
                myChart.setOption(this.option)
                window.addEventListener('resize', function() {
                myChart.resize()
                })
            }
        },
    },
    mounted() {
        this.$nextTick(()=>{
             this.getEchartData()
        })

       
    },
}
</script>

<style lang="scss" scoped>
</style>