import Vue from 'vue'

class ChartSetOptions {
  constructor (dom) {
    if (!dom) {
      throw new Error('dom is required')
    }
    this.chart = Vue.prototype.$echarts.init(dom)
  }

  setOption (option) {
    this.chart.clear()
    this.chart.setOption(option)
  }

  // 接收二维数组
  drawBar (list) {
    console.log(this)
    const option = {
      tooltip: { trigger: 'item' },
      legend: {},
      xAxis: { type: 'category', data: [] },
      yAxis: { type: 'value' },
      series: list.map((data, index) => {
        return {
          data,
          type: 'bar'
        }
      })
    }
    this.setOption(option)
  }

  drawLine (list) {
    const option = {
      tooltip: { trigger: 'item' },
      legend: {},
      xAxis: { type: 'category', data: [] },
      yAxis: { type: 'value' },
      series: list.map((data, index) => {
        return {
          data,
          type: 'line'
        }
      })
    }
    this.setOption(option)
  }

  // 接收一组数据
  drawPie (list) {
    console.log(list)
    const option = {
      tooltip: { trigger: 'item' },
      legend: {},
      series: [
        {
          name: '',
          type: 'pie',
          radius: '50%',
          label: {
            show: true,
            formatter: '{b} {c}'
          },
          data: list,
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    }
    this.setOption(option)
  }
}

export default ChartSetOptions
