BarSection.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import React from 'react'
  2. import { Row, Col, Modal, Icon, InputNumber, Select, Checkbox } from 'antd'
  3. const Option = Select.Option
  4. import { CheckboxChangeEvent } from 'antd/lib/checkbox'
  5. import ColorPicker from 'components/ColorPicker'
  6. import { PIVOT_CHART_LINE_STYLES } from 'app/globalConstants'
  7. import { getAggregatorLocale, decodeMetricName } from '../../util'
  8. import { IBarConfig } from './'
  9. import { IDataParams } from '../OperatingPanel'
  10. import { StackConfigModal, StackMetrics, StackConfig } from '../../Config/Stack'
  11. const styles = require('../Workbench.less')
  12. interface IBarSectionProps {
  13. dataParams: IDataParams
  14. config: IBarConfig
  15. onChange: (
  16. prop: string,
  17. value: number | string | boolean | StackConfig,
  18. propPath: string[]
  19. ) => void
  20. }
  21. interface IBarSectionStates {
  22. stackConfigVisible: boolean
  23. stackMetrics: StackMetrics
  24. }
  25. export class BarSection extends React.PureComponent<
  26. IBarSectionProps,
  27. IBarSectionStates
  28. > {
  29. constructor (props: IBarSectionProps) {
  30. super(props)
  31. const { dataParams } = props
  32. this.state = {
  33. stackMetrics: this.getStackMetrics(dataParams),
  34. stackConfigVisible: false
  35. }
  36. }
  37. public componentDidUpdate (prevProps: IBarSectionProps) {
  38. const { dataParams } = this.props
  39. if (dataParams !== prevProps.dataParams) {
  40. this.setState({
  41. stackMetrics: this.getStackMetrics(dataParams)
  42. })
  43. }
  44. }
  45. private lineStyles = PIVOT_CHART_LINE_STYLES.map((l) => (
  46. <Option key={l.value} value={l.value}>
  47. {l.name}
  48. </Option>
  49. ))
  50. private propChange = (propName: string, propPath?: string) => (
  51. e: number | string | CheckboxChangeEvent
  52. ) => {
  53. const value = (e as CheckboxChangeEvent).target
  54. ? (e as CheckboxChangeEvent).target.checked
  55. : (e as string | number)
  56. this.props.onChange(propName, value, propPath ? [propPath] : [])
  57. }
  58. private getStackMetrics = (dataParams: IDataParams) => {
  59. const metrics = dataParams['metrics'].items.reduce((acc, { name, agg }) => {
  60. acc[name] = `[${getAggregatorLocale(agg)}]${decodeMetricName(name)}`
  61. return acc
  62. }, {})
  63. return metrics
  64. }
  65. private deleteStackConfig = () => {
  66. const { onChange } = this.props
  67. Modal.confirm({
  68. title: '确认删除堆叠设置?',
  69. onOk: () => {
  70. onChange('stack', undefined, [])
  71. }
  72. })
  73. }
  74. private showStackConfig = () => {
  75. this.setState({ stackConfigVisible: true })
  76. }
  77. private cancelStackConfig = () => {
  78. this.setState({ stackConfigVisible: false })
  79. }
  80. private saveStackConfig = (config: StackConfig) => {
  81. this.setState({ stackConfigVisible: false })
  82. this.props.onChange('stack', config, [])
  83. }
  84. public render () {
  85. const { config } = this.props
  86. const { barChart, border, gap, width: gapWidth, stack } = config
  87. const { color, width, type, radius } = border
  88. const { stackConfigVisible, stackMetrics } = this.state
  89. return (
  90. <>
  91. <div className={styles.paneBlock}>
  92. <h4>
  93. <span>堆叠</span>
  94. <Icon type="delete" onClick={this.deleteStackConfig} />
  95. <Icon type="edit" onClick={this.showStackConfig} />
  96. </h4>
  97. </div>
  98. <div className={styles.paneBlock}>
  99. <div className={styles.blockBody}>
  100. <Row
  101. gutter={8}
  102. type="flex"
  103. align="middle"
  104. className={styles.blockRow}
  105. >
  106. <Col span={24}>
  107. <Checkbox
  108. checked={barChart}
  109. onChange={this.propChange('barChart')}
  110. >
  111. 条形图
  112. </Checkbox>
  113. </Col>
  114. </Row>
  115. <Row
  116. gutter={8}
  117. type="flex"
  118. align="middle"
  119. className={styles.blockRow}
  120. >
  121. <Col span={10}>
  122. <Select
  123. placeholder="样式"
  124. className={styles.blockElm}
  125. value={type}
  126. onChange={this.propChange('type', 'border')}
  127. >
  128. {this.lineStyles}
  129. </Select>
  130. </Col>
  131. <Col span={10}>
  132. <InputNumber
  133. placeholder="粗细"
  134. className={styles.blockElm}
  135. value={width}
  136. min={0}
  137. onChange={this.propChange('width', 'border')}
  138. />
  139. </Col>
  140. <Col span={4}>
  141. <ColorPicker
  142. value={color}
  143. onChange={this.propChange('color', 'border') as (e: string) => void}
  144. />
  145. </Col>
  146. </Row>
  147. <Row
  148. gutter={8}
  149. type="flex"
  150. align="middle"
  151. className={styles.blockRow}
  152. >
  153. <Col span={14}>边框圆角</Col>
  154. <Col span={10}>
  155. <InputNumber
  156. className={styles.blockElm}
  157. value={radius}
  158. min={0}
  159. onChange={this.propChange('radius', 'border')}
  160. />
  161. </Col>
  162. </Row>
  163. <Row
  164. gutter={8}
  165. type="flex"
  166. align="middle"
  167. className={styles.blockRow}
  168. >
  169. <Col span={14}>柱条宽度</Col>
  170. <Col span={10}>
  171. <InputNumber
  172. className={styles.blockElm}
  173. value={gapWidth}
  174. onChange={this.propChange('width')}
  175. />
  176. </Col>
  177. </Row>
  178. <Row
  179. gutter={8}
  180. type="flex"
  181. align="middle"
  182. className={styles.blockRow}
  183. >
  184. <Col span={14}>不同系列间柱条间隔</Col>
  185. <Col span={10}>
  186. <InputNumber
  187. className={styles.blockElm}
  188. value={gap}
  189. onChange={this.propChange('gap')}
  190. />
  191. </Col>
  192. </Row>
  193. </div>
  194. </div>
  195. <StackConfigModal
  196. visible={stackConfigVisible}
  197. stack={stack}
  198. metrics={stackMetrics}
  199. onCancel={this.cancelStackConfig}
  200. onSave={this.saveStackConfig}
  201. />
  202. </>
  203. )
  204. }
  205. }
  206. export default BarSection