Skip to content

Commit 39ee6cc

Browse files
committed
Refactor deck.gl charts to new API; Fix Multi Layer results error
1 parent 8406a82 commit 39ee6cc

39 files changed

+2400
-34
lines changed

superset-frontend/plugins/legacy-preset-chart-deckgl/src/CategoricalDeckGLContainer.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,12 @@ const CategoricalDeckGLContainer = (props: CategoricalDeckGLContainerProps) => {
169169
}));
170170
}
171171
case COLOR_SCHEME_TYPES.color_breakpoints: {
172-
const defaultBreakpointColor = fd.deafult_breakpoint_color
172+
const defaultBreakpointColor = fd.default_breakpoint_color
173173
? [
174-
fd.deafult_breakpoint_color.r,
175-
fd.deafult_breakpoint_color.g,
176-
fd.deafult_breakpoint_color.b,
177-
fd.deafult_breakpoint_color.a * 255,
174+
fd.default_breakpoint_color.r,
175+
fd.default_breakpoint_color.g,
176+
fd.default_breakpoint_color.b,
177+
fd.default_breakpoint_color.a * 255,
178178
]
179179
: [
180180
DEFAULT_DECKGL_COLOR.r,
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
import {
20+
buildQueryContext,
21+
ensureIsArray,
22+
SqlaFormData,
23+
} from '@superset-ui/core';
24+
import {
25+
getSpatialColumns,
26+
addSpatialNullFilters,
27+
SpatialFormData,
28+
} from '../spatialUtils';
29+
30+
export interface DeckArcFormData extends SqlaFormData {
31+
start_spatial: SpatialFormData['spatial'];
32+
end_spatial: SpatialFormData['spatial'];
33+
dimension?: string;
34+
js_columns?: string[];
35+
}
36+
37+
export default function buildQuery(formData: DeckArcFormData) {
38+
const { start_spatial, end_spatial, dimension, js_columns } = formData;
39+
40+
if (!start_spatial || !end_spatial) {
41+
throw new Error(
42+
'Start and end spatial configurations are required for Arc charts',
43+
);
44+
}
45+
46+
return buildQueryContext(formData, baseQueryObject => {
47+
const startSpatialColumns = getSpatialColumns(start_spatial);
48+
const endSpatialColumns = getSpatialColumns(end_spatial);
49+
50+
let columns = [
51+
...(baseQueryObject.columns || []),
52+
...startSpatialColumns,
53+
...endSpatialColumns,
54+
];
55+
56+
if (dimension) {
57+
columns = [...columns, dimension];
58+
}
59+
60+
// Add js_columns to ensure they're available for JavaScript functions
61+
const jsColumns = ensureIsArray(js_columns || []);
62+
jsColumns.forEach(col => {
63+
if (!columns.includes(col)) {
64+
columns.push(col);
65+
}
66+
});
67+
68+
let filters = addSpatialNullFilters(
69+
start_spatial,
70+
ensureIsArray(baseQueryObject.filters || []),
71+
);
72+
filters = addSpatialNullFilters(end_spatial, filters);
73+
74+
const isTimeseries = !!formData.time_grain_sqla;
75+
76+
return [
77+
{
78+
...baseQueryObject,
79+
columns,
80+
filters,
81+
is_timeseries: isTimeseries,
82+
row_limit: baseQueryObject.row_limit,
83+
},
84+
];
85+
});
86+
}

superset-frontend/plugins/legacy-preset-chart-deckgl/src/layers/Arc/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
import { t, ChartMetadata, ChartPlugin, Behavior } from '@superset-ui/core';
2020
import thumbnail from './images/thumbnail.png';
2121
import example from './images/example.png';
22-
import transformProps from '../../transformProps';
22+
import transformProps from './transformProps';
23+
import buildQuery from './buildQuery';
2324
import controlPanel from './controlPanel';
2425

2526
const metadata = new ChartMetadata({
@@ -36,13 +37,13 @@ const metadata = new ChartMetadata({
3637
name: t('deck.gl Arc'),
3738
thumbnail,
3839
exampleGallery: [{ url: example }],
39-
useLegacyApi: true,
4040
tags: [t('deckGL'), t('Geo'), t('3D'), t('Relational'), t('Web')],
4141
});
4242

4343
export default class ArcChartPlugin extends ChartPlugin {
4444
constructor() {
4545
super({
46+
buildQuery,
4647
loadChart: () => import('./Arc'),
4748
controlPanel,
4849
metadata,
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
import { ChartProps } from '@superset-ui/core';
20+
import {
21+
processSpatialData,
22+
getMapboxApiKey,
23+
addJsColumnsToExtraProps,
24+
DataRecord,
25+
} from '../spatialUtils';
26+
import { DeckArcFormData } from './buildQuery';
27+
28+
const NOOP = () => {};
29+
30+
interface ArcPoint {
31+
sourcePosition: [number, number];
32+
targetPosition: [number, number];
33+
cat_color?: string;
34+
__timestamp?: number;
35+
extraProps?: Record<string, unknown>;
36+
[key: string]: unknown;
37+
}
38+
39+
function processArcData(
40+
records: DataRecord[],
41+
startSpatial: DeckArcFormData['start_spatial'],
42+
endSpatial: DeckArcFormData['end_spatial'],
43+
dimension?: string,
44+
jsColumns?: string[],
45+
): ArcPoint[] {
46+
if (!startSpatial || !endSpatial || !records.length) {
47+
return [];
48+
}
49+
50+
const startFeatures = processSpatialData(records, startSpatial);
51+
const endFeatures = processSpatialData(records, endSpatial);
52+
53+
return records
54+
.map((record, index) => {
55+
const startFeature = startFeatures[index];
56+
const endFeature = endFeatures[index];
57+
58+
if (!startFeature || !endFeature) {
59+
return null;
60+
}
61+
62+
let arcPoint: ArcPoint = {
63+
sourcePosition: startFeature.position,
64+
targetPosition: endFeature.position,
65+
extraProps: {},
66+
};
67+
68+
arcPoint = addJsColumnsToExtraProps(arcPoint, record, jsColumns);
69+
70+
if (dimension && record[dimension] != null) {
71+
arcPoint.cat_color = String(record[dimension]);
72+
}
73+
74+
// eslint-disable-next-line no-underscore-dangle
75+
if (record.__timestamp != null) {
76+
// eslint-disable-next-line no-underscore-dangle
77+
arcPoint.__timestamp = Number(record.__timestamp);
78+
}
79+
Object.keys(record).forEach(key => {
80+
if (
81+
key !== '__timestamp' &&
82+
key !== dimension &&
83+
!(jsColumns || []).includes(key)
84+
) {
85+
arcPoint[key] = record[key];
86+
}
87+
});
88+
89+
return arcPoint;
90+
})
91+
.filter((point): point is ArcPoint => point !== null);
92+
}
93+
94+
export default function transformProps(chartProps: ChartProps) {
95+
const {
96+
datasource,
97+
height,
98+
hooks,
99+
queriesData,
100+
rawFormData: formData,
101+
width,
102+
filterState,
103+
emitCrossFilters,
104+
} = chartProps;
105+
106+
const {
107+
onAddFilter = NOOP,
108+
onContextMenu = NOOP,
109+
setControlValue = NOOP,
110+
setDataMask = NOOP,
111+
} = hooks;
112+
113+
const { start_spatial, end_spatial, dimension, js_columns } =
114+
formData as DeckArcFormData;
115+
116+
const queryData = queriesData[0];
117+
const records = queryData?.data || [];
118+
const features = processArcData(
119+
records,
120+
start_spatial,
121+
end_spatial,
122+
dimension,
123+
js_columns,
124+
);
125+
126+
return {
127+
datasource,
128+
emitCrossFilters,
129+
formData,
130+
height,
131+
onAddFilter,
132+
onContextMenu,
133+
payload: {
134+
...queryData,
135+
data: {
136+
features,
137+
mapboxApiKey: getMapboxApiKey(),
138+
},
139+
},
140+
setControlValue,
141+
filterState,
142+
viewport: {
143+
...formData.viewport,
144+
height,
145+
width,
146+
},
147+
width,
148+
setDataMask,
149+
setTooltip: () => {},
150+
};
151+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
import { SpatialFormData, buildSpatialQuery } from '../spatialUtils';
20+
21+
export interface DeckContourFormData extends SpatialFormData {
22+
cellSize?: string;
23+
aggregation?: string;
24+
contours?: Array<{
25+
color: { r: number; g: number; b: number };
26+
lowerThreshold: number;
27+
upperThreshold?: number;
28+
strokeWidth?: number;
29+
}>;
30+
}
31+
32+
export default function buildQuery(formData: DeckContourFormData) {
33+
return buildSpatialQuery(formData);
34+
}

superset-frontend/plugins/legacy-preset-chart-deckgl/src/layers/Contour/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
* under the License.
1818
*/
1919
import { t, ChartMetadata, ChartPlugin, Behavior } from '@superset-ui/core';
20-
import transformProps from '../../transformProps';
21-
import controlPanel from './controlPanel';
2220
import thumbnail from './images/thumbnail.png';
2321
import example from './images/example.png';
22+
import buildQuery from './buildQuery';
23+
import transformProps from './transformProps';
24+
import controlPanel from './controlPanel';
2425

2526
const metadata = new ChartMetadata({
2627
category: t('Map'),
@@ -31,14 +32,14 @@ const metadata = new ChartMetadata({
3132
exampleGallery: [{ url: example }],
3233
name: t('deck.gl Contour'),
3334
thumbnail,
34-
useLegacyApi: true,
3535
tags: [t('deckGL'), t('Spatial'), t('Comparison')],
3636
behaviors: [Behavior.InteractiveChart],
3737
});
3838

3939
export default class ContourChartPlugin extends ChartPlugin {
4040
constructor() {
4141
super({
42+
buildQuery,
4243
loadChart: () => import('./Contour'),
4344
controlPanel,
4445
metadata,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
import { transformSpatialProps } from '../spatialUtils';
20+
21+
// Use the generic spatial transform function directly
22+
export default transformSpatialProps;

superset-frontend/plugins/legacy-preset-chart-deckgl/src/layers/Grid/Grid.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export const getLayer: GetLayerType<GridLayer> = function ({
7272

7373
const colorSchemeType = fd.color_scheme_type;
7474
const colorRange = getColorRange({
75-
defaultBreakpointsColor: fd.deafult_breakpoint_color,
75+
defaultBreakpointsColor: fd.default_breakpoint_color,
7676
colorSchemeType,
7777
colorScale,
7878
colorBreakpoints,

0 commit comments

Comments
 (0)