<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: () => {}
    }
  },
  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: [
            {
              data: obj.y,
            },
          ]
        })
        return
      }
      // 基于dom，初始化echarts实例
      let myChart = this.$echarts.init(document.getElementById(this.id));
      this.myChart = myChart
      // 绘制图表
      myChart.setOption({
        title: { text: this.title },
        legend: {
          orient: "horizontal",
          left: "center",
          top: "0",
          itemWidth: 20,
          itemHeight: 10,
          icon: "roundRect",
          textStyle: {
            fontSize: "14px",
            color: "#000",
          },
        },
        tooltip: {},
        color:'#6e98ed',
        xAxis: {
          type: "category",
          data: [],
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "2%",
          containLabel: true,
        },
        yAxis: {},
        series: [
          {
            name: this.legendName,
            type: "line",
            data: [],
            smooth: true,
            // itemStyle: {
            //   lineStyle: {
            //     type: "dotted", //'dotted'虚线 'solid'实线
            //   },
            // },
          },
        ],
      });
    },
  },
};
</script>

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