Skip to content
Snippets Groups Projects
Commit 14ae7a26 authored by esikkala's avatar esikkala
Browse files

Bar chart race: y-axis zoom handling

parent 12b94554
No related branches found
No related tags found
No related merge requests found
...@@ -7,10 +7,10 @@ import Paper from '@material-ui/core/Paper' ...@@ -7,10 +7,10 @@ import Paper from '@material-ui/core/Paper'
// https://www.amcharts.com/docs/v5/ // https://www.amcharts.com/docs/v5/
const stepDuration = 2000 const stepDuration = 1000
const startYear = 500 const startYear = 1907
let year = startYear let year = startYear
const yearIncrement = 10 const yearIncrement = 1
class BarChartRace extends React.Component { class BarChartRace extends React.Component {
componentDidMount = () => { componentDidMount = () => {
this.props.fetchData({ this.props.fetchData({
...@@ -23,7 +23,7 @@ class BarChartRace extends React.Component { ...@@ -23,7 +23,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, startYear)) {
// this.setInitialData() this.setInitialData()
this.playAnimation() this.playAnimation()
} }
} }
...@@ -40,19 +40,6 @@ class BarChartRace extends React.Component { ...@@ -40,19 +40,6 @@ class BarChartRace extends React.Component {
// https://www.amcharts.com/docs/v5/getting-started/#Root_element // https://www.amcharts.com/docs/v5/getting-started/#Root_element
const root = am5.Root.new('chartdiv') const root = am5.Root.new('chartdiv')
// root.numberFormatter.setAll({
// numberFormat: '#a',
// // Group only into M (millions), and B (billions)
// bigNumberPrefixes: [
// { number: 1e6, suffix: 'M' },
// { number: 1e9, suffix: 'B' }
// ],
// // Do not use small number prefixes at all
// smallNumberPrefixes: []
// })
// Set themes // Set themes
// https://www.amcharts.com/docs/v5/concepts/themes/ // https://www.amcharts.com/docs/v5/concepts/themes/
root.setThemes([am5themesAnimated.new(root)]) root.setThemes([am5themesAnimated.new(root)])
...@@ -79,8 +66,6 @@ class BarChartRace extends React.Component { ...@@ -79,8 +66,6 @@ class BarChartRace extends React.Component {
// hide grid // hide grid
yRenderer.grid.template.set('visible', false) yRenderer.grid.template.set('visible', false)
// console.log(yRenderer.labels.template)
this.yAxis = chart.yAxes.push(am5xy.CategoryAxis.new(root, { this.yAxis = chart.yAxes.push(am5xy.CategoryAxis.new(root, {
maxDeviation: 0, maxDeviation: 0,
extraMax: 0.1, extraMax: 0.1,
...@@ -128,7 +113,6 @@ class BarChartRace extends React.Component { ...@@ -128,7 +113,6 @@ class BarChartRace extends React.Component {
return am5.Bullet.new(root, { return am5.Bullet.new(root, {
locationX: 1, locationX: 1,
sprite: am5.Label.new(root, { sprite: am5.Label.new(root, {
// text: "{valueXWorking.formatNumber('#.# a')}",
text: "{valueXWorking.formatNumber('#.')}", text: "{valueXWorking.formatNumber('#.')}",
fill: root.interfaceColors.get('alternativeText'), fill: root.interfaceColors.get('alternativeText'),
centerX: am5.p100, centerX: am5.p100,
...@@ -158,64 +142,61 @@ class BarChartRace extends React.Component { ...@@ -158,64 +142,61 @@ class BarChartRace extends React.Component {
this.series.data.push({ category: id, value, prefLabel }) this.series.data.push({ category: id, value, prefLabel })
this.yAxis.data.push({ category: id, prefLabel }) this.yAxis.data.push({ category: id, prefLabel })
} }
this.yAxis.zoomToIndexes(0, 1) // hack to fix initial pan / zoom setTimeout(() => {
this.yAxis.zoomToIndexes(0, this.yAxis.dataItems.length - 1)
}, 100)
} }
updateData = () => { updateData = () => {
// let itemsWithNonZero = 0 if (this.props.results[year] == null) { return }
if (this.props.results[year]) { for (const [key, value] of Object.entries(this.props.results[year])) {
for (const [key, value] of Object.entries(this.props.results[year])) { 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())
am5.array.each(this.series.dataItems, dataItem => {
const category = dataItem.get('categoryY')
const value = this.props.results[year][category].value
// if (value > 0) {
// itemsWithNonZero++
// }
dataItem.animate({
key: 'valueX',
to: value,
duration: stepDuration,
easing: am5.ease.linear
})
dataItem.animate({
key: 'valueXWorking',
to: value,
duration: stepDuration,
easing: am5.ease.linear
})
})
this.yAxis.zoomToIndexes(0, 15)
// this.yAxis.zoom(0, itemsWithNonZero / this.yAxis.dataItems.length)
} }
this.label.set('text', year.toString())
am5.array.each(this.series.dataItems, dataItem => {
const category = dataItem.get('categoryY')
const value = this.props.results[year][category].value
dataItem.animate({
key: 'valueX',
to: value,
duration: stepDuration,
easing: am5.ease.linear
})
dataItem.animate({
key: 'valueXWorking',
to: value,
duration: stepDuration,
easing: am5.ease.linear
})
})
const endIndex = this.yAxis.dataItems.length < 10
? this.yAxis.dataItems.length - 1
: 10
this.yAxis.zoomToIndexes(0, endIndex)
} }
playAnimation = () => { playAnimation = () => {
// update data with values each 1.5 sec const sortInterval = setInterval(() => {
this.sortCategoryAxis()
}, 100)
const interval = setInterval(() => { const interval = setInterval(() => {
// year++
year += yearIncrement year += yearIncrement
if (year > 2020) {
if (year > 1900) {
clearInterval(interval) clearInterval(interval)
clearInterval(this.sortInterval) clearInterval(sortInterval)
// sort category axis one more time // sort category axis one more time
setTimeout(() => { setTimeout(() => {
this.sortCategoryAxis() this.sortCategoryAxis()
}, 2000) }, 2000)
} }
this.updateData() this.updateData()
}, stepDuration) }, stepDuration)
this.setInitialData()
// 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/
this.series.appear(2000) this.series.appear(2000)
...@@ -233,10 +214,6 @@ class BarChartRace extends React.Component { ...@@ -233,10 +214,6 @@ class BarChartRace extends React.Component {
return null return null
} }
sortInterval = setInterval(() => {
this.sortCategoryAxis()
}, 100)
// Axis sorting // Axis sorting
sortCategoryAxis = () => { sortCategoryAxis = () => {
// sort by value // sort by value
...@@ -249,8 +226,7 @@ class BarChartRace extends React.Component { ...@@ -249,8 +226,7 @@ class BarChartRace extends React.Component {
am5.array.each(this.yAxis.dataItems, dataItem => { am5.array.each(this.yAxis.dataItems, dataItem => {
// get corresponding series item // get corresponding series item
const seriesDataItem = this.getSeriesItem(dataItem.get('category')) const seriesDataItem = this.getSeriesItem(dataItem.get('category'))
if (seriesDataItem !== null) {
if (seriesDataItem) {
// get index of series data item // get index of series data item
const index = this.series.dataItems.indexOf(seriesDataItem) const index = this.series.dataItems.indexOf(seriesDataItem)
// calculate delta position // calculate delta position
...@@ -283,7 +259,6 @@ class BarChartRace extends React.Component { ...@@ -283,7 +259,6 @@ class BarChartRace extends React.Component {
<Paper square style={{ width: 'calc(100% - 64px)', height: 'calc(100% - 72px)', paddingLeft: 32, paddingRight: 32 }}> <Paper square style={{ width: 'calc(100% - 64px)', height: 'calc(100% - 72px)', paddingLeft: 32, paddingRight: 32 }}>
<div style={{ width: '100%', height: '100%' }} id='chartdiv' /> <div style={{ width: '100%', height: '100%' }} id='chartdiv' />
</Paper> </Paper>
) )
} }
} }
......
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