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

fix: legend sizing issue with no initial legend #441

Merged
merged 6 commits into from
Oct 25, 2019
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"concat:sass": "node scripts/concat_sass.js",
"autoprefix:css": "yarn postcss dist/*.css --no-map --use autoprefixer -d dist",
"build": "yarn build:clean && yarn build:ts && yarn build:sass && yarn autoprefix:css && yarn concat:sass",
"build:watch": "yarn build:clean && yarn build:sass && yarn autoprefix:css && yarn concat:sass && yarn build:ts -w ",
"start": "yarn storybook",
"storybook": "start-storybook -p 9001 -c .storybook --ci",
"storybook:build": "rm -rf .out && build-storybook -c .storybook -o .out",
Expand Down
49 changes: 34 additions & 15 deletions src/components/legend/legend.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import classNames from 'classnames';
import { inject, observer } from 'mobx-react';
import React, { createRef } from 'react';
import { inject, observer } from 'mobx-react';
import classNames from 'classnames';

import { isVerticalAxis, isHorizontalAxis } from '../../chart_types/xy_chart/utils/axis_utils';
import { LegendItem as SeriesLegendItem } from '../../chart_types/xy_chart/legend/legend';
import { ChartStore } from '../../chart_types/xy_chart/store/chart_state';
Expand Down Expand Up @@ -34,6 +35,7 @@ interface LegendListStyle {

class LegendComponent extends React.Component<LegendProps, LegendState> {
static displayName = 'Legend';
legendItemCount = 0;

state = {
width: undefined,
Expand All @@ -42,19 +44,7 @@ class LegendComponent extends React.Component<LegendProps, LegendState> {
private echLegend = createRef<HTMLDivElement>();

componentDidUpdate() {
const { chartInitialized, chartTheme, legendPosition } = this.props.chartStore!;
if (
this.echLegend.current &&
isVerticalAxis(legendPosition.get()) &&
this.state.width === undefined &&
!chartInitialized.get()
) {
const buffer = chartTheme.legend.spacingBuffer;

this.setState({
width: this.echLegend.current.offsetWidth + buffer,
});
}
this.tryLegendResize();
}

render() {
Expand Down Expand Up @@ -91,6 +81,35 @@ class LegendComponent extends React.Component<LegendProps, LegendState> {
);
}

tryLegendResize = () => {
const { chartInitialized, chartTheme, legendPosition, legendItems } = this.props.chartStore!;
const { width } = this.state;

if (
this.echLegend.current &&
isVerticalAxis(legendPosition.get()) &&
!chartInitialized.get() &&
width === undefined &&
this.echLegend.current.offsetWidth > 0
) {
const buffer = chartTheme.legend.spacingBuffer;
this.legendItemCount = legendItems.size;

return this.setState({
width: this.echLegend.current.offsetWidth + buffer,
});
}

// Need to reset width to enable downsizing of width
if (width !== undefined && legendItems.size !== this.legendItemCount) {
this.legendItemCount = legendItems.size;

this.setState({
width: undefined,
});
}
};

getLegendListStyle = (position: Position, { chartMargins, legend }: Theme): LegendListStyle => {
const { top: paddingTop, bottom: paddingBottom, left: paddingLeft, right: paddingRight } = chartMargins;

Expand Down