Skip to content

Commit 19d6b79

Browse files
committed
[ML] Fix special character escaping for Vega scatterplot matrix.
1 parent 58d4164 commit 19d6b79

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

x-pack/plugins/ml/public/application/components/scatterplot_matrix/scatterplot_matrix_vega_lite_spec.ts

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,26 +59,11 @@ export const getColorSpec = (
5959
return { value: DEFAULT_COLOR };
6060
};
6161

62-
// Replace dots in field names with an alternative UTF-8 character
63-
// since VEGA treats dots in field names as nested values and escaping
64-
// in columns/rows for repeated charts isn't working as expected.
62+
// Escapes the characters .[] in field names with double backslashes
63+
// since VEGA treats dots/brackets in field names as nested values.
64+
// See https://vega.github.io/vega-lite/docs/field.html for details.
6565
function getEscapedVegaFieldName(fieldName: string) {
66-
return fieldName.replace(/\./g, '․');
67-
}
68-
69-
// Replace dots for all keys of all data items with an alternative UTF-8 character
70-
// since VEGA treats dots in field names as nested values and escaping
71-
// in columns/rows for repeated charts isn't working as expected.
72-
function getEscapedVegaValues(values: VegaValue[]): VegaValue[] {
73-
return values.map((d) =>
74-
Object.keys(d).reduce(
75-
(p, c) => ({
76-
...p,
77-
[getEscapedVegaFieldName(c)]: d[c],
78-
}),
79-
{} as VegaValue
80-
)
81-
);
66+
return fieldName.replace(/([\.|\[|\]])/g, '\\$1');
8267
}
8368

8469
type VegaValue = Record<string, string | number>;
@@ -92,7 +77,7 @@ export const getScatterplotMatrixVegaLiteSpec = (
9277
legendType?: LegendType,
9378
dynamicSize?: boolean
9479
): TopLevelSpec => {
95-
const vegaValues = getEscapedVegaValues(values);
80+
const vegaValues = values;
9681
const vegaColumns = columns.map(getEscapedVegaFieldName);
9782
const outliers = resultsField !== undefined;
9883

@@ -193,7 +178,10 @@ export const getScatterplotMatrixVegaLiteSpec = (
193178
...(color !== undefined
194179
? [{ type: colorSpec.type, field: getEscapedVegaFieldName(color) }]
195180
: []),
196-
...vegaColumns.map((d) => ({ type: LEGEND_TYPES.QUANTITATIVE, field: d })),
181+
...vegaColumns.map((d) => ({
182+
type: LEGEND_TYPES.QUANTITATIVE,
183+
field: d,
184+
})),
197185
...(outliers
198186
? [{ type: LEGEND_TYPES.QUANTITATIVE, field: escapedOutlierScoreField, format: '.3f' }]
199187
: []),

x-pack/plugins/ml/public/application/components/vega_chart/vega_chart.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77

88
import React, { FC, Suspense } from 'react';
99

10+
import { EuiErrorBoundary } from '@elastic/eui';
11+
1012
import { VegaChartLoading } from './vega_chart_loading';
1113
import type { VegaChartViewProps } from './vega_chart_view';
1214

1315
const VegaChartView = React.lazy(() => import('./vega_chart_view'));
1416

1517
export const VegaChart: FC<VegaChartViewProps> = (props) => (
16-
<Suspense fallback={<VegaChartLoading />}>
17-
<VegaChartView {...props} />
18-
</Suspense>
18+
<EuiErrorBoundary>
19+
<Suspense fallback={<VegaChartLoading />}>
20+
<VegaChartView {...props} />
21+
</Suspense>
22+
</EuiErrorBoundary>
1923
);

0 commit comments

Comments
 (0)