score:12
You can do this, but it's a bit hack-y.
First the data labels. In the datalabels config section, you can try something like:
/* Adjust data label font size according to chart size */
font: function(context) {
var width = context.chart.width;
var size = Math.round(width / 32);
return {
weight: 'bold',
size: size
};
}
Change the size calculation as necessary.
For the y-axis labels, there's an answer here, however apparently since Chart.js 2.7.0, the line:
c.scales['y-axis-0'].options.ticks.fontSize
..should be changed to:
c.scales['y-axis-0'].options.ticks.minor.fontSize
(ref)
So to scale the y-axis labels font size according to chart height, it might look like:
plugins: [{
/* Adjust axis labelling font size according to chart size */
beforeDraw: function(c) {
var chartHeight = c.chart.height;
var size = chartHeight * 5 / 100;
c.scales['y-axis-0'].options.ticks.minor.fontSize = size;
}
}]
Note: This requires "maintainAspectRatio:" to be set to "true".
There's still one problem however, and that's that the part of the chart containing the y-axis labels will remain at the same pixel width even when resized.
We need to also resize this area to keep it at a constant % of the overall chart width, e.g. 40%, instead of a fixed pixel width (added to yAxes config section):
/* Keep y-axis width proportional to overall chart width */
afterFit: function(scale) {
var chartWidth = scale.chart.width;
var new_width=chartWidth*0.4;
scale.width = new_width;
}
(You might not notice this as a problem with your original example, since there is a oversized line that seems to cause the y-axis width to keep expanding when the window is enlarged. But when the labels don't overflow, then the width stays constant unless the above is used.)
Complete jsFiddle: https://jsfiddle.net/0kxt25v3/2/ (fullscreen)
I'm not sure about wrapping labels on to the next line, you might just need to pre-process the labels to limit the maximum number of characters per label.
I also haven't attempted to scale the x-axis label font sizes, but it should be easy enough to add it in to the "beforeDraw:" section.
score:0
I ran into the clipping problem myself recently and fixed this by setting a suggestedMax value that was wider than the largest value in my dataset.
score:1
If you're using chartjs-plugin-datalabels; here's how i was able to make the labels responsive. Considering the chart may have variable width and height, we can get the average of both (height and width) and calculate the font size. I am also setting the max font size limit to 12.
datalabels: {
font: function (context) {
var avgSize = Math.round((context.chart.height + context.chart.width) / 2);
var size = Math.round(avgSize / 32);
size = size > 12 ? 12 : size; // setting max limit to 12
return {
size: size,
weight: 'bold'
};
},
}
Source: stackoverflow.com
Related Query
- Making the labels responsive in chart js
- How to align Chart.JS line chart labels to the center
- How to display the labels in doughnut chart using ng2 charts?
- ChartJS: Show all labels of a mixed chart in the tooltip
- ChartJs - Pie Chart - how to remove labels that are on the pie chart
- Chartjs 3.5.0 - Radar Chart - Converting the labels to images
- ChartJS: Remove padding to the right of chart caused by tick labels
- Modify the labels in chart.js when I update the chart
- How to draw outer labels for polar chart using ng2-charts and chart.js at fixed positions outside the rings?
- Pie chart isn't loading, but the labels are
- How do I customize y-axis labels and randomly pick the value from the data range for x-axis in Chart js
- chart.js: How do I make the radar chart axis labels bigger?
- Chart JS stacked labels instead of making them horizontal
- show multiple responsive chart in the same page using chart.js
- How to show the chartjs bar chart data values labels as text?
- ChartJs(Java) Can I remove only specific gridLines for a Linear Chart withouth removing the labels on xAxis?
- How to add labels on top of the chart bar with Chart.js 2
- Angular-Charts bar chart does not update when I change the data, series, labels
- Modifying the X-Axis Labels of a Bar chart in Chart.js 2
- How to display labels outside the pie chart border?
- Is there any way to change the font size of labels in bar chart in Chart.js v3?
- How write the labels of the data inside a Doughnut Chart made with Chart.js?
- Chart.js how to show line chart without displaying the labels on xaxis and yaxis
- How to increase the spacing between labels and the chart
- Chartjs + DataLabelPlugin: Suddenly data labels are shown in every chart even without the plugin?
- Chart js: I'm getting the labels crossed on my pieChart and doughnut. Not able to get the chart itself
- Show the latest labels in a bar chart with React.js using react-chartjs
- 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
- The title of my ChartJs on the chart is undefined, but I can't change it because my labels use a function: Object.keys(groupMedia)
- Aligning zero on y axis to the labels on x axis of line chart in chartjs
More Query from same tag
- Chartjs - Stacked bar chart blocking other values
- what is the correct way to destructure a nested json object with fetch?
- Unable to hide X-Axis in horizontal stacked chart.js
- int Array with empty value
- using array values in chart.js data and label field
- Chart.Js - Background Bar in Chart Bar
- Create data in data with Chartjs
- How to map multiple charts with chart.js in react
- Inserting data from arrays into a pie chart (chart.js)
- Chart.js how to highlight a part of a label
- Chart.js: Dynamic Changing of Chart Type (Line to Bar as Example)
- How to change the label and grid line position on a timeseries chart in Chart.js 3?
- Getting 'Uncaught TypeError: Cannot read property 'helpers' of undefined'
- Angular 5 and chart.js ts error
- Charts not showing in wicked pdf
- Unable to produce desired UI output for Chartjs tooltips
- Chart.js chart positions variable at 0 even though value is >0
- core.js:4197 ERROR TypeError: chart_js__WEBPACK_IMPORTED_MODULE_2__ is not a constructor
- How to increase the spacing between labels and the chart
- How to reduce the gap between bars on bar chart
- Angular 12 + Chartjs 3.3.2 TypeError: Cannot read property 'left'
- How is it possible to make curved bar chart or curved column charts in chart.js?
- Unwanted comma in the tooltips when line breaking a label in ChartsJS
- Chart.js - Bar values showing on the middle of bar
- chartJS, how to disable user from clicking on legend names and changing the graph?
- Charts.js Pie Chart
- Chart.js stacked line chart with differently styled sections
- How to create a gradient fill line chart in latest Chart JS version (3.3.2)?
- Connect the dot vertically instead of horizontally on Line Chart
- Chart.js - Tooltips overlapped by bar values