I just started learning how to include graphs in code but faced some issues.
I have a horizontal bar chart here that shows datas of transportations in countries.
under the chart there are sections with different ids.
Please Help…
<script>
var xValues = [['California', 'United States'], ['New York City', 'United States'], ['London','United Kingdom'], ['Paris','France'], ['Shanghai', 'China'], ['Hong Kong'], ['Kuala Lumpur', 'Malaysia'], "Singapore"];
Chart.defaults.global.backgroundColor = '#f9f9f9'
Chart.defaults.global.defaultFontColor = '#f9f9f9'
const config = {
type: "horizontalBar",
data: {
labels: xValues,
datasets: [
{
label: 'Bicycle/Electric Bikes',
backgroundColor: "rgba(160, 206, 128, 0.8)",
data: [0.8, 1.5, 5.6, 3.25, 20, 1, 0.3, 2.08],
maxBarThickness: 30,
link: '#data-bicycle'
},
{
label: 'Electric Car',
backgroundColor: "rgba(249, 199, 132, 0.8)",
data: [25, 2.5, 34.3, 25.26, 4.87, 1.1, 1.15, 0.2],
maxBarThickness: 30,
link: '#data-electric-car'
},
{
label: 'Electric Bus/Trolley Bus',
backgroundColor: "rgba(237, 106, 90, 0.8)",
data: [4.7, 12, 5.15, 7.64, 11.11, 0.83, 0.77, 0.79],
maxBarThickness: 30,
link: '#data-bus'
},
{
label: 'Tram',
backgroundColor: "rgba(205, 160, 204, 0.8)",
data: [2, 0.08, 1.81, 16.3, 0, 3.1, 0, 0],
maxBarThickness: 30,
link: '#data-tram'
},
{
label: 'Metro/Trains',
backgroundColor: "rgba(81, 163, 163, 0.8)",
data: [2.68, 66.59, 49.22, 32.6, 49.39, 56, 19.14, 62.4],
maxBarThickness: 30,
link: '#data-train'
},
{
label: 'Electric Ships',
backgroundColor: "rgba(52, 107, 134, 0.8)",
data: [0.002, 0.24, 0.16, 0.06, 0.04, 0.2, 0, 0.02],
maxBarThickness: 30,
link: '#data-ship'
},
{
label: 'Others',
backgroundColor: "rgba(249, 249, 249, 0.2)",
data: [64.818, 17.09, 3.76, 14.89, 14.59, 37.77, 78.64, 34.51],
maxBarThickness: 30,
link: '#data-other'
}
],
},
options: {
legend: {
display: true,
},
title: {
display: false,
},
scales: {
xAxes: [{
ticks: {
min: 0,
max: 100,
},
type: 'linear',
stacked: true,
gridLines: {
color: 'rgba(255, 255, 255, 0.1)'
},
afterTickToLabelConversion : function(q){
for(var tick in q.ticks){
q.ticks[tick] += '%';
}
},
}],
yAxes: [{
stacked: true,
gridLines: {
display: false,
},
}],
},
responsive: true,
maintainAspectRatio: false,
}
};
document.addEventListener('DOMContentLoaded', function() {
const ctx = document.getElementById('hero-chart');
const heroChart = new Chart(ctx, config);
function clickHandler(event) {
const firstPoint = heroChart.getElementsAtEventForMode(event, 'nearest', { intersect: true }, true)[0];
if (firstPoint) {
const dataset = heroChart.data.datasets[firstPoint.datasetIndex];
const link = dataset.link;
if (link) {
scrollToSection(link);
}
}
}
ctx.onclick = clickHandler;
function scrollToSection(sectionId) {
const section = document.querySelector(sectionId);
if (section) {
section.scrollIntoView({ behavior: 'smooth' });
}
}
});
</script>
<section class="data-bicycle"></section>
<section class="data-car"></section>
<section class="data-bus"></section>
<section class="data-tram"></section>
<section class="data-trains"></section>
<section class="data-ship"></section>
<section class="data-other"></section>
I tried to make the page scrolled down to specific sections when the bar is clicked
I tried different codes for 24 hours but nothing seems to work as expected