king
2021-08-31 72419e2f826031a158173f46d723a672064e37cd
src/tabviews/custom/components/chart/antv-dashboard/index.jsx
@@ -7,12 +7,13 @@
import Api from '@/api'
import Utils from '@/utils/utils.js'
import asyncComponent from '@/utils/asyncComponent'
// import asyncComponent from '@/utils/asyncComponent'
import UtilsDM from '@/utils/utils-datamanage.js'
import MKEmitter from '@/utils/events.js'
import NormalHeader from '@/tabviews/custom/components/share/normalheader'
import './index.scss'
const NormalHeader = asyncComponent(() => import('@/tabviews/custom/components/share/normalheader'))
// const NormalHeader = asyncComponent(() => import('@/tabviews/custom/components/share/normalheader'))
registerShape('point', 'pointer', {
  draw(cfg, container) {
@@ -45,6 +46,12 @@
  },
})
registerShape('point', 'hidden', {
  draw(cfg, container) {
    return container.addGroup({})
  }
})
class DashboardChart extends Component {
  static propTpyes = {
    BID: PropTypes.any,              // 父级Id
@@ -62,28 +69,32 @@
    title: '',                 // 组件标题
    sync: false,               // 是否统一请求数据
    plot: null,                // 图表设置
    data: null,                // 数据
    data: {},                  // 数据
  }
  UNSAFE_componentWillMount () {
    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
    }
    _config.style.height = config.plot.height || 400
@@ -99,7 +110,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()
      }
    })
@@ -112,23 +123,30 @@
    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}, () => {
        this.handleData()
      })
    } else if (nextProps.mainSearch && !is(fromJS(this.props.mainSearch), fromJS(nextProps.mainSearch))) {
      if (config.setting.syncRefresh === 'true') {
        this.setState({}, () => {
          this.loadData()
        })
      }
    } else if (config.setting.syncRefresh && nextProps.mainSearch && !is(fromJS(this.props.mainSearch), fromJS(nextProps.mainSearch))) {
      this.setState({}, () => {
        this.loadData()
      })
    }
  }
@@ -159,16 +177,7 @@
    if (!config.timer) return
    const _change = {
      '15s': 15000,
      '30s': 30000,
      '1min': 60000,
      '5min': 300000,
      '10min': 600000,
      '15min': 900000,
      '30min': 1800000,
      '1hour': 3600000
    }
    const _change = { '15s': 15000, '30s': 30000, '1min': 60000, '5min': 300000, '10min': 600000, '15min': 900000, '30min': 1800000, '1hour': 3600000 }
    let timer = _change[config.timer]
@@ -237,7 +246,7 @@
    if (_element) {
      _element.innerHTML = ''
    }
    this.dashboardrender()
    this.viewrender()
  }
  async loadData (hastimer) {
@@ -253,21 +262,10 @@
      return
    }
    let searches = []
    if (mainSearch && mainSearch.length > 0) { // 主表搜索条件
      let keys = []
      mainSearch.forEach(item => {
        if (!keys.includes(item.key)) {
          searches.push(item)
        }
      })
    }
    let searches = config.setting.useMSearch && mainSearch ? mainSearch : []
    let requireFields = searches.filter(item => item.required && (!item.value || item.value.length === 0))
    let requireFields = searches.filter(item => item.required && item.value === '')
    if (requireFields.length > 0) {
      this.setState({
        loading: false
      })
      return
    }
@@ -282,9 +280,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 {
        data = {}
        if (result.data && result.data[0] && result.data[0].hasOwnProperty(config.plot.valueField)) {
          data.value = result.data[0][config.plot.valueField]
        }
      }
      this.setState({
@@ -306,6 +309,129 @@
    }
  }
  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: this.wrap.offsetHeight - 30,
    })
    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 仪表盘渲染
   */
@@ -313,17 +439,28 @@
    const { plot, chartId, data } = this.state
    let _data = fromJS(data).toJS()
    if (_data.value && _data.value > plot.maxValue) {
      _data.value = plot.maxValue
    if (_data.hasOwnProperty('value')) {
      if (_data.value === '' || _data.value === null) {
        delete _data.value
      } else {
        _data.value = +_data.value
        if (isNaN(_data.value)) {
          delete _data.value
        } else if (_data.value > plot.maxValue) {
          _data.value = plot.maxValue
        }
      }
    }
    const chart = new Chart({
      container: chartId,
      autoFit: true,
      height: plot.height ? (plot.height - 80) : 320,
      height: this.wrap.offsetHeight - 30,
      padding: [0, 0, 0, 0],
    })
    chart.data([_data]);
    chart.data([_data])
    chart.scale('value', {
      min: 0,
      max: plot.maxValue,
@@ -335,7 +472,7 @@
      radius: 0.75,
    })
    chart.axis('1', false);
    chart.axis('1', false)
    chart.axis('value', {
      line: null,
      label: {
@@ -465,7 +602,7 @@
          </div> : null
        }
        <NormalHeader config={config} />
        <div className="canvas-wrap">
        <div className="canvas-wrap" ref={ref => this.wrap = ref}>
          <div className={'canvas' + (empty ? ' empty' : '')} id={this.state.chartId}></div>
        </div>
      </div>