Bar Chart Race in Vue

Using ApexCharts with Vue

This Bar Chart Race example wires ApexCharts into Vue through the vue3-apexcharts component.

Install it with npm install vue3-apexcharts apexcharts, then mount the chart with <apexchart type="..." :options="options" :series="series" />.

Vue charts guide
<template>
  <div>
    <div id="chart">
      <apexchart
        type="bar"
        height="420"
        ref="chart"
        :options="chartOptions"
        :series="series"
      ></apexchart>
    </div>

    <div class="explanation">
      A "bar chart race": each tick re-sorts the entities by value and updates
      the chart. The bars slide to their new ranks and the left-hand category
      labels ride along with them (automatic whenever a reorder update runs with
      <code>dynamicAnimation</code> enabled). The value labels ride each bar
      too, which every bar chart does by default. What completes the race is
      <code>dataLabels.countUp</code>: opt into it and each number counts up or
      down from its previous value instead of switching.
    </div>
  </div>
</template>

<script>
import VueApexCharts from 'vue-apexcharts'

var raceEntities = [
  'United States',
  'China',
  'Germany',
  'Japan',
  'France',
  'United Kingdom',
  'India',
  'Brazil',
  'Canada',
  'Italy',
]

// A fixed color per country. With distributed bars the color is applied by bar
// POSITION, but this is a race where the bars re-sort every tick, so raceFrame()
// re-orders this palette to match the current ranking below. That keeps each
// country's color stable as it rides up and down the ranks.
var raceColors = {
  'United States': '#3B82F6',
  China: '#EF4444',
  Germany: '#F59E0B',
  Japan: '#10B981',
  France: '#06B6D4',
  'United Kingdom': '#EC4899',
  India: '#14B8A6',
  Brazil: '#F97316',
  Canada: '#64748B',
  Italy: '#84CC16',
}

// A flag emoji per country, prepended to its axis label by
// yaxis.labels.formatter. SVG axis labels are plain text, so an emoji is the
// simplest way to put an icon in front of a label (real image flags would need
// custom DOM work). Emoji flags render on most platforms; a few Windows
// browsers show the two-letter country code instead.
var raceFlags = {
  'United States': '🇺🇸',
  China: '🇨🇳',
  Germany: '🇩🇪',
  Japan: '🇯🇵',
  France: '🇫🇷',
  'United Kingdom': '🇬🇧',
  India: '🇮🇳',
  Brazil: '🇧🇷',
  Canada: '🇨🇦',
  Italy: '🇮🇹',
}

// Persistent value per entity, nudged a little each tick so ranks swap
// gradually (a smooth race rather than a random shuffle).
var raceValues = {}
raceEntities.forEach(function (name, i) {
  raceValues[name] = 300 + (raceEntities.length - i) * 90
})

function raceFrame() {
  raceEntities.forEach(function (name) {
    raceValues[name] = Math.max(
      50,
      raceValues[name] + (Math.random() - 0.45) * 220,
    )
  })
  var ranked = raceEntities
    .map(function (name) {
      return { name: name, value: Math.round(raceValues[name]) }
    })
    .sort(function (a, b) {
      return b.value - a.value
    })
  return {
    categories: ranked.map(function (r) {
      return r.name
    }),
    data: ranked.map(function (r) {
      return r.value
    }),
    // Colors follow the ranking so each country keeps its own color (see above).
    colors: ranked.map(function (r) {
      return raceColors[r.name]
    }),
  }
}

var raceInitial = raceFrame()

export default {
  components: {
    apexchart: VueApexCharts,
  },
  data: function () {
    return {
      series: [
        {
          name: 'Value',
          data: raceInitial.data,
        },
      ],
      chartOptions: {
        chart: {
          id: 'horizontal-bar-race',
          type: 'bar',
          height: 420,
          animations: {
            dynamicAnimation: {
              speed: 800,
            },
          },
          toolbar: {
            show: false,
          },
        },
        plotOptions: {
          bar: {
            borderRadius: 4,
            borderRadiusApplication: 'end',
            horizontal: true,
            // Each bar gets its own color, taken from the `colors` palette by position.
            distributed: true,
          },
        },
        colors: raceInitial.colors,
        dataLabels: {
          enabled: true,
          // Riding to each bar's new rank is the default; counting the number up or
          // down from its previous value on every tick is the opt-in that makes it
          // read as a race.
          countUp: {
            enabled: true,
          },
        },
        title: {
          text: 'Bar Chart Race',
          align: 'left',
        },
        xaxis: {
          categories: raceInitial.categories,
        },
        yaxis: {
          labels: {
            // Prepend each country's flag emoji to its axis label.
            formatter: function (val) {
              return (raceFlags[val] || '') + '  ' + val
            },
          },
        },
        legend: {
          show: false,
        },
      },
    }
  },
  mounted: function () {
    var me = this
    window.setInterval(function () {
      var frame = raceFrame()
      me.$refs.chart.updateOptions({
        series: [{ data: frame.data }],
        xaxis: { categories: frame.categories },
        colors: frame.colors,
      })
    }, 1200)
  },
}
</script>

<style>
#chart {
  max-width: 650px;
  margin: 35px auto;
}
.explanation {
  max-width: 620px;
  margin: 0 auto 12px;
  font-size: 13px;
  color: #777;
}
</style>