Skip to content

Commit 9e907be

Browse files
zhaoyongjieeschutho
authored andcommitted
fix: annotation broken (apache#20651)
* fix: annotation broken * fix UT * add annotation data to mixed timeseries chart (cherry picked from commit 7f918a4)
1 parent 61b6449 commit 9e907be

File tree

7 files changed

+73
-51
lines changed

7 files changed

+73
-51
lines changed

superset-frontend/packages/superset-ui-chart-controls/src/sections/annotationsAndLayers.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const annotationsAndLayersControls: ControlPanelSectionConfig = {
3434
label: '',
3535
default: annotationLayers,
3636
description: t('Annotation Layers'),
37-
renderTrigger: true,
37+
renderTrigger: false,
3838
},
3939
},
4040
],

superset-frontend/packages/superset-ui-core/src/query/types/AnnotationLayer.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ export function isTimeseriesAnnotationResult(
178178
}
179179

180180
export function isRecordAnnotationResult(
181-
result: AnnotationResult,
181+
result: any,
182182
): result is RecordAnnotationResult {
183-
return 'columns' in result && 'records' in result;
183+
return Array.isArray(result?.columns) && Array.isArray(result?.records);
184184
}
185185

186186
export type AnnotationData = { [key: string]: AnnotationResult };

superset-frontend/packages/superset-ui-core/src/query/types/QueryResponse.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export interface ChartDataResponseResult {
4747
/**
4848
* Data for the annotation layer.
4949
*/
50-
annotation_data: AnnotationData[] | null;
50+
annotation_data: AnnotationData | null;
5151
cache_key: string | null;
5252
cache_timeout: number | null;
5353
cached_dttm: string | null;

superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/transformProps.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ import {
5050
extractDataTotalValues,
5151
extractShowValueIndexes,
5252
} from '../utils/series';
53-
import { extractAnnotationLabels } from '../utils/annotation';
53+
import {
54+
extractAnnotationLabels,
55+
getAnnotationData,
56+
} from '../utils/annotation';
5457
import {
5558
extractForecastSeriesContext,
5659
extractForecastValuesFromTooltipParams,
@@ -83,11 +86,11 @@ export default function transformProps(
8386
filterState,
8487
datasource,
8588
theme,
86-
annotationData = {},
8789
} = chartProps;
8890
const { verboseMap = {} } = datasource;
8991
const data1 = (queriesData[0].data || []) as TimeseriesDataRecord[];
9092
const data2 = (queriesData[1].data || []) as TimeseriesDataRecord[];
93+
const annotationData = getAnnotationData(chartProps);
9194

9295
const {
9396
area,

superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ import {
5555
extractDataTotalValues,
5656
extractShowValueIndexes,
5757
} from '../utils/series';
58-
import { extractAnnotationLabels } from '../utils/annotation';
58+
import {
59+
extractAnnotationLabels,
60+
getAnnotationData,
61+
} from '../utils/annotation';
5962
import {
6063
extractForecastSeriesContext,
6164
extractForecastSeriesContexts,
@@ -93,12 +96,12 @@ export default function transformProps(
9396
queriesData,
9497
datasource,
9598
theme,
96-
annotationData = {},
9799
} = chartProps;
98100
const { verboseMap = {} } = datasource;
99101
const [queryData] = queriesData;
100102
const { data = [] } = queryData as TimeseriesChartDataResponseResult;
101103
const dataTypes = getColtypesMapping(queryData);
104+
const annotationData = getAnnotationData(chartProps);
102105

103106
const {
104107
area,

superset-frontend/plugins/plugin-chart-echarts/src/utils/annotation.ts

+14
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* specific language governing permissions and limitations
1818
* under the License.
1919
*/
20+
import { isEmpty } from 'lodash';
21+
2022
import {
2123
Annotation,
2224
AnnotationData,
@@ -30,6 +32,8 @@ import {
3032
isTimeseriesAnnotationResult,
3133
TimeseriesDataRecord,
3234
} from '@superset-ui/core';
35+
import { EchartsTimeseriesChartProps } from '../types';
36+
import { EchartsMixedTimeseriesProps } from '../MixedTimeseries/types';
3337

3438
export function evalFormula(
3539
formula: FormulaAnnotationLayer,
@@ -130,3 +134,13 @@ export function extractAnnotationLabels(
130134

131135
return formulaAnnotationLabels.concat(timeseriesAnnotationLabels);
132136
}
137+
138+
export function getAnnotationData(
139+
chartProps: EchartsTimeseriesChartProps | EchartsMixedTimeseriesProps,
140+
): AnnotationData {
141+
const data = chartProps?.queriesData[0]?.annotation_data as AnnotationData;
142+
if (!isEmpty(data)) {
143+
return data;
144+
}
145+
return {};
146+
}

superset-frontend/plugins/plugin-chart-echarts/test/Timeseries/transformProps.test.ts

+45-43
Original file line numberDiff line numberDiff line change
@@ -174,60 +174,62 @@ describe('EchartsTimeseries transformProps', () => {
174174
titleColumn: '',
175175
value: 3,
176176
};
177-
const chartProps = new ChartProps({
178-
...chartPropsConfig,
179-
formData: {
180-
...formData,
181-
annotationLayers: [event, interval, timeseries],
177+
const annotationData = {
178+
'My Event': {
179+
columns: [
180+
'start_dttm',
181+
'end_dttm',
182+
'short_descr',
183+
'long_descr',
184+
'json_metadata',
185+
],
186+
records: [
187+
{
188+
start_dttm: 0,
189+
end_dttm: 1000,
190+
short_descr: '',
191+
long_descr: '',
192+
json_metadata: null,
193+
},
194+
],
182195
},
183-
annotationData: {
184-
'My Event': {
185-
columns: [
186-
'start_dttm',
187-
'end_dttm',
188-
'short_descr',
189-
'long_descr',
190-
'json_metadata',
191-
],
192-
records: [
196+
'My Interval': {
197+
columns: ['start', 'end', 'title'],
198+
records: [
199+
{
200+
start: 2000,
201+
end: 3000,
202+
title: 'My Title',
203+
},
204+
],
205+
},
206+
'My Timeseries': [
207+
{
208+
key: 'My Line',
209+
values: [
193210
{
194-
start_dttm: 0,
195-
end_dttm: 1000,
196-
short_descr: '',
197-
long_descr: '',
198-
json_metadata: null,
211+
x: 10000,
212+
y: 11000,
199213
},
200-
],
201-
},
202-
'My Interval': {
203-
columns: ['start', 'end', 'title'],
204-
records: [
205214
{
206-
start: 2000,
207-
end: 3000,
208-
title: 'My Title',
215+
x: 20000,
216+
y: 21000,
209217
},
210218
],
211219
},
212-
'My Timeseries': [
213-
{
214-
key: 'My Line',
215-
values: [
216-
{
217-
x: 10000,
218-
y: 11000,
219-
},
220-
{
221-
x: 20000,
222-
y: 21000,
223-
},
224-
],
225-
},
226-
],
220+
],
221+
};
222+
const chartProps = new ChartProps({
223+
...chartPropsConfig,
224+
formData: {
225+
...formData,
226+
annotationLayers: [event, interval, timeseries],
227227
},
228+
annotationData,
228229
queriesData: [
229230
{
230231
...queriesData[0],
232+
annotation_data: annotationData,
231233
},
232234
],
233235
});

0 commit comments

Comments
 (0)