The dataset label in my chart.js bar chart does not show correct values. I am trying different chart configurations but nothing seems to work. Here is my JS Fiddle. What I want to achieve is that the bar chart should show values like 'HIGH : 30' or 'MED: 25' but right now the tooltip is showing only the first label (HIGH: 30, HIGH: 25, HIGH: 56) on all the 3 bars. This is strange. Need some assistance.
Some more details about what I tried so far: If I use arroChartConfig1 it gives tooltip title as HIGH for all graphs and label as HIGH: 30 (red bar), HIGH: 25 (yellow bar), HIGH: 56 (blue bar).
If I use arroChartConfig2 it gives correct title and labels but shows only one legend with label HIGH,MED,LOW.
If I use arroChartConfig3 distinct labels in legend but bar tooltip title and labels have values same as in arroChartConfig1.
Thanks in advance.
var oMyBarChart;
var ctx = document.getElementById("canvas").getContext("2d");
function GetSelectedChartType(sSelectedOption){
return sSelectedOption.options[sSelectedOption.selectedIndex].value;
}
arroLevels = ["HIGH","MED","LOW"];
arroLevelsCount = [30,25,56];
arroLevelColors =
['rgba(230,0,0,1)','rgba(255,255,79,1)','rgba(150,198,250,1)'];
arroDataset=[];
sChartType = GetSelectedChartType(document.getElementById('chartSelector'));
arroLevels.map(function(sValue,iIndex){
let oData = [arroLevelsCount[iIndex]];
let sColorData = arroLevelColors[iIndex];
arroDataset.push({
label: sValue,
data: oData,
backgroundColor: sColorData,
pointStyle: 'triangle',
});
});
barChartData = {
labels: arroLevels,
datasets: arroDataset,
}
arroChartConfig1 = {
type: sChartType,
data: barChartData,
options: {
responsive: true,
legend: {
position: 'right',
generateLabels: function(data){
return data.text;
}
},
title: {
display: true,
text: 'My Bar Chart'
},
tooltips: {
callbacks:{
label: function(ttitem,data){
return ttitem.xLabel+": "+ttitem.yLabel
}
}
}
}
}
arroChartConfig2 = {
type: sChartType,
data: {
datasets: [{
label: arroLevels,
data: arroLevelsCount,
backgroundColor: arroLevelColors,
pointStyle: 'triangle',
}],
labels: arroLevels
},
options: {
responsive: true,
legend: {
position: 'right',
generateLabels: function(data){
return data.text;
}
},
title: {
display: true,
text: 'My Bar Chart'
},
tooltips: {
callbacks:{
label: function(ttitem,data){
return ttitem.xLabel+": "+ttitem.yLabel
}
}
}
}
}
arroChartConfig3 = {
type: sChartType,
data: {
datasets: [{
label: "HIGH",
data: ["30"],
backgroundColor: 'rgba(230,0,0,1)',
pointStyle: 'triangle',
},
{
label: "MED",
data: ["25"],
backgroundColor: 'rgba(255,255,79,1)',
pointStyle: 'triangle',
},
{
label: "LOW",
data: ["56"],
backgroundColor: 'rgba(150,198,250,1)',
pointStyle: 'triangle',
}],
labels: ["HIGH","MED","LOW"]
},
options: {
responsive: true,
legend: {
position: 'right',
generateLabels: function(data){
return data.text;
}
},
title: {
display: true,
text: 'My Bar Chart'
},
tooltips: {
callbacks:{
title: function(ttitem,data){
return '';
},
label: function(ttitem,data){
return ttitem.xLabel+": "+ttitem.yLabel;
}
}
}
}
}
//oMyBarChart = new Chart(ctx,arroChartConfig1);
//oMyBarChart = new Chart(ctx,arroChartConfig2);
oMyBarChart = new Chart(ctx,arroChartConfig3);