Skip to content

Commit

Permalink
fix: fix error of Brush when data is empty, but chart width or height…
Browse files Browse the repository at this point in the history
… or Brush update, fix #2093
  • Loading branch information
xile611 committed May 11, 2020
1 parent 6cc535c commit 7f2e681
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/cartesian/Brush.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Brush extends PureComponent<Props, State> {
endX: this.handleTravellerDragStart.bind(this, 'endX'),
};

this.state = props.data && props.data.length ? this.updateScale(props) : {};
this.state = props.data && props.data.length ? this.createScale(props) : {};
}

leaveTimer?: number;
Expand All @@ -104,9 +104,13 @@ class Brush extends PureComponent<Props, State> {
UNSAFE_componentWillReceiveProps(nextProps: Props) {
const { data, width, x, travellerWidth, updateId } = this.props;

if ((nextProps.data !== data || nextProps.updateId !== updateId) && nextProps.data && nextProps.data.length) {
this.setState(this.updateScale(nextProps));
} else if (nextProps.width !== width || nextProps.x !== x || nextProps.travellerWidth !== travellerWidth) {
if ((nextProps.data !== data || nextProps.updateId !== updateId)) {
if (nextProps.data && nextProps.data.length) {
this.setState(this.createScale(nextProps));
} else {
this.removeScale();
}
} else if (this.scale && nextProps.width !== width || nextProps.x !== x || nextProps.travellerWidth !== travellerWidth) {
this.scale.range([nextProps.x, nextProps.x + nextProps.width - nextProps.travellerWidth]);
this.scaleValues = this.scale.domain().map(entry => this.scale(entry));

Expand All @@ -118,9 +122,7 @@ class Brush extends PureComponent<Props, State> {
}

componentWillUnmount() {
this.scale = null;
this.scaleValues = null;

this.removeScale();
if (this.leaveTimer) {
clearTimeout(this.leaveTimer);
this.leaveTimer = null;
Expand Down Expand Up @@ -319,7 +321,7 @@ class Brush extends PureComponent<Props, State> {
);
}

updateScale(props: Props) {
createScale(props: Props) {
const { data, startIndex, endIndex, x, width, travellerWidth } = props;
const len = data.length;
this.scale = scalePoint<number>()
Expand All @@ -335,6 +337,11 @@ class Brush extends PureComponent<Props, State> {
};
}

removeScale() {
this.scale = null;
this.scaleValues = null;
}

renderBackground() {
const { x, y, width, height, fill, stroke } = this.props;

Expand Down

0 comments on commit 7f2e681

Please sign in to comment.