score:38
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 + ' €';
}
}
},
}
score:0
Just some additional info (can't comment, but thought this would be helpful) I added a small bit of code to the answer above to get "..." to show if labels are truncated.
truncateLabel: function(label) {
var xSubstring = label.substring(0, this.labelLength);
if(xSubstring.length < this.labelLength) {
return xSubstring;
} else {
// Check if a word is cut off
if ( ' ' != label.charAt( (this.labelLength-1) ) && ' ' != label.charAt( this.labelLength ) ) {
// If so, cut the label off at the last space instead of mid-word
var last_space_pos = label.lastIndexOf(" ", this.labelLength);
last_space_pos = (0 > last_space_pos)? this.labelLength: last_space_pos;
xSubstring = label.substring(0, last_space_pos);
}
return xSubstring+"...";
}
},
score:0
This can be achieved without modifying the plugin code with customTooltips:function(tooltip)
option documented here.
A sample code is also provided here to modifying tooltips for line graph. Using that snippet its fairly easy to achieve the desired behavior.
- Create a div for custom tooltip.
- Truncate the labels for x-axis but keep the mapping to original text in an array.
- In customTooltip function, populate tooltip div with original text by using tooltip.text
as key in mapping array.
score:1
So the way i went about this was to add a new option called labelLength
which, if passed a number greater than 0, will trim the labels on the x axis to that length.
It happens in the scale class of the chart and will only apply to the axis label and not the tool tip.
If you wanted you could extract the relevant bits and just override the scale class and also the build scale function in the bar chart to include the new option.
In the scale class here is what was added
Chart.Scale = Chart.Element.extend({
initialize: function() {
//truncate the labels if option is grater than 0
this.xLabels = this.labelLength > 0 ? this.xLabels.map(this.truncateLabel, this) : this.xLabels;
this.fit();
},
truncateLabel: function(label) {
return label.substring(0, this.labelLength);
},
addXLabel: function(label) {
//also added here for when adding single items of data to a graph
this.xLabels.push(this.labelLength > 0 ? this.truncateLabel(label) : label);
this.valuesCount++;
this.fit();
},
}
then in the bar buildscale
function you would need to pass the option to the scale.
Or i have included this in my fork of chart js (https://github.com/leighquince/Chart.js) to use just pass the option labelLength
with your desired labels length, example below
var randomScalingFactor = function() {
return Math.round(Math.random() * 100)
};
var barChartData = {
labels: ["January a really long label", "February a really long label", "March a really long label", "April a really long label", "May a really long label", "June a really long label", "July a really long label"],
datasets: [{
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(220,220,220,0.8)",
highlightFill: "rgba(220,220,220,0.75)",
highlightStroke: "rgba(220,220,220,1)",
data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
}, {
fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,0.8)",
highlightFill: "rgba(151,187,205,0.75)",
highlightStroke: "rgba(151,187,205,1)",
data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
}, {
fillColor: "rgba(15,18,20,0.5)",
strokeColor: "rgba(15,18,20,0.8)",
highlightFill: "rgba(15,18,20,0.75)",
highlightStroke: "rgba(15,18,20,1)",
data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
}]
}
window.onload = function() {
var ctx = document.getElementById("canvas").getContext("2d");
window.myBar = new Chart(ctx).Bar(barChartData, {
labelLength: 10,
});
}
<script src="http://www.quincewebdesign.com/cdn/Chart.js"></script>
<div style="width: 50%">
<canvas id="canvas" height="450" width="600"></canvas>
</div>
Source: stackoverflow.com
Related Query
- Truncating canvas labels in ChartJS while keeping the full label value in the tooltips
- ChartJS 3.7.1 tooltip callback, get label value for the next index
- Chart.js displaying each letter of label and data as a single value on chart. The data and labels provided are arrays
- ChartJS fails to render one of the lines in cartesian chart following update after change to max Y axis label value
- ChartJS - PieChart's Label too long and hide the PieChart
- Chartjs change the specific label color in x axis in callback function
- Remove the label and show only value in tooltips of a bar chart
- change long labels to multiline(wrap) label in ChartJs
- unable to add background color to the canvas using jspdf and chartjs
- Charts.js - Display data label only for the last value
- How to Change the Label Strike-Through with light gray on a ChartJS Doughnut?
- label too long in yAxis with chartJs
- ChartJs - Pie Chart - how to remove labels that are on the pie chart
- Chartjs 3.5.0 - Radar Chart - Converting the labels to images
- ChartJS do not render any legend if the label is falsy
- ChartJS rotate label value vertical
- ChartJS - Finding the minimum and maximum labels that are inside a pan (When zooming)
- ChartJS - How to increase the maximum degree of label rotation on x-axis?
- chartJS label on each side of the chart
- Setting Common labels and background color common for all the charts in ChartJs
- I am using chartjs node canvas to render a chart however the graph options are being ignored
- how to show labels in laravel charts when the name of the label is on another table?
- ChartJS align axis label to the top
- how to put a y-axis and x-axis label while using html and chartjs
- Is there any way to change the font color and size of labels in Chartjs 3.0.0
- How do I customize y-axis labels and randomly pick the value from the data range for x-axis in Chart js
- Conditional in ChartJS axes tick callback function isn't returning the expected labels
- How to set the chartjs bar graph scale to the highest value in the result data
- Why are the chartjs tooltip labels always showing the first x-axis label?
- Full size download Chartjs canvas on mobile device
More Query from same tag
- How to change the Y-Axis to show $ in graph
- How to wait for all items to load within ng-repeat before then rendering a chart for each item
- How to add background color for doughnut mid using chart,js
- React ChartJS prevent new data from being added into state after it's redrawn?
- Chart.js Line graph legend yields error: Uncaught TypeError: Cannot read property '0' of undefined
- ChartJs showing wrong labels of data (x-axis dates)
- different colors for bar chart with chart.js
- Remove data after adding it (chart.js)
- chartjs xaxis ticks with round values from shifted input data
- ngx-charts-advanced-pie-chart is reading my rest api response as null
- How to add new dataset in line chart of chart.js by iterating over dictionary in JavaScript?
- ChartJS dynamically adding values with jQuery array
- Chart.js: Don't stretch axes beyond chart
- How to set label in dataPoints chart on javascript?
- How to create an animate-on-update Stacked Bar chart using Chart.js(or Chartist)?
- laravel chartjs dates skipped
- How to hide other section of chartjs on click of lengend
- ChartJS x axis title not visible when axis position is centered
- Chart.js v2 charts do not display inside angular 2 (but Chart.js v1 works perfectly)
- Chart JS chart not rendering in ArcGIS popup when using navigation arrows
- Is it possible to produce circular (round) shaped radar chart in Chart.js?
- Chart.js responsive pie chart
- Combo bar chart graph line gets obscured by bars
- How to dynamically update data of line chart used with chart Js?
- Can you set seperate colors for different axes in Chart.js?
- Chart.js - add 1 more tick step to an axis
- Chartjs Radar - Change color of end point labels
- Remove Decimal like " [Decimal('1220'), Decimal('160')] "from Django Queryset results
- Chart.js How can i make a horizontal lines like this?
- Horizontal Bar-Chart - angular-chart.js