score:1
You can trim the labels depending on the screen width, like so
Chart.types.Bar.extend({
name: "BarAlt",
initialize: function (data) {
var originalLabels;
// figure out the best label length for the current window size
if (this.options.responsiveLabelLengths !== undefined) {
var maxLabelLength;
this.options.responsiveLabelLengths
.sort(function (a, b) {
return b.uptoWidth - a.uptoWidth;
})
.forEach(function (spec) {
if (screen.width < spec.uptoWidth)
maxLabelLength = spec.maxLabelLength;
})
// update the labels if required
if (maxLabelLength !== undefined) {
originalLabels = Chart.helpers.clone(data.labels);
data.labels.forEach(function (label, i) {
if (data.labels[i].length > maxLabelLength)
data.labels[i] = data.labels[i].substring(0, maxLabelLength) + '...';
})
}
}
Chart.types.Bar.prototype.initialize.apply(this, arguments);
// optional - now restore the original labels for the tooltips
if (originalLabels !== undefined) {
this.datasets.forEach(function (dataset) {
dataset.bars.forEach(function (bar, i) {
bar.label = originalLabels[i];
})
})
}
}
});
var data = {
labels: ["Really Really Really Really Really Long Text", "February", "March", "April", "May", "June", "July"],
datasets: [
{
label: "My First dataset",
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(220,220,220,0.8)",
highlightFill: "rgba(220,220,220,0.75)",
highlightStroke: "rgba(220,220,220,1)",
data: [65, 59, 80, 81, 56, 55, 40]
},
{
label: "My Second dataset",
fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,0.8)",
highlightFill: "rgba(151,187,205,0.75)",
highlightStroke: "rgba(151,187,205,1)",
data: [28, 48, 40, 19, 86, 27, 90]
}
]
};
var ctx = document.getElementById("myChart").getContext("2d");
var myLineChart = new Chart(ctx).BarAlt(data, {
responsiveLabelLengths: [
{
uptoWidth: 480,
maxLabelLength: 5
},
{
uptoWidth: 768,
maxLabelLength: 10
},
{
uptoWidth: 992,
maxLabelLength: 20
},
{
uptoWidth: 1200,
maxLabelLength: undefined
},
]
});
Since we are using screen width you need to use Chrome Developer tools and choose a proper viewport sizing to see the effect. For the same reason, this is not going to have any effect if you are resizing your window - if you need that, just replace screen.width by window.innerWidth.
To add this as a new directive to angular-chart is pretty simple. After the above block of code (and after the script include for angular-chart.js) add a new directive for BarAlt, like so
angular.module('chart.js')
.directive('chartBarAlt', ['ChartJsFactory', function (ChartJsFactory) { return new ChartJsFactory('BarAlt'); }]);
Then use the directive like so
<canvas id="line" class="chart chart-bar-alt" data="data" options="options"
labels="labels"></canvas>
Notice options? You will need to include the responsiveLabelLengths in your options for BarAlt to kick in (otherwise it just acts like a normal bar chart). You do that like this (in your controller)
$scope.options = {
responsiveLabelLengths: [
{
uptoWidth: 480,
maxLabelLength: 5
},
{
uptoWidth: 768,
maxLabelLength: 10
},
{
uptoWidth: 992,
maxLabelLength: 20
},
{
uptoWidth: 1200,
maxLabelLength: undefined
},
]
};
Note that you need to use Chrome Developer tools and choose a proper viewport sizing to see the effect
Fiddle - http://jsfiddle.net/mqo24Lnx/
Here's how the trimming works on different screen sizes
Source: stackoverflow.com
Related Query
- How to handle epic long text at y-axis for angular-chart.js?
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- Chart Js reduce text size for label on X axis
- How to rotate the Label text in doughnut chart slice vertically in chart.js canvas, Angular 12?
- How do you set x and y axis and Title for a line chart using charts.js?
- How to change text colour of specific labels of axis in Chart JS?
- How to add custom text inside the bar and how to reduce the step size in y axis in chart js( Bar chart )
- How to start Y Axis Line at 0 from right for negative values in chart js 2?
- How to set max and min value for Y axis
- How to add text inside the doughnut chart using Chart.js?
- How can labels/legends be added for all chart types in chart.js (chartjs.org)?
- Chart Js change text label orientation on Ox axis
- Chart.js how to show cursor pointer for labels & legends in line chart
- show label in tooltip but not in x axis for chartjs line chart
- How to add second Y-axis for Bar and Line chart in Chart.js?
- How to add text in centre of the doughnut chart using Chart.js?
- ng2-charts: How to set fixed range for y axis
- Chart JS - set start of week for x axis time series
- Problem for display a chart with Chart.js and Angular
- Chart js. How to change font styles for "labels" array?
- Chart js. How to align text by the center of the tooltip?
- How do I change the 'months' language displayed on the date axis in Chart JS?
- Can I specify a different font size for each row of text in Chart Title?
- How to show Y axis ticks for every point in the graph
- Chart js different background for y axis
- How to use 'time' (data from database, data type: timestamp ) for plotting graph in Chart JS
- How do I obtain a Chart instance for Chart.js
- how to change Y axis value dynamically based on user input in Chartjs for Line chart?
- Ng2-charts / chart.js - how to refresh/update chart - angular 4
- how to set chart.js grid color for line chart
More Query from same tag
- ChartJS: Update tooltip
- Chart.js Show label/tooltip for single point when multiple points coincide
- Bar chart from Chart.js not showing the second dataset
- Error in implementing chart.js in AngularJS
- Loading or Activating ChartJS on Load
- How to enable timescale in chart.js line chart?
- how can i put a chart in a div with *ngIf
- Why is Chart.JS linking my start and end nodes in the line chart?
- PrimeFaces: ChartJs stacked bar chart label removal
- Unable to separate elements of JSON
- Chart.js - change the scale background color of Radar type
- chart.js Line chart with different background colors for each section
- How to increase space between chart and legend? (googlecharts)
- ChartJS - How to add Text between Pie Chart and Legend
- angular-chart.js showing black color chart in Firefox, Safari, and IE
- How to add an array to my line data?
- chartjs: doughnut graph onHover get labels and data
- Update chart axis labels after hover - chart.js
- how to change size of point in ChartJS
- Getting (X,Y) point on mouse click on chart
- Cannot resize chart from chart.js
- Real time colored bar chart
- Use react-icons for pointStyles in Chart.js graph on Next.js app
- Chart.js zoom shows decimal points
- Php add dummy data to array
- How can i change a numpy.ndarray to point format with curly brackets - Python 3.7
- How to display data from database into line graph using php and mysql?
- Chart.js Bar Chart with a switch/radio button to group data / Troubleshooting bar color
- Stacked bar chart with chartjs with included values
- How can I get chart.js to automatically add colours for dynamic labels?