king
2022-09-29 b883ae5d7d46fc7a3503236f16a250c2264ea7c7
src/menu/components/chart/antv-bar/index.jsx
@@ -1,7 +1,7 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Popover, notification } from 'antd'
import { Popover } from 'antd'
import { PlusCircleOutlined, PlusSquareOutlined, ToolOutlined, DeleteOutlined, FontColorsOutlined } from '@ant-design/icons'
import { Chart } from '@antv/g2'
import DataSet from '@antv/data-set'
@@ -18,7 +18,6 @@
const SettingComponent = asyncIconComponent(() => import('@/menu/datasource'))
const ChartCompileForm = asyncIconComponent(() => import('./chartcompile'))
const LogComponent = asyncIconComponent(() => import('@/menu/components/share/logcomponent'))
const CopyComponent = asyncIconComponent(() => import('@/menu/components/share/copycomponent'))
const PasteComponent = asyncIconComponent(() => import('@/menu/components/share/pastecomponent'))
const NormalHeader = asyncComponent(() => import('@/menu/components/share/normalheader'))
@@ -91,7 +90,6 @@
        search: [],
        action: [],
        plot: _plot,
        btnlog: [],
      }
      if (card.config) {
@@ -114,10 +112,8 @@
          return col
        })
      }
      this.setState({
        card: _card
      })
      this.props.updateConfig(_card)
      this.updateComponent(_card, true)
    } else {
      this.setState({
        card: fromJS(card).toJS()
@@ -126,7 +122,7 @@
  }
  componentDidMount () {
    MKEmitter.addListener('submitStyle', this.getStyle)
    MKEmitter.addListener('plusSearch', this.plusSearch)
    MKEmitter.addListener('tabsChange', this.handleTabsChange)
    setTimeout(() => {
      this.viewrender()
@@ -144,14 +140,14 @@
    this.setState = () => {
      return
    }
    MKEmitter.removeListener('submitStyle', this.getStyle)
    MKEmitter.removeListener('plusSearch', this.plusSearch)
    MKEmitter.removeListener('tabsChange', this.handleTabsChange)
  }
  handleTabsChange = (parentId) => {
    const { card } = this.state
    if (parentId === card.parentId) {
    if (parentId === card.parentId || parentId === 'all') {
      let _element = document.getElementById(card.uuid + 'canvas')
      if (_element) {
        _element.innerHTML = ''
@@ -788,13 +784,17 @@
      }
    }
    const view2 = chart.createView({
      region: {
        start: { x: 0, y: 0 },
        end: { x: 1, y: 1 }
      },
      padding
    })
    let view2 = chart // 无独立柱状图时不做分面
    if (Bar_axis.length) {
      view2 = chart.createView({
        region: {
          start: { x: 0, y: 0 },
          end: { x: 1, y: 1 }
        },
        padding
      })
    }
    view2.data(dv.rows)
@@ -1210,26 +1210,67 @@
    }
  }
  updateComponent = (component) => {
    const card = fromJS(this.state.card).toJS()
    if (!is(fromJS(component.plot), fromJS(card.plot)) || !is(fromJS(component.style), fromJS(card.style)) || !is(fromJS(component.search), fromJS(card.search))) {
      let _element = document.getElementById(card.uuid + 'canvas')
      if (_element) {
        _element.innerHTML = ''
  updateComponent = (card, init) => {
    if (!init) {
      if (!is(fromJS({plot: card.plot, style: card.style, search: card.search}), fromJS({plot: this.state.card.plot, style: this.state.card.style, search: this.state.card.search}))) {
        let _element = document.getElementById(this.state.card.uuid + 'canvas')
        if (_element) {
          _element.innerHTML = ''
        }
        this.$timer && clearTimeout(this.$timer)
        this.$timer = setTimeout(() => {
          this.viewrender()
        }, 150)
      }
      this.$timer && clearTimeout(this.$timer)
      this.$timer = setTimeout(() => {
        this.viewrender()
      }, 150)
    }
    component.width = component.plot.width
    component.name = component.plot.name
    card.width = card.plot.width
    card.name = card.plot.name
    card.errors = []
    let columns = card.columns.map(c => c.field)
    if (card.setting.interType === 'system' && card.setting.execute !== 'false' && !card.setting.dataresource) {
      card.errors.push({ level: 0, detail: '未设置数据源!'})
    } else if (card.setting.interType === 'system' && card.setting.execute === 'false' && card.scripts.filter(script => script.status !== 'false').length === 0) {
      card.errors.push({ level: 0, detail: '数据源中无可用脚本!'})
    } else if (!card.setting.primaryKey) {
      card.errors.push({ level: 0, detail: '未设置主键!'})
    } else if (!columns.includes(card.setting.primaryKey)) {
      card.errors.push({ level: 0, detail: '主键已失效!'})
    } else if (!card.setting.supModule) {
      card.errors.push({ level: 0, detail: '未设置上级组件!'})
    }
    
    if (!card.plot.Xaxis) {
      card.errors.push({ level: 0, detail: '坐标轴尚未设置!'})
    } else if (card.plot.datatype === 'query') {
      if (!columns.includes(card.plot.Xaxis)) {
        card.errors.push({ level: 1, detail: 'X轴在字段集中不存在'})
      }
      if (card.plot.Yaxis) {
        card.plot.Yaxis.forEach(m => {
          if (!columns.includes(m)) {
            card.errors.push({ level: 1, detail: `Y轴中字段“${m}”已失效`})
          }
        })
      }
    } else if (card.plot.datatype === 'statistics') {
      if (!columns.includes(card.plot.Xaxis)) {
        card.errors.push({ level: 1, detail: 'X轴在字段集中不存在'})
      }
      if (!columns.includes(card.plot.InfoType)) {
        card.errors.push({ level: 1, detail: '图表中统计类型字段已失效'})
      }
      if (!columns.includes(card.plot.InfoValue)) {
        card.errors.push({ level: 1, detail: '图表中统计值字段已失效'})
      }
    }
    this.setState({
      card: component
      card: card
    })
    this.props.updateConfig(component)
    this.props.updateConfig(card)
  }
  addSearch = () => {
@@ -1249,6 +1290,16 @@
    // 注册事件-添加搜索
    MKEmitter.emit('addSearch', card.uuid, newcard)
  }
  plusSearch = (uuid, search, type) => {
    const { card } = this.state
    if (card.uuid !== uuid || type !== 'simple') return
    search.uuid = Utils.getuuid()
    search.focus = true
    MKEmitter.emit('addSearch', card.uuid, search)
  }
  addButton = () => {
@@ -1282,43 +1333,13 @@
  changeStyle = () => {
    const { card } = this.state
    MKEmitter.emit('changeStyle', [card.uuid], ['background', 'border', 'padding', 'margin', 'shadow'], card.style)
    MKEmitter.emit('changeStyle', ['background', 'border', 'padding', 'margin', 'shadow'], card.style, this.getStyle)
  }
  getStyle = (comIds, style) => {
    const { card } = this.state
    if (comIds[0] !== card.uuid || comIds.length > 1) return
    let _card = {...card, style}
  getStyle = (style) => {
    let _card = {...this.state.card, style}
    this.updateComponent(_card)
  }
  handleLog = (type, logs, item) => {
    let card = fromJS(this.state.card).toJS()
    if (type === 'revert') {
      card.action = card.action ? [...card.action, item] : [item]
      card.btnlog = logs
      this.setState({ card })
      this.props.updateConfig(card)
      notification.success({
        top: 92,
        message: '恢复成功!',
        duration: 2
      })
    } else {
      card.btnlog = logs
      this.setState({ card })
      this.props.updateConfig(card)
      notification.success({
        top: 92,
        message: '清除成功!',
        duration: 2
      })
    }
  }
  clickComponent = (e) => {
@@ -1340,9 +1361,8 @@
            {appType !== 'mob' ? <PlusSquareOutlined className="plus" title="添加按钮" onClick={this.addButton}/> : null}
            <ChartCompileForm config={card} dict={this.state.dict} plotchange={this.updateComponent}/>
            <CopyComponent type="line" card={card}/>
            <PasteComponent config={card} options={['action']} updateConfig={this.updateComponent} />
            <PasteComponent config={card} options={['action', 'search']} updateConfig={this.updateComponent} />
            <FontColorsOutlined className="style" title="调整样式" onClick={this.changeStyle}/>
            <LogComponent btnlog={card.btnlog || []} handlelog={this.handleLog} />
            <ClockComponent config={card} updateConfig={this.updateComponent}/>
            <UserComponent config={card}/>
            <DeleteOutlined className="close" title="delete" onClick={() => this.props.deletecomponent(card.uuid)} />
@@ -1354,10 +1374,23 @@
        <NormalHeader config={card} updateComponent={this.updateComponent}/>
        <div className="canvas" id={card.uuid + 'canvas'} ref={ref => this.wrap = ref}></div>
        {appType !== 'mob' ? <ActionComponent
          type="chart"
          config={card}
          updateaction={this.updateComponent}
        /> : null}
        <div className="component-name">
          <div className="center">
            <div className="title">{card.name}</div>
            <div className="content">
              {card.errors && card.errors.map((err, index) => {
                if (err.level === 0) {
                  return <span key={index} className="error">{err.detail}</span>
                } else {
                  return <span key={index} className="waring">{err.detail};</span>
                }
              })}
            </div>
          </div>
        </div>
      </div>
    )
  }