Javascript examples for Chart.js:Legend
Chart.js - Increase spacing between legend and chart
<html lang="en"> <head> <title>Chart.js Add Space After Legend (Placed On Top)</title> </head> <body translate="no"> <div id="canvas-holder" style="width: 600px;"> <canvas id="chart-area" width="300" height="300"></canvas> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.bundle.min.js"></script> <script> function getBoxWidth(labelOpts, fontSize) { return labelOpts.usePointStyle ?// ww w. j ava 2s . c o m fontSize * Math.SQRT2 : labelOpts.boxWidth; }; Chart.NewLegend = Chart.Legend.extend({ afterFit: function() { this.height = this.height + 50; }, }); function createNewLegendAndAttach(chartInstance, legendOpts) { var legend = new Chart.NewLegend({ ctx: chartInstance.chart.ctx, options: legendOpts, chart: chartInstance }); if (chartInstance.legend) { Chart.layoutService.removeBox(chartInstance, chartInstance.legend); delete chartInstance.newLegend; } chartInstance.newLegend = legend; Chart.layoutService.addBox(chartInstance, legend); } Chart.plugins.register({ beforeInit: function(chartInstance) { var legendOpts = chartInstance.options.legend; if (legendOpts) { createNewLegendAndAttach(chartInstance, legendOpts); } }, beforeUpdate: function(chartInstance) { var legendOpts = chartInstance.options.legend; if (legendOpts) { legendOpts = Chart.helpers.configMerge(Chart.defaults.global.legend, legendOpts); if (chartInstance.newLegend) { chartInstance.newLegend.options = legendOpts; } else { createNewLegendAndAttach(chartInstance, legendOpts); } } else { Chart.layoutService.removeBox(chartInstance, chartInstance.newLegend); delete chartInstance.newLegend; } }, afterEvent: function(chartInstance, e) { var legend = chartInstance.newLegend; if (legend) { legend.handleEvent(e); } } }); window.chartColors = { red: 'rgb(255, 99, 132)', orange: 'rgb(255, 159, 64)', yellow: 'rgb(255, 205, 86)', green: 'rgb(75, 192, 192)', blue: 'rgb(54, 162, 235)', purple: 'rgb(153, 102, 255)', grey: 'rgb(231,233,237)' }; var config = { type: 'doughnut', data: { datasets: [{ data: [300, 50, 100, 40, 10], backgroundColor: [ window.chartColors.red, window.chartColors.orange, window.chartColors.yellow, window.chartColors.green, window.chartColors.blue, ], }], labels: [ "Red", "Orange", "Yellow", "Green", "Blue" ] }, options: { responsive: true, legend: { display: true, labels: { padding: 20 }, }, tooltips: { enabled: false, } } }; window.onload = function() { var ctx = document.getElementById("chart-area").getContext("2d"); window.myPie = new Chart(ctx, config); }; </script> </body> </html>