Skip to content

Commit dbabe81

Browse files
committed
[Fix] recreate timeline chart on resize #429
1 parent 8cb8e7a commit dbabe81

File tree

5 files changed

+71
-6
lines changed

5 files changed

+71
-6
lines changed

package-lock.json

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"react-dom": "^16.6.3",
7272
"react-intersection-observer": "3.0.3",
7373
"react-redux": "5.0.7",
74+
"react-resize-detector": "^3.4.0",
7475
"redux": "3.7.2",
7576
"semantic-ui-react": "^0.84.0"
7677
},
@@ -88,6 +89,7 @@
8889
"@types/react": "^16.4.11",
8990
"@types/react-beautiful-dnd": "6.0.1",
9091
"@types/react-redux": "^6.0.11",
92+
"@types/react-resize-detector": "^3.1.0",
9193
"@types/storybook__react": "^3.0.9",
9294
"@zeit/next-typescript": "0.1.1",
9395
"apollo-codegen": "^0.20.2",

src/components/atoms/Timeline/index.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from 'react';
2+
import ReactResizeDetector from 'react-resize-detector';
23
import { draw } from '@devinit/charts';
34

45
export interface Props {
@@ -15,16 +16,20 @@ class Timeline extends React.Component<Props> {
1516
public props: Props;
1617
public element: HTMLDivElement | null;
1718

18-
constructor(props: Props) {
19+
constructor(props) {
1920
super(props);
21+
22+
this.onResize = this.onResize.bind(this);
2023
}
2124

2225
public render() {
2326
return (
2427
<div
2528
ref={ element => { this.element = element; } }
2629
style={ { width: this.props.width || '100%', height: this.props.height } }
27-
/>
30+
>
31+
<ReactResizeDetector handleWidth onResize={ this.onResize }/>
32+
</div>
2833
);
2934
}
3035

@@ -57,6 +62,12 @@ class Timeline extends React.Component<Props> {
5762
xchart.onAnchorMoved(props.onYearChanged);
5863
});
5964
}
65+
66+
private onResize() {
67+
this.chart.destroy();
68+
this.chart = undefined;
69+
this.renderChart(this.props);
70+
}
6071
}
6172

6273
export default Timeline;

src/components/molecules/LinePartition/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export default class LinePartition extends React.Component<Props, State> {
107107
<Container>
108108
{ this.renderLinePartitionHeader(!this.props.inverted || !tree.length) }
109109
<Grid style={ { paddingBottom: '40px' } }>
110-
<Grid.Column mobile={ 16 } computer={ 5 } width={ 5 } style={ { padding: 0 } }>
110+
<Grid.Column mobile={ 16 } computer={ 5 } style={ { padding: 0 } }>
111111
<Intro step={ 2 } intro={ howTo.countryProfile.governmentFinance.timeline }>
112112
<CardContainer>
113113
<Timeline
@@ -130,7 +130,7 @@ export default class LinePartition extends React.Component<Props, State> {
130130
</Intro>
131131
</Grid.Column>
132132

133-
<Grid.Column mobile={ 16 } computer={ 11 } width={ 11 } style={ { padding: 0 } }>
133+
<Grid.Column mobile={ 16 } computer={ 11 } style={ { padding: 0 } }>
134134
{ this.renderTreeChart(tree, showLegend) }
135135
</Grid.Column>
136136
</Grid>

src/components/molecules/MultiLinePartition/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,9 @@ export default class MultiLinePartition extends React.Component<Props> {
193193
}
194194

195195
public static createInitialState(props: Props) {
196-
const everything = Array.prototype.concat.apply([], props.items.map(item => item.data));
197-
const currencyOptions = createCurrencyOptions(props.currencyCode, props.currencyUSD, props.supportLocalCurrencyOnly);
196+
const { currencyCode, currencyUSD, items, supportLocalCurrencyOnly } = props;
197+
const everything = Array.prototype.concat.apply([], items.map(item => item.data));
198+
const currencyOptions = createCurrencyOptions(currencyCode, currencyUSD, supportLocalCurrencyOnly);
198199
const budgetTypeOptions = MultiLinePartition.createBudgetTypeOptions(everything);
199200
const { lowestYear, highestYear } = MultiLinePartition.createTimeLimits(everything);
200201
const possibleStartYear = props.year || props.startYear;

0 commit comments

Comments
 (0)