I am looking for a solution to display some data based on what you hover on inside the chart. Here’s my jsfiddle and a demo of what I want.
gif image
I hope I’ve made everything clear, and thanks in advance!
const chart = document.getElementById('chart');
new Chart(chart, {
type: 'doughnut',
data: {
labels: ['Bitcoin', 'Ethereum', 'Litecoin', 'Tether'],
datasets: [{
data: [50, 30, 10, 10],
backgroundColor: [
'orange',
'purple',
'darkblue',
'green'
],
borderWidth: 2,
}]
},
options: {
responsive: false,
cutout: '72%',
plugins: {
legend: {
display: false,
},
},
}
});
.chart-wrap {
display: flex;
align-items: center;
justify-content: center;
}
.chart-balance {
position: absolute;
}
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<div class="chart-wrap">
<canvas id="chart" class="kz-relative kz-index-10" width="224" height="224"></canvas>
<div class="chart-balance">
<p class="chart-asset-type">Ethereum</p>
<h5 class="chart-asset-value">$1,000</h5>
</div>
</div>
2
Answers
The problem with Chart.js is that it creates a canvas and you cannot manipulate these elements with external javascript code. I really don’t know if there are possible configurations to simulate what you want in Chart.js because I know very little about it.
Anyway, I managed to do it with the Plotly library. With it I do part of the work with a circular chart and, after that, we have SVG elements in the HTML. With this I can manipulate the default tooltip values so that it appears in the center in custom elements.
The complete code is there, but I recommend seeing the Plotly documentation and if you don’t know about MutationObserver.
You can use the onHover hook to update your HTML