Dynamic Sorting Bar Chart
Related Options
Bar race is a chart that shows changes in the ranking of data over time and it is supported by default since ECharts 5.
Bar race charts usually use horizontal bars. If you want to use vertical bars, just take the X axis and Y axis in this tutorial to the opposite.
- Set
realtimeSort
of the bar series to betrue
to enable bar race - Set
yAxis.inverse
to betrue
to display longer bars at top yAxis.animationDuration
is suggested to be set to be300
for bar reordering animation for the first timeyAxis.animationDurationUpdate
is suggested to be set to be300
for bar reordering animation for later times- Set
yAxis.max
to be n - 1 where n is the number of bars to be displayed; otherwise, all bars are displayed xAxis.max
is suggested to be set to be'dataMax'
so that the maximum of data is used as X maximum.- If realtime label changing is required, set
series.label.valueAnimation
to betrue
- Set
animationDuration
to be0
so that the first animation doesn't start from 0; if you wish otherwise, set it to be the same value asanimationDurationUpdate
animationDurationUpdate
is suggested to be set to be3000
for animation update duration, which should be the same as the frequency of callingsetOption
- Call
setOption
to update data to be of next time withsetInterval
at the frequency ofanimationDurationUpdate
Demo
A complete demo:
var data = []; for (let i = 0; i < 5; ++i) { data.push(Math.round(Math.random() * 200)); } option = { xAxis: { max: 'dataMax' }, yAxis: { type: 'category', data: ['A', 'B', 'C', 'D', 'E'], inverse: true, animationDuration: 300, animationDurationUpdate: 300, max: 2 // only the largest 3 bars will be displayed }, series: [ { realtimeSort: true, name: 'X', type: 'bar', data: data, label: { show: true, position: 'right', valueAnimation: true } } ], legend: { show: true }, animationDuration: 0, animationDurationUpdate: 3000, animationEasing: 'linear', animationEasingUpdate: 'linear' }; function run() { var data = option.series[0].data; for (var i = 0; i < data.length; ++i) { if (Math.random() > 0.9) { data[i] += Math.round(Math.random() * 2000); } else { data[i] += Math.round(Math.random() * 200); } } myChart.setOption(option); } setTimeout(function() { run(); }, 0); setInterval(function() { run(); }, 3000);
live