From 72419e2f826031a158173f46d723a672064e37cd Mon Sep 17 00:00:00 2001 From: king <18310653075@163.com> Date: 星期二, 31 八月 2021 22:42:51 +0800 Subject: [PATCH] 2021-08-31 --- src/tabviews/custom/components/chart/antv-dashboard/index.jsx | 284 +++++++++++++++++++++++++++++++++++++++++++------------- 1 files changed, 215 insertions(+), 69 deletions(-) diff --git a/src/tabviews/custom/components/chart/antv-dashboard/index.jsx b/src/tabviews/custom/components/chart/antv-dashboard/index.jsx index 46a23ab..af8a9bf 100644 --- a/src/tabviews/custom/components/chart/antv-dashboard/index.jsx +++ b/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,36 +69,35 @@ 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 || {} - if (_data && Array.isArray(_data)) { - _data = _data[0] || {} - } + _data = initdata || [] _sync = false } - - if (_config.style) { - _config.style = {..._config.style, minHeight: (config.plot.height || 400)} + if (_config.subtype === 'ratioboard') { + _data = _data || [] } else { - _config.style = {minHeight: (config.plot.height || 400)} + if (_data && Array.isArray(_data)) { + _data = _data[0] || {} + } else { + _data = {} + } } + + _config.style.height = config.plot.height || 400 this.setState({ config: _config, @@ -104,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() } }) @@ -117,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() + }) } } @@ -164,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] @@ -242,7 +246,7 @@ if (_element) { _element.innerHTML = '' } - this.dashboardrender() + this.viewrender() } async loadData (hastimer) { @@ -258,14 +262,11 @@ 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 === '') + if (requireFields.length > 0) { + return } if (!hastimer) { @@ -279,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({ @@ -303,30 +309,170 @@ } } - /** - * @description 楗煎浘娓叉煋 - */ - dashboardrender = () => { - const { plot, chartId, data } = this.state + 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 ? (plot.height - 80) : 320, + 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 浠〃鐩樻覆鏌� + */ + dashboardrender = () => { + const { plot, chartId, data } = this.state + + let _data = fromJS(data).toJS() + + 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: this.wrap.offsetHeight - 30, padding: [0, 0, 0, 0], }) - chart.data([data]); + chart.data([_data]) chart.scale('value', { min: 0, max: plot.maxValue, tickInterval: plot.tickInterval, - }); + }) chart.coordinate('polar', { startAngle: (-9 / 8) * Math.PI, endAngle: (1 / 8) * Math.PI, radius: 0.75, - }); + }) - chart.axis('1', false); + chart.axis('1', false) chart.axis('value', { line: null, label: { @@ -345,9 +491,9 @@ } }, grid: null, - }); - chart.legend(false); - chart.tooltip(false); + }) + chart.legend(false) + chart.tooltip(false) chart .point() .position('value*1') @@ -371,7 +517,7 @@ appear: { animation: 'fade-in' } - }); + }) // 缁樺埗浠〃鐩樿儗鏅� chart.annotation().arc({ @@ -394,7 +540,7 @@ lineWidth: 18, lineDash: null, }, - }); + }) } else { let start = 0 plot.colors.forEach(item => { @@ -438,7 +584,7 @@ textAlign: 'center', }, offsetY: 15, - }); + }) } chart.render() @@ -456,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> -- Gitblit v1.8.0