Javascript examples for Chart.js:Chart Configuration
Make font size of labels in charts.js responsive
<html> <head> <title>Data labels responsive font</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/chart.js@2.7.1/dist/Chart.min.js"></script> <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels@0.2.0"></script> <script type="text/javascript"> window.onload=function(){//from ww w. j a va 2 s. c o m var labels = [0,1,2,3,4,5,6,7,8,9]; var data = [0,1,2,3,2,3,0.5,8,4,2]; var chart = new Chart('chart', { type: 'bar', data: { labels: labels, datasets: [{ backgroundColor: '#ccddee', borderColor: '#5566aa', data: data }] }, options: { legend: false, tooltip: false, layout: { padding: 24 }, plugins: { datalabels: { anchor: 'end', align: 'end', backgroundColor: null, borderColor: null, borderRadius: 4, borderWidth: 1, color: '#223388', font: function(context) { var width = context.chart.width; var size = Math.round(width / 32); return { size: size, weight: 600 }; }, offset: 4, padding: 0, formatter: function(value) { return Math.round(value * 10) / 10 } } } } }); } </script> </head> <body> <div class="wrapper"> <canvas id="chart"></canvas> </div> </body> </html>