score:38
Accepted answer
You could achieve that using the following tooltips callback function ...
callbacks: {
label: function (t, d) {
if (t.datasetIndex === 0) {
return '$' + t.yLabel.toFixed(2)
} else if (t.datasetIndex === 1) {
return Math.round(+t.yLabel.toString().replace(/(\d{2})(.*)/, '$1.$2')) + 'M';
}
}
}
ᴅᴇᴍᴏ
var ctx = document.getElementById("canvas").getContext("2d");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["January", "February", "March", "April", "May"],
datasets: [{
type: 'line',
label: "Sales",
data: [144.36534, 146.42534, 145.23534, 147.19534, 145],
fill: false,
borderColor: '#EC932F',
backgroundColor: '#EC932F',
tension: 0,
yAxisID: 'y-axis-2'
}, {
type: 'bar',
label: "Visitor",
data: [22345343, 23345343, 24345343, 25345343, 230245343],
backgroundColor: '#71B37C',
yAxisID: 'y-axis-1'
}]
},
options: {
responsive: false,
tooltips: {
mode: 'index',
intersect: false,
callbacks: {
label: function (t, d) {
if (t.datasetIndex === 0) {
return '$' + t.yLabel.toFixed(2);
} else if (t.datasetIndex === 1) {
if (t.yLabel.toString().length === 9) {
return Math.round(+t.yLabel.toString().replace(/(\d{3})(.*)/, '$1.$2')) + 'M';
} else return Math.round(+t.yLabel.toString().replace(/(\d{2})(.*)/, '$1.$2')) + 'M';
}
}
}
},
scales: {
yAxes: [{
id: "y-axis-1",
position: "left"
}, {
id: "y-axis-2",
position: "right"
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<canvas id="canvas" width="400" height="190"></canvas>
score:0
Thanks, GRUNT! But since my datasets could me mixed it's better to use the yAxisID to check for the correct dataset.
tooltips: {
callbacks: {
label: function (tooltipItem, details) {
if (details.datasets[tooltipItem.datasetIndex].yAxisID == "$") {
let dataset = details.datasets[tooltipItem.datasetIndex];
let currentValue = dataset.data[tooltipItem.index];
return dataset.label + ": " + currentValue.toFixed(2) + " $";
} else {
let dataset = details.datasets[tooltipItem.datasetIndex];
let currentValue = dataset.data[tooltipItem.index];
return dataset.label + ": " + currentValue +" pieces";
}
}
}
}
score:2
Try using below code!
let DoughnutForSavingCount = {
labels: [
intl.formatMessage({ id: 'Guarantee' }),
intl.formatMessage({ id: 'ILAS' }),
intl.formatMessage({ id: 'No Idea' })
],
datasets: [
/* Outer doughnut data starts*/
{
label: 'Graph1',
data: [
_.get(getClientSavingILASPolicyData[0], 'countwithGuaranttee') >
0 &&
_.get(getClientSavingILASPolicyData[0], 'totalWithGuarantee') ===
0
? 0.1
: _.get(getClientSavingILASPolicyData[0], 'totalWithGuarantee'),
_.get(getClientSavingILASPolicyData[0], 'countwithILAS', 0) > 0 &&
_.get(getClientSavingILASPolicyData[0], 'totalWithILAS') === 0
? 0.1
: _.get(getClientSavingILASPolicyData[0], 'totalWithILAS'),
_.get(getClientSavingILASPolicyData[0], 'countNoIdea', 0) > 0 &&
_.get(getClientSavingILASPolicyData[0], 'totalWithNoIdea') === 0
? 0.1
: _.get(getClientSavingILASPolicyData[0], 'totalWithNoIdea')
],
backgroundColor: ['#8c1aff', '#BF80FF', '#E9e9e9'],
hoverBackgroundColor: ['#8c1aff', '#BF80FF', '#E9e9e9']
},
/* Outer doughnut data ends*/
/* Inner doughnut data starts*/
{
label: 'Graph2',
data: [
_.get(getClientSavingILASPolicyData[0], 'countwithGuaranttee'),
_.get(getClientSavingILASPolicyData[0], 'countwithILAS'),
_.get(getClientSavingILASPolicyData[0], 'countNoIdea')
],
backgroundColor: ['#8c1aff', '#BF80FF', '#E9e9e9'],
hoverBackgroundColor: ['#8c1aff', '#BF80FF', '#E9e9e9']
}
/* Inner doughnut data ends*/
],
borderWidth: [1]
};
let DoughnutForSavingCountConfig = {
cutoutPercentage: 70,
legend: {
display: true,
position: 'bottom',
labels: {
fontColor: '#34A0DC',
fontSize: 10,
fontFamily: 'Helvetica',
boxWidth: 10,
usePointStyle: true
}
},
responsive: true,
plugins: {
datalabels: {
display: false
}
},
tooltips: {
enabled: true,
//*****add for reference********** */
callbacks: {
label: function(tooltipItems, data) {
if (tooltipItems.datasetIndex) {
var label = data.labels[tooltipItems.index] || '';
var currentValue =
data.datasets[tooltipItems.datasetIndex].data[
tooltipItems.index
];
if (label) {
label += ': ';
}
label += currentValue == '0.1' ? '0' : currentValue;
return label;
} else {
var label = data.labels[tooltipItems.index] || '';
var currentValue =
data.datasets[tooltipItems.datasetIndex].data[
tooltipItems.index
];
if (label) {
label += ': ';
}
label += intl.formatMessage({ id: 'HKD' }) + ' ';
label +=
currentValue == '0.1'
? '0'
: currentValue
.toString()
.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
return label;
}
}
}
}
};
Source: stackoverflow.com
Related Query
- Chartjs v2 - format tooltip for multiple data sets (bar and line)
- Chartjs - data format for bar chart with multi-level x-axes
- ChartJS bar chart fixed width for dynamic data sets
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- chartjs display data in one bar line with 3 different data sets in 3 different colors
- Line chart plotting multiple points for duplicate data on x and y axis using chart.js
- Line chart: align x axis (timestamps) for multiple data sets
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- show label in tooltip but not in x axis for chartjs line chart
- ChartJS Line Graph - Multiple Lines, Show one Value on Tooltip
- How to add second Y-axis for Bar and Line chart in Chart.js?
- ng2-charts customize data and whole html content of tooltip displayed when hovering on bar chart
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- ChartJS bar not showing up for simple data points
- How to show data values in top of bar chart and line chart in chart.js 3
- Chart.js: Combined Line and Bar Data
- Missing Tooltip for some data points using chartjs
- Chartjs stacked bar separate tooltip for all stacked
- How to draw multiple line on y axis for same x axis in chartjs v2?
- react-chartjs-2 line chart with time on X-axes with multiple data sets plotted wrong
- ChartJS getting an unwanted line between first data point and last data point
- ChartJS Version 3 - common legend for multiple line charts
- How to create a custom tooltip for chartJS graph with data in JSON array in JavaScript?
- ChartJS 2.9.4 can't overlay line data on Horizontal Bar chart
- Multiple data sets not working when using ChartJS
- How to get fixed legend and tooltip for dynamically generated data vue-chartjs
- ChartJS not showing data for time series bar chart
- How to set min max for x axis depend on data with Chartjs and Spring Boot?
- Charts JS: Doughnut chart and hiding tooltip for specific data index within each dataset
- Chart.js combined line and bar chart with differing data points
More Query from same tag
- my anchor tags are not working in my website after i added chart.js in it
- How can I hide points on chart?
- Horizontal overlapping of bars, not whole graph
- ChartJS and DiscordJS implementation question
- Two tone colours in chart.js pie chart
- Show base64 image next to canvas with charts.js
- How to add an offset to a dataset in Chart js
- Chartjs - Add backgroundColor for labels radar chart
- Ajax reload data chat.js showing error on mouse hover on graph
- Custom Tooltips On Line Chart Using Chart.js
- Chart.js ng2-charts colors in pie chart not showing
- How can I arrange according to month using chart Js?
- In react.js with chart.js, I can't access to my data in a componant
- Chart.js beginAtZero doesn't work
- The chart doesn't display the data from my call to the API with Axios
- How can I debug maxTicksLimits in charts.js?
- Can a service get access to component's template
- How can I give the automatically generated y-axis values on bar charts in Chart.JS commas?
- How to keep bars same size even when adding new datasets
- ChartJS multiple sub labels for label
- Can you set seperate colors for different axes in Chart.js?
- zoom and pan on charts in angular
- Angular-Chart.js chart doesn't shown, if data added automatically
- chartjs Asp.net MVC Remote Data
- Chart.js place y value position and its x-label position relative to the x-value
- Wrong display of xAxes in Chartjs
- How to change space between columns in horizontalBar in Chart.js?
- Loop through multiple rows as labels and data in chart.js and PHP
- How can i get my chartjs to show up graphically?
- Chartjs barchart generate data for an array dataset