LineCharts.vue 3.52 KB
<template>
  <div>
    <div :id="domId" style="width: 100%;" :style="{ height }"></div>
  </div>
</template>
<script>
import * as echarts from 'echarts'
export default {
  name: 'BarLineChart',
  props: {
    height: {
      type: String
    },
    domId: {
      type: String,
      default: 'bar-line'
    },
    dataList: {
      type: Array,
      default: () => []
    },
    xAxisData: {
      type: Array,
      default: () => ['1', '2', '3', '4', '5', '6']
    },
    linetype: Number,
    lineChartsYear: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      barLineOptions: null,
    }
  },
  mounted() {
    this.barLineDom = document.getElementById(this.domId)
    this[this.domId] = echarts.init(this.barLineDom)
  },
  methods: {
    getLeft(list) {
      const maxNum = Math.max(...list)
      if(maxNum<=1){
        return '9%'
      }
      let left = 6
      switch(true){
        case maxNum<10:
          left=6
          break;
        case maxNum<100:
          left=9
          break;
        case maxNum<1000:
          left=12
          break;
        case maxNum<10000:
          left=15
          break;
        default:
          left=18
          break;
      }
      return `${left}%`
    },
    drawBarLine() {
      this.barLineOptions = {
        grid: {
          right: '5%',
          left: this.getLeft(this.dataList || [0])
        },
        xAxis: {
          type: 'category',
          // name: (this.linetype == 1 ? '周' : '天')+(this.lineChartsYear && '\n('+this.lineChartsYear + '年'+')'),
          // nameLocation: 'center',
          // nameGap: 20,
          // nameTextStyle: {
          //   fontSize: 10,
          //   lineHeight: 16
          // },
          boundaryGap: false,//坐标轴两边留白
          data: this.xAxisData,
          axisLabel: { //坐标轴刻度标签的相关设置。
            interval: this.linetype == 1 ? (this.dataList.length > 10 ? 1 : 0): 0,//设置为 1,表示『隔一个标签显示一个标签』
            //	margin:15,
            textStyle: {
              color: '#FFFFFF',
              fontSize: 12,
            }
          },
        },
        yAxis: {
          type: 'value',
          name: '公里',
          boundaryGap: true,//坐标轴两边留白
          // min: 0,
          // max: 1500,
          // interval: 300,
          axisLabel: {
            interval: 0,
            margin: 2,
            textStyle: {
              color: '#FFFFFF',
              fontSize: 12,
            },
          },
          splitLine: {
            lineStyle: {
              color: '#ffffff00;',
            },
          },
          grid: {
            left: '15%'
          },
        },
        series: [
          {
            name: 'usecar',
            type: 'line',
            symbol: "circle",
            symbolSize: 0,
            itemStyle: {
              normal: {
                color: '#419fff',
                lineStyle: {
                  color: "#1890FF",
                  width: 3
                },
                areaStyle: {
                  color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [{
                    offset: 0,
                    color: 'rgba(65,159,255,0)'
                  }, {
                    offset: 1,
                    color: 'rgba(65,159,255,0.19)'
                  }]),
                }
              }
            },
            data: this.dataList
          },
        ]
      };
      this[this.domId].setOption(this.barLineOptions)
    },
  }
}
</script>
<style scoped lang="scss"></style>