diff --git a/src/plugins/vis_type_timeseries/common/empty_label.ts b/src/plugins/vis_type_timeseries/common/empty_label.ts new file mode 100644 index 0000000000000..d55a58f17dbf3 --- /dev/null +++ b/src/plugins/vis_type_timeseries/common/empty_label.ts @@ -0,0 +1,13 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { i18n } from '@kbn/i18n'; + +export const emptyLabel = i18n.translate('visTypeTimeseries.emptyTextValue', { + defaultMessage: '(empty)', +}); diff --git a/src/plugins/vis_type_timeseries/public/application/components/lib/convert_series_to_vars.js b/src/plugins/vis_type_timeseries/public/application/components/lib/convert_series_to_vars.js index 3e5de5ea0f14c..df9057456fdd6 100644 --- a/src/plugins/vis_type_timeseries/public/application/components/lib/convert_series_to_vars.js +++ b/src/plugins/vis_type_timeseries/public/application/components/lib/convert_series_to_vars.js @@ -9,6 +9,7 @@ import { set } from '@elastic/safer-lodash-set'; import _ from 'lodash'; import { getLastValue } from '../../../../common/get_last_value'; +import { emptyLabel } from '../../../../common/empty_label'; import { createTickFormatter } from './tick_formatter'; import { labelDateFormatter } from './label_date_formatter'; import moment from 'moment'; @@ -19,7 +20,8 @@ export const convertSeriesToVars = (series, model, dateFormat = 'lll', getConfig series .filter((row) => _.startsWith(row.id, seriesModel.id)) .forEach((row) => { - const varName = [_.snakeCase(row.label), _.snakeCase(seriesModel.var_name)] + const label = row.label ? _.snakeCase(row.label) : emptyLabel; + const varName = [label, _.snakeCase(seriesModel.var_name)] .filter((v) => v) .join('.'); @@ -43,7 +45,7 @@ export const convertSeriesToVars = (series, model, dateFormat = 'lll', getConfig }, }; set(variables, varName, data); - set(variables, `${_.snakeCase(row.label)}.label`, row.label); + set(variables, `${label}.label`, row.label); /** * Handle the case when a field has "key_as_string" value. @@ -54,7 +56,7 @@ export const convertSeriesToVars = (series, model, dateFormat = 'lll', getConfig */ if (row.labelFormatted) { const val = labelDateFormatter(row.labelFormatted, dateFormat); - set(variables, `${_.snakeCase(row.label)}.formatted`, val); + set(variables, `${label}.formatted`, val); } }); }); diff --git a/src/plugins/vis_type_timeseries/public/application/components/lib/replace_vars.js b/src/plugins/vis_type_timeseries/public/application/components/lib/replace_vars.js index 50cf135bbc8e9..acfaf51c73d00 100644 --- a/src/plugins/vis_type_timeseries/public/application/components/lib/replace_vars.js +++ b/src/plugins/vis_type_timeseries/public/application/components/lib/replace_vars.js @@ -8,11 +8,13 @@ import _ from 'lodash'; import handlebars from 'handlebars/dist/handlebars'; +import { emptyLabel } from '../../../../common/empty_label'; import { i18n } from '@kbn/i18n'; export function replaceVars(str, args = {}, vars = {}) { try { - const template = handlebars.compile(str, { strict: true, knownHelpersOnly: true }); + // we need add '[]' for emptyLabel because this value contains special characters. (https://handlebarsjs.com/guide/expressions.html#literal-segments) + const template = handlebars.compile(str.replace(emptyLabel, `[${emptyLabel}]`), { strict: true, knownHelpersOnly: true }); const string = template(_.assign({}, vars, { args })); diff --git a/src/plugins/vis_type_timeseries/public/application/components/vis_types/table/vis.js b/src/plugins/vis_type_timeseries/public/application/components/vis_types/table/vis.js index 24d0ca1b588f7..bf1622bafd90a 100644 --- a/src/plugins/vis_type_timeseries/public/application/components/vis_types/table/vis.js +++ b/src/plugins/vis_type_timeseries/public/application/components/vis_types/table/vis.js @@ -18,6 +18,7 @@ import { replaceVars } from '../../lib/replace_vars'; import { fieldFormats } from '../../../../../../../plugins/data/public'; import { FormattedMessage } from '@kbn/i18n/react'; import { getFieldFormats, getCoreStart } from '../../../../services'; +import { emptyLabel } from '../../../../../common/empty_label'; function getColor(rules, colorKey, value) { let color; @@ -90,10 +91,7 @@ class TableVis extends Component { return (