import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import {connect} from 'react-redux'
|
import { is, fromJS } from 'immutable'
|
import { Icon, Popover } from 'antd'
|
import { Chart } from '@antv/g2'
|
import DataSet from '@antv/data-set'
|
|
import MKEmitter from '@/utils/events.js'
|
import asyncComponent from '@/utils/asyncComponent'
|
import asyncIconComponent from '@/utils/asyncIconComponent'
|
|
import Utils from '@/utils/utils.js'
|
import zhCN from '@/locales/zh-CN/model.js'
|
import enUS from '@/locales/en-US/model.js'
|
import './index.scss'
|
|
const SettingComponent = asyncIconComponent(() => import('@/menu/datasource'))
|
const ChartCompileForm = asyncIconComponent(() => import('./chartcompile'))
|
const NormalHeader = asyncComponent(() => import('@/menu/components/share/normalheader'))
|
|
class antvBarLineChart extends Component {
|
static propTpyes = {
|
card: PropTypes.object,
|
updateConfig: PropTypes.func,
|
deletecomponent: PropTypes.func,
|
}
|
|
state = {
|
dict: localStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
|
card: null,
|
eventListener: null
|
}
|
|
UNSAFE_componentWillMount () {
|
const { card } = this.props
|
|
if (card.isNew) {
|
let _plot = {
|
shape: card.subtype, // 图表类型
|
width: 12,
|
height: 400,
|
label: 'outer',
|
name: card.name
|
}
|
|
if (card.subtype === 'ring') {
|
_plot.innerRadius = 50
|
}
|
|
let _card = {
|
uuid: card.uuid,
|
type: card.type,
|
floor: card.floor,
|
tabId: card.tabId || '',
|
parentId: card.parentId || '',
|
format: 'array', // 组件属性 - 数据格式
|
pageable: false, // 组件属性 - 是否可分页
|
switchable: false, // 组件属性 - 数据是否可切换
|
dataName: card.dataName || '',
|
width: _plot.width,
|
name: _plot.name,
|
subtype: card.subtype,
|
setting: { interType: 'system' },
|
style: {
|
borderWidth: '1px', borderColor: 'rgb(217, 217, 217)',
|
marginLeft: '8px', marginRight: '8px', marginTop: '8px', marginBottom: '8px'
|
},
|
headerStyle: { fontSize: '16px', borderBottomWidth: '1px', borderBottomColor: '#e8e8e8' },
|
columns: [],
|
scripts: [],
|
search: [],
|
action: [],
|
plot: _plot,
|
btnlog: [],
|
}
|
this.props.updateConfig(_card)
|
this.setState({
|
card: _card
|
})
|
} else {
|
this.setState({
|
card: fromJS(card).toJS()
|
})
|
}
|
}
|
|
componentDidMount () {
|
this.pierender()
|
MKEmitter.addListener('tabsChange', this.handleTabsChange)
|
MKEmitter.addListener('submitStyle', this.getStyle)
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.state), fromJS(nextState)) || (!this.props.menu && nextProps.menu)
|
}
|
|
/**
|
* @description 组件销毁,清除state更新,清除快捷键设置
|
*/
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
MKEmitter.removeListener('tabsChange', this.handleTabsChange)
|
MKEmitter.removeListener('submitStyle', this.getStyle)
|
}
|
|
handleTabsChange = (parentId) => {
|
const { card } = this.state
|
|
if (parentId === card.parentId) {
|
let _element = document.getElementById(card.uuid)
|
if (_element) {
|
_element.innerHTML = ''
|
}
|
|
setTimeout(this.pierender, 100)
|
}
|
}
|
|
getdata = (X_axis, Y_axis) => {
|
let xdata = [
|
{ label: '2001', value: 41.8 },
|
{ label: '2002', value: 38 },
|
{ label: '2003', value: 33.7 },
|
{ label: '2004', value: 30.7 },
|
{ label: '2005', value: 25.8 },
|
{ label: '2006', value: 31.7 },
|
{ label: '2007', value: 33 },
|
{ label: '2008', value: 46 },
|
{ label: '2009', value: 38.3 },
|
{ label: '2010', value: 28 },
|
{ label: '2011', value: 42.5 },
|
{ label: '2012', value: 30.3 }
|
]
|
|
let data = xdata.map(item => {
|
return {
|
[X_axis]: item.label,
|
[Y_axis]: item.value,
|
}
|
})
|
|
return data
|
}
|
|
pierender = () => {
|
const { card } = this.state
|
let plot = {...card.plot, height: card.plot.height - 80}
|
let color = plot.color || 'rgba(0, 0, 0, 0.85)'
|
|
let X_axis = plot.Xaxis || 'x'
|
let Y_axis = plot.Yaxis || 'y'
|
|
let data = this.getdata(X_axis, Y_axis)
|
|
const ds = new DataSet()
|
const dv = ds.createView().source(data)
|
|
const chart = new Chart({
|
container: card.uuid,
|
autoFit: true,
|
height: plot.height || 400
|
})
|
|
if (plot.shape !== 'nightingale' && plot.show !== 'value') {
|
dv.transform({
|
type: 'percent',
|
field: Y_axis,
|
dimension: X_axis,
|
as: 'percent'
|
})
|
|
chart.scale('percent', {
|
formatter: (val) => {
|
val = val * 100 + '%'
|
return val
|
}
|
})
|
|
Y_axis = 'percent' // 显示百分比
|
}
|
chart.data(dv.rows)
|
|
if (plot.shape === 'nightingale') {
|
chart.coordinate('polar', {
|
innerRadius: plot.innerRadius ? (plot.innerRadius / 100) : 0,
|
radius: plot.radius ? (plot.radius / 100) : 0.85,
|
})
|
} else {
|
chart.coordinate('theta', {
|
innerRadius: plot.shape !== 'pie' && plot.innerRadius ? (plot.innerRadius / 100) : 0,
|
radius: plot.radius ? (plot.radius / 100) : 0.85,
|
})
|
}
|
|
if (!plot.legend || plot.legend === 'hidden') {
|
chart.legend(false)
|
} else if (plot.shape === 'nightingale') {
|
chart.legend(X_axis, {
|
position: plot.legend,
|
itemName: {
|
style: {
|
fill: color,
|
}
|
}
|
})
|
} else {
|
chart.legend({
|
position: plot.legend,
|
itemName: {
|
style: {
|
fill: color,
|
}
|
}
|
})
|
}
|
|
if (plot.tooltip !== 'true') {
|
chart.tooltip(false)
|
} else {
|
chart.tooltip({
|
showTitle: false,
|
showMarkers: false
|
})
|
}
|
|
if (plot.shape !== 'nightingale') {
|
let _chart = chart
|
.interval()
|
.adjust('stack')
|
.position(Y_axis)
|
.color(X_axis)
|
.tooltip(`${X_axis}*${Y_axis}`, (name, value) => {
|
if (plot.show !== 'value') {
|
value = (value * 100).toFixed(2) + '%'
|
}
|
return {
|
name: name,
|
value: value
|
}
|
})
|
if (plot.label !== 'false') {
|
if (plot.label === 'inner') {
|
_chart.label(Y_axis, {
|
offset: -30,
|
content: (data) => {
|
let _label = ''
|
let _val = ''
|
if (plot.show !== 'value') {
|
_val = `${(data[Y_axis] * 100).toFixed(2)}%`
|
} else {
|
_val = `${data[Y_axis]}`
|
}
|
if (plot.label === 'inner') {
|
_label = _val
|
} else {
|
_label = `${data[X_axis]}: ${_val}`
|
}
|
return _label
|
},
|
style: {
|
textAlign: 'center',
|
fontSize: 16,
|
shadowBlur: 2,
|
shadowColor: 'rgba(0, 0, 0, .45)',
|
fill: '#fff',
|
}
|
})
|
} else {
|
_chart.label(Y_axis, {
|
layout: { type: 'pie-spider' },
|
labelHeight: 20,
|
content: (data) => {
|
let _label = ''
|
let _val = ''
|
if (plot.show !== 'value') {
|
_val = `${(data[Y_axis] * 100).toFixed(2)}%`
|
} else {
|
_val = `${data[Y_axis]}`
|
}
|
if (plot.label === 'inner') {
|
_label = _val
|
} else {
|
_label = `${data[X_axis]}: ${_val}`
|
}
|
return _label
|
},
|
labelLine: {
|
style: {
|
lineWidth: 0.5,
|
},
|
},
|
style: {
|
fill: color
|
}
|
})
|
}
|
}
|
chart.interaction('element-active')
|
} else {
|
chart.axis(false)
|
chart.interaction('element-highlight')
|
let _chart = chart
|
.interval()
|
.position(`${X_axis}*${Y_axis}`)
|
.color(X_axis)
|
|
if (plot.label !== 'false') {
|
let _label = {}
|
if (plot.label === 'inner') {
|
_label.offset = -15
|
} else {
|
_label.style = {
|
fill: color
|
}
|
}
|
|
_chart.label(X_axis, _label)
|
.style({
|
lineWidth: 1,
|
stroke: '#fff',
|
})
|
}
|
}
|
|
chart.render()
|
}
|
|
updateComponent = (component) => {
|
const card = fromJS(this.state.card).toJS()
|
let refresh = false
|
if (!is(fromJS(component.plot), fromJS(card.plot))) {
|
let _element = document.getElementById(card.uuid)
|
if (_element) {
|
_element.innerHTML = ''
|
}
|
refresh = true
|
}
|
|
component.width = component.plot.width
|
component.name = component.plot.name
|
|
this.setState({
|
card: component
|
}, () => {
|
if (refresh) {
|
setTimeout(() => {
|
this.pierender()
|
}, 100)
|
}
|
})
|
this.props.updateConfig(component)
|
}
|
|
addSearch = () => {
|
const { card } = this.state
|
|
let newcard = {}
|
newcard.uuid = Utils.getuuid()
|
newcard.focus = true
|
|
newcard.label = 'label'
|
newcard.initval = ''
|
newcard.type = 'select'
|
newcard.resourceType = '0'
|
newcard.options = []
|
newcard.setAll = 'false'
|
newcard.orderType = 'asc'
|
newcard.display = 'dropdown'
|
newcard.match = '='
|
|
// 注册事件-添加搜索
|
MKEmitter.emit('addSearch', card.uuid, newcard)
|
}
|
|
changeStyle = () => {
|
const { card } = this.state
|
|
MKEmitter.emit('changeStyle', [card.uuid], ['background', 'border', 'padding', 'margin'], card.style)
|
}
|
|
getStyle = (comIds, style) => {
|
const { card } = this.state
|
|
if (comIds[0] !== card.uuid || comIds.length > 1) return
|
|
let _card = {...card, style}
|
|
this.setState({
|
card: _card
|
})
|
|
this.props.updateConfig(_card)
|
}
|
|
render() {
|
const { card } = this.state
|
|
return (
|
<div className="menu-pie-chart-edit-box" style={{...card.style, height: card.plot.height || 400}}>
|
<NormalHeader config={card} updateComponent={this.updateComponent}/>
|
<Popover overlayClassName="mk-popover-control-wrap" mouseLeaveDelay={0.2} mouseEnterDelay={0.2} content={
|
<div className="mk-popover-control">
|
<Icon className="plus" title="添加搜索" onClick={this.addSearch} type="plus-circle" />
|
<ChartCompileForm config={card} dict={this.state.dict} plotchange={this.updateComponent}/>
|
<Icon className="style" title="调整样式" onClick={this.changeStyle} type="font-colors" />
|
<Icon className="close" title="delete" type="delete" onClick={() => this.props.deletecomponent(card.uuid)} />
|
<SettingComponent config={card} updateConfig={this.updateComponent}/>
|
</div>
|
} trigger="hover">
|
<Icon type="tool" />
|
</Popover>
|
<div className="canvas" id={card.uuid}></div>
|
</div>
|
)
|
}
|
}
|
|
const mapStateToProps = (state) => {
|
return {
|
menu: state.customMenu
|
}
|
}
|
|
const mapDispatchToProps = () => {
|
return {}
|
}
|
|
export default connect(mapStateToProps, mapDispatchToProps)(antvBarLineChart)
|