Skip to content

Commit

Permalink
fix: annotation broken (#20651)
Browse files Browse the repository at this point in the history
* fix: annotation broken

* fix UT

* add annotation data to mixed timeseries chart
  • Loading branch information
zhaoyongjie committed Jul 11, 2022
1 parent 6ee9be2 commit 7f918a4
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const annotationsAndLayersControls: ControlPanelSectionConfig = {
label: '',
default: annotationLayers,
description: t('Annotation Layers'),
renderTrigger: true,
renderTrigger: false,
},
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ export function isTimeseriesAnnotationResult(
}

export function isRecordAnnotationResult(
result: AnnotationResult,
result: any,
): result is RecordAnnotationResult {
return 'columns' in result && 'records' in result;
return Array.isArray(result?.columns) && Array.isArray(result?.records);
}

export type AnnotationData = { [key: string]: AnnotationResult };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface ChartDataResponseResult {
/**
* Data for the annotation layer.
*/
annotation_data: AnnotationData[] | null;
annotation_data: AnnotationData | null;
cache_key: string | null;
cache_timeout: number | null;
cached_dttm: string | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ import {
getColtypesMapping,
getLegendProps,
} from '../utils/series';
import { extractAnnotationLabels } from '../utils/annotation';
import {
extractAnnotationLabels,
getAnnotationData,
} from '../utils/annotation';
import {
extractForecastSeriesContext,
extractForecastValuesFromTooltipParams,
Expand Down Expand Up @@ -81,11 +84,11 @@ export default function transformProps(
filterState,
datasource,
theme,
annotationData = {},
} = chartProps;
const { verboseMap = {} } = datasource;
const data1 = (queriesData[0].data || []) as TimeseriesDataRecord[];
const data2 = (queriesData[1].data || []) as TimeseriesDataRecord[];
const annotationData = getAnnotationData(chartProps);

const {
area,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ import {
extractDataTotalValues,
extractShowValueIndexes,
} from '../utils/series';
import { extractAnnotationLabels } from '../utils/annotation';
import {
extractAnnotationLabels,
getAnnotationData,
} from '../utils/annotation';
import {
extractForecastSeriesContext,
extractForecastSeriesContexts,
Expand Down Expand Up @@ -93,12 +96,12 @@ export default function transformProps(
queriesData,
datasource,
theme,
annotationData = {},
} = chartProps;
const { verboseMap = {} } = datasource;
const [queryData] = queriesData;
const { data = [] } = queryData as TimeseriesChartDataResponseResult;
const dataTypes = getColtypesMapping(queryData);
const annotationData = getAnnotationData(chartProps);

const {
area,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* specific language governing permissions and limitations
* under the License.
*/
import { isEmpty } from 'lodash';

import {
Annotation,
AnnotationData,
Expand All @@ -30,6 +32,8 @@ import {
isTimeseriesAnnotationResult,
TimeseriesDataRecord,
} from '@superset-ui/core';
import { EchartsTimeseriesChartProps } from '../types';
import { EchartsMixedTimeseriesProps } from '../MixedTimeseries/types';

export function evalFormula(
formula: FormulaAnnotationLayer,
Expand Down Expand Up @@ -130,3 +134,13 @@ export function extractAnnotationLabels(

return formulaAnnotationLabels.concat(timeseriesAnnotationLabels);
}

export function getAnnotationData(
chartProps: EchartsTimeseriesChartProps | EchartsMixedTimeseriesProps,
): AnnotationData {
const data = chartProps?.queriesData[0]?.annotation_data as AnnotationData;
if (!isEmpty(data)) {
return data;
}
return {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,60 +174,62 @@ describe('EchartsTimeseries transformProps', () => {
titleColumn: '',
value: 3,
};
const chartProps = new ChartProps({
...chartPropsConfig,
formData: {
...formData,
annotationLayers: [event, interval, timeseries],
const annotationData = {
'My Event': {
columns: [
'start_dttm',
'end_dttm',
'short_descr',
'long_descr',
'json_metadata',
],
records: [
{
start_dttm: 0,
end_dttm: 1000,
short_descr: '',
long_descr: '',
json_metadata: null,
},
],
},
annotationData: {
'My Event': {
columns: [
'start_dttm',
'end_dttm',
'short_descr',
'long_descr',
'json_metadata',
],
records: [
'My Interval': {
columns: ['start', 'end', 'title'],
records: [
{
start: 2000,
end: 3000,
title: 'My Title',
},
],
},
'My Timeseries': [
{
key: 'My Line',
values: [
{
start_dttm: 0,
end_dttm: 1000,
short_descr: '',
long_descr: '',
json_metadata: null,
x: 10000,
y: 11000,
},
],
},
'My Interval': {
columns: ['start', 'end', 'title'],
records: [
{
start: 2000,
end: 3000,
title: 'My Title',
x: 20000,
y: 21000,
},
],
},
'My Timeseries': [
{
key: 'My Line',
values: [
{
x: 10000,
y: 11000,
},
{
x: 20000,
y: 21000,
},
],
},
],
],
};
const chartProps = new ChartProps({
...chartPropsConfig,
formData: {
...formData,
annotationLayers: [event, interval, timeseries],
},
annotationData,
queriesData: [
{
...queriesData[0],
annotation_data: annotationData,
},
],
});
Expand Down

0 comments on commit 7f918a4

Please sign in to comment.