LabelSection.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import React from 'react'
  2. import { Row, Col, Checkbox, Select } from 'antd'
  3. const Option = Select.Option
  4. const CheckboxGroup = Checkbox.Group
  5. import ColorPicker from 'components/ColorPicker'
  6. import {
  7. CHART_LABEL_POSITIONS,
  8. CHART_PIE_LABEL_POSITIONS,
  9. CHART_FUNNEL_LABEL_POSITIONS
  10. } from 'app/globalConstants'
  11. import { chartFontFamilyOptions, chartFontSizeOptions } from './constants'
  12. const styles = require('../Workbench.less')
  13. export interface ILabelConfig {
  14. showLabel: boolean
  15. labelPosition?: string
  16. labelFontFamily: string
  17. labelFontSize: string
  18. labelColor: string
  19. labelParts?: Array<
  20. | 'dimensionName'
  21. | 'dimensionValue'
  22. | 'indicatorName'
  23. | 'indicatorValue'
  24. | 'percentage'
  25. | 'conversion'
  26. | 'arrival'
  27. >
  28. pieLabelPosition?: string
  29. funnelLabelPosition?: string
  30. }
  31. interface ILabelSectionProps {
  32. title: string
  33. config: ILabelConfig
  34. onChange: (prop: string, value: any) => void
  35. name: string
  36. }
  37. export class LabelSection extends React.PureComponent<ILabelSectionProps, {}> {
  38. private static LabelOptions: Array<{
  39. label: string
  40. value: ILabelConfig['labelParts'][number]
  41. charts: string[]
  42. }> = [
  43. { label: '维度名称', value: 'dimensionName', charts: [] },
  44. { label: '维度值', value: 'dimensionValue', charts: ['pie', 'funnel'] },
  45. { label: '指标名称', value: 'indicatorName', charts: ['radar'] },
  46. {
  47. label: '指标值',
  48. value: 'indicatorValue',
  49. charts: ['pie', 'funnel', 'radar']
  50. },
  51. { label: '转化率', value: 'conversion', charts: ['funnel'] },
  52. { label: '到达率', value: 'arrival', charts: ['funnel'] },
  53. { label: '百分比', value: 'percentage', charts: ['pie', 'funnel'] }
  54. ]
  55. private checkboxChange = (prop) => (e) => {
  56. this.props.onChange(prop, e.target.checked)
  57. }
  58. private selectChange = (prop) => (value) => {
  59. this.props.onChange(prop, value)
  60. }
  61. private colorChange = (prop) => (color) => {
  62. this.props.onChange(prop, color)
  63. }
  64. public render () {
  65. const { title, config, name } = this.props
  66. const {
  67. showLabel,
  68. labelPosition,
  69. labelFontFamily,
  70. labelFontSize,
  71. labelColor,
  72. labelParts,
  73. pieLabelPosition,
  74. funnelLabelPosition
  75. } = config
  76. let positionValues
  77. let positionName
  78. let positionChangeName
  79. const labelOptions = LabelSection.LabelOptions.filter((option) =>
  80. option.charts.includes(name)
  81. ).map(({ label, value }) => ({ label, value }))
  82. switch (name) {
  83. case 'pie':
  84. positionValues = CHART_PIE_LABEL_POSITIONS
  85. positionName = pieLabelPosition
  86. positionChangeName = 'pieLabelPosition'
  87. break
  88. case 'funnel':
  89. positionValues = CHART_FUNNEL_LABEL_POSITIONS
  90. positionName = funnelLabelPosition
  91. positionChangeName = 'funnelLabelPosition'
  92. break
  93. default:
  94. positionValues = CHART_LABEL_POSITIONS
  95. positionName = labelPosition
  96. positionChangeName = 'labelPosition'
  97. break
  98. }
  99. const positions = positionValues.map((p) => (
  100. <Option key={p.value} value={p.value}>
  101. {p.name}
  102. </Option>
  103. ))
  104. const labelPositionSetting = positionName !== void 0 && (
  105. <>
  106. <Col key="posLabel" span={4}>
  107. 位置
  108. </Col>
  109. <Col key="posSetting" span={10}>
  110. <Select
  111. placeholder="位置"
  112. className={styles.blockElm}
  113. value={positionName}
  114. onChange={this.selectChange(positionChangeName)}
  115. >
  116. {positions}
  117. </Select>
  118. </Col>
  119. </>
  120. )
  121. return (
  122. <div className={styles.paneBlock}>
  123. <h4>{title}</h4>
  124. <div className={styles.blockBody}>
  125. <Row
  126. gutter={8}
  127. type="flex"
  128. align="middle"
  129. className={styles.blockRow}
  130. >
  131. <Col span={10}>
  132. <Checkbox
  133. checked={showLabel}
  134. onChange={this.checkboxChange('showLabel')}
  135. >
  136. 显示标签
  137. </Checkbox>
  138. </Col>
  139. {labelPositionSetting}
  140. </Row>
  141. <Row
  142. gutter={8}
  143. type="flex"
  144. align="middle"
  145. className={styles.blockRow}
  146. >
  147. <Col span={10}>
  148. <Select
  149. placeholder="字体"
  150. className={styles.blockElm}
  151. value={labelFontFamily}
  152. onChange={this.selectChange('labelFontFamily')}
  153. >
  154. {chartFontFamilyOptions}
  155. </Select>
  156. </Col>
  157. <Col span={10}>
  158. <Select
  159. placeholder="文字大小"
  160. className={styles.blockElm}
  161. value={labelFontSize}
  162. onChange={this.selectChange('labelFontSize')}
  163. >
  164. {chartFontSizeOptions}
  165. </Select>
  166. </Col>
  167. <Col span={4}>
  168. <ColorPicker
  169. value={labelColor}
  170. onChange={this.colorChange('labelColor')}
  171. />
  172. </Col>
  173. </Row>
  174. {!!labelOptions.length && (
  175. <Row
  176. gutter={8}
  177. type="flex"
  178. align="middle"
  179. >
  180. <Col span={24}>
  181. <CheckboxGroup
  182. value={labelParts}
  183. options={labelOptions}
  184. onChange={this.selectChange('labelParts')}
  185. />
  186. </Col>
  187. </Row>
  188. )}
  189. </div>
  190. </div>
  191. )
  192. }
  193. }
  194. export default LabelSection