In Apple Health there's an article called "Learn About Cardio Fitness" (found in the Browse tab (at the bottom) > Activity, then scroll down). And I stumble across the chart and I really want to emulate it in HTML.
The chart I'm talking about...
So here are my questions:
- From the stacked bar chart in official docs, I found a "Randomize" button that can seamlessly switch between data. How can I implement Bootstrap tabs that allow switching between data like the Apple Health article above?
- How do I add the number label in the bar? And turn off the hover feature?
- Bonus: How do I rotate the "VO2 max" label to horizontal? I tried the code from here (https://stackoverflow.com/a/75432153/22078763), but unfortunately it doesn't work (probably because I'm using chart.js 4.3.0).
Here is my code and data so far (I forgot to add the Male/Female/All tab *facepalm*):
function createChart(id, data) {
const ctx = document.getElementById(id).getContext('2d');
new Chart(ctx, {
type: 'bar',
data: {
labels: ['20-29', '30-39', '40-49', '50-59', '60+'],
datasets: data
},
options: {
scales: {
x: {
stacked: true,
title: {
display: true,
text: 'Age Ranges',
align: 'start'
}
},
y: {
stacked: true,
title: {
display: true,
text: 'VO2 max',
align: 'end'
},
position: 'right'
}
},
plugins: {
legend: {
display: false
}
}
}
});
}
const lowData = [{
label: 'Low',
data: {
"20-29": [29, 38],
"30-39": [27, 34],
"40-49": [24, 31],
"50-59": [21, 26],
"60+": [17, 18]
},
}];
const belowAverageData = [{
label: 'Below Average',
data: {
"20-29": [38, 48],
"30-39": [34, 43],
"40-49": [31, 38],
"50-59": [26, 33],
"60+": [18, 28]
},
}];
const aboveAverageData = [{
label: 'Above Average',
data: {
"20-29": [48, 57],
"30-39": [43, 52],
"40-49": [38, 47],
"50-59": [33, 41],
"60+": [28, 36]
},
}];
const highData = [{
label: 'High',
data: {
"20-29": [57, 66],
"30-39": [52, 60],
"40-49": [47, 56],
"50-59": [41, 51],
"60+": [36, 42]
},
}];
createChart('lowChart', lowData);
createChart('belowAverageChart', belowAverageData);
createChart('aboveAverageChart', aboveAverageData);
createChart('highChart', highData);
const triggerTabList = document.querySelectorAll('#chartTabs button')
triggerTabList.forEach(triggerEl => {
const tabTrigger = new bootstrap.Tab(triggerEl)
triggerEl.addEventListener('click', event => {
event.preventDefault()
tabTrigger.show()
})
})
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
<ul class="nav nav-pills" id="chartTabs" role="tablist">
<li class="nav-item" role="presentation">
<button class="nav-link active" id="low-tab" data-bs-toggle="tab" data-bs-target="#low" type="button" role="tab" aria-controls="low" aria-selected="true">Low</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="below-average-tab" data-bs-toggle="tab" data-bs-target="#below-average" type="button" role="tab" aria-controls="below-average" aria-selected="false">Below Average</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="above-average-tab" data-bs-toggle="tab" data-bs-target="#above-average" type="button" role="tab" aria-controls="above-average" aria-selected="false">Above Average</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="high-tab" data-bs-toggle="tab" data-bs-target="#high" type="button" role="tab" aria-controls="high" aria-selected="false">High</button>
</li>
</ul>
<div class="tab-content" id="chartTabsContent">
<div class="tab-pane show active" id="low" role="tabpanel" aria-labelledby="low-tab">
<div>
<canvas id="lowChart"></canvas>
</div>
</div>
<div class="tab-pane" id="below-average" role="tabpanel" aria-labelledby="below-average-tab">
<div>
<canvas id="belowAverageChart"></canvas>
</div>
</div>
<div class="tab-pane" id="above-average" role="tabpanel" aria-labelledby="above-average-tab">
<div>
<canvas id="aboveAverageChart"></canvas>
</div>
</div>
<div class="tab-pane" id="high" role="tabpanel" aria-labelledby="high-tab">
<div>
<canvas id="highChart"></canvas>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>