index.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import React, { createRef } from 'react'
  2. import classnames from 'classnames'
  3. import Pivot from '../Pivot'
  4. import Chart from '../Chart'
  5. import { Icon } from 'antd'
  6. import {
  7. AggregatorType,
  8. DragType,
  9. IDataParamConfig
  10. } from '../Workbench/Dropbox'
  11. import { IDataParamProperty } from '../Workbench/OperatingPanel'
  12. import { IFieldFormatConfig } from '../Config/Format'
  13. import { IFieldConfig } from '../Config/Field'
  14. import { IFieldSortConfig } from '../Config/Sort'
  15. import { IAxisConfig } from '../Workbench/ConfigSections/AxisSection'
  16. import { ISplitLineConfig } from '../Workbench/ConfigSections/SplitLineSection'
  17. import { IPivotConfig } from '../Workbench/ConfigSections/PivotSection'
  18. import { ILabelConfig } from '..//Workbench/ConfigSections/LabelSection'
  19. import { ISpecConfig } from '../Workbench/ConfigSections/SpecSection'
  20. import { ILegendConfig } from '../Workbench/ConfigSections/LegendSection'
  21. import { IVisualMapConfig } from '../Workbench/ConfigSections/VisualMapSection'
  22. import { IToolboxConfig } from '../Workbench/ConfigSections/ToolboxSection'
  23. import { IAreaSelectConfig } from '../Workbench/ConfigSections/AreaSelectSection'
  24. import { IScorecardConfig } from '../Workbench/ConfigSections/ScorecardSection'
  25. import { IGaugeConfig } from '../Workbench/ConfigSections/GaugeSection'
  26. import { IframeConfig } from '../Workbench/ConfigSections/IframeSection'
  27. import { ITableConfig } from '../Config/Table'
  28. import {
  29. IRichTextConfig,
  30. IBarConfig,
  31. IRadarConfig
  32. } from '../Workbench/ConfigSections'
  33. import { IDoubleYAxisConfig } from '../Workbench/ConfigSections/DoubleYAxisSection'
  34. import { IViewModel } from 'containers/View/types'
  35. import { IQueryVariableMap } from 'containers/Dashboard/types'
  36. import { IControl } from 'app/components/Control/types'
  37. import { RichTextNode } from 'app/components/RichText'
  38. import { IReference } from '../Workbench/Reference/types'
  39. import { ControlQueryMode } from 'app/components/Control/constants'
  40. import { ViewModelTypes } from 'containers/View/constants'
  41. const styles = require('../Pivot/Pivot.less')
  42. export type DimetionType = 'row' | 'col'
  43. export type RenderType =
  44. | 'rerender'
  45. | 'clear'
  46. | 'refresh'
  47. | 'resize'
  48. | 'loading'
  49. | 'select'
  50. | 'flush'
  51. export type WidgetMode = 'pivot' | 'chart'
  52. export type Coordinate = 'cartesian' | 'polar' | 'other'
  53. export interface IWidgetDimension {
  54. name: string
  55. field: IFieldConfig
  56. format: IFieldFormatConfig
  57. sort: IFieldSortConfig
  58. type?: ViewModelTypes
  59. }
  60. export interface IWidgetMetric {
  61. name: string
  62. agg: AggregatorType
  63. chart: IChartInfo
  64. field: IFieldConfig
  65. format: IFieldFormatConfig
  66. sort?: IFieldSortConfig
  67. }
  68. export interface IWidgetSecondaryMetric {
  69. name: string
  70. agg: AggregatorType
  71. field: IFieldConfig
  72. format: IFieldFormatConfig
  73. sort?: IFieldSortConfig
  74. from?: string
  75. type?: any
  76. visualType?: any
  77. }
  78. export interface IWidgetFilter {
  79. name: string
  80. type: DragType
  81. config: IDataParamConfig
  82. }
  83. export interface IChartStyles {
  84. pivot?: IPivotConfig
  85. xAxis?: IAxisConfig
  86. yAxis?: IAxisConfig
  87. axis?: IAxisConfig
  88. splitLine?: ISplitLineConfig
  89. label?: ILabelConfig
  90. legend?: ILegendConfig
  91. toolbox?: IToolboxConfig
  92. areaSelect?: IAreaSelectConfig
  93. spec?: ISpecConfig
  94. visualMap?: IVisualMapConfig
  95. scorecard?: IScorecardConfig
  96. gauge?: IGaugeConfig
  97. iframe?: IframeConfig
  98. table?: ITableConfig
  99. richText?: IRichTextConfig
  100. bar?: IBarConfig
  101. radar?: IRadarConfig
  102. doubleYAxis?: IDoubleYAxisConfig
  103. }
  104. export interface IChartRule {
  105. dimension: number | [number, number]
  106. metric: number | [number, number]
  107. }
  108. export interface IChartInfo {
  109. id: number
  110. name: string
  111. title: string
  112. icon: string
  113. coordinate: Coordinate
  114. rules: IChartRule[]
  115. dimetionAxis?: DimetionType
  116. data: object
  117. style: object
  118. }
  119. export interface IPaginationParams {
  120. pageNo: number
  121. pageSize: number
  122. totalCount: number
  123. withPaging: boolean
  124. }
  125. export interface IWidgetConfigBase {
  126. data: object[]
  127. cols: IWidgetDimension[]
  128. rows: IWidgetDimension[]
  129. metrics: IWidgetMetric[]
  130. secondaryMetrics?: IWidgetSecondaryMetric[]
  131. filters: IWidgetFilter[]
  132. chartStyles: IChartStyles
  133. selectedChart: number
  134. color?: IDataParamProperty
  135. label?: IDataParamProperty
  136. size?: IDataParamProperty
  137. xAxis?: IDataParamProperty
  138. tip?: IDataParamProperty
  139. yAxis?: IDataParamProperty
  140. dimetionAxis?: DimetionType
  141. renderType?: RenderType
  142. orders: Array<{ column: string; direction: string }>
  143. mode: WidgetMode
  144. model: IViewModel
  145. pagination?: IPaginationParams
  146. editing?: boolean
  147. queryVariables?: IQueryVariableMap
  148. references?: IReference[]
  149. computed?: any[]
  150. }
  151. export interface IWidgetProps extends IWidgetConfigBase {
  152. interacting?: boolean
  153. onCheckTableInteract?: () => boolean
  154. onDoInteract?: (triggerData: object) => void
  155. getDataDrillDetail?: (position: string) => void
  156. onPaginationChange?: (
  157. pageNo: number,
  158. pageSize: number,
  159. order?: { column: string; direction: string }
  160. ) => void
  161. onChartStylesChange?: (
  162. propPath: string[],
  163. value: string | RichTextNode[]
  164. ) => void
  165. isDrilling?: boolean
  166. whichDataDrillBrushed?: boolean | object[]
  167. selectedItems?: number[]
  168. onSelectChartsItems?: (selectedItems: number[]) => void
  169. // onHideDrillPanel?: (swtich: boolean) => void
  170. onError?: (error: Error) => void
  171. }
  172. export interface IWidgetConfig extends IWidgetConfigBase {
  173. controls: IControl[]
  174. limit: number
  175. cache: boolean
  176. expired: number
  177. autoLoadData: boolean
  178. queryMode: ControlQueryMode
  179. }
  180. export interface IWidgetWrapperProps extends IWidgetProps {
  181. loading?: boolean | JSX.Element
  182. empty?: boolean | JSX.Element
  183. }
  184. export interface IWidgetWrapperStates {
  185. width: number
  186. height: number
  187. }
  188. export class Widget extends React.Component<
  189. IWidgetWrapperProps,
  190. IWidgetWrapperStates
  191. > {
  192. public static defaultProps = {
  193. editing: false
  194. }
  195. constructor(props) {
  196. super(props)
  197. this.state = {
  198. width: 0,
  199. height: 0
  200. }
  201. }
  202. private container = createRef<HTMLDivElement>()
  203. private remeasureRenderTypes = [
  204. 'rerender',
  205. 'clear',
  206. 'refresh',
  207. 'resize',
  208. 'flush'
  209. ]
  210. public componentDidMount() {
  211. this.getContainerSize()
  212. }
  213. public componentWillReceiveProps(nextProps: IWidgetProps) {
  214. if (this.remeasureRenderTypes.includes(nextProps.renderType)) {
  215. this.getContainerSize()
  216. }
  217. }
  218. private getContainerSize = () => {
  219. const { offsetWidth, offsetHeight } = this.container
  220. .current as HTMLDivElement
  221. const { width, height } = this.state
  222. if (
  223. offsetWidth &&
  224. offsetHeight &&
  225. (offsetWidth !== width || offsetHeight !== height)
  226. ) {
  227. this.setState({
  228. width: offsetWidth,
  229. height: offsetHeight
  230. })
  231. }
  232. }
  233. public render() {
  234. const { loading, empty, ...rest } = this.props
  235. const { width, height } = this.state
  236. const widgetProps = { width, height, ...rest }
  237. let widgetContent: JSX.Element
  238. if (width && height) {
  239. // FIXME
  240. widgetContent =
  241. widgetProps.mode === 'chart' ? (
  242. <Chart {...widgetProps} />
  243. ) : (
  244. <Pivot {...widgetProps} />
  245. )
  246. }
  247. return (
  248. <div className={styles.wrapper} ref={this.container}>
  249. {widgetContent}
  250. {loading}
  251. {empty}
  252. </div>
  253. )
  254. }
  255. }
  256. export default Widget