score:1
You need to pass a function in formatter option inside datalabels plugin configuration:
datalabels: {
anchor: "end",
align: "left",
formatter: function(context) {
return context / 1000 + "k";
},
font: {
color: "black"
}
}
You can check the solution in this demo: https://stackblitz.com/edit/angular-chart-js-thousand?file=src/app/app.component.ts
score:0
I have found the solution (just now). In config
option
its will have animation
and call back with function onComplete
. Here you can draw the label with any format you want. Please see this picture for code
score:1
For styling only the labels you want something like this:
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: "line",
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: "# of Votes",
data: [12000, 1900, 3000, 5000, 2201, 3492]
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
setTimeout(function() {
myChart.options.scales = {
xAxes: [],
yAxes: [{
gridLines: {
display: false
},
type: 'logarithmic',
position: 'left',
ticks: {
min: 0, //minimum tick
max: 100000, //maximum tick
callback: function(value, index, values) {
return Number((value / 1000).toString()) + 'K'; //pass tick values as a string into Number function
}
},
afterBuildTicks: function(chartObj) { //Build ticks labelling as per your need
chartObj.ticks = [];
chartObj.ticks.push(0);
chartObj.ticks.push(10);
chartObj.ticks.push(100);
chartObj.ticks.push(1000);
chartObj.ticks.push(10000);
chartObj.ticks.push(100000);
},
scaleLabel: {
display: false
}
}]
};
myChart.update();
}, 1000);
.myChartDiv {
max-width: 600px;
max-height: 400px;
}
<script src="https://npmcdn.com/chart.js@latest/dist/Chart.bundle.js"></script>
<div class="myChartDiv">
<canvas id="myChart" width="600" height="400"></canvas>
</div>
In case you want to change the format of the value shown when hovering the chart and displaying the value inside the tooltip, you need to use the tooltip callbacks.
For example:
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: "line",
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [
{
label: "# of Votes",
data: [12000, 1900, 3000, 5000, 2201, 3492]
}
]
},
options: {
tooltips: {
callbacks: {
label: function(tooltipItem, data) {
return (tooltipItem.yLabel/1000)+'K';
}
}
},
scales: {
yAxes: [
{
ticks: {
beginAtZero: true
}
}
]
}
}
});
setTimeout(function() {
myChart.options.scales = {
xAxes: [],
yAxes: [ {
gridLines: {
display: false
},
type: 'logarithmic',
position: 'left',
ticks: {
min: 0, //minimum tick
max: 100000, //maximum tick
callback: function (value, index, values) {
return Number((value/1000).toString())+'K'; //pass tick values as a string into Number function
}
},
afterBuildTicks: function (chartObj) { //Build ticks labelling as per your need
chartObj.ticks = [];
chartObj.ticks.push(0);
chartObj.ticks.push(10);
chartObj.ticks.push(100);
chartObj.ticks.push(1000);
chartObj.ticks.push(10000);
chartObj.ticks.push(100000);
},
scaleLabel: {
display: false
}
}
]
};
myChart.update();
}, 1000);
.myChartDiv {
max-width: 600px;
max-height: 400px;
}
<script src="https://npmcdn.com/chart.js@latest/dist/Chart.bundle.js"></script>
<div class="myChartDiv">
<canvas id="myChart" width="600" height="400"></canvas>
</div>
Last example involves the plugin you use in your question.
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: "line",
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
datalabels: {
color: '#000'
},
label: "# of Votes",
data: [12000, 1900, 3000, 5000, 2201, 3492]
}]
},
options: {
plugins: {
datalabels: {
color: '#000',
formatter: function(value, context) {
return context.chart.data.labels[context.dataIndex] + ': ' + Math.round(value / 1000) + 'K';
}
}
},
tooltips: {
callbacks: {
label: function(tooltipItem, data) {
return (tooltipItem.yLabel / 1000) + 'K';
}
}
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
setTimeout(function() {
myChart.options.scales = {
xAxes: [],
yAxes: [{
gridLines: {
display: false
},
type: 'logarithmic',
position: 'left',
ticks: {
min: 0, //minimum tick
max: 100000, //maximum tick
callback: function(value, index, values) {
return Number((value / 1000).toString()) + 'K'; //pass tick values as a string into Number function
}
},
afterBuildTicks: function(chartObj) { //Build ticks labelling as per your need
chartObj.ticks = [];
chartObj.ticks.push(0);
chartObj.ticks.push(10);
chartObj.ticks.push(100);
chartObj.ticks.push(1000);
chartObj.ticks.push(10000);
chartObj.ticks.push(100000);
},
scaleLabel: {
display: false
}
}]
};
myChart.update();
}, 1000);
.myChartDiv {
max-width: 600px;
max-height: 400px;
}
<script src="https://npmcdn.com/chart.js@latest/dist/Chart.bundle.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels"></script>
<div class="myChartDiv">
<canvas id="myChart" width="600" height="400"></canvas>
</div>
Source: stackoverflow.com
Related Query
- How to show thousand in k format for bar values in chart.js
- PrimeNg bar chart how to show a label for the y-axis
- How to show data values in top of bar chart and line chart in chart.js 3
- How to show different product name for every bar in chart js
- How to show the chartjs bar chart data values labels as text?
- how to show bar chart for every product
- Chart.js how to show cursor pointer for labels & legends in line chart
- How to add second Y-axis for Bar and Line chart in Chart.js?
- How to display inline values in a stacked bar chart with Chart.js?
- Chartjs - data format for bar chart with multi-level x-axes
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- How to show gradient vertically on chart js grouped bar chart?
- angular-chart.js : how to show numbers in each bar of stacked bar chart
- Chart.js: How to get x-axis labels to show on top of bars in bar chart
- How to use set the color for each bar in a bar chart using chartjs?
- How can I have different values for the chart and the tooltip in chart.js?
- How to draw baseline for bar chart in chart.js
- How to plot chart from external JSON and format X-AXIS to show time using Chart.JS?
- How to set custom Y Axis Values (Chart.js v2.8.0) in Chart Bar JS
- How to display k as thousand for the values of chart.js - Javascript
- How to set X coordinate for each bar with react chart js 2?
- I am using chart.js I sent my time values throw my api as timestamp know i need to show them in the chart as date format
- How to write better code in es6 for formatting an object with array values
- How do I get a different label for each bar in a bar chart in ChartJS?
- how can i show labels and value in both on bar chart
- How can I get my Chart.JS bar chart to stack two data values together on each bar, and print a calculated value on each bar?
- chart.js 3 stacked bar chart - tooltip showing for zero values
- How to show symbols after the label and before the numeric value using chart.js Bar chart in Angular
- How remove duplicates xAxis labels and show all values on chart
- How to change the position of the tooltip for the Bar type chart in Chart.Js
More Query from same tag
- Chart.js: How can I manually set Y-axis range?
- Chart js old chart data not clearing
- ChartJS create chart Ajax
- Chart.js on web-app
- Mapping data from JSON not parsing well
- I want to show the value of label inside the pie graph. (vue-chartjs / pieceLabel)
- Can we have both ajax and jquery scripts in one function?
- Vue ChartJS is not updating
- how change color of labels of legends of chart.js
- Chart.js remove zero value sector in pie chart
- polarArea Chart.js starting from zero
- ChartJS - Size of the point doesn't change
- How to change the default barchart colour in chart.JS and angular 7?
- Chart.js chart not rendering
- Chart.js Radar Labels Coordinates
- Binding Chart to Canvas height width
- Unexpected behavior with chart.js
- Change `font-family` in Angular Chart.js
- How to add datas to chart js from javascript array itself?
- Connect the dot vertically instead of horizontally on Line Chart
- Chart.js: Creating a Barchart with overlaying and offset Bars
- Chart.js not working after Django deployment on Heroku
- how can conditionally show/hide gridlines on the yAxis using Chart.js?
- Angular 8 & ChartJs change color in pie chart
- Chart.js Adapting different X axis with same scale
- Can't resolve 'chart.js/auto'
- How can I increase the size of the pie (Chart.JS)?
- Show Dates in x-axis when using chart.js
- Problem creating chart out of GET request with Angular
- Function for Label in ChartJS?