Legend.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import React from 'react'
  2. import classnames from 'classnames'
  3. import { IDataParamProperty } from '../Workbench/OperatingPanel'
  4. import { IDataParamSource } from '../Workbench/Dropbox'
  5. import { IChartStyles } from 'containers/Widget/components/Widget'
  6. import { getStyleConfig } from 'containers/Widget/components/util'
  7. const styles = require('./Pivot.less')
  8. interface ILegendProps {
  9. color: IDataParamProperty
  10. chartStyles: IChartStyles
  11. onLegendSelect: (name: string, key: string) => void
  12. }
  13. interface ILengdItem extends IDataParamSource {
  14. localConfig?: {
  15. values?: {
  16. [key: string]: {
  17. value: string
  18. visible: boolean
  19. }
  20. }
  21. }
  22. }
  23. interface ILegendStates {
  24. list: ILengdItem[]
  25. }
  26. export class Legend extends React.PureComponent<ILegendProps, ILegendStates> {
  27. constructor (props) {
  28. super(props)
  29. this.state = {
  30. list: []
  31. }
  32. }
  33. public componentWillMount () {
  34. this.initList(this.props)
  35. }
  36. public componentWillReceiveProps (nextProps) {
  37. this.initList(nextProps)
  38. }
  39. private initList = (props) => {
  40. const { color } = props
  41. const { list } = this.state
  42. if (color && color.items.length) {
  43. this.setState({
  44. list: color.items.map((item) => {
  45. const originItem = list.find((i) => i.name === item.name)
  46. const originConfig = originItem && originItem.localConfig
  47. const configValues = Object.entries(item.config.values).reduce((obj, [key, value]) => {
  48. obj[key] = {
  49. value,
  50. visible: originConfig ? originConfig.values[key].visible : true
  51. }
  52. return obj
  53. }, {})
  54. return {
  55. ...item,
  56. localConfig: {
  57. values: configValues
  58. }
  59. }
  60. })
  61. })
  62. } else {
  63. this.setState({
  64. list: []
  65. })
  66. }
  67. }
  68. private legendSelect = (name: string, key: string) => () => {
  69. const { list } = this.state
  70. const selectedItem = list.find((i) => i.name === name)
  71. const visible = selectedItem.localConfig.values[key].visible
  72. selectedItem.localConfig.values[key].visible = !visible
  73. this.setState({list})
  74. this.props.onLegendSelect(name, key)
  75. }
  76. public render () {
  77. const { chartStyles } = this.props
  78. const { list } = this.state
  79. const { color: fontColor } = getStyleConfig(chartStyles).pivot
  80. const legendClass = classnames({
  81. [styles.legend]: true,
  82. [styles.shown]: list.length
  83. })
  84. const legendBoxes = list.map((i) => {
  85. const { values } = i.localConfig
  86. const listItems = Object.entries(values).map(([key, value]: [string, { value: string, visible: boolean }]) => {
  87. return (
  88. <li
  89. key={key}
  90. onClick={this.legendSelect(i.name, key)}
  91. className={classnames({[styles.disabled]: !value.visible})}
  92. style={{
  93. color: fontColor
  94. }}
  95. >
  96. <span style={{background: value.value}} />
  97. {key}
  98. </li>
  99. )
  100. })
  101. return (
  102. <div key={i.name} className={styles.legendBox}>
  103. <h4
  104. style={{
  105. color: fontColor
  106. }}
  107. >
  108. {i.name}
  109. </h4>
  110. <ul className={styles.list}>
  111. {listItems}
  112. </ul>
  113. </div>
  114. )
  115. })
  116. return (
  117. <div className={legendClass}>
  118. {legendBoxes}
  119. </div>
  120. )
  121. }
  122. }
  123. export default Legend