<template>
  <div ref="dom" style="width:100% ;height:300px;">
  </div>

</template>

<script>
import * as echarts from "echarts";
import {loadCategoryData} from '@/api/api';


export default {
  name: "BarData",
  data() {
    return {
      a: [],
      b: [],
      sjyt: [],
    }
  },
  props: {
    resultXData: {},
    seriesName: {
      type: String,
    },
    titleText: {
      type: String,
    },
    dw: {
      type: String,
    },
  },
  mounted() {
    this.initDictConfig()
    /*setTimeout(() => {
      this.getPie();
    }, 1000)*/
  },
  created() {

  },
  watch: {
    resultXData: {
      handler(newVal) {
        this.$nextTick(function () {
          this.resultYData = newVal
          this.getPie()
        })
      },
      deep: true,
      immediate: true

    },
  },
  methods: {
    initDictConfig() {
      loadCategoryData({code: 'A04'}).then((res) => {
        if (res.success) {
          this.sjyt = res.result
        }
      })
    },
    getPie() {
      var _this = this;
      // 绘制图表
      /* this.$nextTick(() => {*/
      var option = {
        title: {
          text: this.titleText,
          x: 'left',              //标题位置
          // textStyle: { //标题内容的样式
          //   color: '#000',
          //   fontStyle: 'normal',
          //   fontWeight: 100,
          //   fontSize: 16 //主题文字字体大小，默认为18px
          // },
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: this.resultYData.name,
            axisTick: {
              alignWithLabel: true
            },
            axisLabel: {
              interval: 0,
              rotate: 20,
            }
          }
        ],
        legend: {
          top: '5%', // 控制 板块控制器的位置
          right: 'center',
          // icon: 'rect',//形状  类型包括 circle，rect,line，roundRect，triangle，diamond，pin，arrow，none
          // itemWidth: 10,  // 设置宽度
          // itemHeight: 4, // 设置高度
          itemGap: 40, // 设置两个legend之间的间距
        },
        yAxis: [
          {
            type: 'log',
            name: this.dw,
            splitLine: {
              lineStyle: {
                type: 'dashed',
              },
            },
            axisLabel: {
              // 轴文字
              fontSize: 12,
            },
          },
        ],
        series: [
          {
            name: this.seriesName,
            type: 'bar',
            barWidth: '50%',
            label: {
              show: true,
              position: 'top',
            },
            data: this.resultYData.value,
            itemStyle: {
              normal: {
                barBorderRadius: [10, 10, 0, 0],
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: 'rgba(1,186,188,1)',
                  },
                  {
                    offset: 1,
                    color: 'rgba(150,214,104,.9)',
                  },
                ]),
              }
            }
          },
        ]

      }

      // 使用刚指定的配置项和数据显示图表。
      var myChart = echarts.init(this.$refs.dom)
      myChart.on('click', (params) => {
        for (const param of this.sjyt) {
          if (param.title == params.name){

            _this.$emit('visible', {sjyt: param.value,sfsb:"3"});
            _this.$emit('visibleSz', {pzyt: param.value,sfsb:"3"});
          }

        }
      })
      myChart.setOption(option)

    }
  },

}
</script>

<style scoped>

</style>