I am using Chartjs with React. I have a cryptocurrency price chart with three datasets with one being a list of prices, one being a horizontal line that is tangent to the initial price and another horizontal line that is a tangent of the highest point. Here they are:
datasets: [
{
label: 'pricesData',
fill: false,
lineTension: 0,
backgroundColor: 'rgba(75,192,192,1)',
borderColor: chartColor,
borderWidth: 2,
data: prices
},
{
label: '0 line',
radius: 0,
fill: false,
borderDash: [10,5],
data: zeroLine,
backgroundColor: 'rgba(255,255,255,1)',
borderColor: 'rgba(255, 255, 255,.5)',
},
{
label: 'top Line',
radius: 0,
fill: false,
borderDash: [10,5],
data: topLine,
borderColor: 'rgba(255, 255, 255,0)'
}
I am trying to create a feature that changes the displayed price in another component based on where you are on the graph. Here is the callback function in the label option:
callbacks: {
label: function(tooltipItem, data) {
var item = data.datasets[0].data[tooltipItem.index];
var zeroLine = data.datasets[1].data[tooltipItem.index]
let properties = {
price: item,
percentChange: (((item-zeroLine)/zeroLine)*100).toFixed(2)
}
props.parentCallback(properties);
return(item)
}
}
Now this works like expected except it runs three times and the returned item fills the tooltip with three instances. I imagine this is because I have three datasets. How would I prevent this and only call the props callback once and fill the tooltip with only one value? There is also a slight framerate hitch that I imagine is caused by this.
I have answered my own question. For anyone with a similar issue you can configure your tooltip with this option so that it will only use one dataset:
tooltips: {
filter: function (tooltipItem) {
return tooltipItem.datasetIndex === 0;
}
}