From cfa0f14e144a11eff82597ac24b28d322a3cec8f Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Thu, 19 Aug 2021 06:49:50 -0400 Subject: [PATCH] [TSVB] Long legend values support (#108023) (#109207) * [TSVB] Supports legends with long values * Add a unit test * Design optimization * Revert changes * Add the missing prop type * Ensure that the limits are respected Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: Stratoula Kalafateli --- .../common/types/panel_model.ts | 2 + .../panel_config/timeseries.test.tsx | 82 ++++++++++++ .../components/panel_config/timeseries.tsx | 122 ++++++++++++------ .../components/vis_types/timeseries/vis.js | 2 + .../visualizations/views/timeseries/index.js | 10 ++ .../public/metrics_type.ts | 2 + x-pack/yarn.lock | 27 ---- 7 files changed, 184 insertions(+), 63 deletions(-) create mode 100644 src/plugins/vis_type_timeseries/public/application/components/panel_config/timeseries.test.tsx diff --git a/src/plugins/vis_type_timeseries/common/types/panel_model.ts b/src/plugins/vis_type_timeseries/common/types/panel_model.ts index 2ac9125534ac7..ff942a30abbdc 100644 --- a/src/plugins/vis_type_timeseries/common/types/panel_model.ts +++ b/src/plugins/vis_type_timeseries/common/types/panel_model.ts @@ -161,6 +161,8 @@ export interface Panel { series: Series[]; show_grid: number; show_legend: number; + truncate_legend?: number; + max_lines_legend?: number; time_field?: string; time_range_mode?: string; tooltip_mode?: TOOLTIP_MODES; diff --git a/src/plugins/vis_type_timeseries/public/application/components/panel_config/timeseries.test.tsx b/src/plugins/vis_type_timeseries/public/application/components/panel_config/timeseries.test.tsx new file mode 100644 index 0000000000000..02f28f3135880 --- /dev/null +++ b/src/plugins/vis_type_timeseries/public/application/components/panel_config/timeseries.test.tsx @@ -0,0 +1,82 @@ +/* + * 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 React from 'react'; +import { shallowWithIntl as shallow } from '@kbn/test/jest'; + +jest.mock('../lib/get_default_query_language', () => ({ + getDefaultQueryLanguage: () => 'kuery', +})); + +import { TimeseriesPanelConfig } from './timeseries'; +import { PanelConfigProps } from './types'; + +describe('TimeseriesPanelConfig', () => { + it('sets the number input to the given value', () => { + const props = ({ + fields: {}, + model: { + max_lines_legend: 2, + }, + onChange: jest.fn(), + } as unknown) as PanelConfigProps; + const wrapper = shallow(); + wrapper.instance().setState({ selectedTab: 'options' }); + expect( + wrapper.find('[data-test-subj="timeSeriesEditorDataMaxLegendLines"]').prop('value') + ).toEqual(2); + }); + + it('switches on the truncate legend switch if the prop is set to 1 ', () => { + const props = ({ + fields: {}, + model: { + max_lines_legend: 2, + truncate_legend: 1, + }, + onChange: jest.fn(), + } as unknown) as PanelConfigProps; + const wrapper = shallow(); + wrapper.instance().setState({ selectedTab: 'options' }); + expect( + wrapper.find('[data-test-subj="timeSeriesEditorDataTruncateLegendSwitch"]').prop('value') + ).toEqual(1); + }); + + it('switches off the truncate legend switch if the prop is set to 0', () => { + const props = ({ + fields: {}, + model: { + max_lines_legend: 2, + truncate_legend: 0, + }, + onChange: jest.fn(), + } as unknown) as PanelConfigProps; + const wrapper = shallow(); + wrapper.instance().setState({ selectedTab: 'options' }); + expect( + wrapper.find('[data-test-subj="timeSeriesEditorDataTruncateLegendSwitch"]').prop('value') + ).toEqual(0); + }); + + it('disables the max lines number input if the truncate legend switch is off', () => { + const props = ({ + fields: {}, + model: { + max_lines_legend: 2, + truncate_legend: 0, + }, + onChange: jest.fn(), + } as unknown) as PanelConfigProps; + const wrapper = shallow(); + wrapper.instance().setState({ selectedTab: 'options' }); + expect( + wrapper.find('[data-test-subj="timeSeriesEditorDataMaxLegendLines"]').prop('disabled') + ).toEqual(true); + }); +}); diff --git a/src/plugins/vis_type_timeseries/public/application/components/panel_config/timeseries.tsx b/src/plugins/vis_type_timeseries/public/application/components/panel_config/timeseries.tsx index cdad8c1aeff4b..25e6c7906d831 100644 --- a/src/plugins/vis_type_timeseries/public/application/components/panel_config/timeseries.tsx +++ b/src/plugins/vis_type_timeseries/public/application/components/panel_config/timeseries.tsx @@ -23,6 +23,7 @@ import { EuiFieldText, EuiTitle, EuiHorizontalRule, + EuiFieldNumber, } from '@elastic/eui'; // @ts-expect-error not typed yet @@ -102,6 +103,9 @@ const legendPositionOptions = [ }, ]; +const MAX_TRUNCATE_LINES = 5; +const MIN_TRUNCATE_LINES = 1; + export class TimeseriesPanelConfig extends Component< PanelConfigProps, { selectedTab: PANEL_CONFIG_TABS } @@ -344,7 +348,7 @@ export class TimeseriesPanelConfig extends Component< /> - + - - + - + - + + + + - + + + - - - + + + + + + - + + + + + + + + + + { + const val = Number(e.target.value); + this.props.onChange({ + max_lines_legend: Math.min( + MAX_TRUNCATE_LINES, + Math.max(val, MIN_TRUNCATE_LINES) + ), + }); + }} + /> + + + + + + + diff --git a/src/plugins/vis_type_timeseries/public/application/components/vis_types/timeseries/vis.js b/src/plugins/vis_type_timeseries/public/application/components/vis_types/timeseries/vis.js index 097b0a7b5e332..d9440804701b2 100644 --- a/src/plugins/vis_type_timeseries/public/application/components/vis_types/timeseries/vis.js +++ b/src/plugins/vis_type_timeseries/public/application/components/vis_types/timeseries/vis.js @@ -238,6 +238,8 @@ class TimeseriesVisualization extends Component { showGrid={Boolean(model.show_grid)} legend={Boolean(model.show_legend)} legendPosition={model.legend_position} + truncateLegend={Boolean(model.truncate_legend)} + maxLegendLines={model.max_lines_legend} tooltipMode={model.tooltip_mode} xAxisFormatter={this.xAxisFormatter(interval)} annotations={this.prepareAnnotations()} diff --git a/src/plugins/vis_type_timeseries/public/application/visualizations/views/timeseries/index.js b/src/plugins/vis_type_timeseries/public/application/visualizations/views/timeseries/index.js index a818d1d5843de..b470352eec56a 100644 --- a/src/plugins/vis_type_timeseries/public/application/visualizations/views/timeseries/index.js +++ b/src/plugins/vis_type_timeseries/public/application/visualizations/views/timeseries/index.js @@ -56,6 +56,8 @@ export const TimeSeries = ({ showGrid, legend, legendPosition, + truncateLegend, + maxLegendLines, tooltipMode, series, yAxis, @@ -172,6 +174,9 @@ export const TimeSeries = ({ background: { color: backgroundColor, }, + legend: { + labelOptions: { maxLines: truncateLegend ? maxLegendLines ?? 1 : 0 }, + }, }, chartTheme, ]} @@ -216,6 +221,7 @@ export const TimeSeries = ({ lines, data, hideInLegend, + truncateLegend, xScaleType, yScaleType, groupId, @@ -249,6 +255,7 @@ export const TimeSeries = ({ name={getValueOrEmpty(seriesName)} data={data} hideInLegend={hideInLegend} + truncateLegend={truncateLegend} bars={bars} color={finalColor} stackAccessors={stackAccessors} @@ -274,6 +281,7 @@ export const TimeSeries = ({ name={getValueOrEmpty(seriesName)} data={data} hideInLegend={hideInLegend} + truncateLegend={truncateLegend} lines={lines} color={finalColor} stackAccessors={stackAccessors} @@ -336,6 +344,8 @@ TimeSeries.propTypes = { showGrid: PropTypes.bool, legend: PropTypes.bool, legendPosition: PropTypes.string, + truncateLegend: PropTypes.bool, + maxLegendLines: PropTypes.number, series: PropTypes.array, yAxis: PropTypes.array, onBrush: PropTypes.func, diff --git a/src/plugins/vis_type_timeseries/public/metrics_type.ts b/src/plugins/vis_type_timeseries/public/metrics_type.ts index d639604c7cd29..b68812b9828e3 100644 --- a/src/plugins/vis_type_timeseries/public/metrics_type.ts +++ b/src/plugins/vis_type_timeseries/public/metrics_type.ts @@ -93,6 +93,8 @@ export const metricsVisDefinition: VisTypeDefinition< axis_formatter: 'number', axis_scale: 'normal', show_legend: 1, + truncate_legend: 1, + max_lines_legend: 1, show_grid: 1, tooltip_mode: TOOLTIP_MODES.SHOW_ALL, drop_last_bucket: 0, diff --git a/x-pack/yarn.lock b/x-pack/yarn.lock index def2ba279bfff..fb57ccd13afbd 100644 --- a/x-pack/yarn.lock +++ b/x-pack/yarn.lock @@ -2,30 +2,3 @@ # yarn lockfile v1 -"@kbn/interpreter@link:../packages/kbn-interpreter": - version "0.0.0" - uid "" - -"@kbn/optimizer@link:../packages/kbn-optimizer": - version "0.0.0" - uid "" - -"@kbn/plugin-helpers@link:../packages/kbn-plugin-helpers": - version "0.0.0" - uid "" - -"@kbn/storybook@link:../packages/kbn-storybook": - version "0.0.0" - uid "" - -"@kbn/test@link:../packages/kbn-test": - version "0.0.0" - uid "" - -"@kbn/ui-framework@link:../packages/kbn-ui-framework": - version "0.0.0" - uid "" - -"@kbn/ui-shared-deps@link:../packages/kbn-ui-shared-deps": - version "0.0.0" - uid ""