import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { Icon, Popover } from 'antd'
|
import { Chart } from '@antv/g2'
|
import DataSet, { DataView } from '@antv/data-set'
|
|
import MKEmitter from '@/utils/events.js'
|
import asyncComponent from '@/utils/asyncComponent'
|
import asyncIconComponent from '@/utils/asyncIconComponent'
|
import { resetStyle } from '@/utils/utils-custom.js'
|
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 CopyComponent = asyncIconComponent(() => import('@/menu/components/share/copycomponent'))
|
const NormalHeader = asyncComponent(() => import('@/menu/components/share/normalheader'))
|
const UserComponent = asyncIconComponent(() => import('@/menu/components/share/usercomponent'))
|
const ClockComponent = asyncIconComponent(() => import('@/menu/components/share/clockcomponent'))
|
|
class antvBarLineChart extends Component {
|
static propTpyes = {
|
card: PropTypes.object,
|
updateConfig: PropTypes.func,
|
deletecomponent: PropTypes.func,
|
}
|
|
state = {
|
dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
|
card: null,
|
ismob: sessionStorage.getItem('appType') === 'mob',
|
eventListener: null
|
}
|
|
UNSAFE_componentWillMount () {
|
const { card, ismob } = this.props
|
|
if (card.isNew) {
|
let _plot = {
|
shape: card.subtype, // 图表类型
|
width: card.width || 12,
|
height: 400,
|
label: 'outer',
|
name: card.name
|
}
|
|
if (ismob) {
|
_plot.width = 24
|
}
|
|
if (card.subtype === 'ring') {
|
_plot.innerRadius = 50
|
} else if (card.subtype === 'nest') {
|
_plot.innerRadius = 30
|
_plot.radius = 80
|
}
|
|
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: [],
|
}
|
|
if (card.config) {
|
let config = fromJS(card.config).toJS()
|
|
_card.plot = config.plot
|
_card.plot.name = card.name
|
_card.style = config.style
|
_card.headerStyle = config.headerStyle
|
|
_card.search = config.search.map(col => {
|
col.uuid = Utils.getuuid()
|
return col
|
})
|
}
|
|
this.props.updateConfig(_card)
|
this.setState({
|
card: _card
|
})
|
} else {
|
this.setState({
|
card: fromJS(card).toJS()
|
})
|
}
|
}
|
|
componentDidMount () {
|
MKEmitter.addListener('tabsChange', this.handleTabsChange)
|
MKEmitter.addListener('submitStyle', this.getStyle)
|
|
setTimeout(() => {
|
this.viewrender()
|
}, 1000)
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
/**
|
* @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 + 'canvas')
|
if (_element) {
|
_element.innerHTML = ''
|
}
|
|
this.$timer && clearTimeout(this.$timer)
|
this.$timer = setTimeout(this.viewrender, 100)
|
}
|
}
|
|
viewrender = () => {
|
const { card } = this.state
|
|
if (card.plot.shape === 'nest') {
|
this.nestrender()
|
} else {
|
this.pierender()
|
}
|
}
|
|
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 },
|
]
|
|
let data = xdata.map(item => {
|
return {
|
[X_axis]: item.label,
|
[Y_axis]: item.value,
|
}
|
})
|
|
return data
|
}
|
|
getnestdata = (X_axis, Y_axis, type) => {
|
const xdata = [
|
{ name: '狮子', type: '火象', value: 11 },
|
{ name: '白羊', type: '火象', value: 10 },
|
{ name: '水瓶', type: '风向', value: 14 },
|
{ name: '射手', type: '火象', value: 10 },
|
{ name: '双子', type: '风向', value: 7 },
|
{ name: '天平', type: '风向', value: 7 },
|
{ name: '摩羯', type: '土象', value: 14 },
|
{ name: '金牛', type: '土象', value: 3 },
|
{ name: '处女', type: '土象', value: 3 },
|
{ name: '天蝎', type: '水象', value: 11 },
|
{ name: '巨蟹', type: '水象', value: 5 },
|
{ name: '双鱼', type: '水象', value: 5 },
|
]
|
|
let map = new Map()
|
let sort = 1
|
let data = xdata.map(item => {
|
let _sort = sort
|
if (map.has(item.type)) {
|
_sort = map.get(item.type)
|
} else {
|
map.set(item.type, _sort)
|
sort++
|
}
|
return {
|
[X_axis]: item.name,
|
[Y_axis]: item.value,
|
[type]: item.type,
|
$sort: _sort
|
}
|
})
|
|
return data
|
}
|
|
nestrender = () => {
|
const { card } = this.state
|
const plot = card.plot
|
|
let color = plot.color || 'rgba(0, 0, 0, 0.85)'
|
let X_axis = plot.Xaxis || 'x'
|
let Y_axis = plot.Yaxis || 'y'
|
let type = plot.type || 'type'
|
|
const _data = this.getnestdata(X_axis, Y_axis, type)
|
const dvx = new DataView().source(_data)
|
|
dvx.transform({
|
type: 'sort-by',
|
fields: ['$sort']
|
})
|
|
let data = dvx.rows
|
|
// 通过 DataSet 计算百分比
|
const dv = new DataView()
|
dv.source(data).transform({
|
type: 'percent',
|
field: Y_axis,
|
dimension: type,
|
as: '$percent'
|
})
|
|
const dv1 = new DataView()
|
dv1.source(data).transform({
|
type: 'percent',
|
field: Y_axis,
|
dimension: X_axis,
|
as: '$percent',
|
})
|
|
const chart = new Chart({
|
container: card.uuid + 'canvas',
|
autoFit: true,
|
height: this.wrap.offsetHeight - 30,
|
padding: 0,
|
})
|
|
chart.data(dv.rows)
|
|
if (plot.show !== 'value') {
|
chart.scale('percent', {
|
formatter: (val) => {
|
val = val * 100 + '%'
|
return val
|
}
|
})
|
|
Y_axis = '$percent'
|
}
|
let radius = plot.radius / 100
|
let innerRadius = plot.innerRadius / 100
|
|
chart.coordinate('theta', {
|
innerRadius: innerRadius,
|
radius: innerRadius + (radius - innerRadius) / 2,
|
})
|
|
if (plot.tooltip !== 'true') {
|
chart.tooltip(false)
|
} else {
|
chart.tooltip({
|
showTitle: false,
|
showMarkers: false
|
})
|
}
|
|
chart.legend(false)
|
let chart1 = chart
|
.interval()
|
.adjust('stack')
|
.position(Y_axis)
|
.color(type)
|
.tooltip(`${type}*${Y_axis}`, (type, percent) => {
|
if (plot.show !== 'value') {
|
percent = (percent * 100).toFixed(2) + '%'
|
}
|
return {
|
name: type,
|
value: percent,
|
}
|
})
|
|
if (plot.splitLine) {
|
chart1.style({
|
lineWidth: plot.splitLine,
|
stroke: plot.splitColor,
|
})
|
}
|
if (plot.label !== 'false') {
|
chart1.label(type, {
|
offset: -10,
|
})
|
}
|
|
const outterView = chart.createView()
|
|
outterView.data(dv1.rows)
|
|
if (plot.show !== 'value') {
|
outterView.scale('percent', {
|
formatter: (val) => {
|
val = val * 100 + '%'
|
return val
|
}
|
})
|
}
|
outterView.coordinate('theta', {
|
innerRadius: (innerRadius + (radius - innerRadius) / 2) / radius,
|
radius: radius
|
})
|
let chart2 = outterView
|
.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.splitLine) {
|
chart2.style({
|
lineWidth: plot.splitLine,
|
stroke: plot.splitColor,
|
})
|
}
|
|
if (plot.label !== 'false') {
|
if (plot.label === 'inner') {
|
chart2.label(Y_axis, {
|
offset: -30,
|
content: (data) => {
|
let _val = ''
|
if (plot.show !== 'value') {
|
_val = `${(data[Y_axis] * 100).toFixed(2)}%`
|
} else {
|
_val = `${data[Y_axis]}`
|
}
|
return _val
|
},
|
style: {
|
textAlign: 'center',
|
fontSize: 16,
|
shadowBlur: 2,
|
shadowColor: 'rgba(0, 0, 0, .45)',
|
fill: '#fff',
|
}
|
})
|
} else {
|
chart2.label(Y_axis, {
|
layout: { type: plot.label === 'outer' ? 'pie-spider' : 'fixed-overlap' },
|
labelHeight: 20,
|
content: (data) => {
|
let _val = ''
|
if (plot.show !== 'value') {
|
_val = `${(data[Y_axis] * 100).toFixed(2)}%`
|
} else {
|
_val = `${data[Y_axis]}`
|
}
|
|
return `${data[X_axis]}: ${_val}`
|
},
|
labelLine: {
|
style: {
|
lineWidth: 0.5,
|
},
|
},
|
style: {
|
fill: color
|
}
|
})
|
}
|
}
|
|
if (plot.interaction && plot.interaction.length) {
|
plot.interaction.forEach(t => {
|
chart.interaction(t)
|
})
|
}
|
|
chart.render()
|
}
|
|
pierender = () => {
|
const { card } = this.state
|
const plot = card.plot
|
|
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 + 'canvas',
|
autoFit: true,
|
height: this.wrap.offsetHeight - 30
|
})
|
|
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.splitLine) {
|
_chart.style({
|
lineWidth: plot.splitLine,
|
stroke: plot.splitColor,
|
})
|
}
|
|
if (plot.label !== 'false') {
|
if (plot.label === 'inner') {
|
_chart.label(Y_axis, {
|
offset: -30,
|
content: (data) => {
|
let _val = ''
|
if (plot.show !== 'value') {
|
_val = `${(data[Y_axis] * 100).toFixed(2)}%`
|
} else {
|
_val = `${data[Y_axis]}`
|
}
|
return _val
|
},
|
style: {
|
textAlign: 'center',
|
fontSize: 16,
|
shadowBlur: 2,
|
shadowColor: 'rgba(0, 0, 0, .45)',
|
fill: '#fff',
|
}
|
})
|
} else {
|
_chart.label(Y_axis, {
|
layout: { type: plot.label === 'outer' ? 'pie-spider' : 'fixed-overlap' },
|
labelHeight: 20,
|
content: (data) => {
|
let _val = ''
|
if (plot.show !== 'value') {
|
_val = `${(data[Y_axis] * 100).toFixed(2)}%`
|
} else {
|
_val = `${data[Y_axis]}`
|
}
|
|
return `${data[X_axis]}: ${_val}`
|
},
|
labelLine: {
|
style: {
|
lineWidth: 0.5,
|
},
|
},
|
style: {
|
fill: color
|
}
|
})
|
}
|
}
|
} else {
|
chart.axis(false)
|
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)
|
}
|
if (plot.splitLine) {
|
_chart.style({
|
lineWidth: plot.splitLine,
|
stroke: plot.splitColor,
|
})
|
}
|
}
|
|
if (plot.interaction && plot.interaction.length) {
|
plot.interaction.forEach(t => {
|
chart.interaction(t)
|
})
|
}
|
|
chart.render()
|
}
|
|
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 = ''
|
}
|
this.$timer && clearTimeout(this.$timer)
|
this.$timer = setTimeout(() => {
|
this.viewrender()
|
}, 150)
|
}
|
|
component.width = component.plot.width
|
component.name = component.plot.name
|
|
this.setState({
|
card: component
|
})
|
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.match = '='
|
|
// 注册事件-添加搜索
|
MKEmitter.emit('addSearch', card.uuid, newcard)
|
}
|
|
changeStyle = () => {
|
const { card } = this.state
|
|
MKEmitter.emit('changeStyle', [card.uuid], ['background', 'border', 'padding', 'margin', 'shadow'], card.style)
|
}
|
|
getStyle = (comIds, style) => {
|
const { card } = this.state
|
|
if (comIds[0] !== card.uuid || comIds.length > 1) return
|
|
let _card = {...card, style}
|
|
this.updateComponent(_card)
|
}
|
|
clickComponent = (e) => {
|
if (sessionStorage.getItem('style-control') === 'true' || sessionStorage.getItem('style-control') === 'component') {
|
e.stopPropagation()
|
MKEmitter.emit('clickComponent', this.state.card)
|
}
|
}
|
|
render() {
|
const { card, ismob } = this.state
|
let _style = resetStyle(card.style)
|
|
return (
|
<div className="menu-pie-chart-edit-box" style={{..._style, height: card.plot.height || 400}} onClick={this.clickComponent} id={card.uuid}>
|
<Popover overlayClassName="mk-popover-control-wrap" mouseLeaveDelay={0.2} mouseEnterDelay={0.2} content={
|
<div className="mk-popover-control">
|
{!ismob ? <Icon className="plus" title="添加搜索" onClick={this.addSearch} type="plus-circle" /> : null}
|
<ChartCompileForm config={card} dict={this.state.dict} plotchange={this.updateComponent}/>
|
<CopyComponent type="pie" card={card}/>
|
<Icon className="style" title="调整样式" onClick={this.changeStyle} type="font-colors" />
|
<ClockComponent config={card} updateConfig={this.updateComponent}/>
|
<UserComponent config={card}/>
|
<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>
|
<NormalHeader config={card} updateComponent={this.updateComponent}/>
|
<div className="canvas" id={card.uuid + 'canvas'} ref={ref => this.wrap = ref}></div>
|
</div>
|
)
|
}
|
}
|
|
export default antvBarLineChart
|