<template>
  <div class="inerank-container">
    <div :style="{height:'100%',width:'100%'}" ref="myLineRank"></div>
  </div>
</template>
<script>
export default {
  props:{
    data:{
      type:Array,
      default:[]
    }
  },
  watch:{
    data:{
      handler: function (val, oldVal) {
        if (val) {
          this.init()
        }
      }
    }
  },
  data() {
    return {};
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      let myChart = this.$echarts.init(this.$refs.myLineRank);
      var dataAxis = [];
      var data = [];
      let tempData=JSON.parse(JSON.stringify(this.data))
      tempData.reverse().forEach(x => {
        dataAxis.push(x.departmentName+' '+(x.sales/10000).toFixed(2)+'W')
        data.push(x.sales)
      });
      
      var yMax = 2000;
      var dataShadow = [];
      for (var i = 0; i < data.length; i++) {
        dataShadow.push(yMax);
      }
      let option = {
        title: {
          text: "",
          subtext: "",
          show: false
        },
        xAxis: {
          type: "value",
          axisTick: {
            show: false
          },
          axisLine: {
            show: false
          },
          z: 10,
          splitLine: {
            show:false
          }
        },
        yAxis: {
          type: "category",
          data: dataAxis,
          inside:true,
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            textStyle: {
              color: "#aaa",
              fontFamily: "pingfang"
            },
            inside:true,
            padding:[-30,0,0,-3]
          },
          splitLine: {
            lineStyle: {
              color: "#272938",
              type: "dotted"
            },
            show:false
          }
        },
        grid: {
          left: "5",
          right: "30",
          top: "10",
          bottom:'0'
        },
        series: [
          {
            // For shadow
            type: "bar",
            itemStyle: {
              normal: { color: "#11172f", barBorderRadius: [4, 4, 4, 4] }
            },
            barGap: "-100%",
            barCategoryGap: "40%",
            data: dataShadow,
            animation: false,
            barWidth: 4
          },
          {
            type: "bar",
            itemStyle: {
              normal: {
                color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: "#4e5afd" },
                  { offset: 1, color: "#44c4ed" }
                ]),
                barBorderRadius: [4, 4, 4, 4]
              },
              emphasis: {
                color: "#44c4ed"
              }
            },
            barWidth: 4,
            data: data,
            animationType: "scale",
            animationEasing: "elasticOut",
            animationDelay: function(idx) {
              return Math.random() * 200;
            }
          }
        ]
      };
      myChart.setOption(option);
    }
  }
};
</script>
<style>
.inerank-container {
  height: 100%;
  width: 100%;
}
</style>
