Skip to content

Commit

Permalink
fix(legend): width with scroll bar (opensearch-project#1019)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickofthyme authored Feb 11, 2021
1 parent 4f99c63 commit 3f9c858
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 8 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions packages/osd-charts/integration/tests/legend_stories.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ describe('Legend stories', () => {
});
});

it('should adjust legend width for scrollbar', async () => {
await common.expectChartAtUrlToMatchScreenshot(
'http://localhost:9001/?path=/story/small-multiples-alpha--grid-lines&knob-Debug=true&knob-Show Legend=true',
);
});

describe('Tooltip placement with legend', () => {
it('should render tooltip with left legend', async () => {
await common.expectChartWithMouseAtUrlToMatchScreenshot('http://localhost:9001/?path=/story/legend--left', {
Expand Down
8 changes: 1 addition & 7 deletions packages/osd-charts/src/components/legend/_legend.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@
grid-row-gap: $echLegendRowGap;
margin-top: $echLegendRowGap;
margin-bottom: $echLegendRowGap;

@include internetExplorerOnly {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
}
}

Expand All @@ -34,7 +28,7 @@
}

&--debug {
background: red;
background: rgba(238, 130, 238, 0.2);
position: relative;
}

Expand Down
9 changes: 8 additions & 1 deletion packages/osd-charts/src/state/selectors/get_legend_size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ import { getSettingsSpecSelector } from './get_settings_specs';

const getParentDimensionSelector = (state: GlobalChartState) => state.parentDimensions;

const SCROLL_BAR_WIDTH = 6; // ~1em
const MARKER_WIDTH = 16;
const MARKER_LEFT_MARGIN = 4;
const VALUE_LEFT_MARGIN = 4;
const VERTICAL_PADDING = 4;
const TOP_MARGIN = 2;

/** @internal */
export type LegendSizing = BBox & {
Expand Down Expand Up @@ -83,8 +85,13 @@ export const getLegendSizeSelector = createCachedSelector(
MARKER_WIDTH + MARKER_LEFT_MARGIN + bbox.width + (showLegendDisplayValue ? VALUE_LEFT_MARGIN : 0);
if (isVerticalAxis(position)) {
const legendItemHeight = bbox.height + VERTICAL_PADDING * 2;
const legendHeight = legendItemHeight * labels.length + TOP_MARGIN;
const scollBarDimension = legendHeight > parentDimensions.height ? SCROLL_BAR_WIDTH : 0;

return {
width: Math.floor(Math.min(legendItemWidth + spacingBuffer + actionDimension, verticalWidth)),
width: Math.floor(
Math.min(legendItemWidth + spacingBuffer + actionDimension + scollBarDimension, verticalWidth),
),
height: legendItemHeight,
margin,
position,
Expand Down

0 comments on commit 3f9c858

Please sign in to comment.