diff --git a/e2e/screenshots/metric_stories.test.ts-snapshots/metric/text-value-with-trend-chrome-linux.png b/e2e/screenshots/metric_stories.test.ts-snapshots/metric/text-value-with-trend-chrome-linux.png new file mode 100644 index 0000000000..4342dde16f Binary files /dev/null and b/e2e/screenshots/metric_stories.test.ts-snapshots/metric/text-value-with-trend-chrome-linux.png differ diff --git a/e2e/tests/metric_stories.test.ts b/e2e/tests/metric_stories.test.ts index 367710e5f0..02cf078b4e 100644 --- a/e2e/tests/metric_stories.test.ts +++ b/e2e/tests/metric_stories.test.ts @@ -36,4 +36,9 @@ test.describe('Metric', () => { 'http://localhost:9001/?path=/story/metric-alpha--grid&globals=theme:eui-dark&knob-layout=grid&knob-max trend data points=30&knob-progress bar direction=horizontal&knob-use progress bar=', ); }); + test('text value with trend', async ({ page }) => { + await common.expectChartAtUrlToMatchScreenshot(page)( + 'http://localhost:9001/?path=/story/metric-alpha--basic&globals=theme:light&knob-EUI icon glyph name=warning&knob-color=rgba(166, 219, 208, 0.47)&knob-extra=1310 (-74% week before)&knob-is numeric metric=false&knob-progress bar direction=vertical&knob-progress max=100&knob-progress or trend=trend&knob-subtitle=&knob-title=Most used in&knob-trend a11y description=The trend shows a peak of CPU usage in the last 5 minutes&knob-trend a11y title=The Cluster CPU Usage trend&knob-trend data points=30&knob-trend shape=area&knob-value=United States&knob-value postfix=&knob-value prefix=&knob-show icon=', + ); + }); }); diff --git a/packages/charts/api/charts.api.md b/packages/charts/api/charts.api.md index cc265f667e..492677e3b2 100644 --- a/packages/charts/api/charts.api.md +++ b/packages/charts/api/charts.api.md @@ -1859,7 +1859,7 @@ export type MetricWText = MetricBase & { }; // @alpha (undocumented) -export type MetricWTrend = MetricWNumber & { +export type MetricWTrend = (MetricWNumber | MetricWText) & { trend: { x: number; y: number; diff --git a/packages/charts/src/chart_types/metric/specs/index.ts b/packages/charts/src/chart_types/metric/specs/index.ts index 57b40e5b46..2bd98b4e5e 100644 --- a/packages/charts/src/chart_types/metric/specs/index.ts +++ b/packages/charts/src/chart_types/metric/specs/index.ts @@ -52,7 +52,7 @@ export const MetricTrendShape = Object.freeze({ export type MetricTrendShape = $Values; /** @alpha */ -export type MetricWTrend = MetricWNumber & { +export type MetricWTrend = (MetricWNumber | MetricWText) & { trend: { x: number; y: number }[]; trendShape: MetricTrendShape; trendA11yTitle?: string; @@ -87,6 +87,10 @@ export type MetricSpecProps = ComponentProps; export function isMetricWNumber(datum: MetricDatum): datum is MetricWNumber { return typeof datum.value === 'number' && datum.hasOwnProperty('valueFormatter'); } +/** @internal */ +export function isMetricWText(datum: MetricDatum): datum is MetricWNumber { + return typeof datum.value === 'string'; +} /** @internal */ export function isMetricWProgress(datum: MetricDatum): datum is MetricWProgress { @@ -95,5 +99,9 @@ export function isMetricWProgress(datum: MetricDatum): datum is MetricWProgress /** @internal */ export function isMetricWTrend(datum: MetricDatum): datum is MetricWTrend { - return isMetricWNumber(datum) && datum.hasOwnProperty('trend') && !datum.hasOwnProperty('domainMax'); + return ( + (isMetricWNumber(datum) || isMetricWText(datum)) && + datum.hasOwnProperty('trend') && + !datum.hasOwnProperty('domainMax') + ); } diff --git a/storybook/stories/metric/1_basic.story.tsx b/storybook/stories/metric/1_basic.story.tsx index 3904695c43..4ef4bf53e4 100644 --- a/storybook/stories/metric/1_basic.story.tsx +++ b/storybook/stories/metric/1_basic.story.tsx @@ -89,9 +89,18 @@ export const Example = () => { } : {}), }; - const textualData: MetricWText = { + const textualData: MetricWText | MetricWTrend = { ...data, value, + ...(progressOrTrend === 'bar' ? { domainMax: progressMax, progressBarDirection } : {}), + ...(progressOrTrend === 'trend' + ? { + trend: KIBANA_METRICS.metrics.kibana_os_load[1].data.slice(0, maxDataPoints).map(([x, y]) => ({ x, y })), + trendShape, + trendA11yTitle, + trendA11yDescription, + } + : {}), }; const onEventClickAction = action('click');