score:13
Accepted answer
You can accomplish this using the following y-axis ticks callback function ...
yAxes: [{
ticks: {
stepSize: 100000,
callback: function(value) {
var ranges = [
{ divider: 1e6, suffix: 'M' },
{ divider: 1e3, suffix: 'k' }
];
function formatNumber(n) {
for (var i = 0; i < ranges.length; i++) {
if (n >= ranges[i].divider) {
return (n / ranges[i].divider).toString() + ranges[i].suffix;
}
}
return n;
}
return '$' + formatNumber(value);
}
}
}]
You would also need to add some padding to the left of the chart, so that, the y-axis labels fits properly to the view ...
layout: {
padding: {
left: 10
}
}
ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ ⧩
var ctx = document.getElementById('c').getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
datasets: [{
label: 'Profit',
data: [666666, 777777, 888888, 999999, 1100000],
backgroundColor: ['#30799f', '#ac51c3', '#4ba57b', '#e3297d', '#e35c32'],
borderColor: ['#30799f', '#ac51c3', '#4ba57b', '#e3297d', '#e35c32'],
borderWidth: 1
}]
},
options: {
legend: { display: false },
responsive: false,
layout: {
padding: {
left: 10 //set as you wish
}
},
scales: {
yAxes: [{
ticks: {
stepSize: 100000,
callback: function(value) {
var ranges = [
{ divider: 1e6, suffix: 'M' },
{ divider: 1e3, suffix: 'k' }
];
function formatNumber(n) {
for (var i = 0; i < ranges.length; i++) {
if (n >= ranges[i].divider) {
return (n / ranges[i].divider).toString() + ranges[i].suffix;
}
}
return n;
}
return '$' + formatNumber(value);
}
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="c" width="350" height="200"></canvas>
Source: stackoverflow.com
Related Query
- y axis Formatting with Metric prefix 1000=> 1k Chart.js
- 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
- 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 X axis on top and bottom in line chart rendered with Chart.js 2.4.0
- Chart.js Dynamically Updating Chart with X Axis Time
- Obtain max value of y axis of line chart rendered with Chart.js
- Line chart with large number of labels on X axis
- Add buffer to Y axis with chart js
- Chartjs 3.x - How to duplicate X axis on both sides of horizontal bar chart with only 1 dataset?
- how to label axis within radar chart with chart.js
- chart js - bar chart with time scale on Y axis in Hours and Minutes
- Chart.js Chart Formatting - Legend & Y Axis & Title
- How to write better code in es6 for formatting an object with array values
- Chart with Time axis only displaying first grid line and tick label (unitStepSize)
- How to use chart.js to plot line chart with x axis as time stamp in seconds
- what is wrong with my code ? java script chart position
- Time chart labels with some X axis labels using Chart js v3
- Chartjs chart with only one axis
- Chart.js bar chart with time on X axis and category on Y axis is not rendered
- Charts.js - Bubble chart with two word axis
- Rotating a chart.js pie chart with two datapoints to align both sectors on horizontal axis
- hereChartJS Line Chart with Time Axis
- 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
- Multiple axis line chart with Chart.js and JSON data from Google Sheet
- ChartJS: Bar chart with axis ticks wider than categories
- Chart for Real time data with duplicate x axis
- How to make chart which has both positive and negative value, starts with 0 - x axis
- In Chart.js set chart title, name of x axis and y axis?
More Query from same tag
- How to add overlay color to chart.js pie chart segment?
- How to display pie slice data and tooltip together using chart.js
- scattered graph with xAxes of date react-chratjs-2
- Ajax asynchronicity calls end function before inner loop function
- chart.js API returns bad x and y points
- In ng 2 chart during the load an undefined legend gets created automatically ,Is there any way to remove it?
- chart.js substitute tooltipItems
- Can "Bar" be placed center of vertical grid line in Chart.js?
- How to make rectangle in chart.js
- Chartjs v2.8 showAllTooltips does not exist
- How to zoom Y axis on time series or X-Y chartjs graph
- Laravel passing orders to Chart.js
- ChartJS - How to choose the visible datalabels
- How can i refere to an object variable dynamically?
- Pushing data from angular typescript to chart.js library complication
- Chartjs disable legend click does not work when condition is not evaluated
- php/laravel: Creating chartdata, return 0 if no data is being submitted to database
- HTML/Chart JS deserialize JSON from Python Flask
- Chart.js Mixed Bar and Line chart with different scales
- Chart.js animate chart after clicking a button
- Chart.js: Thousand Separator and Tittle ind the Middle of the Doughnut
- Chart.js - Creating a single bar chart with states
- How to pass a variable to the dataset for a graph in chart.js?
- ChartJS - Customize Ticks/Labels on Y Axis
- Automatically show 0 sales for the date which is not in database
- Object.values not seperated by parentheses / Chartjs only displays first element of array
- Chart.js piechart, only have border on the outside of arc
- Mixed Chart calculating difference between two bars - ChartJS
- Charts.js & Bootstrap Accordion
- I want to add label on only specific vue chart