score:0
It seems you might be actually be talking about data labels and not the scale labels. In this case you'd want to use the pointLabelFontSize
option. See below example:
var ctx = $("#myChart").get(0).getContext("2d");
var data = {
labels: ["Eating", "Sleeping", "Coding"],
datasets: [
{
label: "First",
strokeColor: "#f00",
pointColor: "#f00",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "#ccc",
data: [45, 59, 90]
},
{
label: "Second",
strokeColor: "#00f",
pointColor: "#00f",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "#ccc",
data: [68, 48, 40]
}
]
};
// This is the important part
var options = {
pointLabelFontSize : 20
};
var myRadarChart = new Chart(ctx).Radar(data, options);
Finally you may want to play with the dimensions of your < canvas > element as I've found sometimes giving the Radar chart more height helps the auto scaling of everything.
score:0
I found the best way to manipulate the labels on the radar chart was by using the pointlabels configuration from Chartjs.
let skillChartOptions = {
scale: {
pointLabels: {
callback: (label: any) => {
return label.length > 5 ? label.substr(0, 5) + '...' : label;
},
}, ...
}, ...
}
score:0
I'd like to further extend on Fermin's answer with a slightly more readable version. As previously pointed out, it's possible to give Chart.js an array of strings to make it wrap the text. To make this array of strings from a longer string, I propose this function:
function chunkString(str, maxWidth){
const sections = [];
const words = str.split(" ");
let builder = "";
for (const word of words) {
if(word.length > maxWidth) {
sections.push(builder.trim())
builder = ""
sections.push(word.trim())
continue
}
let temp = `${builder} ${word}`
if(temp.length > maxWidth) {
sections.push(builder.trim())
builder = word
continue
}
builder = temp
}
sections.push(builder.trim())
return sections;
}
const str = "This string is a bit on the longer side, and contains the long word Supercalifragilisticexpialidocious for good measure."
console.log(str)
console.log(chunkString(str, 10))
.as-console-wrapper {
max-height: 100vh!important;
}
score:1
Unfortunately there is no solution for this until now (April 5th 2016). There are multiple issues on Chart.js to deal with this:
- https://github.com/nnnick/Chart.js/issues/358 (closed with fix)
- https://github.com/nnnick/Chart.js/issues/608 (closed with no fix)
- https://github.com/nnnick/Chart.js/issues/358 (closed with no fix)
- https://github.com/nnnick/Chart.js/issues/780 (closed with no fix)
- https://github.com/nnnick/Chart.js/issues/752 (closed with no fix)
This is a workaround: Remove x-axis label/text in chart.js
score:2
You can write a javascript function to customize the label:
// Interpolated JS string - can access value
scaleLabel: "<%=value%>",
See http://www.chartjs.org/docs/#getting-started-global-chart-configuration
score:10
To wrap the xAxes label, put the following code into optoins. (this will split from white space and wrap into multiple lines)
scales: {
xAxes: [
{
ticks: {
callback: function(label) {
if (/\s/.test(label)) {
return label.split(" ");
}else{
return label;
}
}
}
}
]
}
score:30
With ChartJS 2.1.6 and using @ArivanBastos answer
Just pass your long label to the following function, it will return your label in an array form, each element respecting your assigned maxWidth.
/**
* Takes a string phrase and breaks it into separate phrases
* no bigger than 'maxwidth', breaks are made at complete words.
*/
function formatLabel(str, maxwidth){
var sections = [];
var words = str.split(" ");
var temp = "";
words.forEach(function(item, index){
if(temp.length > 0)
{
var concat = temp + ' ' + item;
if(concat.length > maxwidth){
sections.push(temp);
temp = "";
}
else{
if(index == (words.length-1)) {
sections.push(concat);
return;
}
else {
temp = concat;
return;
}
}
}
if(index == (words.length-1)) {
sections.push(item);
return;
}
if(item.length < maxwidth) {
temp = item;
}
else {
sections.push(item);
}
});
return sections;
}
console.log(formatLabel("This string is a bit on the longer side, and contains the long word Supercalifragilisticexpialidocious for good measure.", 10))
score:59
For Chart.js 2.0+ you can use an array as label:
Quoting the DOCs:
"Usage: If a label is an array as opposed to a string i.e. [["June","2015"], "July"] then each element is treated as a seperate line."
var data = {
labels: [["My", "long", "long", "long", "label"], "another label",...],
...
}
Source: stackoverflow.com
Related Query
- Chart.js and long labels
- Chart JS: Ignoring x values and putting point data on first available labels
- chart js Labels and Grouping labels
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- How to draw outer labels for polar chart using ng2-charts and chart.js at fixed positions outside the rings?
- How do I customize y-axis labels and randomly pick the value from the data range for x-axis in Chart js
- Click on interactive chart.js bar chart and get value for labels and groups in JS
- Labels (category type) on left and right of bar chart using chart.js?
- Chart, X and Y-Axis labels are blurred in horizontal bar chart using chart.js
- how can i show labels and value in both on bar chart
- X and Y axis labels not displaying on line chart (Chart.js)
- how to write labels along with data on top and bottom of each stack in bar chart
- Chart.js how to show line chart without displaying the labels on xaxis and yaxis
- How remove duplicates xAxis labels and show all values on chart
- How to increase the spacing between labels and the chart
- Chart js: I'm getting the labels crossed on my pieChart and doughnut. Not able to get the chart itself
- I am using chart js to draw a chart. I did everything right but i don't know why the x axis and y axis label is not comming in chart. code below
- how to show X and Y labels on points in Chart js 2 Line Chart
- Chartjs create chart with big data and fixed labels
- Chart.js, PHP and radar chart labels
- Chart.js bar chart bars and labels do not align
- In Chart.js set chart title, name of x axis and y axis?
- Limit labels number on Chart.js line chart
- Chart.js Show labels on Pie chart
- Chart.js - Increase spacing between legend and chart
- ng2-charts update labels and data
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- chart.js: Show labels outside pie chart
- Chart.js how to show cursor pointer for labels & legends in line chart
- Chart.js: bar chart first bar and last bar not displaying in full
More Query from same tag
- Chartjs: How do you toggle the visibility of charts in Ionic 4
- Adding trendlines to existing chart Chart.js
- Change Chart JS Bar Chart Border to Dotted Line
- Show nested objects in Chart.js
- Using Chart.js with Dates as x-Axis labels
- Chart.js 2.2.1 - Chart doesn't render correctly before resizing
- Bootstrap grid not working with canvas
- How to mutate VueJS prop?
- How to loop over Chart.js with Django list view
- Load data from Google Analytics in a Chart.js chart
- Grouped bar chart with label in Chart.js
- How to destroy line graph in ChartJS
- Run Chartjs Dart example
- Chart options in angularJs (NodeRed Charts)
- Referencing locally declared variable into an array for data for chartjs
- Chart.js Subtitle won't display
- ChartJS remove previous chart when making new one
- Nest function not summarising values
- Y Axes not displaying properly on Chartjs
- Display a single tooltip with Chart.JS?
- Chart.js - scaleType='date' not working
- Chartjs barchart generate data for an array dataset
- Is it possible to maintain two different font size in Chart.JS based on screen size?
- Bar chart not starting at 0
- Resizing chart before downloading as image
- Including Chart.js in a webpage without node.js and without CDN
- Chart JS : Display the percentage of labels without getContext('2d')
- Why is ChartJS combining two charts?
- Changing x axis labels in Chart.js line chart
- Remove padding from chartJs horizontal bar chart