score:8
You could override the ticks.callback
method as documented here: https://www.chartjs.org/docs/latest/axes/labelling.html#creating-custom-tick-formats
For example, to abbreviate the y-axis zeros to simply 'M':
var chart = new Chart(ctx, {
type: 'line',
data: data,
options: {
scales: {
yAxes: [{
ticks: {
// Abbreviate the millions
callback: function(value, index, values) {
return value / 1e6 + 'M';
}
}
}]
}
}
});
My fiddle: https://jsfiddle.net/robhirstio/hsvxbjkg/17/
score:0
Support 'K', 'M', 'B'
:
This is my solution, to be generic when you use the same options
object for multiple charts, that possibly contain lower numbers or negative numbers.
formatNumbers(value) {
if (value >= 1000000000 || value <= -1000000000 ) {
return value / 1e9 + 'B';
} else if (value >= 1000000 || value <= -1000000) {
return value / 1e6 + 'M';
} else if (value >= 1000 || value <= -1000) {
return value / 1e3 + 'K';
}
return value;
}
My fiddle: https://jsfiddle.net/epsilontal/v0qnsbwk/45/
Example:
score:1
Adding commarize feature for k, M, B and T
function commarize(min) {
min = min || 1e3;
// Alter numbers larger than 1k
if (this >= min) {
var units = ["k", "M", "B", "T"];
var order = Math.floor(Math.log(this) / Math.log(1000));
var unitname = units[(order - 1)];
var num = Math.floor(this / 1000 ** order);
// output number remainder + unitname
return num + unitname
}
// return formatted original number
return this.toLocaleString()
}
In chart JS you could use config property ticks
into yAxes
var chart = new Chart(ctx, {
type: 'line',
data: data,
options: {
scales: {
yAxes: [{
ticks: {
// Include a dollar sign in the ticks
callback: function(value, index, values) {
return String(value).commarize();
}
}
}]
}
}
});
Chart JS Reference https://www.chartjs.org/docs/latest/axes/labelling.html Commarize reference https://gist.github.com/MartinMuzatko/1060fe584d17c7b9ca6e
Source: stackoverflow.com
Related Query
- Shorten number labels in Charts.js
- Limit labels number on Chart.js line chart
- How to add images as labels to Canvas Charts using chart.js
- 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 - displaying multiple line charts using multiple labels
- Chart.js Y axis label, reverse tooltip order, shorten X axis labels
- Angular2 charts (chart.js) Labels not vertical even though option is set
- Chart.js Globally Formatted Number Labels
- Setting Common labels and background color common for all the charts in ChartJs
- PrimeNG/Chart.js - max Number of labels to show
- how to show labels in laravel charts when the name of the label is on another table?
- creating a dynamic number of charts using chart.js using angular
- Inverting X axis number labels in chart.js scatter chart
- How I can increase the number of colors for bar labels in ChartJS?
- ChartJS have xAxes labels match data source
- ChartJS/High Charts Radar chart - Different radial axis labels for each category that appear on hover
- Chartjs removing labels and begin number
- Generate an indefinite number of charts as images (chart.js)
- Chart.js, increase space between bars and its labels when increasing the charts width
- angular 7 how to draw dynamic number of charts
- Chart.js 2 line charts with separate dataset labels
- chart.js number of labels equal to number of datapoints
- charts labels and data with php arrays
- dynamic number of chart.js charts on the same page
- Is there a better way to create an 'n' number of charts in ChartJS and ASP.NET C#?
- Angular 8 and ng2 charts - Updating labels and data
- How to run Chart.js samples using source code
- Data Labels on top of points in line charts
- Is it possible to shorten outer labels on Radar graph using Chart.js, without affecting the other labels?
More Query from same tag
- issue upgrading from chart.js 1 to 3.5
- How can I add shadow color below the line in chart.js charts?
- Chartjs is stripping trailing zeros in decimal datasets
- injecting chart.js sparkline to jqxGrid widget in Angular with typescript
- How to convert a bar chart into a triangle shaped chart using Chart JS?
- Changing data when click button in chart.js
- vertical grid line not taking full hight of the canvas in chart js 3.3.2
- Get data from sql database in chartjs using codeigniter
- How to pass sql query data onto the datasets field of chartjs using nodejs (ejs)
- Chart.js not stretching width as per data
- ChartJS Data Overflowing Chart area
- ChartJS 2.6 - Bar chart fixed height scrollable-X
- Chart.js minimal width of Chart with scrollable overflow
- How to position yAxes labels in chartJS
- Is it possible to change pointStyle for elements of a ChartJS bubble chart per dataset?
- How to make Chart JS responsive?
- Add value to horizontal bar in chart.js
- How to sort MMMYY on Chart.Js and display the values that match the month?
- Can't display proper time on chart.js timeline
- hide label on doughnut chart of chartjs
- How to create a Doughnut chart in django admin interface using foreign key field data?
- How do I change the bar colors in chart.js barcharts?
- chart-options not registering scaleStartValue
- How to add an empty data point to a linechart on Chart.js?
- How to make tool tip contents display on multiple lines
- How to display value labels above graph bars using chart.js
- code works fine on jsfiddle but one function is not working on website
- chart.js-plugin-annotations multiple horizontal lines in one chart
- Chart.js v3.x time series on x axis
- Show Dates in x-axis when using chart.js