@@ -3,58 +3,56 @@ import { XYChart, PointSeries } from '@data-ui/xy-chart';
3
3
import { chartTheme , ChartTheme } from '@data-ui/theme' ;
4
4
import { Margin , Dimension } from '@superset-ui/dimension' ;
5
5
import { WithLegend } from '@superset-ui/chart-composition' ;
6
- import Encoder , { Encoding , ChannelOutput } from './Encoder' ;
7
- import { Dataset , PlainObject } from '../encodeable/types/Data' ;
8
- import { PartialSpec } from '../encodeable/types/Specification' ;
6
+ import { isFieldDef } from 'encodable/lib/typeGuards/ChannelDef' ;
7
+ import { Dataset , PlainObject } from 'encodable/lib/types/Data' ;
8
+ import {
9
+ scatterPlotEncoderFactory ,
10
+ ScatterPlotEncoder ,
11
+ ScatterPlotChannelOutputs ,
12
+ ScatterPlotEncodingConfig ,
13
+ ScatterPlotEncoding ,
14
+ } from './Encoder' ;
9
15
import createMarginSelector , { DEFAULT_MARGIN } from '../utils2/createMarginSelector' ;
10
16
import DefaultTooltipRenderer from './DefaultTooltipRenderer' ;
11
17
import convertScaleToDataUIScale from '../utils/convertScaleToDataUIScaleShape' ;
12
- import { isScaleFieldDef } from '../encodeable/types/ChannelDef' ;
13
- import createXYChartLayoutWithTheme from '../utils/createXYChartLayoutWithTheme' ;
14
- import createEncoderSelector from '../encodeable/createEncoderSelector' ;
15
- import createRenderLegend from '../components/legend/createRenderLegend' ;
16
- import { LegendHooks } from '../components/legend/types' ;
18
+ import createXYChartLayoutWithTheme from '../utils2/createXYChartLayoutWithTheme' ;
19
+ import createRenderLegend from '../components/legend2/createRenderLegend' ;
20
+ import { LegendHooks } from '../components/legend2/types' ;
17
21
18
22
export interface TooltipProps {
19
23
datum : EncodedPoint ;
20
- encoder : Encoder ;
24
+ encoder : ScatterPlotEncoder ;
21
25
}
22
26
23
27
const defaultProps = {
24
28
className : '' ,
25
29
margin : DEFAULT_MARGIN ,
30
+ encoding : { } ,
26
31
theme : chartTheme ,
27
32
TooltipRenderer : DefaultTooltipRenderer ,
28
33
} as const ;
29
34
30
35
export type HookProps = {
31
36
TooltipRenderer ?: React . ComponentType < TooltipProps > ;
32
- } & LegendHooks < Encoder > ;
37
+ } & LegendHooks < ScatterPlotEncodingConfig > ;
33
38
34
39
type Props = {
35
40
className ?: string ;
36
41
width : string | number ;
37
42
height : string | number ;
38
43
margin ?: Margin ;
39
44
data : Dataset ;
45
+ encoding ?: Partial < ScatterPlotEncoding > ;
40
46
theme ?: ChartTheme ;
41
- } & PartialSpec < Encoding > &
42
- HookProps &
47
+ } & HookProps &
43
48
Readonly < typeof defaultProps > ;
44
49
45
- export interface EncodedPoint {
46
- x : ChannelOutput < 'x' > ;
47
- y : ChannelOutput < 'y' > ;
48
- size : ChannelOutput < 'size' > ;
49
- fill : ChannelOutput < 'fill' > ;
50
- stroke : ChannelOutput < 'stroke' > ;
51
- group : ChannelOutput < 'group' > [ ] ;
52
- tooltip : ChannelOutput < 'tooltip' > [ ] ;
50
+ export type EncodedPoint = ScatterPlotChannelOutputs & {
53
51
data : PlainObject ;
54
- }
52
+ } ;
55
53
56
54
export default class ScatterPlot extends PureComponent < Props > {
57
- private createEncoder = createEncoderSelector ( Encoder ) ;
55
+ private createEncoder = scatterPlotEncoderFactory . createSelector ( ) ;
58
56
59
57
private createMargin = createMarginSelector ( ) ;
60
58
@@ -68,34 +66,18 @@ export default class ScatterPlot extends PureComponent<Props> {
68
66
69
67
renderChart ( dim : Dimension ) {
70
68
const { width, height } = dim ;
71
- const { data, margin, theme, TooltipRenderer } = this . props ;
72
- const encoder = this . createEncoder ( this . props ) ;
69
+ const { data, margin, theme, TooltipRenderer, encoding } = this . props ;
70
+ const encoder = this . createEncoder ( encoding ) ;
73
71
const { channels } = encoder ;
74
72
75
- if ( typeof channels . x . scale !== 'undefined' ) {
76
- const xDomain = channels . x . getDomain ( data ) ;
77
- channels . x . scale . setDomain ( xDomain ) ;
78
- }
79
- if ( typeof channels . y . scale !== 'undefined' ) {
80
- const yDomain = channels . y . getDomain ( data ) ;
81
- channels . y . scale . setDomain ( yDomain ) ;
82
- }
83
- if (
84
- isScaleFieldDef ( channels . size . definition ) &&
85
- channels . size . definition . type === 'quantitative'
86
- ) {
87
- const domain = channels . size . getDomain ( data ) as number [ ] ;
88
- const [ min , max ] = domain ;
89
- const adjustedDomain = [ Math . min ( min || 0 , 0 ) , Math . max ( max || 1 , 1 ) ] ;
90
- channels . size . scale ! . setDomain ( adjustedDomain ) ;
91
- }
73
+ encoder . setDomainFromDataset ( data ) ;
92
74
93
75
const encodedData = data . map ( d => ( {
94
- x : channels . x . get ( d ) ,
95
- y : channels . y . get ( d ) ,
96
- size : channels . size . encode ( d ) ,
97
- fill : channels . fill . encode ( d ) ,
98
- stroke : channels . stroke . encode ( d ) ,
76
+ x : channels . x . encodeDatum ( d ) ,
77
+ y : channels . y . encodeDatum ( d ) ,
78
+ size : channels . size . encodeDatum ( d ) ,
79
+ fill : channels . fill . encodeDatum ( d ) ,
80
+ stroke : channels . stroke . encodeDatum ( d ) ,
99
81
data : d ,
100
82
} ) ) ;
101
83
@@ -119,13 +101,13 @@ export default class ScatterPlot extends PureComponent<Props> {
119
101
< TooltipRenderer datum = { datum } encoder = { encoder } />
120
102
) }
121
103
theme = { theme }
122
- xScale = { convertScaleToDataUIScale ( channels . x . scale ! . config ) }
123
- yScale = { convertScaleToDataUIScale ( channels . y . scale ! . config ) }
104
+ xScale = { convertScaleToDataUIScale ( channels . x . definition . scale as any ) }
105
+ yScale = { convertScaleToDataUIScale ( channels . y . definition . scale as any ) }
124
106
>
125
107
{ layout . renderXAxis ( ) }
126
108
{ layout . renderYAxis ( ) }
127
109
< PointSeries
128
- key = { channels . x . definition . field }
110
+ key = { isFieldDef ( channels . x . definition ) ? channels . x . definition . field : '' }
129
111
data = { encodedData }
130
112
fill = { ( d : EncodedPoint ) => d . fill }
131
113
fillOpacity = { 0.5 }
@@ -137,9 +119,9 @@ export default class ScatterPlot extends PureComponent<Props> {
137
119
}
138
120
139
121
render ( ) {
140
- const { className, data, width, height } = this . props ;
122
+ const { className, data, width, height, encoding } = this . props ;
141
123
142
- const encoder = this . createEncoder ( this . props ) ;
124
+ const encoder = this . createEncoder ( encoding ) ;
143
125
144
126
return (
145
127
< WithLegend
0 commit comments