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
- Charts .js update when array changes
- chartjs loading data from ajax doesn't work fine
- Blazor Chart.js 2.0.2 raises onClick error
- chart.js update is not a function
- Formatting x-axis labels as time values in Chart.js
- Primeng line chart not updating automatically,only update on window refresh
- How to pass an array to chart.js data:[] while creating a chart in php
- Charts.JS - Too Many Charts? - Loading Issue
- Disable stacking for 1 dataset
- Trying to create something from chart.js
- Uncaught ReferenceError: Chart is not defined
- How to add links to chart.js (Doughnut Charts)?
- start y-axis from 0 in ng2-charts
- Chart.js - Creating a single bar chart with states
- Set Start Value as '0' With Dynamic Data In Chart.js
- Adding images/icons to specific coordinates in chart.js
- Chart.js: Legend doesn't update on first call to chart.update()
- Manipulating data point in chart.js external tooltip
- How to make points in chartjs draggable?
- Remove bullet points from chart.js legend
- How to reduce the number of data points displayed in react-chart-js2 (data decimation)
- I want to draw a horizontal line
- getBoundingClientRect gives incorrect value
- Chartjs: Bar width
- How to get data on chart based on selected date (Filtering data Chart JS with datepicker)
- Angular Radar Chart :OnClick event of label or sector
- Changing the background of a ChartJS on node js
- Chart.js scatter plot fill portions of chart based on value
- Chart.js: Don't stretch axes beyond chart
- ChartJS keeps resizing canvas to the default size