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 SearchComponent = asyncComponent(() => import('@/menu/searchcomponent'))
|
const ActionComponent = asyncComponent(() => import('@/menu/actioncomponent'))
|
const ChartCompileForm = asyncIconComponent(() => import('./chartcompile'))
|
|
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, menu } = this.props
|
|
if (card.isNew) {
|
let _plot = {
|
chartType: card.type, // 图表类型
|
enabled: 'false', // 是否使用自定义设置
|
datatype: 'query', // 数据类型查询或统计
|
customs: [],
|
width: 24,
|
height: 400,
|
name: card.name
|
}
|
|
if (card.subtype === 'bar') {
|
_plot.coordinate = 'angle' // 二维坐标或极坐标
|
_plot.transpose = 'false' // 坐标轴变换
|
} else if (card.subtype === 'bar1') {
|
_plot.coordinate = 'angle'
|
_plot.transpose = 'true'
|
} else if (card.subtype === 'line') {
|
_plot.shape = 'smooth'
|
} else if (card.subtype === 'line1') {
|
_plot.shape = 'hv'
|
}
|
|
let dataName = ''
|
|
if (card.floor === 1) {
|
while (!dataName) {
|
let _dataName = Utils.getdataName()
|
if (menu.components.filter(com => com.dataName === _dataName).length === 0) {
|
dataName = _dataName
|
}
|
}
|
}
|
|
let _card = {
|
uuid: card.uuid,
|
type: card.type,
|
floor: card.floor,
|
tabId: card.tabId || '',
|
parentId: card.parentId || '',
|
format: 'array', // 组件属性 - 数据格式
|
pageable: false, // 组件属性 - 是否可分页
|
switchable: false, // 组件属性 - 数据是否可切换
|
dataName: dataName,
|
width: _plot.width,
|
name: _plot.name,
|
subtype: card.subtype,
|
setting: { interType: 'system' },
|
style: {
|
fontSize: '16px',
|
borderWidth: '1px', borderColor: 'rgb(217, 217, 217)',
|
marginLeft: '8px', marginRight: '8px', marginTop: '8px', marginBottom: '8px'
|
},
|
columns: [],
|
scripts: [],
|
search: [],
|
action: [],
|
plot: _plot
|
}
|
this.setState({
|
card: _card
|
})
|
this.props.updateConfig(_card)
|
} else {
|
this.setState({
|
card: fromJS(card).toJS()
|
})
|
}
|
}
|
|
componentDidMount () {
|
this.viewrender()
|
MKEmitter.addListener('tabsChange', this.handleTabsChange)
|
MKEmitter.addListener('submitStyle', this.getStyle)
|
}
|
|
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)
|
if (_element) {
|
_element.innerHTML = ''
|
}
|
|
setTimeout(this.viewrender, 100)
|
}
|
}
|
|
getdata = (X_axis, Y_axis) => {
|
let data = []
|
let xdata = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
|
let point = 7
|
|
for (let i = 0; i < point; i++) {
|
let item = {}
|
|
item[X_axis] = xdata[i]
|
|
if (typeof(Y_axis) === 'string') {
|
item[Y_axis] = Math.floor(Math.random() * 5 * (i + 1)) + i
|
} else {
|
Y_axis.forEach(y => {
|
item[y] = Math.floor(Math.random() * 5 * (i + 1)) + i
|
})
|
}
|
|
data.push(item)
|
}
|
|
return data
|
}
|
|
viewrender = () => {
|
const { card } = this.state
|
|
if (card.plot.chartType === 'line') {
|
this.linerender()
|
} else if (card.plot.chartType === 'bar') {
|
this.barrender()
|
}
|
}
|
|
linerender = () => {
|
const { card } = this.state
|
let plot = {...card.plot, height: card.plot.height - 80} // 去除title所占空间
|
let color = plot.color || 'rgba(0, 0, 0, 0.85)'
|
|
let transfield = {}
|
card.columns.forEach(col => {
|
if (col.field) {
|
transfield[col.field] = col.label
|
}
|
})
|
|
let X_axis = plot.Xaxis || 'x'
|
let Y_axis = plot.Yaxis || ['y']
|
|
let data = this.getdata(X_axis, Y_axis)
|
|
if (plot.enabled !== 'true') {
|
const ds = new DataSet()
|
const dv = ds.createView().source(data)
|
|
dv.transform({
|
type: 'fold',
|
fields: [...Y_axis],
|
key: 'key',
|
value: 'value'
|
})
|
|
if (plot.Xaxis) {
|
dv.transform({
|
type: 'map',
|
callback(row) {
|
row.key = transfield[row.key]
|
return row
|
},
|
})
|
}
|
|
const chart = new Chart({
|
container: card.uuid,
|
autoFit: true,
|
height: plot.height || 400
|
})
|
|
chart.data(dv.rows)
|
|
chart.axis(X_axis, {
|
label: {
|
style: {
|
fill: color,
|
}
|
},
|
line: {
|
style: {
|
fill: color,
|
}
|
}
|
})
|
chart.axis('value', {
|
grid: {
|
style: {
|
fill: color,
|
}
|
},
|
label: {
|
style: {
|
fill: color,
|
}
|
}
|
})
|
|
if (plot.coordinate !== 'polar') {
|
chart.scale(X_axis, {
|
range: [0, 1]
|
})
|
}
|
chart.scale('value', {
|
nice: true
|
})
|
|
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({
|
shared: true
|
})
|
}
|
|
if (plot.transpose === 'true') {
|
chart.coordinate().transpose()
|
}
|
|
if (plot.coordinate === 'polar') {
|
chart.coordinate('polar', {
|
innerRadius: 0.1,
|
radius: 0.8
|
})
|
}
|
|
let _chart = chart
|
.line()
|
.position(`${X_axis}*value`)
|
.color('key')
|
.shape(plot.shape || 'smooth')
|
|
if (plot.label === 'true') {
|
_chart.label('value')
|
}
|
|
if (plot.point === 'true') {
|
chart
|
.point()
|
.position(`${X_axis}*value`)
|
.color('key')
|
.size(3)
|
.shape('circle')
|
}
|
chart.render()
|
} else {
|
this.customrender(data, transfield)
|
}
|
}
|
|
customrender = (data, transfield) => {
|
const { card } = this.state
|
let plot = {...card.plot, height: card.plot.height - 80} // 去除title所占空间
|
let color = plot.color || 'rgba(0, 0, 0, 0.85)'
|
|
let barfields = []
|
let fields = []
|
let legends = []
|
|
plot.customs.forEach(item => {
|
item.name = transfield[item.field] || item.field
|
if (item.axis === 'left') {
|
item.index = 0
|
} else if (item.axis === 'right') {
|
item.index = 1
|
} else {
|
item.index = 2
|
}
|
|
if (item.chartType === 'bar') {
|
barfields.push(item.field)
|
fields.unshift(item)
|
} else {
|
fields.push(item)
|
}
|
|
legends.push({
|
value: item.name,
|
name: item.name,
|
marker: { symbol: item.chartType === 'bar' ? 'square' : 'hyphen', style: { stroke: item.color,fill: item.color, r: 5, lineWidth: 2 } }
|
})
|
})
|
|
fields.sort((a, b) => a.index - b.index)
|
|
const ds = new DataSet()
|
const dv = ds.createView().source(data)
|
dv.transform({
|
type: 'map',
|
callback(row) {
|
fields.forEach(line => {
|
row[line.name] = row[line.field]
|
})
|
return row
|
}
|
})
|
|
const chart = new Chart({
|
container: plot.uuid,
|
autoFit: true,
|
height: plot.height || 400
|
})
|
|
chart.data(dv.rows)
|
|
chart.axis(plot.Xaxis, {
|
label: {
|
style: {
|
fill: color,
|
}
|
},
|
line: {
|
style: {
|
fill: color,
|
}
|
}
|
})
|
chart.axis('value', {
|
grid: {
|
style: {
|
fill: color,
|
}
|
},
|
label: {
|
style: {
|
fill: color,
|
}
|
}
|
})
|
|
if (plot.coordinate !== 'polar' && barfields.length === 0) {
|
chart.scale(plot.Xaxis, {
|
range: [0, 1]
|
})
|
}
|
|
if (!plot.legend || plot.legend === 'hidden') {
|
chart.legend(false)
|
} else {
|
chart.legend({
|
custom: true,
|
position: plot.legend,
|
items: legends,
|
itemName: {
|
style: {
|
fill: color,
|
}
|
}
|
})
|
}
|
|
if (plot.tooltip !== 'true') {
|
chart.tooltip(false)
|
} else {
|
chart.tooltip({
|
shared: true,
|
})
|
}
|
|
if (plot.transpose === 'true') {
|
chart.coordinate().transpose()
|
}
|
|
if (plot.coordinate === 'polar') {
|
chart.coordinate('polar', {
|
innerRadius: 0.1,
|
radius: 0.8
|
})
|
}
|
|
chart.scale({
|
nice: true
|
})
|
|
fields.forEach((item, i) => {
|
if (i === 0) {
|
chart.axis(item.name, {
|
grid: {},
|
title: {},
|
label: {}
|
})
|
} else if (i === 1 && item.axis !== 'unset') {
|
chart.axis(item.name, {
|
grid: null,
|
title: {},
|
label: {}
|
})
|
} else {
|
chart.axis(item.name, {
|
grid: null,
|
title: null,
|
label: null
|
})
|
}
|
|
if (item.chartType === 'bar') {
|
let _chart = chart
|
.interval()
|
.position(`${plot.Xaxis}*${item.name}`)
|
.color(item.color)
|
.shape(item.shape)
|
|
if (item.label === 'true') {
|
_chart.label(item.name)
|
}
|
} else if (item.chartType === 'line') {
|
let _chart = chart
|
.line()
|
.position(`${plot.Xaxis}*${item.name}`)
|
.color(item.color)
|
.shape(item.shape)
|
|
if (item.label === 'true') {
|
_chart.label(item.name)
|
}
|
|
if (plot.point === 'true') {
|
chart
|
.point()
|
.position(`${plot.Xaxis}*${item.name}`)
|
.color(item.color)
|
.size(3)
|
.shape('circle')
|
}
|
}
|
})
|
|
chart.render()
|
}
|
|
barrender = () => {
|
const { card } = this.state
|
let plot = {...card.plot, height: card.plot.height - 80}
|
let color = plot.color || 'rgba(0, 0, 0, 0.85)'
|
|
let transfield = {}
|
card.columns.forEach(col => {
|
if (col.field) {
|
transfield[col.field] = col.label
|
}
|
})
|
let X_axis = plot.Xaxis || 'x'
|
let Y_axis = plot.Yaxis || ['y']
|
|
let data = this.getdata(X_axis, Y_axis)
|
|
if (plot.enabled !== 'true') {
|
const ds = new DataSet()
|
const dv = ds.createView().source(data)
|
|
dv.transform({
|
type: 'fold',
|
fields: [...Y_axis],
|
key: 'key',
|
value: 'value'
|
})
|
|
if (plot.Xaxis) {
|
dv.transform({
|
type: 'map',
|
callback(row) {
|
row.key = transfield[row.key]
|
return row
|
},
|
})
|
}
|
|
const chart = new Chart({
|
container: card.uuid,
|
autoFit: true,
|
height: plot.height || 400
|
})
|
|
chart.data(dv.rows)
|
|
chart.axis(X_axis, {
|
label: {
|
style: {
|
fill: color,
|
}
|
},
|
line: {
|
style: {
|
fill: color,
|
}
|
}
|
})
|
chart.axis('value', {
|
grid: {
|
style: {
|
fill: color,
|
}
|
},
|
label: {
|
style: {
|
fill: color,
|
}
|
}
|
})
|
|
chart.scale('value', {
|
nice: true
|
})
|
|
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({
|
shared: true
|
})
|
}
|
|
if (plot.transpose === 'true') {
|
chart.coordinate().transpose()
|
}
|
|
if (plot.coordinate === 'polar') {
|
chart.coordinate('polar', {
|
innerRadius: 0.1,
|
radius: 0.8
|
})
|
}
|
|
if (plot.adjust !== 'stack') {
|
let _chart = chart
|
.interval()
|
.position(`${X_axis}*value`)
|
.color('key')
|
.adjust([
|
{
|
type: 'dodge',
|
marginRatio: 0
|
}
|
])
|
.shape(plot.shape || 'rect')
|
|
if (plot.label === 'true') {
|
_chart.label('value')
|
}
|
} else if (plot.adjust === 'stack') {
|
let _chart = chart
|
.interval()
|
.position(`${X_axis}*value`)
|
.color('key')
|
.adjust('stack')
|
.shape(plot.shape || 'rect')
|
|
if (plot.label === 'true') {
|
_chart.label('value')
|
}
|
}
|
|
chart.render()
|
} else {
|
this.customrender(data, transfield)
|
}
|
}
|
|
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.viewrender()
|
}, 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)
|
}
|
|
addButton = () => {
|
const { card } = this.state
|
|
let newcard = {}
|
newcard.uuid = Utils.getuuid()
|
newcard.focus = true
|
|
newcard.label = '导出Excel'
|
newcard.sqlType = ''
|
newcard.Ot = 'requiredSgl'
|
newcard.OpenType = 'excelOut'
|
newcard.icon = 'download'
|
newcard.class = 'dgreen'
|
newcard.intertype = card.setting.interType
|
newcard.innerFunc = card.setting.innerFunc || ''
|
newcard.sysInterface = card.setting.sysInterface || ''
|
newcard.outerFunc = card.setting.outerFunc || ''
|
newcard.interface = card.setting.interface || ''
|
newcard.method = 'POST'
|
newcard.execSuccess = 'grid'
|
newcard.execError = 'never'
|
newcard.popClose = 'never'
|
newcard.errorTime = 10
|
newcard.verify = null
|
newcard.show = 'icon'
|
|
// 注册事件-添加按钮
|
MKEmitter.emit('addButton', card.uuid, newcard)
|
}
|
|
changeStyle = () => {
|
const { card } = this.state
|
|
MKEmitter.emit('changeStyle', [card.uuid], ['font', 'background', 'border', 'padding', 'margin'], card.style)
|
}
|
|
getStyle = (comIds, style) => {
|
const { card } = this.state
|
|
if (comIds.length !== 1 || comIds[0] !== card.uuid) return
|
|
let _card = {...card, style}
|
|
this.setState({
|
card: _card
|
})
|
|
this.props.updateConfig(_card)
|
}
|
|
render() {
|
const { card } = this.state
|
|
return (
|
<div className="menu-line-chart-edit-box" style={{...card.style, height: card.plot.height || 400}}>
|
<div className="chart-header">
|
<span className="chart-title">{card.plot.title || ''}</span>
|
<SearchComponent
|
config={card}
|
updatesearch={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" />
|
<Icon className="plus" title="添加按钮" onClick={this.addButton} type="plus-square" />
|
<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>
|
<ActionComponent
|
type="chart"
|
plus="false"
|
config={card}
|
updateaction={this.updateComponent}
|
/>
|
<div className="canvas" id={card.uuid}></div>
|
</div>
|
)
|
}
|
}
|
|
const mapStateToProps = (state) => {
|
return {
|
menu: state.customMenu
|
}
|
}
|
|
const mapDispatchToProps = () => {
|
return {}
|
}
|
|
export default connect(mapStateToProps, mapDispatchToProps)(antvBarLineChart)
|