ColumnHeader.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import React from 'react'
  2. import classnames from 'classnames'
  3. import { IDrawingData } from './Pivot'
  4. import { IWidgetMetric, DimetionType, IChartStyles } from '../Widget'
  5. import { spanSize, getPivotCellWidth, getAggregatorLocale, getPivot, getStyleConfig } from '../util'
  6. import { DEFAULT_SPLITER } from 'app/globalConstants'
  7. const styles = require('./Pivot.less')
  8. interface IColumnHeaderProps {
  9. cols: string[]
  10. colKeys: string[][]
  11. colTree: object
  12. metrics: IWidgetMetric[]
  13. chartStyles: IChartStyles
  14. drawingData: IDrawingData
  15. dimetionAxis: DimetionType
  16. }
  17. export class ColumnHeader extends React.Component<IColumnHeaderProps, {}> {
  18. public render () {
  19. const { cols, colKeys, colTree, metrics, chartStyles, drawingData, dimetionAxis } = this.props
  20. const { elementSize, unitMetricWidth } = drawingData
  21. const {
  22. color: fontColor,
  23. fontSize,
  24. fontFamily,
  25. lineColor,
  26. lineStyle,
  27. headerBackgroundColor
  28. } = getStyleConfig(chartStyles).pivot
  29. let tableWidth = 0
  30. let headers
  31. if (cols.length) {
  32. if (dimetionAxis === 'col' && cols.length === 1) {
  33. tableWidth = colKeys.length * elementSize
  34. }
  35. headers = cols.map((c, i) => {
  36. const header = []
  37. let elementCount = 0
  38. let cellWidth = 0
  39. let x = -1
  40. colKeys.forEach((ck, j) => {
  41. const flatColKey = ck.join(String.fromCharCode(0))
  42. const { width } = colTree[flatColKey]
  43. if (dimetionAxis === 'col') {
  44. if (i === cols.length - 1) {
  45. return
  46. }
  47. if (i === cols.length - 2) {
  48. const nextCk = colKeys[j + 1] || []
  49. elementCount += 1
  50. if (ck[i] === nextCk[i]) {
  51. return
  52. } else {
  53. cellWidth = elementCount * elementSize
  54. x = elementCount
  55. tableWidth += cellWidth
  56. elementCount = 0
  57. }
  58. } else {
  59. x = spanSize(colKeys, j, i)
  60. }
  61. } else {
  62. if (i === cols.length - 1) {
  63. cellWidth = dimetionAxis === 'row' ? unitMetricWidth * metrics.length : getPivotCellWidth(width)
  64. tableWidth += cellWidth
  65. }
  66. x = spanSize(colKeys, j, i)
  67. }
  68. const columnClass = classnames({
  69. [styles.leftBorder]: true,
  70. [styles.rightBorder]: true
  71. })
  72. if (x !== -1) {
  73. let colContent
  74. if (ck[i].includes(DEFAULT_SPLITER)) {
  75. const [name, id, agg] = ck[i].split(DEFAULT_SPLITER)
  76. colContent = `[${getAggregatorLocale(agg)}]${name}`
  77. } else {
  78. colContent = ck[i]
  79. }
  80. header.push(
  81. <th
  82. key={flatColKey}
  83. colSpan={x}
  84. className={columnClass}
  85. style={{
  86. ...(!!cellWidth && {width: cellWidth}),
  87. ...!dimetionAxis && {backgroundColor: headerBackgroundColor},
  88. color: fontColor,
  89. fontSize: Number(fontSize),
  90. fontFamily,
  91. borderColor: lineColor,
  92. borderStyle: lineStyle
  93. }}
  94. >
  95. <p
  96. className={styles.colContent}
  97. {...(!!cellWidth && {style: {width: cellWidth - 2}})}
  98. >
  99. {colContent}
  100. </p>
  101. </th>
  102. )
  103. }
  104. })
  105. return (
  106. <tr key={c}>
  107. {header}
  108. </tr>
  109. )
  110. })
  111. }
  112. const containerClass = classnames({
  113. [styles.columnHeader]: true,
  114. [styles.raw]: !dimetionAxis
  115. })
  116. return (
  117. <div className={containerClass}>
  118. <table className={styles.pivot} style={{width: tableWidth}}>
  119. <thead>
  120. {headers}
  121. </thead>
  122. </table>
  123. </div>
  124. )
  125. }
  126. }
  127. export default ColumnHeader