index.js 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import Base from '../base'
import { checkKeyInModel, init } from '../../common'

const TYPE = 'radar'

/**
23
 * Radar chart
24 25 26
 */
export default class Radar extends Base {
  /**
27 28 29 30
   * Initialization method called on separate export
   * @param {*} el Selector or DOM object
   * @param {*} data data source
   * @param {*} options Optional
31 32 33 34 35 36
   */
  static init (el, data, options) {
    return init(Radar, el, data, options)
  }

  /**
37
   * Convert user configuration to a configuration format that conforms to the format of echarts API
38 39 40
   */
  transform () {
    const {
41
      // data
42
      data = [],
43 44 45
      // Chart title
      title = 'Radar chart',
      // Attribute dictionary
46 47 48 49 50 51 52 53
      keyMap = {
        textKey: 'key',
        legendKey: 'typeName',
        dataKey: 'value'
      }
    } = this.settings

    if (data.length === 0) {
54
      throw new Error('Data source is empty!')
55 56
    }

57
    // Attribute name corresponding to text, attribute name corresponding to legend, attribute name corresponding to data value
58 59 60 61 62 63 64
    const { textKey, legendKey, dataKey } = keyMap
    checkKeyInModel(data[0], textKey, legendKey, dataKey)

    const legendData = []
    const seriesData = []
    const indicator = []

65
    // Set legend and initialize data series
66 67 68 69 70
    for (let i = 0; i < data.length; i++) {
      const legendItem = data[i][legendKey]
      const textItem = data[i][textKey]
      const dataItem = data[i][dataKey]

71
      // Legend
72 73 74 75
      if (!legendData.includes(legendItem)) {
        legendData.push(legendItem)
      }

76
      // series
77 78 79 80 81 82 83 84 85 86 87 88
      let targetSeries = seriesData.find(s => s.name === legendItem)
      if (!targetSeries) {
        targetSeries = {
          name: legendItem,
          value: [],
          _raw: []
        }
        seriesData.push(targetSeries)
      }
      targetSeries.value.push(dataItem)
      targetSeries._raw.push(data[i])

89
      // index
90
      const targetIndicator = indicator.find(i => i.name === textItem)
91 92 93 94 95 96 97 98 99
      if (!targetIndicator) {
        indicator.push({ name: textItem })
      }
    }

    return { title, seriesData, legendData, indicator }
  }

  /**
100
   * Drawing charts
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
   */
  apply () {
    const { title, seriesData, legendData = [], indicator } = this.options
    this.echart.setOption({
      title: {
        text: title
      },
      tooltip: {},
      legend: {
        data: legendData
      },
      radar: {
        name: {
          textStyle: {
            color: '#fff',
            backgroundColor: '#999',
            borderRadius: 3,
            padding: [3, 5]
          }
        },
        indicator
      },
      series: [{
        type: TYPE,
        data: seriesData
      }]
    }, true)
  }
}