HTML
<button id="toggleStack" type="button">Unstack bars</button>
<div id="chart" style="width: 100%; height: 360px;"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/5.5.0/echarts.min.js"></script>
<script>
const categoryData = [
'2026_S01', '2026_S02', '2026_S03', '2026_S04', '2026_S05',
];
const activities = ['LV', 'CL', 'AB', 'II', 'EC'];
const valuesByPerson = {
Jim: [
[3, 5, 7, 6, 8],
[1, 0, 0, 1, 1],
[0, 0, 0, 0, 0],
[0, 1, 0, 1, 0],
[6, 7, 9, 8, 7],
],
Mag: [
[0, 1, 2, 1, 0],
[2, 0, 0, 0, 1],
[3, 0, 1, 0, 0],
[1, 1, 1, 1, 1],
[5, 8, 6, 9, 7],
],
};
const chart = echarts.init(document.getElementById('chart'));
const toggleButton = document.getElementById('toggleStack');
let stacked = true;
const bars = Object.entries(valuesByPerson).flatMap(([person, values]) =>
activities.map((activity, index) => ({
person,
activity,
values: values[index],
})),
);
function showStack({ seriesIndex, dataIndex }) {
const hoveredBar = bars[seriesIndex];
const selected = chart.getOption().legend[0].selected ?? {};
const matchingBars = stacked
? bars.filter(bar => bar.person === hoveredBar.person)
: [hoveredBar];
const tooltip = document.createElement('div');
const heading = document.createElement('strong');
heading.textContent = `${categoryData[dataIndex]} — ${hoveredBar.person}`;
tooltip.append(heading);
for (const bar of matchingBars) {
if (selected[bar.activity] === false) {
continue;
}
const row = document.createElement('div');
row.textContent = `${bar.activity}: ${bar.values[dataIndex]}`;
tooltip.append(row);
}
return tooltip;
}
function renderChart(selected = {}) {
chart.setOption({
tooltip: {
trigger: 'item',
formatter: showStack,
...