<script>
import * as echarts from 'echarts'
import { markRaw } from 'vue'

export default {
  data() {
    return {
      chartColumn: null,
      option: {
        grid:{
          height:'75%',
          top:'20px'
        },
        xAxis: {
          type: 'category',
          data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月','十二月']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: [120, 200, 150, 80, 70, 110, 130, 150, 80, 70, 110, 130],
            type: 'bar',
            stack: 'a',
            name: 'a',
            color: '#3b75d2',
            barWidth: '12'
          },
          {
            data: [30, '100', 30, 20, 40, '100', 90, 30, '100', 30, 20, 90],
            type: 'bar',
            stack: 'a',
            name: 'c',
            color: '#e19053'
          },
        ]
      }
    }
  },
  mounted() {
    const stackInfo = {}
    for (let i = 0; i < this.option.series[0].data.length; ++i) {
      for (let j = 0; j < this.option.series.length; ++j) {
        const stackName = this.option.series[j].stack
        if (!stackName) {
          continue
        }
        if (!stackInfo[stackName]) {
          stackInfo[stackName] = {
            stackStart: [],
            stackEnd: []
          }
        }
        const info = stackInfo[stackName]
        const data = this.option.series[j].data[i]
        if (data && data !== '-') {
          if (info.stackStart[i] == null) {
            info.stackStart[i] = j
          }
          info.stackEnd[i] = j
        }
      }
    }
    for (let i = 0; i < this.option.series.length; ++i) {
      const data = this.option.series[i].data
      const info = stackInfo[this.option.series[i].stack]
      for (let j = 0; j < this.option.series[i].data.length; ++j) {
        // const isStart = info.stackStart[j] === i;
        const isEnd = info.stackEnd[j] === i
        const topBorder = isEnd ? 40 : 0
        const bottomBorder = 0
        data[j] = {
          value: data[j],
          itemStyle: {
            borderRadius: [topBorder, topBorder, bottomBorder, bottomBorder]
          }
        }
      }
    }
    this.initChart()
  },
  methods: {
    initChart() {
      this.chartColumn = markRaw(echarts.init(this.$refs.chartColumn))
      this.chartColumn.setOption(this.option)
      this.chartColumn.resize()
    }
  }
}


</script>

<template>
  <div ref="chartColumn" className="echarts"></div>
</template>

<style scoped lang="scss">
.echarts {
  width: 100%;
  height: 100%;
}
</style>