Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(metric): trend with string value #2011

Merged
merged 6 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions e2e/tests/metric_stories.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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=',
);
});
});
2 changes: 1 addition & 1 deletion packages/charts/api/charts.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1859,7 +1859,7 @@ export type MetricWText = MetricBase & {
};

// @alpha (undocumented)
export type MetricWTrend = MetricWNumber & {
export type MetricWTrend = (MetricWNumber | MetricWText) & {
trend: {
x: number;
y: number;
Expand Down
12 changes: 10 additions & 2 deletions packages/charts/src/chart_types/metric/specs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const MetricTrendShape = Object.freeze({
export type MetricTrendShape = $Values<typeof MetricTrendShape>;

/** @alpha */
export type MetricWTrend = MetricWNumber & {
export type MetricWTrend = (MetricWNumber | MetricWText) & {
trend: { x: number; y: number }[];
trendShape: MetricTrendShape;
trendA11yTitle?: string;
Expand Down Expand Up @@ -87,6 +87,10 @@ export type MetricSpecProps = ComponentProps<typeof Metric>;
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 {
Expand All @@ -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')
);
}
11 changes: 10 additions & 1 deletion storybook/stories/metric/1_basic.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down