/* 水平柱状图 */
import Echart from '../echart'
import getOption from '../../libs/get_option'

class WordCloud extends Echart {
  constructor(layer, screen) {
    super(layer, screen)
    this.data = []
  }

  /**
   * 传入已经处理的表格数据，更新图表视图
   */
  setPaint() {
    let paint = this._initPaint()
    let option = this._getOption(paint)
    if (paint.series.shape) {
      let image = new Image()
      image.src = paint.series.shape
      image.onload = () => {
        option.series[0].maskImage = image
        this._render(option)
      }
    } else {
      this._render(option)
    }
  }

  _getOption(paint) {
    let option = getOption(paint, this.type)
    option.series[0].data = this.data
    return option
  }

  reduceData(paint) {
    if (!this.originalData || !this.originalData.length) {
      this.data = []
      return
    }
    if (paint.series.nameField && paint.series.valueField) {
      let t = []
      this.originalData.forEach(d => {
        t.push({
          name: d[paint.series.nameField],
          value: +d[paint.series.valueField]
        })
      })
      this.data = t
    }
  }
}

export default WordCloud
