import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { Popover, message } from 'antd'
|
import { ToolOutlined, DeleteOutlined, FontColorsOutlined, PlusCircleOutlined } from '@ant-design/icons'
|
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, getTables, getHeight, checkComponent } from '@/utils/utils-custom.js'
|
import Utils from '@/utils/utils.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 ClockComponent = asyncIconComponent(() => import('@/menu/components/share/clockcomponent'))
|
|
class antvBarLineChart extends Component {
|
static propTpyes = {
|
card: PropTypes.object,
|
updateConfig: PropTypes.func,
|
deletecomponent: PropTypes.func,
|
}
|
|
state = {
|
card: null,
|
appType: sessionStorage.getItem('appType'),
|
eventListener: null
|
}
|
|
UNSAFE_componentWillMount () {
|
const { card } = this.props
|
const { appType } = this.state
|
|
if (card.isNew) {
|
let _plot = {
|
shape: card.subtype, // 图表类型
|
width: card.width || 12,
|
height: 400,
|
label: 'outer',
|
name: card.name
|
}
|
|
if (appType === 'mob') {
|
_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,
|
format: 'array', // 组件属性 - 数据格式
|
pageable: false, // 组件属性 - 是否可分页
|
switchable: false, // 组件属性 - 数据是否可切换
|
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,
|
}
|
|
this.updateComponent(_card, true)
|
} else {
|
this.setState({
|
card: fromJS(card).toJS()
|
})
|
}
|
}
|
|
componentDidMount () {
|
MKEmitter.addListener('tabsChange', this.handleTabsChange)
|
|
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)
|
}
|
|
handleTabsChange = (parentId) => {
|
const { card } = this.state
|
|
if (parentId.indexOf(card.uuid) > -1 || parentId === 'all') {
|
let _element = document.getElementById(card.uuid + 'canvas')
|
if (_element) {
|
_element.innerHTML = ''
|
}
|
|
this.$timer && clearTimeout(this.$timer)
|
this.$timer = setTimeout(this.viewrender, 100)
|
}
|
}
|
|
getHeight = (val) => {
|
if (typeof(val) === 'string') {
|
if (val.indexOf('px') > -1) {
|
val = parseFloat(val)
|
} else if (val.indexOf('vw') > -1) {
|
val = parseFloat(val)
|
val = document.body.clientWidth * val / 100
|
} else if (val.indexOf('vh') > -1) {
|
val = parseFloat(val)
|
val = document.body.clientHeight * val / 100
|
}
|
}
|
|
return parseInt(val || 400)
|
}
|
|
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: getHeight(plot.height),
|
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: getHeight(plot.height)
|
})
|
|
if (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)
|
|
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 {
|
chart.legend({
|
position: plot.legend,
|
itemName: {
|
style: {
|
fill: color,
|
}
|
}
|
})
|
}
|
|
if (plot.tooltip !== 'true') {
|
chart.tooltip(false)
|
} else {
|
chart.tooltip({
|
showTitle: false,
|
showMarkers: false
|
})
|
}
|
|
// 饼图或环图
|
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
|
}
|
})
|
}
|
}
|
|
if (plot.interaction && plot.interaction.length) {
|
plot.interaction.forEach(t => {
|
chart.interaction(t)
|
})
|
}
|
|
chart.render()
|
}
|
|
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(card.uuid + 'canvas')
|
if (_element) {
|
_element.innerHTML = ''
|
}
|
this.$timer && clearTimeout(this.$timer)
|
this.$timer = setTimeout(() => {
|
this.viewrender()
|
}, 150)
|
}
|
}
|
|
card.width = card.plot.width
|
card.name = card.plot.name
|
|
card.$c_ds = true
|
card.errors = checkComponent(card)
|
|
if (card.errors.length === 0) {
|
card.$tables = getTables(card)
|
}
|
|
delete card.$c_ds
|
|
if (!card.plot.Xaxis) {
|
card.errors.push({ level: 0, detail: '名称字段尚未设置!'})
|
} else {
|
let columns = card.columns.map(c => c.field)
|
if (!columns.includes(card.plot.Xaxis)) {
|
card.errors.push({ level: 0, detail: '名称字段在字段集中不存在'})
|
} else if (!columns.includes(card.plot.Yaxis)) {
|
card.errors.push({ level: 0, detail: '值字段在字段集中不存在'})
|
}
|
}
|
|
this.setState({
|
card: card
|
})
|
this.props.updateConfig(card)
|
}
|
|
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.orderType = 'asc'
|
newcard.match = '='
|
|
// 注册事件-添加搜索
|
MKEmitter.emit('addSearch', card.uuid, newcard)
|
}
|
|
changeStyle = () => {
|
const { card } = this.state
|
|
MKEmitter.emit('changeStyle', ['background', 'border', 'padding', 'margin', 'shadow', 'clear'], card.style, this.getStyle)
|
}
|
|
getStyle = (style) => {
|
let _card = {...this.state.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.uuid, null, (style) => {
|
let _card = {...this.state.card}
|
_card.style = {..._card.style, ...style}
|
|
this.updateComponent(_card)
|
})
|
}
|
}
|
|
render() {
|
const { card, appType } = this.state
|
let _style = resetStyle(card.style)
|
_style.height = 'auto'
|
|
return (
|
<div className="menu-pie-chart-edit-box" style={_style} onClick={this.clickComponent} id={card.uuid}>
|
<Popover overlayClassName="mk-popover-control-wrap" mouseLeaveDelay={0.2} mouseEnterDelay={0.2} content={
|
<div className="mk-popover-control">
|
{appType !== 'mob' ? <PlusCircleOutlined className="plus" title="添加搜索" onClick={this.addSearch}/> : null}
|
<ChartCompileForm config={card} plotchange={this.updateComponent}/>
|
<CopyComponent type="pie" card={card}/>
|
<FontColorsOutlined className="style" title="调整样式" onClick={this.changeStyle}/>
|
<ClockComponent config={card} updateConfig={this.updateComponent}/>
|
<DeleteOutlined className="close" title="delete" onClick={() => this.props.deletecomponent(card.uuid)} />
|
<SettingComponent config={card} updateConfig={this.updateComponent}/>
|
</div>
|
} trigger="hover">
|
<ToolOutlined />
|
</Popover>
|
<NormalHeader config={card} updateComponent={this.updateComponent}/>
|
<div className="canvas" style={{minHeight: card.plot.height}} id={card.uuid + 'canvas'}></div>
|
<div className="component-name">
|
<div className="center">
|
<div className="title" onDoubleClick={() => {
|
let oInput = document.createElement('input')
|
oInput.value = 'anchor' + card.uuid
|
document.body.appendChild(oInput)
|
oInput.select()
|
document.execCommand('Copy')
|
document.body.removeChild(oInput)
|
message.success('复制成功。')
|
}}>{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>
|
)
|
}
|
}
|
|
export default antvBarLineChart
|