on Hover tooltip works for one chart at a time, it should display tooltip on both the charts. The second chart does displays active points but not the tooltip. The tooltip is a custom one used here.
const lineChartOptions: ChartOptions<"line"> = {
responsive: true,
maintainAspectRatio: false,
interaction: {
mode: "index",
intersect: false,
},
onHover: handleHover,
plugins: {
crosshair: {
value: 10, // Adjust this value as needed
line: {
color: "red", // crosshair line color
width: 1, // crosshair line width
},
sync: {
enabled: true, // enable trace line syncing with other charts
group: 1, // chart group
// suppressTooltips: false, // suppress tooltips when showing a synced tracer
},
zoom: {
enabled: false,
},
},
legend: {
display: false,
},
tooltip: {
position: "nearest",
enabled: false, // Disable the default tooltip
external: customTooltip, // Enable custom tooltip
callbacks: {
beforeTitle: function (context) {
// return[context.parsed.y]
return [`${context[0].label}`];
},
label: function (context) {
console.log(context);
if (context.datasetIndex === 0) {
return `<div class="flex flex-col">
<div class="flex items-center mt-1">
<div class="w-2 h-2 bg-[#8C94BD] rounded-full mr-2"></div>
<div class="block text-[#8C94BD]">$${context.parsed.y} <span class='text-black pl-2'>${context.dataset.label}</span></div>
</div>
</div>`;
}
if (context.datasetIndex === 1) {
return `<div class="flex flex-col">
<div class="flex items-center mt-1">
<div class="w-2 h-2 bg-[#C3C34B] rounded-full mr-2"></div>
<div class="block text-[#C3C34B]">$${context.parsed.y} <span class='text-black pl-2'>${context.dataset.label}</span></div>
</div>
</div>`;
}
return context.label;
},
title: function (context) {
//Display month and year
// return context[0].label;
},
},
},
},
scales: {
x: {
border: {
dash: [4, 8],
width: 0,
},
ticks: {
callback: function (val: any) {
// Hide every 2nd tick label
const label = this.getLabelForValue(val);
if (label[0] === "Jul") {
const year = label[1]; // Since label is in the format [month, year]
return year;
}
return null;
},
},
offset: true, // This will extend the line chart beyond the data points
// display: false
grid: {
display: true, // Hides/show X-axis gridlines
},
},
y: {
// offset: true,
border: {
dash: [4, 8],
width: 0,
},
//to start with 0 tick
beginAtZero: true,
ticks: {
autoSkip: false,
maxTicksLimit: 6,
callback: function (value) {
return "$" + value;
},
},
grid: {
display: true, // Hide/Shows Y-axis gridlines
},
},
},
};
<Line id="chart1" data={data} options={lineChartOptions} />
<Line id="chart2" data={data} options={lineChartOptions} />
https://www.youtube.com/watch?v=TQ7XYMTr7t4
This is exactly what needs to be achieved but how can it be done the same in nextjs