Stacked Bar in JavaScript
Using ApexCharts with JavaScript
This Stacked Bar example uses ApexCharts.js directly in JavaScript, with no wrapper component.
Install it with npm install apexcharts, then mount the chart with new ApexCharts(element, options).render().
var options = {
series: [
{
name: 'Fantasy',
data: [44, 55, 41, 37, 22, 43, 21],
},
{
name: 'Mystery',
data: [53, 32, 33, 52, 13, 43, 32],
},
{
name: 'Romance',
data: [12, 17, 11, 9, 15, 11, 20],
},
{
name: 'Sci-Fi',
data: [9, 7, 5, 8, 6, 9, 4],
},
{
name: 'Thriller',
data: [25, 12, 19, 32, 25, 24, 10],
},
],
chart: {
type: 'bar',
height: 350,
stacked: true,
},
plotOptions: {
bar: {
horizontal: true,
dataLabels: {
total: {
enabled: true,
offsetX: 0,
style: {
fontSize: '13px',
fontWeight: 900,
},
},
},
},
},
stroke: {
width: 1,
colors: ['#fff'],
},
title: {
text: 'Book Sales by Genre',
},
xaxis: {
categories: [2019, 2020, 2021, 2022, 2023, 2024, 2025],
labels: {
formatter: function (val) {
return val + 'K'
},
},
},
yaxis: {
title: {
text: undefined,
},
},
tooltip: {
y: {
formatter: function (val) {
return val + 'K'
},
},
},
fill: {
opacity: 1,
},
legend: {
position: 'top',
horizontalAlign: 'left',
offsetX: 40,
},
}
var chart = new ApexCharts(document.querySelector('#chart'), options)
chart.render()