score:20
Unfortunately, since there is no config option to handle this the only way you can achieve the desired result is to extend Chart.Legend and implement the afterFit()
callback.
Here is a quick codepen showing how to do just that. To change the spacing, just change the value in line 9 (currently set to 50). Also, this of course only works with the legend at the top. Hopefully, the example is clear enough for you to modify in case you want to move your legend elsewhere.
score:-7
You can use layout property under options. This helped me:
layout: {
padding: {
left: 50,
right: 130,
top: 0,
bottom: 0
}
}
score:0
After searching the chart.js file I found out that the height of the labels bar is defined there in
height = this._fitRows(titleHeight, fontSize, boxWidth, itemHeight)
OR
this.height = Math.min(height, options.maxHeight || this.maxHeight)
in the fit() function
fit() {
const {options, ctx} = this;
if (!options.display) {
this.width = this.height = 0;
return;
}
const labelOpts = options.labels;
const labelFont = toFont(labelOpts.font);
const fontSize = labelFont.size;
const titleHeight = this._computeTitleHeight();
const {boxWidth, itemHeight} = getBoxSize(labelOpts, fontSize);
let width, height;
ctx.font = labelFont.string;
if (this.isHorizontal()) {
width = this.maxWidth;
height = this._fitRows(titleHeight, fontSize, boxWidth, itemHeight);
} else {
height = this.maxHeight;
width = this._fitCols(titleHeight, fontSize, boxWidth, itemHeight);
}
this.width = Math.min(width, options.maxWidth || this.maxWidth);
this.height = Math.min(height, options.maxHeight || this.maxHeight) + 40;}
And I changed it to +40 as shown above. and it worked fine for me so I wanted to share.
score:1
I have tried the above approaches on my react(react-chartjs-2) project but no luck. Here I have one different approach like creating custom legends outside the chart. so you can get more control over it
- Hide default legend
- Get legend object using any ref method.
- Loop and make a custom legend using html and css.
- Sample codesanbox
I am aware that OP is not about reactjs component, but as it is a common issue it will help someone.
.
score:5
I'm using react-chartjs-2 (but this is just a port and uses the same configurations object) and I was able to achieve that by changing the labels configuration nested on legend configuration:
chartOptions = {
legend: {
labels: {
padding: 50 -> this one.
}
},
You can check the property description here: https://www.chartjs.org/docs/latest/configuration/legend.html
Hope it helps.
score:9
This helped me after 2 days of research.
Chart.Legend.prototype.afterFit = function() {
this.height = this.height + 50;
};
update this in module.ts file
score:12
If anyone is wondering why the afterFit
solution is not working in Chart.js 3.3.0 it is because afterFit
function was removed from the legend plugin.
If you want to make this work anyway by taking advantage over the fit
function, you can try this hacky solution / workaround:
const plugin = {
beforeInit(chart) {
// Get reference to the original fit function
const originalFit = chart.legend.fit;
// Override the fit function
chart.legend.fit = function fit() {
// Call original function and bind scope in order to use `this` correctly inside it
originalFit.bind(chart.legend)();
// Change the height as suggested in another answers
this.height += 15;
}
};
}
I know that this is not an ideal solution, but until we have native support for this legend padding, I'm afraid this is as good we can do right now.
score:30
If you want to apply padding below legend for some charts only in your app:
ChartJS >= 2.1.0
Chart.plugins.register({
id: 'paddingBelowLegends',
beforeInit: function(chart, options) {
chart.legend.afterFit = function() {
this.height = this.height + 50;
};
}
});
// ----------------------------------
// disable the plugin only for charts
// where you DO NOT WANT the padding
// ----------------------------------
// for raw ChartJS use:
var chart = new Chart(ctx, {
config: {
plugins: {
paddingBelowLegends: false
}
}
});
// for angular-chartjs:
$scope.myChart.options.plugins = { paddingBelowLegends: false }
// then in template:
// <canvas class="chart ..." chart-options="myChart.options" ... />
ChartJS >= 2.5.0
Specific plugins for each chart are supported, it should be possible to do:
var chart = new Chart(ctx, {
plugins: [{
beforeInit: function(chart, options) {
chart.legend.afterFit = function() {
this.height = this.height + 50;
};
}
}]
});
See ChartJS documentation + inspired by this other answer
score:42
If you want do increase spacing in all charts you can put this code before creating :
Chart.Legend.prototype.afterFit = function() {
this.height = this.height + 50;
};
Of course, I don't try but i think you can change it (or copy the original Chart object before, to keep the original padding).
Bye,
Source: stackoverflow.com
Related Query
- Chart.js - Increase spacing between legend and chart
- React chartjs-2 - Increase spacing between legend and chart
- Chart.js - Increase spacing between bottom legend and chart
- How can I reduce the spacing between my legend and chart proper in my Chart.JS bar chart, and increase it in the graph area?
- Increase padding between legend and chart with react-chartjs-2
- Increase space between legend and chart
- how to increase space between legend and chart in chartjs (ng2charts ) using angular
- How to increase the spacing between labels and the chart
- How do I increase the space between the Legend and the Chart when using angular-chart.js?
- Chart,js Pie Chart can the gap between a pie chart and the legend be adjusted
- to increase space between x axis and first horizontal bar in chart js
- How to increase space between label and chart area in chart.js
- Increase gap between line-chart and legend ChartJS
- Create space between legend and chart in charts.js
- chart.js line chart and "correct" spacing between points? (i.e. horizontal position based on percent of width, not fixed)
- ChartJS - How to add Text between Pie Chart and Legend
- How to increase the size of the donut or pie chart and keep the legend next to it in chart JS?
- Chartjs Line chart options display increase and decrease percentage between each datapoint
- ChartJS distinguish clicks between chart area, dataset and legend
- How to increase space between chart and legend? (googlecharts)
- Chart js: how can I align the legend and the title
- increase the gap between y-axis and first x-axis value chart.js
- JsPDF and chart.js: Increase chart resolution
- Chart.js - Increase space between y Axes and the first column bar
- How to create spacing between Charts and Legends in Chart.js
- Chart JS: Set Spacing Between Ticks on Y-Axis
- Doughnut chart from Chart.js, custom legend and call of original handler not works as expected
- Draw line between starting point and Ending point in semi doughnut chart in chart js
- ng2 charts bar chart need spacing between 2 bars in series Angular
- Chart.js, increase space between bars and its labels when increasing the charts width
More Query from same tag
- Chart.js line doesn't fit in
- onZoom not triggered when zooming
- How to change grid line width for one specific line
- Convert a dynamic piechart from CanvasJS to Google Charts?
- How can I show percentages when hovering over my chart?
- Defining y-starting point for floating horizontal bar charts in Chart.js?
- Chart JS - single lines (points) tooltips
- Property 'elements' does not exist on type 'typeof Chart'
- How to customize percentage label in Doughnutchart ng2 chart.js
- Chartjs displays numbers not time
- ChartJS doughnut colors not showing from a Flask app. All gray
- Chart.js Tooltips customization
- Boxplot chartjs make min and max dynamic by leaving some space on both side
- Tiny error in the ChartJs book
- Chart.js showing only last data value
- Chart.js 2.0 - How to change default appearance of canvas/chart elements
- ChartJs days of the week from numbers
- Styling background (fill) with ChartJs and React
- Passing json_encode to chart.js won't work
- Is it possible to maintain two different font size in Chart.JS based on screen size?
- How to check if HTML5 canvas element contains something in given coordinate region using javascript
- How can I set the scale with alphabet on chartjs?
- Resize bars' chart in ReactChartJS
- chart.js increase value on click
- javascript chart library for every minutes data
- Stacked Bar Chart, Two on Same Line w/ Same Color
- Render Javascript in DOMPDF is not working as expected
- Get data from dynamic ID for Chart.js
- Accessing data in an associative array in CodeIgniter 3
- Chart.js annotations in Vue.js break when defined in data ()