diff --git a/x-pack/legacy/plugins/lens/public/metric_visualization_plugin/metric_expression.test.tsx b/x-pack/legacy/plugins/lens/public/metric_visualization_plugin/metric_expression.test.tsx index 81f8a0fbce210..b8c646be91e52 100644 --- a/x-pack/legacy/plugins/lens/public/metric_visualization_plugin/metric_expression.test.tsx +++ b/x-pack/legacy/plugins/lens/public/metric_visualization_plugin/metric_expression.test.tsx @@ -48,7 +48,8 @@ describe('metric_expression', () => { test('it renders the title and value', () => { const { data, args } = sampleArgs(); - expect(shallow()).toMatchInlineSnapshot(` + expect(shallow( x} />)) + .toMatchInlineSnapshot(`
; -export const metricChartRenderer: RenderFunction = { +export const getMetricChartRenderer = ( + formatFactory: FormatFactory +): RenderFunction => ({ name: 'lens_metric_chart_renderer', displayName: 'Metric Chart', help: 'Metric Chart Renderer', validate: () => {}, reuseDomNode: true, render: async (domNode: Element, config: MetricChartProps, _handlers: unknown) => { - ReactDOM.render(, domNode); + ReactDOM.render(, domNode); }, -}; +}); -export function MetricChart({ data, args }: MetricChartProps) { +export function MetricChart({ + data, + args, + formatFactory, +}: MetricChartProps & { formatFactory: FormatFactory }) { const { title, accessor } = args; - const [row] = Object.values(data.tables)[0].rows; - // TODO: Use field formatters here... - const value = Number(Number(row[accessor]).toFixed(3)).toString(); + let value = '-'; + const firstTable = Object.values(data.tables)[0]; + + if (firstTable) { + const column = firstTable.columns[0]; + const row = firstTable.rows[0]; + if (row[accessor]) { + value = + column && column.formatHint + ? formatFactory(column.formatHint).convert(row[accessor]) + : Number(Number(row[accessor]).toFixed(3)).toString(); + } + } return (
metricChart); - interpreter.renderersRegistry.register(() => metricChartRenderer as RenderFunction); + interpreter.renderersRegistry.register( + () => getMetricChartRenderer(fieldFormat.formatFactory) as RenderFunction + ); return metricVisualization; } @@ -65,6 +76,9 @@ export const metricVisualizationSetup = () => renderersRegistry, functionsRegistry, }, + fieldFormat: { + formatFactory: getFormat, + }, }); export const metricVisualizationStop = () => plugin.stop();