score:8
Accepted answer
You can use the following tooltip's label callback function, for showing different tooltip labels when hovered on different graphs :
tooltips: {
callbacks: {
label: function(t, d) {
var xLabel = d.datasets[t.datasetIndex].label;
var yLabel = t.yLabel;
// if line chart
if (t.datasetIndex === 0) return xLabel + ': ' + yLabel + ' unit(s)';
// if bar chart
else if (t.datasetIndex === 1) return xLabel + ': $' + yLabel.toFixed(2);
}
}
}
also, your first dataset should be for line
chart, followed by bar
, like so :
datasets: [{
type: "line",
label: 'Quantity Sold ',
data: quantityData
}, {
label: 'Total Profit ',
data: amountData
}]
ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ ⧩
var chart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
datasets: [{
type: "line",
label: 'Quantity Sold',
data: [40, 50, 60, 70, 80],
borderColor: 'orange',
fill: false
}, {
label: 'Total Profit',
data: [1399.3, 356.62, 1249, 465.23, 1480.4],
backgroundColor: 'rgba(0, 119, 220, 0.4)',
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
},
tooltips: {
callbacks: {
label: function(t, d) {
var xLabel = d.datasets[t.datasetIndex].label;
var yLabel = t.yLabel;
// if line chart
if (t.datasetIndex === 0) return xLabel + ': ' + yLabel + ' unit(s)';
// if bar chart
else if (t.datasetIndex === 1) return xLabel + ': $' + yLabel.toFixed(2);
}
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.0/Chart.min.js"></script>
<canvas id="ctx"></canvas>
Source: stackoverflow.com
Related Query
- Chart.js tooltip hover customization for mixed chart
- show label in tooltip but not in x axis for chartjs line chart
- ChartJS: Show all labels of a mixed chart in the tooltip
- Add a text as tooltip for each point on a line chart
- How can I have different values for the chart and the tooltip in chart.js?
- Issue with chartjs linear gradient for the mixed bar chart in ReactJS is not calculated for each individual Bars
- Chart.js two y axes line chart tooltip value incorrect for 2nd y axis
- How to customize tooltip on mouse hover of a polar area chart of Angular Charts
- Chartjs : Can I overwrite bar value on hover for every bar chart
- ChartJS/High Charts Radar chart - Different radial axis labels for each category that appear on hover
- Charts JS: Doughnut chart and hiding tooltip for specific data index within each dataset
- chart.js 3 stacked bar chart - tooltip showing for zero values
- How to change the position of the tooltip for the Bar type chart in Chart.Js
- Chart JS attempt to stacked bar chart tooltip for only one dataset
- getting additional value fields from data source for dx.chartjs doughnut chart
- Disable tooltip for Line chart for one line
- chart js double tooltip on hover
- Is there any way to show a tooltip for points which are not visible on the chart in Chart.js?
- Chart.js doughnut chart isn't showing tooltip when you hover over the left/right of the doughnut
- Chart JS: Donut/Doughnut Chart: Tooltip to be shown always for all the data. All tooltip is not shown when multiple data are with 0 data
- How to clear a chart from a canvas so that hover events cannot be triggered?
- How can labels/legends be added for all chart types in chart.js (chartjs.org)?
- Chart Js Change Label orientation on x-Axis for Line Charts
- Chartjs random colors for each part of pie chart with data dynamically from database
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- How to disable a tooltip for a specific dataset in ChartJS
- Chart.js - Hover labels to display data for all data points on x-axis
- Chartjs v2 - format tooltip for multiple data sets (bar and line)
- Chart JS custom tooltip option?
- Chart.js how to show cursor pointer for labels & legends in line chart
More Query from same tag
- change date format in tooltip of chart.js
- Why are these 2.9 ChartJS bar charts different?
- Get Dataset Values from Chart JS onHover in Version 3
- Use dynamic data in Chart.js with Node.Js
- Problem in passing values using props in react.js while using chart.js
- Display multiple datasets from array with chart.js
- Can I color the first bar in "Bart Char" differently?
- C# MVC5 JavaScript Chart.js Pie Chart with Realtime Updating from SQL Server Database without Refreshing
- Chart.js Auto Fit Failing
- Getting TypeError: (intermediate value).Bar is not a function with react and chart.js
- Javascript generate random color with equivalent highlight
- How to add panning to chart in chartjs?
- chart.js onAnimationProgress progress percentage
- Update all Chart.js instances in order to apply changed defaults?
- How could be the best way of implementing a 3D fft chart?
- Legend option destroys pie chart in Chart.js
- React - How can I pass API data from one component to another in a different js file?
- Is it possible to add a click event in chart.js that directs you to a url?
- chart.js chart failing to render
- Creating multiple canvas patterns fails
- What kind of graph could I use to achieve this with ChartJS (or similar)?
- Django and chart.js - one bar per 'date' in model
- chartjs cutoutPercentage and tooltips does not works in nextjs
- chart.js Failed to create chart: can't acquire context from the given item
- Is there a way to change part of the title to another font style or set a custom title format in Chart.js?
- Chart.js with Django - Using REST
- How do I make many ChartJS charts on the same page to have the same height and also even column width?
- Chart.js add padding to scales
- Trying to call API and plot a line chart but it does not show
- Chart.js and react-chartjs-2 property translate is missing type