score:196
Try adding the options.scales.xAxes.ticks.maxTicksLimit
option:
xAxes: [{
type: 'time',
ticks: {
autoSkip: true,
maxTicksLimit: 20
}
}]
score:-2
In the Chart.js file, you should find (on line 884 for me)
var Line = function(...
...
function drawScale(){
...
ctx.fillText(data.labels[i], 0,0);
...
If you just wrap that one line call to fillText
with if ( i % config.xFreq === 0){ ... }
and then in chart.Line.defaults
add something line xFreq : 1
you should be able to start using xFreq
in your options
when you call new Chart(ctx).Line(data, options)
.
Mind you this is pretty hacky.
score:-1
This answer works like a charm.
If you are wondering about the clone
function, try this one:
var clone = function(el){ return el.slice(0); }
score:0
To set a custom number of ticks regardless of your chartsjs version:
yAxes: [{
ticks: {
stepSize: Math.round((Math.max.apply(Math, myListOfyValues) / 10)/5)*5,
beginAtZero: true,
precision: 0
}
}]
10 = the number of ticks
5 = rounds tick values to the nearest 5. All your y values will have the same step size.
Similar will work for xAxes too.
score:0
you can limit at as
scales: {
x: {
ticks: {
// For a category axis, the val is the index so the lookup via getLabelForValue is needed
callback: function(val, index) {
// Hide the label of every 2nd dataset
return index % 5 === 0 ? this.getLabelForValue(val) : '';
},
}
}
}
this will skip 4 labels and set the 5th one only.
score:0
you can use the following code:
xAxes: [{
ticks: {
autoSkip: true,
maxRotation: 90
}
}]
score:1
i had a similar type of issue, and was given a nice solution to my specific issue show label in tooltip but not in x axis for chartjs line chart. See if this helps you
score:1
You may well not need anything with this new built-in feature.
A built-in label auto-skip feature detects would-be overlapping ticks and labels and removes every nth label to keep things displaying normally. https://www.chartjs.org/docs/latest/axes/
score:2
According to the chart.js github issue #12. Current solutions include:
- Use 2.0 alpha (not production)
- Hide x-axis at all when it becames too crowd (cannot accept at all)
- manually control label skip of x-axis (not in responsive page)
However, after a few minutes, I thinks there's a better solution.
The following snippet will hide labels automatically. By modify xLabels
with empty string before invoke draw()
and restore them after then. Even more, re-rotating x labels can be applied as there's more space after hiding.
var axisFixedDrawFn = function() {
var self = this
var widthPerXLabel = (self.width - self.xScalePaddingLeft - self.xScalePaddingRight) / self.xLabels.length
var xLabelPerFontSize = self.fontSize / widthPerXLabel
var xLabelStep = Math.ceil(xLabelPerFontSize)
var xLabelRotationOld = null
var xLabelsOld = null
if (xLabelStep > 1) {
var widthPerSkipedXLabel = (self.width - self.xScalePaddingLeft - self.xScalePaddingRight) / (self.xLabels.length / xLabelStep)
xLabelRotationOld = self.xLabelRotation
xLabelsOld = clone(self.xLabels)
self.xLabelRotation = Math.asin(self.fontSize / widthPerSkipedXLabel) / Math.PI * 180
for (var i = 0; i < self.xLabels.length; ++i) {
if (i % xLabelStep != 0) {
self.xLabels[i] = ''
}
}
}
Chart.Scale.prototype.draw.apply(self, arguments);
if (xLabelRotationOld != null) {
self.xLabelRotation = xLabelRotationOld
}
if (xLabelsOld != null) {
self.xLabels = xLabelsOld
}
};
Chart.types.Bar.extend({
name : "AxisFixedBar",
initialize : function(data) {
Chart.types.Bar.prototype.initialize.apply(this, arguments);
this.scale.draw = axisFixedDrawFn;
}
});
Chart.types.Line.extend({
name : "AxisFixedLine",
initialize : function(data) {
Chart.types.Line.prototype.initialize.apply(this, arguments);
this.scale.draw = axisFixedDrawFn;
}
});
Please notice that clone
is an external dependency.
score:4
In chart.js 3.2.0
options: {
scales: {
x: {
ticks: {
maxTicksLimit: 10
}
}
}
}
score:6
For Chart.js 3.3.2, you can use @Nikita Ag's approach with a few changes. You can check the documentation. Put ticks
in xAxis
in scales
. Example:
...
options: {
scales: {
xAxis: {
ticks: {
maxTicksLimit: 10
}
}
}
}
...
score:8
for axis rotation
use this:
scales: {
xAxes: [
{
// aqui controlas la cantidad de elementos en el eje horizontal con autoSkip
ticks: {
autoSkip: true,
maxRotation: 0,
minRotation: 0
}
}
]
}
score:15
UPDATE:
I'v updated my fork with the latest pull (as of Jan 27, 2014) from NNick's Chart.js master branch. https://github.com/hay-wire/Chart.js/tree/showXLabels
ORIGINAL ANSWER:
For those still facing this issue, I forked Chart.js a while back to solve the same problem. You can check it out on: https://github.com/hay-wire/Chart.js/tree/skip-xlabels => Older branch! Check showXLabels branch for latest pull.
How to use:
Applicable to bar chart and line chart.
User can now pass a { showXLabels: 10 }
to display only 10 labels (actual displayed labels count might be a bit different depending on the number of total labels present on x axis, but it will still remain close to 10 however)
Helps a lot when there is a very large amount of data. Earlier, the graph used to look devastated due to x axis labels drawn over each other in the cramped space. With showXLabels
, user now has the control to reduce the number of labels to whatever number of labels fit good into the space available to him.
See the attached images for a comparison.
Without showXLabels
option:
With { showXLabels: 10 }
passed into option:
Here's some discussion on it: https://github.com/nnnick/Chart.js/pull/521#issuecomment-60469304
score:17
For anyone looking to achieve this on Chart JS V2 the following will work:
var options = {
scales: {
xAxes: [{
afterTickToLabelConversion: function(data){
var xLabels = data.ticks;
xLabels.forEach(function (labels, i) {
if (i % 2 == 1){
xLabels[i] = '';
}
});
}
}]
}
}
Then pass the options variable as usual into a:
myLineChart = new Chart(ctx, {
type: 'line',
data: data,
options: options
});`
score:22
For concreteness, let's say your original list of labels looks like:
["0", "1", "2", "3", "4", "5", "6", "7", "8"]
If you only want to display every 4th label, filter your list of labels so that every 4th label is filled in, and all others are the empty string (e.g. ["0", "", "", "", "4", "", "", "", "8"]
).
Source: stackoverflow.com
Related Query
- Limit labels number on Chart.js line chart
- Display a limited number of labels only on X-Axis of Line Chart using Chart.js
- Line chart with large number of labels on X axis
- Chart.js how to show cursor pointer for labels & legends in line chart
- How to display Line Chart dataset point labels with Chart.js?
- Changing x axis labels in Chart.js line chart
- How to align Chart.JS line chart labels to the center
- Multiple line labels for chart js
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- Set minimum number of steps in line chart
- Chart.js line chart with correctly spaced x labels
- How to change background color of labels in line chart from chart.js?
- How to reduce the number of points shown on line chartjs chart with a large dataset?
- Inverting X axis number labels in chart.js scatter chart
- Skip x labels on line chart
- X and Y axis labels not displaying on line chart (Chart.js)
- Chart js animating a line while changing x-axis labels
- Add different labels in a line chart - Chart.js
- Chart.js how to show line chart without displaying the labels on xaxis and yaxis
- 2 Line Chart with different labels | Chart.js
- How to set the number of of rows in a line chart in chart.js?
- ChartJs - Doughnut Chart - how to remove labels if percentage is less than a limit
- Chart.js Draw a Stacked Bar Chart with Limit Line
- Chart.js – Line Chart Tooltip Labels
- ChartJs line chart time cartesian axis number of ticks and wierd offset
- How to limit chart JS hover to take only one value from each line chart when zoomed out?
- Aligning zero on y axis to the labels on x axis of line chart in chartjs
- how to show X and Y labels on points in Chart js 2 Line Chart
- Chart.js multi line chart that has different labels
- ChartJS: Changing Font-Size of X-Axis Labels on Line Chart
More Query from same tag
- Is there a more optimized way to implement the change function of chart js upon changing the dropdowns?
- how to set title in legend of chart.js?
- How to set the number of of rows in a line chart in chart.js?
- Custom labeling/fixed range on x-axis in Chart.js for horizontal bar chart?
- Wkhtmltopdf does not render Chart.JS 2.5.0 graph
- Chartjs-node installation failing
- Chartjs line graph point hover animation buggy / jumpy
- Completely hide the Y-axis on chartjs when data is hidden
- New Chart.js Chart onclick
- Line Chart Js x-axis values all 0 on React
- ChartJS Recreate Chart
- Chart Js Doughnut chart giving error
- Chart.js the point's limit supported
- Create a heatmap/punchcard Using Chart.js
- Chart.js Adapting different X axis with same scale
- How display straight corner in chartjs
- Chart js data decimation not working, parsing issue
- angular-chart.js, how to change colours labels
- Generate an indefinite number of charts as images (chart.js)
- chartjs line graph destroy function is not clearing the old chart instances
- How to add left padding for my charts done in ChartJs and my Google Map so it is not glued to the limit of the page on the left
- How to display currency in Chart js
- Chart.js, PHP and radar chart labels
- Angular 5 and chart.js ts error
- Chart.js time series
- How to change Chart.js horizontal bar chart Width?
- Chart JS not working with Phonegap
- Adding image on chart js
- Data updating but chart isn't
- How can I change color of only one column in Barchart using Chart.js