<template>
  <div>
    <div :id="id" :style="styleObj"></div>
  </div>
</template>

<script>
export default {
  props: {
    title: {
      type: String,
      default: "",
    },
    id: {
      type: String,
      default: "",
    },
    legendName: {
      type: String,
      default: "",
    },
    styleObj: {
      type: Object,
      default: () => { },
    },
    parameter: {
      type: Object,
      default: () => { }
    },
    markLine: {
      type: Number,
      default: () => 0
    },
    option: {
      type: Object,
      default: () => { }
    }
  },
  data() {
    return {
      myChart: null
    };
  },
  watch: {
    title(val) {
      this.myChart && this.myChart.setOption({ title: { text: val } })
    },
    parameter: {
      handler: function (v) {
        this.drawLine(v)
      },
      deep: true
    }
  },
  mounted() {
    this.drawLine();
  },

  methods: {
    drawLine(obj) {
      if (obj && this.myChart) {
        // 更新
        this.myChart.setOption({
          xAxis: {
            data: obj.x
          },
          series: [
            {
              name: this.legendName,
              data: obj.y
            },
          ]
        })
        if (this.markLine) {
          this.myChart.setOption({
            series: [
              {
                markLine: {
                  data: [{
                    name: '目标线',
                    yAxis: this.markLine,
                    label: {
                      formatter: '{b}：' + this.legendName + '不高于20.0%,',
                      position: 'insideStartTop'
                    },
                    lineStyle: {
                      color: "red",
                    }
                  }],
                  label: {
                    color: "red",
                    distance: [20, 8]
                  }
                }
              },
            ]
          })
        }
        return
      }
      // 基于dom，初始化echarts实例
      let myChart = this.$echarts.init(document.getElementById(this.id));
      this.myChart = myChart
      // 绘制图表
      const option = {
        title: {
          text: this.title,
          left: "left",
          textStyle: {
            fontSize: 18,
          },
        },
        color: '#6e98ed',
        legend: {
          orient: "horizontal",
          right: "50px",
          top: "0",
          itemWidth: 20,
          itemHeight: 10,
          icon: "roundRect",
          textStyle: {
            fontSize: "14px",
            color: "#000",
          },
        },
        dataZoom: [
          {
            show: true,
            realtime: true,
            start: 0,
            end: 100
          },
          {
            type: 'inside',
            realtime: true,
            start: 0,
            end: 100
          }
        ],
        tooltip: {},
        xAxis: {
          data: [],
        },
        grid: {
          left: "5%",
          right: "4%",
          bottom: 40,
          containLabel: true,
        },
        // color:[ "#61a0a8",],
        yAxis: {},
        series: [
          {
            name: this.legendName,
            type: "bar",
            data: [],
            itemStyle: {
              lineStyle: {
                type: "dotted", //'dotted'虚线 'solid'实线
              },
            },
          },
        ],
      }
      myChart.setOption(Object.assign({}, option, this.option));
      if (this.markLine) {
        myChart.setOption({
          series: [
            {
              markLine: {
                data: [{
                  name: '目标线',
                  yAxis: this.markLine,
                  label: {
                    formatter: '{b}：旷工率不高于20.0%,',
                    position: 'insideStartTop'
                  },
                  lineStyle: {
                    color: "red",
                  }
                }],
                label: {
                  color: "red",
                  distance: [20, 8]
                }
              }
            },
          ]
        })
      }
      myChart.on('click',  (params)=>{
        this.$emit("click",params)
      })

    },
  },
};
</script>

<style lang="less" scoped></style>
