Skip to content
Snippets Groups Projects
Commit 50647d92 authored by esikkala's avatar esikkala
Browse files

Bar chart race: step config to props

parent b9fc0d08
No related branches found
No related tags found
No related merge requests found
...@@ -7,10 +7,6 @@ import Paper from '@material-ui/core/Paper' ...@@ -7,10 +7,6 @@ import Paper from '@material-ui/core/Paper'
// https://www.amcharts.com/docs/v5/ // https://www.amcharts.com/docs/v5/
const stepDuration = 1000
const startYear = 1907
let year = startYear
const yearIncrement = 1
class BarChartRace extends React.Component { class BarChartRace extends React.Component {
componentDidMount = () => { componentDidMount = () => {
this.props.fetchData({ this.props.fetchData({
...@@ -22,7 +18,7 @@ class BarChartRace extends React.Component { ...@@ -22,7 +18,7 @@ class BarChartRace extends React.Component {
componentDidUpdate = prevProps => { componentDidUpdate = prevProps => {
if (prevProps.resultUpdateID !== this.props.resultUpdateID) { if (prevProps.resultUpdateID !== this.props.resultUpdateID) {
if (this.props.results && Object.prototype.hasOwnProperty.call(this.props.results, startYear)) { if (this.props.results && Object.prototype.hasOwnProperty.call(this.props.results, this.props.stepBegin)) {
this.setInitialData() this.setInitialData()
this.playAnimation() this.playAnimation()
} }
...@@ -85,7 +81,7 @@ class BarChartRace extends React.Component { ...@@ -85,7 +81,7 @@ class BarChartRace extends React.Component {
renderer: am5xy.AxisRendererX.new(root, {}) renderer: am5xy.AxisRendererX.new(root, {})
})) }))
xAxis.set('interpolationDuration', stepDuration / 10) xAxis.set('interpolationDuration', this.props.stepDuration / 10)
xAxis.set('interpolationEasing', am5.ease.linear) xAxis.set('interpolationEasing', am5.ease.linear)
// Add series // Add series
...@@ -122,8 +118,10 @@ class BarChartRace extends React.Component { ...@@ -122,8 +118,10 @@ class BarChartRace extends React.Component {
}) })
}) })
this.animationStep = this.props.stepBegin
this.label = chart.plotContainer.children.push(am5.Label.new(root, { this.label = chart.plotContainer.children.push(am5.Label.new(root, {
text: startYear, text: this.animationStep,
fontSize: '8em', fontSize: '8em',
opacity: 0.2, opacity: 0.2,
x: am5.p100, x: am5.p100,
...@@ -136,7 +134,7 @@ class BarChartRace extends React.Component { ...@@ -136,7 +134,7 @@ class BarChartRace extends React.Component {
} }
setInitialData = () => { setInitialData = () => {
const step = this.props.results[year] const step = this.props.results[this.animationStep]
for (const id in step) { for (const id in step) {
const { prefLabel, value } = step[id] const { prefLabel, value } = step[id]
this.series.data.push({ category: id, value, prefLabel }) this.series.data.push({ category: id, value, prefLabel })
...@@ -148,28 +146,28 @@ class BarChartRace extends React.Component { ...@@ -148,28 +146,28 @@ class BarChartRace extends React.Component {
} }
updateData = () => { updateData = () => {
if (this.props.results[year] == null) { return } if (this.props.results[this.animationStep] == null) { return }
for (const [key, value] of Object.entries(this.props.results[year])) { for (const [key, value] of Object.entries(this.props.results[this.animationStep])) {
const dataItem = this.getSeriesItem(key) const dataItem = this.getSeriesItem(key)
if (dataItem == null) { if (dataItem == null) {
this.series.data.push({ category: key, value: value.value, prefLabel: value.prefLabel }) this.series.data.push({ category: key, value: value.value, prefLabel: value.prefLabel })
this.yAxis.data.push({ category: key, prefLabel: value.prefLabel }) this.yAxis.data.push({ category: key, prefLabel: value.prefLabel })
} }
} }
this.label.set('text', year.toString()) this.label.set('text', this.animationStep.toString())
am5.array.each(this.series.dataItems, dataItem => { am5.array.each(this.series.dataItems, dataItem => {
const category = dataItem.get('categoryY') const category = dataItem.get('categoryY')
const value = this.props.results[year][category].value const value = this.props.results[this.animationStep][category].value
dataItem.animate({ dataItem.animate({
key: 'valueX', key: 'valueX',
to: value, to: value,
duration: stepDuration, duration: this.props.stepDuration,
easing: am5.ease.linear easing: am5.ease.linear
}) })
dataItem.animate({ dataItem.animate({
key: 'valueXWorking', key: 'valueXWorking',
to: value, to: value,
duration: stepDuration, duration: this.props.stepDuration,
easing: am5.ease.linear easing: am5.ease.linear
}) })
}) })
...@@ -185,8 +183,8 @@ class BarChartRace extends React.Component { ...@@ -185,8 +183,8 @@ class BarChartRace extends React.Component {
}, 100) }, 100)
const interval = setInterval(() => { const interval = setInterval(() => {
year += yearIncrement this.animationStep += this.props.stepIncrement
if (year > 2020) { if (this.animationStep > this.props.stepEnd) {
clearInterval(interval) clearInterval(interval)
clearInterval(sortInterval) clearInterval(sortInterval)
// sort category axis one more time // sort category axis one more time
...@@ -195,7 +193,7 @@ class BarChartRace extends React.Component { ...@@ -195,7 +193,7 @@ class BarChartRace extends React.Component {
}, 2000) }, 2000)
} }
this.updateData() this.updateData()
}, stepDuration) }, this.props.stepDuration)
// Make stuff animate on load // Make stuff animate on load
// https://www.amcharts.com/docs/v5/concepts/animations/ // https://www.amcharts.com/docs/v5/concepts/animations/
...@@ -241,7 +239,7 @@ class BarChartRace extends React.Component { ...@@ -241,7 +239,7 @@ class BarChartRace extends React.Component {
dataItem.animate({ dataItem.animate({
key: 'deltaPosition', key: 'deltaPosition',
to: 0, to: 0,
duration: stepDuration / 2, duration: this.props.stepDuration / 2,
easing: am5.ease.out(am5.ease.cubic) easing: am5.ease.out(am5.ease.cubic)
}) })
} }
......
...@@ -272,6 +272,10 @@ const Perspective1 = props => { ...@@ -272,6 +272,10 @@ const Perspective1 = props => {
facetClass='perspective1' facetClass='perspective1'
resultUpdateID={props.perspectiveState.resultUpdateID} resultUpdateID={props.perspectiveState.resultUpdateID}
results={props.perspectiveState.results} results={props.perspectiveState.results}
stepBegin={1000}
stepEnd={1900}
stepIncrement={10}
stepDuration={1000}
/>} />}
/> />
<Route <Route
...@@ -283,6 +287,10 @@ const Perspective1 = props => { ...@@ -283,6 +287,10 @@ const Perspective1 = props => {
facetClass='perspective1' facetClass='perspective1'
resultUpdateID={props.perspectiveState.resultUpdateID} resultUpdateID={props.perspectiveState.resultUpdateID}
results={props.perspectiveState.results} results={props.perspectiveState.results}
stepBegin={1907}
stepEnd={2021}
stepIncrement={1}
stepDuration={1000}
/>} />}
/> />
<Route <Route
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment