line.vue 3.37 KB
<template>
  <div >
    <div :id="id" :style="styleInner"></div>
  </div>
</template>

<script>
// 引入 ECharts 主模块
var echarts = require('echarts/lib/echarts');
// 引入柱状图
require('echarts/lib/chart/line');
// 引入提示框和标题组件
require('echarts/lib/component/tooltip');
require('echarts/lib/component/title');

export default {
  name: 'lineChart',
  data () {
    return {
      newVal:undefined,
    }
  },

  props:{
    id: {
      type: String,
      default:'lineChart'
    },
    optionsInner: {
      type: Object,
      default:() => {
        return {}
      }
    },
    styleInner: {
      type: Object,
      default:() => {
        return {width: '200px', height: '100px'}
      }
    },
  },
  computed:{
    isOpend(){
      return this.$store.getters.sidebar.opened
    }
  },
  watch:{
    optionsInner:{
      handler(newVal,oldVal){
        console.log(newVal,'------------line-------------------newval')
        console.log(oldVal,'-------------------------------oldVal')
        this.drawLine(newVal);
        this.$set(this,'newVal',newVal)
      },
      deep:true,
    },
    isOpend(nn,old){
      console.log(this.newVal,'drawLine');
      // this.drawLine(this.newVal);
    }
  },
  mounted(){
    this.drawLine();
  },
  methods: {
    drawLine(data){
      console.log(data,'drawLine');
      // 基于准备好的dom,初始化echarts实例
      var myChart = echarts.init(document.getElementById(this.id),'light');
      // 绘制图表
      myChart.setOption(this.optionsFunc(data));
      window.addEventListener('resize',function(){
        myChart.resize()
      })
    },
    optionsFunc(data){
      let seriesData,xAxisData;
      if(data){
        var serData = data&&data.series? data.series[0].data:[]
        var serXaxis = data&&data.xAxis?data.xAxis.data:[]
        seriesData = serData
        var nullLength = serXaxis.length - seriesData.length;
        var nullArr = []
        for(var i = 0 ;i<nullLength;i++){
          nullArr.push('-')
        }
        xAxisData =  [...serXaxis].splice(0,seriesData.length).concat(nullArr)
      }else{
        seriesData = [0];
        xAxisData =  [0];
      }
      return {
        grid: {
          right: '20',
          top: '15',
        },
        tooltip: {
          trigger: 'item',
          formatter: "{c}",
          backgroundColor:'#5E77FF',
          borderColor: 'red',
          borderWidth:0,
          padding: [5,15],
        },
        xAxis: {
            show: true,
            type: 'category',
            data: xAxisData,
            axisLine:{
              lineStyle:{
                color: '#808285',
              }
            },
        },
        yAxis: {
            type: 'value',
            show: true,
            axisLine:{
              lineStyle:{
                color: '#808285',
              }
            },
        },
        series: [{
            data: seriesData,
            type: 'line',
            symbol:'circle',  //这句就是去掉点的
            symbolSize: 3,
            itemStyle: {
              borderColor: '#5E77FF',
              color: '#5E77FF',
            },
            smooth: true,
            lineStyle:{
              color:'#5E77FF',
              width:0,
            },
            areaStyle: {
              color:'#5E77FF',
            }
        }]
      };
    },
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>

</style>