From 783ab4e467c95e26f7f031151507bd7ad8333a63 Mon Sep 17 00:00:00 2001 From: king <18310653075@163.com> Date: 星期五, 08 九月 2023 17:34:47 +0800 Subject: [PATCH] 2023-09-08 --- src/tabviews/custom/components/chart/antv-bar-line/index.jsx | 443 ++++++++++++++++++++++++++++++++++++------------------ 1 files changed, 292 insertions(+), 151 deletions(-) diff --git a/src/tabviews/custom/components/chart/antv-bar-line/index.jsx b/src/tabviews/custom/components/chart/antv-bar-line/index.jsx index 2e6524a..46c4260 100644 --- a/src/tabviews/custom/components/chart/antv-bar-line/index.jsx +++ b/src/tabviews/custom/components/chart/antv-bar-line/index.jsx @@ -3,7 +3,7 @@ import { is, fromJS } from 'immutable' import { Chart } from '@antv/g2' import DataSet from '@antv/data-set' -import { Spin, Empty, notification } from 'antd' +import { Spin, Empty, notification, Modal } from 'antd' import { DownloadOutlined } from '@ant-design/icons' import moment from 'moment' @@ -17,15 +17,12 @@ import NormalHeader from '@/tabviews/custom/components/share/normalheader' import './index.scss' -// const NormalHeader = asyncComponent(() => import('@/tabviews/custom/components/share/normalheader')) const ExcelOutButton = asyncComponent(() => import('@/tabviews/zshare/actionList/exceloutbutton')) const ExcelInButton = asyncComponent(() => import('@/tabviews/zshare/actionList/excelInbutton')) class LineChart extends Component { static propTpyes = { - data: PropTypes.array, // 缁熶竴鏌ヨ鏁版嵁 config: PropTypes.object, // 缁勪欢閰嶇疆淇℃伅 - mainSearch: PropTypes.any, // 澶栧眰鎼滅储鏉′欢 } state = { @@ -35,21 +32,20 @@ loading: false, // 鏁版嵁鍔犺浇鐘舵�� chartId: Utils.getuuid(), // 鍥捐〃Id transfield: {}, // 瀛楁鍚嶇О缈昏瘧 - sync: false, // 鏄惁缁熶竴璇锋眰鏁版嵁 plot: null, // 鍥捐〃璁剧疆 - data: null, // 鏁版嵁 search: null, // 鎼滅储鏉′欢 vFields: [], // 鏁板�煎瓧娈� vstFields: null, // 缁熻鏁版嵁鍊煎瓧娈典俊鎭� chart: null } - UNSAFE_componentWillMount () { - const { config, data, initdata } = this.props - let _config = fromJS(config).toJS() - let _data = null - let _sync = config.setting.sync === 'true' + data = [] + loaded = false + UNSAFE_componentWillMount () { + const { config } = this.props + + let _config = fromJS(config).toJS() let BID = '' let BData = '' @@ -60,14 +56,6 @@ } if (BData) { BID = BData.$BID || '' - } - - if (config.setting.sync === 'true' && data) { - _data = data[config.dataName] || [] - _sync = false - } else if (config.setting.sync === 'true' && initdata) { - _data = initdata || [] - _sync = false } let vFields = [] @@ -108,7 +96,13 @@ }) } - _config.style.height = config.plot.height || 400 + _config.plot.height = Utils.getHeight(_config.plot.height) + _config.style.height = 'auto' + _config.style.minHeight = _config.plot.height + 30 + + if (_config.plot.title) { + _config.style.minHeight = _config.style.minHeight + 45 + } let transfield = {} _config.columns.forEach(col => { @@ -213,7 +207,7 @@ let xc = {label: { formatter: (val) => { - if (!val || /^\s*$/.test(val)) return val + if (!val || /^\s+$/.test(val)) return val let _val = `${val}` if (_val.length <= limit) return val return _val.substring(0, limit) + '...' @@ -327,48 +321,33 @@ }) } + if (_config.setting.sync === 'true') { + _config.setting.onload = 'false' + + if (window.GLOB.SyncData.has(_config.dataName)) { + this.data = window.GLOB.SyncData.get(_config.dataName) || [] + + if (_config.$cache) { + Api.writeCacheConfig(_config.uuid, fromJS(this.data).toJS(), BID) + } + + _config.setting.sync = 'false' + + this.loaded = true + + window.GLOB.SyncData.delete(_config.dataName) + } + } + this.setState({ config: _config, - data: _data, BID: BID || '', vFields: vFields, vstFields: vstFields, - arr_field: _config.columns.map(col => col.field).join(','), plot: _config.plot, - sync: _sync, - search: Utils.initMainSearch(config.search), + search: _config.$searches, transfield - }, () => { - if (config.setting.sync !== 'true' && config.setting.onload === 'true') { - setTimeout(() => { - this.loadData() - }, _config.setting.delay || 0) - } else if (config.setting.sync === 'true' && _data) { - this.handleData() - } }) - } - - /** - * @description 鍥捐〃鏁版嵁鏇存柊锛屽埛鏂板唴瀹� - */ - UNSAFE_componentWillReceiveProps (nextProps) { - 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] || [] - } - - this.setState({sync: false, data: _data}, () => { - this.handleData() - }) - } else if (config.setting.syncRefresh && nextProps.mainSearch && !is(fromJS(this.props.mainSearch), fromJS(nextProps.mainSearch))) { - this.setState({}, () => { - this.loadData() - }) - } } shouldComponentUpdate (nextProps, nextState) { @@ -383,12 +362,22 @@ MKEmitter.addListener('queryModuleParam', this.queryModuleParam) MKEmitter.addListener('refreshByButtonResult', this.refreshByButtonResult) + if (config.setting.useMSearch) { + MKEmitter.addListener('searchRefresh', this.searchRefresh) + } + + if (config.setting.sync === 'true') { + MKEmitter.addListener('transferSyncData', this.transferSyncData) + } + if (config.timer) { this.timer = new TimerTask() this.timer.init(config.uuid, config.timer, config.timerRepeats, () => { - this.loadData(true) + this.loadData('timer') }) } + + this.initExec() } /** @@ -399,11 +388,92 @@ return } MKEmitter.removeListener('reloadData', this.reloadData) + MKEmitter.removeListener('searchRefresh', this.searchRefresh) MKEmitter.removeListener('resetSelectLine', this.resetParentParam) + MKEmitter.removeListener('transferSyncData', this.transferSyncData) MKEmitter.removeListener('queryModuleParam', this.queryModuleParam) MKEmitter.removeListener('refreshByButtonResult', this.refreshByButtonResult) this.timer && this.timer.stop() + } + + initExec = () => { + const { config, BID } = this.state + + if (config.$cache) { + if (config.$time) { + if (!this.loaded) { + Api.getLCacheConfig(config.uuid, config.$time, BID).then(res => { + if (!res.valid && config.setting.onload === 'true') { + setTimeout(() => { + this.loadData('init') + }, config.setting.delay || 0) + } + + if (!res.data || this.loaded) return + + this.data = res.data + this.handleData() + }) + } else { + this.handleData() + } + } else { + if (!this.loaded) { + Api.getLCacheConfig(config.uuid, 0, BID).then(res => { + if (!res.data || this.loaded) return + + this.data = res.data + this.handleData() + }) + } + + if (config.setting.onload === 'true') { + setTimeout(() => { + this.loadData('init') + }, config.setting.delay || 0) + } else if (this.loaded) { + this.handleData() + } + } + } else if (config.setting.onload === 'true') { + setTimeout(() => { + this.loadData() + }, config.setting.delay || 0) + } else if (this.loaded) { + this.handleData() + } + } + + transferSyncData = (syncId) => { + const { config, BID } = this.state + + if (config.$syncId !== syncId) return + + let _data = window.GLOB.SyncData.get(config.dataName) || [] + + if (config.$cache) { + Api.writeCacheConfig(config.uuid, fromJS(_data).toJS(), BID) + } + + this.data = _data + this.handleData() + + this.loaded = true + + window.GLOB.SyncData.delete(config.dataName) + + MKEmitter.removeListener('transferSyncData', this.transferSyncData) + } + + searchRefresh = (searchId) => { + const { config } = this.state + + if (config.$searchId !== searchId) return + + this.setState({}, () => { + this.loadData() + }) } /** @@ -421,10 +491,6 @@ MKEmitter.emit('reloadData', config.setting.supModule, BID) } else { this.loadData() - } - - if (position === 'popclose') { // 鎵ц鍚姩寮圭獥鐨勬寜閽墍閫夋嫨鐨勫埛鏂伴」 - btn.$tabId && MKEmitter.emit('refreshPopButton', btn.$tabId) } } @@ -451,23 +517,22 @@ * @description 瀵煎嚭Excel鏃讹紝鑾峰彇椤甸潰鎼滅储鎺掑簭绛夊弬鏁� */ queryModuleParam = (menuId, callback) => { - const { mainSearch } = this.props - const { arr_field, config, search } = this.state + const { config, search } = this.state if (config.uuid !== menuId) return let searches = search ? fromJS(search).toJS() : [] - if (config.setting.useMSearch && mainSearch && mainSearch.length > 0) { // 涓昏〃鎼滅储鏉′欢 - let keys = searches.map(item => item.key.toLowerCase()) + if (config.setting.useMSearch) { // 涓昏〃鎼滅储鏉′欢 + let mainSearch = window.GLOB.SearchBox.get(config.$searchId) || [] + let keys = config.$s_keys || [] mainSearch.forEach(item => { - if (!keys.includes(item.key.toLowerCase())) { - searches.push(item) - } + if (keys.includes(item.key.toLowerCase())) return + + searches.push(item) }) } callback({ - arr_field: arr_field, orderBy: config.setting.order || '', search: searches, menuName: config.name @@ -477,68 +542,96 @@ /** * @description 鏁版嵁鍔犺浇 */ - async loadData (hastimer) { - const { mainSearch } = this.props - const { config, arr_field, BID, search } = this.state + async loadData (type) { + const { config, BID, search } = this.state if (config.setting.supModule && !BID) { // BID 涓嶅瓨鍦ㄦ椂锛屼笉鍋氭煡璇� - this.setState({ - data: [] - }, () => { + if (!is(fromJS(this.data), fromJS([]))) { + this.data = [] this.handleData() - }) + } + this.loaded = true return } let searches = search ? fromJS(search).toJS() : [] - if (config.setting.useMSearch && mainSearch && mainSearch.length > 0) { // 涓昏〃鎼滅储鏉′欢 - let keys = searches.map(item => item.key) + if (config.setting.useMSearch) { // 涓昏〃鎼滅储鏉′欢 + let mainSearch = window.GLOB.SearchBox.get(config.$searchId) || [] + let keys = config.$s_keys || [] mainSearch.forEach(item => { - if (!keys.includes(item.key)) { - searches.push(item) - } + if (keys.includes(item.key.toLowerCase())) return + + searches.push(item) }) } - let requireFields = searches.filter(item => item.required && item.value === '') - if (requireFields.length > 0) { + if (config.$s_req && searches.filter(item => item.required && item.value === '').length > 0) { return } - if (!hastimer) { + if (type !== 'timer') { this.setState({ loading: true }) } let _orderBy = config.setting.order || '' - let param = UtilsDM.getQueryDataParams(config.setting, arr_field, searches, _orderBy, '', '', BID) + let param = UtilsDM.getQueryDataParams(config.setting, searches, _orderBy, '', '', BID) let result = await Api.genericInterface(param) if (result.status) { - let reset = true - - if (hastimer && is(fromJS(result.data), fromJS(this.state.data))) { - reset = false + if (config.$cache && type === 'init') { + Api.writeCacheConfig(config.uuid, result.data || [], BID) } + this.loaded = true + this.setState({ - data: result.data, loading: false - }, () => { - if (!reset) return - this.handleData() }) + + if (!is(fromJS(this.data), fromJS(result.data || []))) { + this.data = result.data || [] + this.handleData() + } + + if (config.timer && config.clearField && result.data && result.data[0]) { + let vals = (config.clearValue || '').split(',') + if (vals.includes(result.data[0][config.clearField])) { + this.timer && this.timer.stop() + } + } + if (result.message) { + if (result.ErrCode === 'Y') { + Modal.success({ + title: result.message + }) + } else if (result.ErrCode === 'S') { + notification.success({ + top: 92, + message: result.message, + duration: 2 + }) + } + } } else { this.setState({ loading: false }) this.timer && this.timer.stop() - notification.error({ - top: 92, - message: result.message, - duration: 10 - }) + + if (!result.message) return + if (result.ErrCode === 'N') { + Modal.error({ + title: result.message, + }) + } else if (result.ErrCode !== '-2') { + notification.error({ + top: 92, + message: result.message, + duration: 10 + }) + } } } @@ -550,7 +643,9 @@ if (_element) { _element.innerHTML = '' } - this.viewrender() + setTimeout(() => { + this.viewrender() + }, 100) } /** @@ -560,15 +655,15 @@ * 3銆佹煴鐘跺浘鏁版嵁琛ラ綈 */ getdata = () => { - const { data, plot, vFields, config } = this.state + const { plot, vFields, config } = this.state - if (!data || data.length === 0) { + if (this.data.length === 0) { this.setState({empty: true}) return [] } let _data = [] - let _cdata = fromJS(data).toJS() + let _cdata = fromJS(this.data).toJS() if (plot.repeat === 'average') { let _mdata = new Map() @@ -702,7 +797,7 @@ * @description 缁熻鏁版嵁棰勫鐞� */ getStaticMsg = () => { - const { plot, vstFields, data } = this.state + const { plot, vstFields } = this.state let percent = false let decimal = vstFields ? vstFields.decimal : 0 @@ -711,13 +806,13 @@ percent = true } - if (!data || data.length === 0) { + if (this.data.length === 0) { this.setState({empty: true}) return [] } let _data = [] - let _cdata = fromJS(data).toJS() + let _cdata = fromJS(this.data).toJS() if (plot.repeat === 'average') { let _mdata = new Map() @@ -896,7 +991,7 @@ const chart = new Chart({ container: this.state.chartId, autoFit: true, - height: this.wrap.offsetHeight - 25 + height: plot.height }) chart.data(_data) @@ -1080,7 +1175,7 @@ const chart = new Chart({ container: this.state.chartId, autoFit: true, - height: this.wrap.offsetHeight - 25 + height: plot.height }) // 鍧愭爣杞存牸寮忓寲 @@ -1450,6 +1545,7 @@ let _valfield = 'value' let _typefield = 'key' let colorIndex = 0 + let barcolors = {} if (plot.datatype === 'statistics') { _valfield = plot.InfoValue @@ -1462,6 +1558,16 @@ if (plot.enabled === 'true') { this.customrender(data) return + } + + if (plot.barcolors && plot.barcolors.length > 0 && plot.Yaxis.length === 1) { + data.forEach((item, i) => { + if (plot.barcolors[i]) { + barcolors[item[plot.Xaxis]] = plot.barcolors[i].color + } + }) + } else { + barcolors = null } const ds = new DataSet() @@ -1490,7 +1596,7 @@ const chart = new Chart({ container: this.state.chartId, autoFit: true, - height: this.wrap.offsetHeight - 25 + height: plot.height }) chart.data(_data) @@ -1596,14 +1702,28 @@ if (plot.$colors) { let limit = chartColors.length - _chart.color(_typefield, (key) => { - if (plot.$colors.has(key)) { - return plot.$colors.get(key) - } else { - colorIndex++ - return chartColors[(colorIndex - 1) % limit] - } - }) + + if (barcolors) { + _chart.color(`${_typefield}*${plot.Xaxis}`, (key, label) => { + if (barcolors[label]) { + return barcolors[label] + } else if (plot.$colors.has(key)) { + return plot.$colors.get(key) + } else { + colorIndex++ + return chartColors[(colorIndex - 1) % limit] + } + }) + } else { + _chart.color(`${_typefield}`, (key) => { + if (plot.$colors.has(key)) { + return plot.$colors.get(key) + } else { + colorIndex++ + return chartColors[(colorIndex - 1) % limit] + } + }) + } } else { _chart.color(_typefield) } @@ -1658,14 +1778,27 @@ if (plot.$colors) { let limit = chartColors.length - _chart.color(_typefield, (key) => { - if (plot.$colors.has(key)) { - return plot.$colors.get(key) - } else { - colorIndex++ - return chartColors[(colorIndex - 1) % limit] - } - }) + if (barcolors) { + _chart.color(`${_typefield}*${plot.Xaxis}`, (key, label) => { + if (barcolors[label]) { + return barcolors[label] + } else if (plot.$colors.has(key)) { + return plot.$colors.get(key) + } else { + colorIndex++ + return chartColors[(colorIndex - 1) % limit] + } + }) + } else { + _chart.color(`${_typefield}`, (key) => { + if (plot.$colors.has(key)) { + return plot.$colors.get(key) + } else { + colorIndex++ + return chartColors[(colorIndex - 1) % limit] + } + }) + } } else { _chart.color(_typefield) } @@ -1710,13 +1843,11 @@ chart.on('element:click', (ev) => { let data = ev.data.data - MKEmitter.emit('resetSelectLine', config.uuid, (data ? data.$$uuid : ''), data) - if (plot.click === 'menus') { let menu = null if (plot.menus && plot.menus.length > 0) { - let s = data[plot.menuType] || '' + let s = data[plot.menuType] + '' plot.menus.forEach(m => { if (s !== m.sign) return menu = m @@ -1724,44 +1855,54 @@ } if (!menu || !menu.MenuID) return - menu.type = menu.tabType - let newtab = { - ...menu, + MenuID: menu.MenuID, + MenuName: menu.MenuName, + MenuNo: menu.MenuNo || '', + type: menu.tabType, param: {} } if (plot.joint === 'true') { newtab.param.$BID = data.$$uuid || '' + + Object.keys(data).forEach(key => { + if (/^\$/.test(key)) return + newtab.param[key] = data[key] + }) } - if (['linkage_navigation', 'linkage', 'menu_board'].includes(window.GLOB.navBar)) { - MKEmitter.emit('modifyTabs', newtab, 'replace') - } else { - MKEmitter.emit('modifyTabs', newtab, 'plus', true) + MKEmitter.emit('modifyTabs', newtab, true) + } else if (plot.click === 'menu') { + let menuId = plot.menu.slice(-1)[0] + let menu = null + + if (window.GLOB.mkThdMenus.has(menuId)) { + menu = {...window.GLOB.mkThdMenus.get(menuId)} + } else if (plot.MenuID) { + menu = { + MenuID: plot.MenuID, + MenuName: plot.MenuName, + type: plot.tabType + } } - } else if (plot.click === 'menu' && plot.MenuID) { - let menu = { - MenuID: plot.MenuID, - MenuName: plot.MenuName, - MenuNo: plot.MenuNo, - type: plot.tabType - } + + if (!menu) return - let newtab = { - ...menu, - param: {} - } + menu.param = {} if (plot.joint === 'true') { - newtab.param.$BID = data.$$uuid || '' + menu.param.$BID = data.$$uuid || '' + + Object.keys(data).forEach(key => { + if (/^\$/.test(key)) return + menu.param[key] = data[key] + }) } - if (['linkage_navigation', 'linkage', 'menu_board'].includes(window.GLOB.navBar)) { - MKEmitter.emit('modifyTabs', newtab, 'replace') - } else { - MKEmitter.emit('modifyTabs', newtab, 'plus', true) - } + MKEmitter.emit('modifyTabs', menu, true) + } else { + MKEmitter.emit('resetSelectLine', config.uuid, (data ? data.$$uuid : ''), data) } }) @@ -1839,7 +1980,7 @@ </div> : null } <NormalHeader config={config} BID={BID} refresh={this.refreshSearch} /> - <div className="canvas-wrap" ref={ref => this.wrap = ref}> + <div className="canvas-wrap"> {config.plot.download === 'enable' && this.state.chart && !empty ? <DownloadOutlined onClick={this.downloadImage} className="system-color download"/> : null} <div className={'chart-action' + (config.plot.download === 'enable' ? ' downable' : '')}> {config.action.map(item => { -- Gitblit v1.8.0