Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
68
rated 0 times [  73] [ 5]  / answers: 1 / hits: 22860  / 10 Years ago, tue, february 3, 2015, 12:00:00

I have some bar charts that have really long labels and they affect the size of the charts.



Example: http://jsfiddle.net/norbiu/aqa8w19d/4/



I'm trying to truncate the labels that show up under the chart while keeping the label that shows up in the tooltips when hovering over a bar. The problem is that the tooltips and the canvas labels both get their values from the labels array in the data structure. Truncating the value will show the shortened version in both locations.



sales: ko.observable({
labels: ['A really really long label', 'Another long labe', 'A third label that is long', 'Q4', 'Q5', 'Q6'],
datasets: [{
label: 'Helados',
fillColor: '#152491',
data: [70, 32, 6, 23, 63, 43]
}]
}),


The documentation has nothing on this. Any ideas?


More From » chart.js

 Answers
40

In Chart JS V2 you can truncate labels passing the options object.
Also you can customize the tooltips.



options:{
scales: {
xAxes: [{
ticks: {
callback: function(value) {
return value.substr(0, 10);//truncate
},
}
}],
yAxes: [{}]
},
tooltips: {
enabled: true,
mode: 'label',
callbacks: {
title: function(tooltipItems, data) {
var idx = tooltipItems[0].index;
return 'Title:' + data.labels[idx];//do something with title
},
label: function(tooltipItems, data) {
//var idx = tooltipItems.index;
//return data.labels[idx] + ' €';
return tooltipItems.xLabel + ' €';
}
}
},
}

[#67972] Sunday, February 1, 2015, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
malkajillc

Total Points: 652
Total Questions: 107
Total Answers: 98

Location: Finland
Member since Sat, Nov 6, 2021
3 Years ago
malkajillc questions
;