score:22
Chart.js uses an integral skipRatio
(to figure out how many labels to skip). With Chart.js v2.1.x, you can write your own plugin to use a fractional skipRatio
Preview
Script
Chart.pluginService.register({
afterUpdate: function (chart) {
var xScale = chart.scales['x-axis-0'];
if (xScale.options.ticks.maxTicksLimit) {
// store the original maxTicksLimit
xScale.options.ticks._maxTicksLimit = xScale.options.ticks.maxTicksLimit;
// let chart.js draw the first and last label
xScale.options.ticks.maxTicksLimit = (xScale.ticks.length % xScale.options.ticks._maxTicksLimit === 0) ? 1 : 2;
var originalXScaleDraw = xScale.draw
xScale.draw = function () {
originalXScaleDraw.apply(this, arguments);
var xScale = chart.scales['x-axis-0'];
if (xScale.options.ticks.maxTicksLimit) {
var helpers = Chart.helpers;
var tickFontColor = helpers.getValueOrDefault(xScale.options.ticks.fontColor, Chart.defaults.global.defaultFontColor);
var tickFontSize = helpers.getValueOrDefault(xScale.options.ticks.fontSize, Chart.defaults.global.defaultFontSize);
var tickFontStyle = helpers.getValueOrDefault(xScale.options.ticks.fontStyle, Chart.defaults.global.defaultFontStyle);
var tickFontFamily = helpers.getValueOrDefault(xScale.options.ticks.fontFamily, Chart.defaults.global.defaultFontFamily);
var tickLabelFont = helpers.fontString(tickFontSize, tickFontStyle, tickFontFamily);
var tl = xScale.options.gridLines.tickMarkLength;
var isRotated = xScale.labelRotation !== 0;
var yTickStart = xScale.top;
var yTickEnd = xScale.top + tl;
var chartArea = chart.chartArea;
// use the saved ticks
var maxTicks = xScale.options.ticks._maxTicksLimit - 1;
var ticksPerVisibleTick = xScale.ticks.length / maxTicks;
// chart.js uses an integral skipRatio - this causes all the fractional ticks to be accounted for between the last 2 labels
// we use a fractional skipRatio
var ticksCovered = 0;
helpers.each(xScale.ticks, function (label, index) {
if (index < ticksCovered)
return;
ticksCovered += ticksPerVisibleTick;
// chart.js has already drawn these 2
if (index === 0 || index === (xScale.ticks.length - 1))
return;
// copy of chart.js code
var xLineValue = this.getPixelForTick(index);
var xLabelValue = this.getPixelForTick(index, this.options.gridLines.offsetGridLines);
if (this.options.gridLines.display) {
this.ctx.lineWidth = this.options.gridLines.lineWidth;
this.ctx.strokeStyle = this.options.gridLines.color;
xLineValue += helpers.aliasPixel(this.ctx.lineWidth);
// Draw the label area
this.ctx.beginPath();
if (this.options.gridLines.drawTicks) {
this.ctx.moveTo(xLineValue, yTickStart);
this.ctx.lineTo(xLineValue, yTickEnd);
}
// Draw the chart area
if (this.options.gridLines.drawOnChartArea) {
this.ctx.moveTo(xLineValue, chartArea.top);
this.ctx.lineTo(xLineValue, chartArea.bottom);
}
// Need to stroke in the loop because we are potentially changing line widths & colours
this.ctx.stroke();
}
if (this.options.ticks.display) {
this.ctx.save();
this.ctx.translate(xLabelValue + this.options.ticks.labelOffset, (isRotated) ? this.top + 12 : this.options.position === "top" ? this.bottom - tl : this.top + tl);
this.ctx.rotate(helpers.toRadians(this.labelRotation) * -1);
this.ctx.font = tickLabelFont;
this.ctx.textAlign = (isRotated) ? "right" : "center";
this.ctx.textBaseline = (isRotated) ? "middle" : this.options.position === "top" ? "bottom" : "top";
this.ctx.fillText(label, 0, 0);
this.ctx.restore();
}
}, xScale);
}
};
}
},
});
Fiddle - http://jsfiddle.net/bh63pe1v/
score:0
Just do this:
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 y values will be incremented evenly
Similar will work for xAxes too.
score:10
A simpler solution until this is permanently fixed by the Chart JS contributors is to include a decimal in maxTicksLimit
.
For example:
maxTicksLimit: 8,
produces a huge gap at the end.
maxTicksLimit: 8.1,
Does not produce a huge gap at the end.
Depending on what you want to set your maxTicksLimit to, you need to play around with different decimals to see which one produces the best result.
Source: stackoverflow.com
Related Query
- Chart.js: evenly distribute ticks when using maxTicksLimit
- Moving vertical line when hovering over the chart using chart.js
- Uncaught TypeError: Chart is not a constructor when using Chart.js
- Chart JS chart not rendering in ArcGIS popup when using navigation arrows
- Bar Chart Not Stacking When Using ChartJs
- Chart.js chart doesn't render when using Angular 2
- Chart js with problems when using too many series
- Bar Chart not displaying when using chart.js
- ReactJS - Moving vertical line when hovering over Line chart using chart.js v3.7.1
- ChartJS Bar Chart not respecting disabled legend when using cdn
- Moving vertical line when hovering over the chart using chart.js in v2.9.4
- How can I show "No Data" message in Pie Chart when there is no data using VueJS?
- 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
- Chart.js returns a console error and does not display the chart when using variables as data input
- Chart changes different on zoom when using multiple charts with chart.js and flask
- Background color does not work when trying to create my data before using scatter chart with chart.js
- How to center radar chart ticks when changing start angle of chart?
- Chart.js - Line chart does not render all points when using Point[] format
- How to run Chart.js samples using source code
- How do I increase the space between the Legend and the Chart when using angular-chart.js?
- Understanding the data flow when fetching chart data using AJAX
- How to add text inside the doughnut chart using Chart.js?
- Chartjs Bar Chart showing old data when hovering
- Converting Chart.js canvas chart to image using .toDataUrl() results in blank image
- create a multi line chart using Chart.js
- How to add an on click event to my Line chart using Chart.js
- Display line chart with connected dots using chartJS
- Dynamically update the options of a chart in chartjs using Javascript
- How to save Chart JS charts as image without black background using blobs and filesaver?
- How to Draw Gantt chart using chart js or other libraries
More Query from same tag
- How to ignore same values on ChartJs?
- How to to change mouse cursor on chart.js doughnut graph?
- How to create a donut chart like this in chart.js
- Mobile page loading in "zoomed" state
- chartjs; cannot read property '0' of undefined
- Chart.js v2, remove padding/margin from radar chart
- How to make `fillColor` as gradient in chart.js?
- Chart.js - How to create an auto-scaling line diagram for a big amount of data happenning over time
- chart.js one of the chart from mixed chart is not plotting
- Show values in Chart.js Pie chart parts
- React - render Chart.js chart based on user selection, after Ajax returns JSON
- How to Reverse Chart.js Label Order?
- Complex Javascript variables containing the values of other assigned variables?
- set color from datasource like you would set argumentField or valueField?
- Chart.js don't work when I open my app as a stand alone app in Chrome
- Disable stacking for 1 dataset
- How to load new chart (chartsjs) from api call with jquery?
- Chart.Mvc scale begin at 0 - Nullable bool property remains null
- Pie Chart shows up only when I zoom in-out
- custom label title for scatter in React-ChartJS-2
- How to get the image from graph created using chartjs
- OffsetWidth / offsetHeight is zero when template loaded by ngRoute
- Chart js PHP and JSON wont diplsay
- Plotting multiple lines (line chart) in Apache Zeppelin using AngularJS
- ChartJs shows the wrong labels
- Using ChartJs In a VueJs component
- Chart.js nuget Package with angularJS
- How to change line color based on data - chartist or chart.js
- Could not find elementById on mat-card-content
- Can we show long numbers as 1k, 1M, 1B on axis in charts?