score:15
Accepted answer
you can use the following chart plugin :
plugins: [{
beforeinit: function(chart) {
chart.data.labels.foreach(function(e, i, a) {
if (/\n/.test(e)) {
a[i] = e.split(/\n/);
}
});
}
}]
add this followed by your chart options
ᴜꜱᴀɢᴇ :
add a new line character (\n
) to your label, wherever you wish to add a line break.
ᴅᴇᴍᴏ
var chart = new chart(ctx, {
type: 'horizontalbar',
data: {
labels: ['jan\n2017', 'feb', 'mar', 'apr'],
datasets: [{
label: 'bar',
data: [1, 2, 3, 4],
backgroundcolor: 'rgba(0, 119, 290, 0.7)'
}]
},
options: {
scales: {
xaxes: [{
ticks: {
beginatzero: true
}
}]
}
},
plugins: [{
beforeinit: function(chart) {
chart.data.labels.foreach(function(e, i, a) {
if (/\n/.test(e)) {
a[i] = e.split(/\n/);
}
});
}
}]
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.6.0/chart.min.js"></script>
<canvas id="ctx"></canvas>
score:19
if you want to have full control over how long labels are broken down across lines you can specify the breaking point by providing labels in a nested array. for example:
var chart = new chart(ctx, {
...
data: {
labels: [["label1 line1:","label1 line2"],["label2 line1","label2 line2"]],
datasets: [{
...
});
Source: stackoverflow.com
Related Query
- How to make labels on both side from horizontal bar chart js
- How do I draw a vertical line on a horizontal bar chart with ChartJS?
- Make Chart.js horizontal bar labels multi-line
- How to draw Horizontal line on Bar Chart Chartjs
- In Stacked horizontal bar chart how to remove the vertical line in Chart.js?
- Chart.js horizontal line chart or modified horizontal bar chart
- ChartJS: how to make a bar chart with a horizontal guideline:
- Chart.js - Horizontal line on Bar chart interferes with tooltip
- Chart.js - draw horizontal line in Bar Chart (type bar)
- ChartJS 2.9.4 can't overlay line data on Horizontal Bar chart
- Chart, X and Y-Axis labels are blurred in horizontal bar chart using chart.js
- Remove top horizontal line in a horizontal bar chart (Chart.js 2)
- Is there any way to remove extra space and Horizontal line from Bar chart of Chart.js?
- Chart.js how to display multiple labels on multi bar stacked chart
- How to draw multiple color bars in a bar chart along a Horizontal Line using chart.js
- Drawing a horizontal line on a stacked bar chart on hover (chart js)
- Draw stacked horizontal bar chart with Average line using ChartJS
- Chart.js multi line chart that has different labels
- ChartJS: Is this horizontal stacked bar and line graph multiaxis chart even possible?
- Can I make a horizontal bar chart with two category axes?
- Chart.js Labelling - Need Two Labels on Y-axis of Horizontal Bar Chart
- Limit labels number on Chart.js line chart
- Draw horizontal line on chart in chart.js on v2
- How can I create a horizontal scrolling Chart.js line chart with a locked y axis?
- create a multi line chart using Chart.js
- Horizontal stacked bar chart with chart.js
- Horizontal bar chart in chart.js
- Chart.js how to show cursor pointer for labels & legends in line chart
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- How to set labels align left in Horizontal Bar using chart.js?
More Query from same tag
- How I create a pie chart based on COUNT(php + mysql)?
- How to add legend on chart js annotation
- Emphasize a point in Chart.js
- Make a chart clickable when placed under another SVG <View>
- Chartjs 2 Line Graph Single Stroke Between Datasets
- ChartJS - Time graph from JSON
- How can I use an array of {x,y} objects to make a chart?
- Using Datalabels and ChartJS with Chartjs-node-canvas
- Chart.js stacked bar chart text on top of the stacked bars
- How to add an on click event to my Line chart using Chart.js
- Add data to dynamically created Chart.js dataset
- Error Message when using ChartJS with React
- How can I trigger the hover mode from outside the chart with charts.js 2?
- Problems hovering over ChartJS bar chart after being redrawn
- chartJS or/and php print_r not working together
- Why is chart.js not showing charts?
- Is it possible to add image behind doughnut chart with transparency color in ng2chart?
- Chart Js, Style some ticks on the axis differently
- Multiple charts on same page not working - ng2-charts
- make chartJS labels clickable and get label details
- Chartjs Doughnut disappears
- Data not displaying in Chart JS from PHP JSON
- Live Update Callback -> afterTitle with Array via JSON file
- ChartJS shows chart but no data in ASP net core
- How to destroy line graph in ChartJS
- Vue Chart.js Doughnut Chart with rounded and spaced arcs (vue3-chart-v2)
- ChartJS '_meta' error for AJAX Dataset
- How to open a new Tab with javascript and display some chart
- How to fetch all data from API files and assign their values to chart?
- Chartjs-node installation failing