king
2021-06-09 73a5124745890650d0fc91234bdbaa66d9bcbc6a
src/tabviews/custom/components/chart/antv-dashboard/index.jsx
@@ -45,6 +45,12 @@
  },
})
registerShape('point', 'hidden', {
  draw(cfg, container) {
    return container.addGroup({})
  }
})
class DashboardChart extends Component {
  static propTpyes = {
    BID: PropTypes.any,              // 父级Id
@@ -69,21 +75,25 @@
    const { config, data, initdata, BID } = this.props
    let _config = fromJS(config).toJS()
    let _data = {}
    let _data = null
    let _sync = _config.setting.sync === 'true'
    if (_sync && data) {
      _data = data[_config.dataName] || {}
      if (_data && Array.isArray(_data)) {
        _data = _data[0] || {}
      }
      _data = data[config.dataName] || []
      _sync = false
    } else if (_sync && initdata) {
      _data = initdata || {}
      _data = initdata || []
      _sync = false
    }
    if (_config.subtype === 'ratioboard') {
      _data = _data || []
    } else {
      if (_data && Array.isArray(_data)) {
        _data = _data[0] || {}
      } else {
        _data = {}
      }
      _sync = false
    }
    let height = config.plot.height || 400
@@ -105,7 +115,7 @@
    }, () => {
      if (config.setting.sync !== 'true' && config.setting.onload === 'true') {
        this.loadData()
      } else if (config.setting.sync === 'true' && _data) {
      } else if (config.setting.sync === 'true') {
        this.handleData()
      }
    })
@@ -118,12 +128,21 @@
    const { sync, config } = this.state
    if (sync && !is(fromJS(this.props.data), fromJS(nextProps.data))) {
      let _data = {}
      if (nextProps.data && nextProps.data[config.dataName]) {
        _data = nextProps.data[config.dataName] || {}
      }
      if (_data.hasOwnProperty(config.plot.valueField)) {
        _data.value = _data[config.plot.valueField]
      let _data = null
      if (config.subtype === 'ratioboard') {
        _data = []
        if (nextProps.data && nextProps.data[config.dataName]) {
          _data = nextProps.data[config.dataName] || []
        }
      } else {
        _data = {}
        if (nextProps.data && nextProps.data[config.dataName]) {
          _data = nextProps.data[config.dataName] || {}
        }
        if (_data.hasOwnProperty(config.plot.valueField)) {
          _data.value = _data[config.plot.valueField]
        }
      }
      this.setState({sync: false, data: _data}, () => {
@@ -234,7 +253,7 @@
    if (_element) {
      _element.innerHTML = ''
    }
    this.dashboardrender()
    this.viewrender()
  }
  async loadData (hastimer) {
@@ -279,9 +298,14 @@
    let result = await Api.genericInterface(param)
    if (result.status) {
      let data = {}
      if (result.data && result.data[0] && result.data[0].hasOwnProperty(config.plot.valueField)) {
        data.value = result.data[0][config.plot.valueField]
      let data = null
      if (config.subtype === 'ratioboard') {
        data = result.data || []
      } else {
        let data = {}
        if (result.data && result.data[0] && result.data[0].hasOwnProperty(config.plot.valueField)) {
          data.value = result.data[0][config.plot.valueField]
        }
      }
      this.setState({
@@ -303,6 +327,130 @@
    }
  }
  viewrender = () => {
    const { config } = this.state
    if (config.subtype === 'ratioboard') {
      this.ratioboardrender()
    } else {
      this.dashboardrender()
    }
  }
  getratiodata = () => {
    const { data, plot } = this.state
    let colors = {}
    if (plot.colors && plot.colors.length > 0) {
      plot.colors.forEach(item => {
        colors[item.tick] = item.color
      })
    }
    return data.map(item => {
      let val = +item[plot.valueField]
      let type = item[plot.labelField] || ''
      if (isNaN(val)) {
        val = 0
      }
      return {
        type: type,
        value: val,
        $percent: val / plot.maxValue,
        $color: colors[type] || '#1890ff'
      }
    })
  }
  ratioboardrender = () => {
    const { plot, chartId } = this.state
    const data = this.getratiodata()
    const chart = new Chart({
      container: chartId,
      autoFit: true,
      height: plot.height,
    })
    chart.data(data)
    chart.coordinate('polar', {
      startAngle: -Math.PI / 2,
      endAngle: 3 * Math.PI / 2,
      radius: (plot.radius || 75) / 100
    })
    chart.scale('$percent', {
      min: 0,
      max: 1,
      tickInterval: 1,
    })
    chart.axis(false)
    chart.facet('rect', {
      fields: ['type'],
      showTitle: false,
      eachView: function eachView(view, facet) {
        const data = facet.data[0]
        view.point().position('').shape('hidden')
        view.annotation().arc({
          top: false,
          start: [0, 1],
          end: [0.9999, 1],
          style: {
            stroke: plot.backColor,
            lineWidth: 10
          }
        })
        let _tick = data.$percent
        if (_tick >= 1) {
          _tick = 0.9999
        }
        view.annotation().arc({
          start: [0, 1],
          end: [_tick, 1],
          style: {
            stroke: data.$color,
            lineWidth: 10,
          }
        })
        // 仪表盘信息
        let text = ''
        if (plot.percent === 'true') {
          text = +(data.$percent * 100).toFixed(2) + '%'
        } else {
          text = data.value
        }
        view.annotation().text({
          position: ['50%', '45%'],
          content: data.type,
          style: {
            fontSize: plot.fontSize * 0.8,
            fill: plot.labelColor,
            fontWeight: 300,
            textAlign: 'center'
          },
          offsetX: 0
        })
        view.annotation().text({
          position: ['50%', '55%'],
          content: text,
          style: {
            fontSize: plot.fontSize,
            fill: plot.labelColor,
            fontWeight: 500,
            textAlign: 'center'
          },
          offsetX: 0,
          offsetY: 10
        })
      }
    })
    chart.render()
  }
  /**
   * @description 仪表盘渲染
   */