score:178
I had the same problem, I think in Chart.js 2.x.x the approach is slightly different like below.
ticks: {
callback: function(label, index, labels) {
return label/1000+'k';
}
}
More in details
var options = {
scales: {
yAxes: [
{
ticks: {
callback: function(label, index, labels) {
return label/1000+'k';
}
},
scaleLabel: {
display: true,
labelString: '1k = 1000'
}
}
]
}
}
score:2
Here you can find a good example of how to format Y-Axis value.
Also, you can use scaleLabel : "<%=value%>"
that you mentioned, It basically means that everything between <%=
and %>
tags will be treated as javascript code (i.e you can use if
statments...)
score:5
As Nevercom said the scaleLable should contain javascript so to manipulate the y value just apply the required formatting.
Note the the value is a string.
var options = {
scaleLabel : "<%= value + ' + two = ' + (Number(value) + 2) %>"
};
if you wish to set a manual y scale you can use scaleOverride
var options = {
scaleLabel : "<%= value + ' + two = ' + (Number(value) + 2) %>",
scaleOverride: true,
scaleSteps: 10,
scaleStepWidth: 10,
scaleStartValue: 0
};
score:7
For anyone using 3.X.X
, here's the updated syntax to change y axis labels:
scales: {
y: {
ticks: {
callback: (label) => `$ ${label}`,
},
},
},
score:9
Chart.js 2.X.X
I know this post is old. But if anyone is looking for more flexible solution, here it is
var options = {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
callback: function(label, index, labels) {
return Intl.NumberFormat().format(label);
// 1,350
return Intl.NumberFormat('hi', {
style: 'currency', currency: 'INR', minimumFractionDigits: 0,
}).format(label).replace(/^(\D+)/, '$1 ');
// ₹ 1,350
// return Intl.NumberFormat('hi', {
style: 'currency', currency: 'INR', currencyDisplay: 'symbol', minimumFractionDigits: 2
}).format(label).replace(/^(\D+)/, '$1 ');
// ₹ 1,350.00
}
}
}]
}
}
'hi' is Hindi. Check here for other locales argument
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_identification_and_negotiation#locales_argument
for more currency symbol
https://www.currency-iso.org/en/home/tables/table-a1.html
score:14
scaleLabel : "<%= Number(value).toFixed(2).replace('.', ',') + ' $'%>"
score:56
An undocumented feature of the ChartJS library is that if you pass in a function instead of a string, it will use your function to render the y-axis's scaleLabel.
So while, "<%= Number(value).toFixed(2).replace('.',',') + ' $' %>"
works, you could also do:
scaleLabel: function (valuePayload) {
return Number(valuePayload.value).toFixed(2).replace('.',',') + '$';
}
If you're doing anything remotely complicated, I'd recommend doing this instead.
Source: stackoverflow.com
Related Query
- Chart.js Chart Formatting - Legend & Y Axis & Title
- 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
- In Chart.js set chart title, name of x axis and y axis?
- Chart.js - Formatting Y axis
- Charts.js Formatting Y Axis with both Currency and Thousands Separator
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- Chart Js change text label orientation on Ox axis
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- Chart.js 2.0 Formatting Y Axis with Currency and Thousands Separator
- show label in tooltip but not in x axis for chartjs line chart
- y axis Formatting with Metric prefix 1000=> 1k Chart.js
- Chart JS - set start of week for x axis time series
- Show X axis on top and bottom in line chart rendered with Chart.js 2.4.0
- Chart.js Timeseries chart - formatting and missing data values
- Chart.js Dynamically Updating Chart with X Axis Time
- How do I change the 'months' language displayed on the date axis in Chart JS?
- Obtain max value of y axis of line chart rendered with Chart.js
- Changing x axis labels in Chart.js line chart
- Line chart with large number of labels on X axis
- chart.js switch x/y axis on line chart
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- Chart js different background for y axis
- Map event position to y axis value in chartjs line chart
- Show all values in Chart js y axis
- Chart Js, Style some ticks on the axis differently
- How to start the line graph from the left Y axis in a line/bar mixed chart (Chart.js)?
- Hovering over chart.js values in Meteor onRendered function causes chart axis shift
- chartjs - multi axis line chart - cannot read property 'min' of undefined
- to increase space between x axis and first horizontal bar in chart js
- Add buffer to Y axis with chart js
More Query from same tag
- Chart.js: adding a custom label to each item, and displaying it with a custom tooltip
- Chartjs : showLine depending on value
- How to add afterDataLimits callback to chart.js
- Why don't my datasets show up on line graph using Chart.js?
- how can I render multiple charts in chartjs?
- convert javascript to dart
- Chart.js time scale showing one of the dates wrong
- Chart.js: Is it possible to get the label of a dataset in a stacked bargraph on clicking it?
- Creating multiple charts based on dictionary length
- How to extract ChartJs data and config properties
- ChartJS - Size of the point doesn't change
- I get to do scroll
- Combining multiple legend elements in chart.js
- Drawing line chart in chart.js placing dots only when value changes
- How can have variable data be from a url in javascript
- Chart.Js pie chart click
- Show values on each arc doughnut Chart.js
- Bubble Chart in Chartjs not working
- Chartjs different length of labels and datasets
- How to get line x value from Label chart js
- Extend chart.js horizontal bar graph to include a vertical line
- How to make custom text appear underneath each bar in chartjs bar chart?
- Cannot read property 'reactiveProp' of undefined in vue-chartjs
- Chart.js - Bar chart with linear cartesian (numerical) X axis?
- Ticks callback not displaying strings containing decimal
- How to use same backgroundcolors out of an array to unknown data in chartjs?
- Hide the element text on line charts since it overlaps with the line
- How to change bar color acording to label value in chartjs
- ChartJS how to sync animations when page loads
- Image-Chart ChartJs Ticks Callback not Working?